#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ memset(char *, int, int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
char *strerror(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fd;
  int ret;
  char buf[2 + 1];
  if (argc != 2) {
    fprintf(stderr, "usage : %s file\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  memset(buf, 0x00, sizeof(buf));
  if ((fd = open(argv[1], O_RDONLY)) == -1)
    do {
      fprintf(stderr, "[%s][%d]:%s %s\n",
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/"
              "master/apue/day01/read.c",
              31, "open", strerror(errno));
      exit(EXIT_FAILURE);
    } while (0);
  for (;;) {
    if ((ret = read(fd, buf, 2)) <= 0)
      break;
    fprintf(stdout, "%s", buf);
    fflush(stdout);
    memset(buf, 0x00, sizeof(buf));
    sleep(1);
  }
  if (ret == -1) {
    close(fd);
    do {
      fprintf(stderr, "[%s][%d]:%s %s\n",
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/"
              "master/apue/day01/read.c",
              44, "read", strerror(errno));
      exit(EXIT_FAILURE);
    } while (0);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11dc <main+0x4c>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe39(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    -0x1b(%rbp),%rdi
xor    %esi,%esi
mov    $0x3,%edx
call   1030 <memset@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1262 <main+0xd2>
jmp    1213 <main+0x83>
mov    0x2dbe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%r9
lea    0xdd7(%rip),%rsi
lea    0xde0(%rip),%rdx
mov    $0x1f,%ecx
lea    0xe67(%rip),%r8
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    1267 <main+0xd7>
mov    -0x14(%rbp),%edi
lea    -0x1b(%rbp),%rsi
mov    $0x2,%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jg     1289 <main+0xf9>
jmp    12d1 <main+0x141>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
lea    -0x1b(%rbp),%rdx
lea    0xe1c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
lea    -0x1b(%rbp),%rdi
xor    %esi,%esi
mov    $0x3,%edx
call   1030 <memset@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
jmp    1267 <main+0xd7>
cmpl   $0xffffffff,-0x18(%rbp)
jne    1332 <main+0x1a2>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    0x2cee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2cda(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%r9
lea    0xd07(%rip),%rsi
lea    0xd10(%rip),%rdx
mov    $0x2c,%ecx
lea    0xd9f(%rip),%r8
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2c9d(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    1265 <main+0xd5>
lea    0xd(%rsp),%rdi
xor    %esi,%esi
mov    $0x3,%edx
call   1030 <memset@plt>
mov    0x8(%rbx),%rdi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1281 <main+0xf1>
mov    %eax,%r15d
lea    0xd(%rsp),%rsi
mov    %eax,%edi
mov    $0x2,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    1247 <main+0xb7>
mov    0x2dcd(%rip),%r12
lea    0xebf(%rip),%r14
lea    0xd(%rsp),%rbx
nop
mov    (%r12),%edi
mov    %r14,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%r12),%edi
call   1070 <fflush@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x3,%edx
call   1030 <memset@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x2,%edx
call   1050 <read@plt>
test   %eax,%eax
jg     1200 <main+0x70>
mov    %eax,%ebp
mov    %r15d,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%ebp
je     12b6 <main+0x126>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xd88(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    12f3 <main+0x163>
mov    0x2d50(%rip),%rax
mov    (%rax),%ebx
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd72(%rip),%rsi
lea    0xd7b(%rip),%rdx
lea    0xe07(%rip),%r8
mov    %ebx,%edi
mov    $0x1f,%ecx
jmp    12e9 <main+0x159>
mov    0x2d1b(%rip),%rax
mov    (%rax),%ebx
mov    0x2d0a(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd3d(%rip),%rsi
lea    0xd46(%rip),%rdx
lea    0xdda(%rip),%r8
mov    %ebx,%edi
mov    $0x2c,%ecx
mov    %rax,%r9
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2cce(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [17 x i8] c"usage : %s file\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [16 x i8] c"[%s][%d]:%s %s\0A\00"
@.str.2 = [147 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/master/apue/day01/read.c\00"
@.str.3 = [5 x i8] c"open\00"
@errno = global i32 0
@stdout = global i32 0
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [5 x i8] c"read\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [3 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %19
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
%20 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 0
%21 = call i32 @memset(i8* %20, i32 0, i32 3)
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = load i32, i32* @O_RDONLY
%26 = call i32 @open(i8* %24, i32 %25)
store i32 %26, i32* %6
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %37
28: ; preds = %19
br label %29
29: ; preds = %28
%30 = load i32, i32* @stderr
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str.2, i64 0, i64 0), i32 31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %32)
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; No predecessors!
br label %37
37: ; preds = %36, %19
br label %38
38: ; preds = %44, %37
%39 = load i32, i32* %6
%40 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 0
%41 = call i32 @read(i32 %39, i8* %40, i32 2)
store i32 %41, i32* %7
%42 = icmp sle i32 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %38
br label %53
44: ; preds = %38
%45 = load i32, i32* @stdout
%46 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 0
%47 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %46)
%48 = load i32, i32* @stdout
%49 = call i32 @fflush(i32 %48)
%50 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 0
%51 = call i32 @memset(i8* %50, i32 0, i32 3)
%52 = call i32 @sleep(i32 1)
br label %38
53: ; preds = %43
%54 = load i32, i32* %7
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %67
56: ; preds = %53
%57 = load i32, i32* %6
%58 = call i32 @close(i32 %57)
br label %59
59: ; preds = %56
%60 = load i32, i32* @stderr
%61 = load i32, i32* @errno
%62 = call i8* @strerror(i32 %61)
%63 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str.2, i64 0, i64 0), i32 44, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* %62)
%64 = load i32, i32* @EXIT_FAILURE
%65 = call i32 @exit(i32 %64)
unreachable
66: ; No predecessors!
br label %67
67: ; preds = %66, %53
%68 = load i32, i32* %6
%69 = call i32 @close(i32 %68)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [17 x i8] c"usage : %s file\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [16 x i8] c"[%s][%d]:%s %s\0A\00"
@.str.2 = [147 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/master/apue/day01/read.c\00"
@.str.3 = [5 x i8] c"open\00"
@errno = global i32 0
@stdout = global i32 0
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [5 x i8] c"read\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [3 x i8]
%4 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %12, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %2
%13 = call i32 @memset(i8* %4, i32 0, i32 3)
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* %15, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %22, label %19
19: ; preds = %12
%20 = call i32 @read(i32 %17, i8* %4, i32 2)
%21 = icmp slt i32 %20, 1
br i1 %21, label %38, label %29
22: ; preds = %12
%23 = load i32, i32* @stderr
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str.2, i64 0, i64 0), i32 31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %25)
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %19, %29
%30 = load i32, i32* @stdout
%31 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %4)
%32 = load i32, i32* @stdout
%33 = call i32 @fflush(i32 %32)
%34 = call i32 @memset(i8* %4, i32 0, i32 3)
%35 = call i32 @sleep(i32 1)
%36 = call i32 @read(i32 %17, i8* %4, i32 2)
%37 = icmp slt i32 %36, 1
br i1 %37, label %38, label %29
38: ; preds = %29, %19
%39 = phi i32 [ %20, %19 ], [ %36, %29 ]
%40 = icmp eq i32 %39, -1
%41 = call i32 @close(i32 %17)
br i1 %40, label %42, label %49
42: ; preds = %38
%43 = load i32, i32* @stderr
%44 = load i32, i32* @errno
%45 = call i8* @strerror(i32 %44)
%46 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str.2, i64 0, i64 0), i32 44, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* %45)
%47 = load i32, i32* @EXIT_FAILURE
%48 = call i32 @exit(i32 %47)
unreachable
49: ; preds = %38
ret i32 0
}