#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>
#include <cstdint>

const char* T0="{\"pairs\":[\n    {\"x0\":";
const char* T1=", \"y0\":";
const char* T2=", \"x1\":";
const char* T3=", \"y1\":";
const char* T4="},\n    {\"x0\":";

typedef int64_t i64;

int
main()
{
  i64 buf_sz = 4096*512;

  /* https://www.github.com/tmick0 code adapted from https://lo.calho.st/posts/black-magic-buffer/ */
  /* create buffer */
  int buffd = memfd_create("readbuf", 0);
  int err = ftruncate(buffd, buf_sz);
  char *buf = (char*)mmap(NULL, 2 * buf_sz, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  
  // Map the buffer at that address
  mmap((void*)buf, buf_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, buffd, 0);
  
  // Now map it again, in the next virtual page
  mmap((void*)(buf + buf_sz), buf_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, buffd, 0);

  const char *s="Hello world!";
  for(int i=0;s[i];++i) buf[i+buf_sz-6]=s[i];
  printf("%s\n",buf);
  printf("%s\n",buf+buf_sz);
  printf("%s\n",buf+buf_sz-6);

  return 0;

}
