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

/* Variables and functions */
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
void fread1() {
  FILE *file = fopen("./test.txt", "r");
  char buf[16];
  if (file != NULL) {
    while (!feof(file)) {
      memset(buf, 0, sizeof(buf));
      size_t i = fread(buf, 1, sizeof(buf), file);
      printf("%s", buf);
    }
  } else {
    perror("fopen");
  }
}


x86 assembly (clang, -O0):
<fread1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xe91(%rip),%rdi
lea    0xe95(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11f2 <fread1+0x92>
jmp    118f <fread1+0x2f>
mov    -0x8(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11ad <fread1+0x4d>
jmp    11ed <fread1+0x8d>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
lea    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x10,%edx
call   1030 <fread@plt>
mov    %rax,-0x28(%rbp)
lea    -0x20(%rbp),%rsi
lea    0xe2a(%rip),%rdi
call   1040 <printf@plt>
jmp    118f <fread1+0x2f>
jmp    11fe <fread1+0x9e>
lea    0xe17(%rip),%rdi
call   1080 <perror@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fread1>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xe90(%rip),%rdi
lea    0xe94(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11e5 <fread1+0x85>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    11db <fread1+0x7b>
mov    %rsp,%r14
lea    0xe73(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x10,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %eax,%eax
je     11a0 <fread1+0x40>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe24(%rip),%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1080 <perror@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"./test.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [6 x i8] c"fopen\00"
define void @fread1() {
%1 = alloca i32*
%2 = alloca [16 x i8]
%3 = alloca i64
%4 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %1
%5 = load i32*, i32** %1
%6 = icmp ne i32* %5, null
br i1 %6, label %7, label %22
7: ; preds = %0
br label %8
8: ; preds = %13, %7
%9 = load i32*, i32** %1
%10 = call i32 @feof(i32* %9)
%11 = icmp ne i32 %10, 0
%12 = xor i1 %11, true
br i1 %12, label %13, label %21
13: ; preds = %8
%14 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%15 = call i32 @memset(i8* %14, i32 0, i32 16)
%16 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%17 = load i32*, i32** %1
%18 = call i64 @fread(i8* %16, i32 1, i32 16, i32* %17)
store i64 %18, i64* %3
%19 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%20 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %19)
br label %8
21: ; preds = %8
br label %24
22: ; preds = %0
%23 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %24
24: ; preds = %22, %21
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"./test.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [6 x i8] c"fopen\00"
define void @fread1() {
%1 = alloca [16 x i8]
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%4 = icmp eq i32* %2, null
br i1 %4, label %14, label %5
5: ; preds = %0
%6 = call i32 @feof(i32* %2)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %16
8: ; preds = %5, %8
%9 = call i32 @memset(i8* %3, i32 0, i32 16)
%10 = call i64 @fread(i8* %3, i32 1, i32 16, i32* %2)
%11 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %3)
%12 = call i32 @feof(i32* %2)
%13 = icmp eq i32 %12, 0
br i1 %13, label %8, label %16
14: ; preds = %0
%15 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %16
16: ; preds = %8, %5, %14
ret void
}