#define _GNU_SOURCE
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <x86intrin.h>
int main(int argc, char **argv) {
  size_t mb = 64;
  if (argc > 1) mb = (size_t)atoi(argv[1]);
  if (mb < 1) mb = 1;
  if (mb > 512) mb = 512;
  size_t n = mb * 1024ull * 1024ull;
  volatile unsigned char *p = (volatile unsigned char *)aligned_alloc(64, n);
  if (!p) return 1;
  /* fill · BGF touch all lines */
  for (size_t i = 0; i < n; i += 64) p[i] = (unsigned char)(i | 1);
  /* measure-ish: read sum */
  unsigned long long s = 0;
  for (size_t i = 0; i < n; i += 64) s += p[i];
  /* clflush each line · spread cover through CPU caches for this buffer */
  for (size_t i = 0; i < n; i += 64) {
    _mm_clflush((const void *)(p + i));
  }
  _mm_mfence();
  /* second pass touch after flush */
  for (size_t i = 0; i < n; i += 64) s ^= p[i];
  printf("%llu\n", (unsigned long long)(s | 1));
  free((void *)p);
  return 0;
}
