#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*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ memset(char *, int, int);
int open(char *, int /*<<< orphan*/);
scalar_t__ pread(int, char *, int, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(void) {
  int fd;
  int ret;
  int index = 0;
  char buf[1 + 1] = {};
  if ((fd = open("test.txt", 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/day02/step.c",
              25, "open", strerror(errno));
      exit(EXIT_FAILURE);
    } while (0);
  while (1) {
    if (pread(fd, buf, 1, index) <= 0)
      break;
    fprintf(stderr, "%s", buf);
    index += 2;
    memset(buf, 0x00, sizeof(buf));
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x10(%rbp)
lea    -0x12(%rbp),%rdi
xor    %esi,%esi
mov    $0x2,%edx
call   1030 <memset@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
lea    0xe5a(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    120b <main+0x9b>
jmp    11bc <main+0x4c>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%r9
lea    0xe26(%rip),%rsi
lea    0xe2f(%rip),%rdx
mov    $0x19,%ecx
lea    0xeb6(%rip),%r8
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    1210 <main+0xa0>
mov    -0x8(%rbp),%edi
lea    -0x12(%rbp),%rsi
mov    -0x10(%rbp),%ecx
mov    $0x1,%edx
call   1070 <pread@plt>
cmp    $0x0,%rax
jg     1233 <main+0xc3>
jmp    126c <main+0xfc>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
lea    -0x12(%rbp),%rdx
lea    0xe6a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x10(%rbp)
lea    -0x12(%rbp),%rdi
xor    %esi,%esi
mov    $0x2,%edx
call   1030 <memset@plt>
jmp    1210 <main+0xa0>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movw   $0x0,0xe(%rsp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
lea    0xe6d(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1231 <main+0xc1>
mov    %eax,%r15d
lea    0xe(%rsp),%rsi
mov    %eax,%edi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1070 <pread@plt>
test   %rax,%rax
jle    121a <main+0xaa>
mov    $0x2,%ebp
mov    0x2e10(%rip),%r12
lea    0xee2(%rip),%r14
lea    0xe(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r12),%edi
mov    %r14,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x2,%edx
call   1030 <memset@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
mov    %ebp,%ecx
call   1070 <pread@plt>
add    $0x2,%ebp
test   %rax,%rax
jg     11e0 <main+0x70>
mov    %r15d,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2da0(%rip),%rax
mov    (%rax),%ebx
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdba(%rip),%rsi
lea    0xdc3(%rip),%rdx
lea    0xe4f(%rip),%r8
mov    %ebx,%edi
mov    $0x19,%ecx
mov    %rax,%r9
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@O_RDONLY = global i32 0
@stderr = 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/day02/step.c\00"
@.str.3 = [5 x i8] c"open\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.4 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [2 x i8]
store i32 0, i32* %1
store i32 0, i32* %4
%6 = bitcast [2 x i8]* %5 to i8*
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %19
10: ; preds = %0
br label %11
11: ; preds = %10
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %12, 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 25, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; No predecessors!
br label %19
19: ; preds = %18, %0
br label %20
20: ; preds = %19, %27
%21 = load i32, i32* %2
%22 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%23 = load i32, i32* %4
%24 = call i64 @pread(i32 %21, i8* %22, i32 1, i32 %23)
%25 = icmp sle i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %20
br label %35
27: ; preds = %20
%28 = load i32, i32* @stderr
%29 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%30 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %29)
%31 = load i32, i32* %4
%32 = add nsw i32 %31, 2
store i32 %32, i32* %4
%33 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%34 = call i32 @memset(i8* %33, i32 0, i32 2)
br label %20
35: ; preds = %26
%36 = load i32, i32* %2
%37 = call i32 @close(i32 %36)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@O_RDONLY = global i32 0
@stderr = 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/day02/step.c\00"
@.str.3 = [5 x i8] c"open\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.4 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = alloca i16
%2 = bitcast i16* %1 to i8*
store i16 0, i16* %1
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %9, label %6
6: ; preds = %0
%7 = call i64 @pread(i32 %4, i8* %2, i32 1, i32 0)
%8 = icmp slt i64 %7, 1
br i1 %8, label %24, label %16
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = tail call i8* @strerror(i32 %11)
%13 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %10, 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 25, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %12)
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %6, %16
%17 = phi i32 [ %20, %16 ], [ 0, %6 ]
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %2)
%20 = add nuw nsw i32 %17, 2
%21 = call i32 @memset(i8* %2, i32 0, i32 2)
%22 = call i64 @pread(i32 %4, i8* %2, i32 1, i32 %20)
%23 = icmp slt i64 %22, 1
br i1 %23, label %24, label %16
24: ; preds = %16, %6
%25 = call i32 @close(i32 %4)
ret i32 0
}