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

int main(int argc, char **argv)
{
    if (argc != 5) {
        fprintf(stderr, "%s\n",
                "usage: fcrop <in_file> <bytes_skip> <bytes_write> <out_file>");
        exit(1);
    }

    FILE *f1 = fopen(argv[1], "rb");
    FILE *f2 = fopen(argv[4], "wb");
    assert(f1 && f2);

    int offset = atoi(argv[2]);
    int length = atoi(argv[3]);
    assert(offset >= 0 && length > 0);

    int res = fseek(f1, offset, SEEK_SET);
    assert(res == 0);

    char *buf = malloc(length);
    assert(buf);

    res = fread(buf, length, 1, f1);
    assert(res == 1);

    res = fwrite(buf, length, 1, f2);
    assert(res == 1);

    free(buf);
    fclose(f1);
    fclose(f2);
}
