#include <stdlib.h>
#include <stdio.h>

int main(int argc, char* argv[]) {
    if (argc != 3) {
        fprintf(stderr, "Usage: compare-with-extra-read FILE1 FILE2\n");
        exit(EXIT_FAILURE);
    }
    FILE* f1 = fopen(argv[1], "r");
    if (f1 == NULL) {
        fprintf(stderr, "compare-with-extra-read: Couldn't open '%s'\n", argv[1]);
        exit(EXIT_FAILURE);
    }
    FILE* f2 = fopen(argv[2], "r");
    if (f2 == NULL) {
        fprintf(stderr, "compare-with-extra-read: Couldn't open '%s'\n", argv[2]);
        exit(EXIT_FAILURE);
    }

    /* Read both files, just to get them into cache! */
    while (getc(f1) != EOF)
        ;
    rewind(f1);
    while (getc(f2) != EOF)
        ;
    rewind(f2);

    int c1, c2;
    int n = 0;
    while ((c1 = getc(f1)) != EOF) {
        c2 = getc(f2);
        if (c1 != c2) {
            printf("Different at char number %d.\n", n);
            return EXIT_SUCCESS;
        }
        ++n;
    }
    if (getc(f2) != EOF) {
        fprintf(stderr, "compare-with-extra-read: Not the same length\n");
        exit(EXIT_FAILURE);
    }
    printf("Same.\n");            

    return EXIT_SUCCESS;
}
