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

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  FILE *fp;
  int i;
  char buf[4];
  printf("BUFSIZ = %d\n", BUFSIZ);
  fp = fopen("test_1.txt", "rb");
  if (fp == NULL) {
    printf("fopen error!\n");
    return -1;
  }
  printf("fread start\n");
  for (i = 0; i < 2048; i++) {
    fread(buf, 4, 1, fp);
  }
  printf("fread end\n");
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xea0(%rip),%rdi
lea    0xea4(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a2 <main+0x62>
lea    0xe8c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1207 <main+0xc7>
lea    0xe80(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x800,-0x14(%rbp)
jge    11e9 <main+0xa9>
lea    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11b7 <main+0x77>
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
lea    0xe98(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe97(%rip),%rdi
lea    0xe9b(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11d2 <main+0x82>
mov    %rax,%rbx
lea    0xe8a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x800,%ebp
lea    0xc(%rsp),%r14
mov    %r14,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
add    $0xffffffff,%ebp
jne    11a0 <main+0x50>
lea    0xe66(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
jmp    11e3 <main+0x93>
lea    0xe58(%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"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [14 x i8] c"fopen error!\0A\00"
@.str.4 = [13 x i8] c"fread start\0A\00"
@.str.5 = [11 x i8] c"fread 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 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %7, i32** %2
%8 = load i32*, i32** %2
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
br label %28
12: ; preds = %0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %3
br label %14
14: ; preds = %21, %12
%15 = load i32, i32* %3
%16 = icmp slt i32 %15, 2048
br i1 %16, label %17, label %24
17: ; preds = %14
%18 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%19 = load i32*, i32** %2
%20 = call i32 @fread(i8* %18, i32 4, i32 1, i32* %19)
br label %21
21: ; preds = %17
%22 = load i32, i32* %3
%23 = add nsw i32 %22, 1
store i32 %23, i32* %3
br label %14
24: ; preds = %14
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%26 = load i32*, i32** %2
%27 = call i32 @fclose(i32* %26)
store i32 0, i32* %1
br label %28
28: ; preds = %24, %10
%29 = load i32, i32* %1
ret i32 %29
}

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"
@.str.2 = [3 x i8] c"rb\00"
@str = [12 x i8] c"fread start\00"
@str.6 = [10 x i8] c"fread end\00"
@str.7 = [13 x i8] c"fopen 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 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.7, i64 0, i64 0))
br label %19
9: ; preds = %0
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %11
11: ; preds = %9, %11
%12 = phi i32 [ 0, %9 ], [ %14, %11 ]
%13 = call i32 @fread(i8* %2, i32 4, i32 1, i32* %5)
%14 = add nuw nsw i32 %12, 1
%15 = icmp eq i32 %14, 2048
br i1 %15, label %16, label %11
16: ; preds = %11
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.6, i64 0, i64 0))
%18 = tail call i32 @fclose(i32* %5)
br label %19
19: ; preds = %16, %7
%20 = phi i32 [ -1, %7 ], [ 0, %16 ]
ret i32 %20
}