/*
 * strike3.c - fork-free (draw, i_gen) pairing on zero-gen inodes.
 * For each of N files: read randseed, create file, read randseed.
 * dist must be exactly 1 and the on-disk di_gen must equal draw/2+1.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

#define RSPHYS 0x1120c80

static unsigned long long rrs(int fd)
{
    unsigned long long v = 0;
    if (pread(fd, &v, 8, RSPHYS) != 8) { perror("pread"); exit(1); }
    return v;
}

int main(int argc, char **argv)
{
    int n = argc > 1 ? atoi(argv[1]) : 3;
    int mfd = open("/dev/mem", O_RDONLY);
    if (mfd < 0) { perror("/dev/mem"); return 1; }
    for (int i = 0; i < n; i++) {
        char path[64];
        snprintf(path, sizeof(path), "/mnt/s3_%d", i);
        unsigned long long b = rrs(mfd);
        int fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0644);
        if (fd < 0) { perror(path); return 1; }
        write(fd, "x", 1);
        close(fd);
        unsigned long long a = rrs(mfd);
        printf("file=%s before=0x%llx after=0x%llx draw=0x%llx expect_gen=0x%llx\n",
               path, b, a, a, a / 2 + 1);
    }
    return 0;
}
