#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 */

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int main(void) {
  int fd;
  int i;
  char buf[4];
  printf("BUFSIZ = %d\n", BUFSIZ);
  fd = open("test_1.txt", O_RDONLY);
  if (fd == -1) {
    printf("open error!\n");
    return -1;
  }
  printf("read start\n");
  for (i = 0; i < 2048; i++) {
    read(fd, buf, 4);
  }
  printf("read end\n");
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
lea    0xe97(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11a2 <main+0x62>
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <main+0xc0>
lea    0xe7c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0xc(%rbp)
cmpl   $0x800,-0xc(%rbp)
jge    11e3 <main+0xa3>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11b7 <main+0x77>
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
lea    0xe98(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0xe8e(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11db <main+0x8b>
mov    %eax,%ebx
lea    0xe86(%rip),%rdi
call   1030 <puts@plt>
mov    $0x800,%ebp
lea    0xc(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1060 <read@plt>
add    $0xffffffff,%ebp
jne    11b0 <main+0x60>
lea    0xe58(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    11ec <main+0x9c>
lea    0xe4a(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"BUFSIZ = %d\0A\00"
@BUFSIZ = global i32 0
@.str.1 = [11 x i8] c"test_1.txt\00"
@O_RDONLY = global i32 0
@.str.2 = [13 x i8] c"open error!\0A\00"
@.str.3 = [12 x i8] c"read start\0A\00"
@.str.4 = [10 x i8] c"read end\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [4 x i8]
store i32 0, i32* %1
%5 = load i32, i32* @BUFSIZ
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %29
13: ; preds = %0
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
br label %15
15: ; preds = %22, %13
%16 = load i32, i32* %3
%17 = icmp slt i32 %16, 2048
br i1 %17, label %18, label %25
18: ; preds = %15
%19 = load i32, i32* %2
%20 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%21 = call i32 @read(i32 %19, i8* %20, i32 4)
br label %22
22: ; preds = %18
%23 = load i32, i32* %3
%24 = add nsw i32 %23, 1
store i32 %24, i32* %3
br label %15
25: ; preds = %15
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%27 = load i32, i32* %2
%28 = call i32 @close(i32 %27)
store i32 0, i32* %1
br label %29
29: ; preds = %25, %11
%30 = load i32, i32* %1
ret i32 %30
}

LLVM IR (-O2):
@.str = [13 x i8] c"BUFSIZ = %d\0A\00"
@BUFSIZ = global i32 0
@.str.1 = [11 x i8] c"test_1.txt\00"
@O_RDONLY = global i32 0
@str = [11 x i8] c"read start\00"
@str.5 = [9 x i8] c"read end\00"
@str.6 = [12 x i8] c"open error!\00"
define i32 @main() {
%1 = alloca [4 x i8]
%2 = getelementptr inbounds [4 x i8], [4 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @BUFSIZ
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.6, i64 0, i64 0))
br label %20
10: ; preds = %0
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
br label %12
12: ; preds = %10, %12
%13 = phi i32 [ 0, %10 ], [ %15, %12 ]
%14 = call i32 @read(i32 %6, i8* %2, i32 4)
%15 = add nuw nsw i32 %13, 1
%16 = icmp eq i32 %15, 2048
br i1 %16, label %17, label %12
17: ; preds = %12
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.5, i64 0, i64 0))
%19 = tail call i32 @close(i32 %6)
br label %20
20: ; preds = %17, %8
%21 = phi i32 [ -1, %8 ], [ 0, %17 ]
ret i32 %21
}