// gnoFile.c — buffered FILE* file I/O on GNO/ME.  Writes a file, reopens
// it, reads it back, and verifies the content round-trips byte-for-byte.
// Proves fopen/fwrite/fread/fclose route to real GS/OS files via libc's
// FILE* layer + GNO's inline-form GS/OS dispatch.
#include <stdint.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char **argv) {
    const char *path = "llvm816.txt";
    const char *msg  = "written by llvm816 under GNO";

    FILE *f = fopen(path, "w");
    if (!f) { printf("fopen(w) failed\n"); return 1; }
    fwrite(msg, 1, strlen(msg), f);
    fclose(f);

    char buf[64];
    f = fopen(path, "r");
    if (!f) { printf("fopen(r) failed\n"); return 1; }
    size_t n = fread(buf, 1, sizeof(buf) - 1, f);
    fclose(f);
    buf[n] = 0;

    int ok = (n == strlen(msg)) && (strcmp(buf, msg) == 0);
    printf("read %d bytes, content %s\n", (int)n, ok ? "OK" : "MISMATCH");

    *(volatile uint16_t *)0x025000UL = ok ? 0xC0DE : 0xBAD0;
    for (volatile unsigned long i = 0; i < 400000UL; i++) {}
    return 0;
}
