#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char **argv)
{
    char buf[4096];
    uintmax_t total = 0;
    int fd;
    int result = 0;

    if (argc != 2) {
        fprintf(stderr, "usage: %s FILE\n", argv[0]);
        return 2;
    }
    fd = open(argv[1], O_RDONLY);
    if (fd < 0) { perror("open"); return 1; }
    for (;;) {
        ssize_t n = read(fd, buf, sizeof(buf));
        if (n < 0 && errno == EINTR) continue;
        if (n < 0) { perror("read"); result = 1; break; }
        if (n == 0) break;
        if ((uintmax_t)n > UINTMAX_MAX - total) {
            fputs("byte count overflow\n", stderr);
            result = 1;
            break;
        }
        total += (uintmax_t)n;
    }
    if (close(fd) < 0) { perror("close"); result = 1; }
    if (printf("bytes_read=%" PRIuMAX "\n", total) < 0) result = 1;
    return result;
}
