#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  FILE *fp;
  char buf[256] = {0};
  size_t read_len = 0;
  fp = fopen("test.txt", "rb");
  if (fp == NULL) {
    perror("test.txt");
    return -1;
  }
  while (1) {
    memset(buf, '\0', 256);
    read_len = fread(buf, 1, 3, fp);
    printf("buf = [%s], read_len = %d\n", buf, (int)read_len);
    perror("fread");
    if (read_len < 3) {
      if (feof(fp)) {
        printf("feof(fp): true\n");
      } else {
        printf("feof(fp): false\n");
      }
      break;
    }
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
movq   $0x0,-0x118(%rbp)
lea    0xe59(%rip),%rdi
lea    0xe5b(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11da <main+0x6a>
lea    0xe37(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1299 <main+0x129>
jmp    11df <main+0x6f>
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
lea    -0x110(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x3,%edx
call   1030 <fread@plt>
mov    %rax,-0x118(%rbp)
lea    -0x110(%rbp),%rsi
mov    -0x118(%rbp),%rax
mov    %eax,%edx
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xdef(%rip),%rdi
call   1090 <perror@plt>
cmpq   $0x3,-0x118(%rbp)
03
jae    1284 <main+0x114>
mov    -0x10(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%rax
je     1271 <main+0x101>
lea    0xdc8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    127f <main+0x10f>
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1289 <main+0x119>
jmp    11df <main+0x6f>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x100,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
lea    0xdff(%rip),%rdi
lea    0xe01(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     12ab <main+0x12b>
mov    %rax,%r13
mov    %rsp,%r14
lea    0xde9(%rip),%r15
lea    0xdfd(%rip),%r12
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x3,%edx
mov    %r13,%rcx
call   1040 <fread@plt>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r12,%rdi
call   10a0 <perror@plt>
cmp    $0x2,%rbx
ja     122a <main+0xaa>
mov    %r13,%rdi
call   1080 <feof@plt>
test   %rax,%rax
lea    0xdad(%rip),%rax
lea    0xdb6(%rip),%rdi
cmove  %rax,%rdi
call   1030 <puts@plt>
mov    %r13,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xd4e(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%eax
jmp    129a <main+0x11a>

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [27 x i8] c"buf = [%s], read_len = %d\0A\00"
@.str.3 = [6 x i8] c"fread\00"
@.str.4 = [16 x i8] c"feof(fp): true\0A\00"
@.str.5 = [17 x i8] c"feof(fp): false\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [256 x i8]
%4 = alloca i64
store i32 0, i32* %1
%5 = bitcast [256 x i8]* %3 to i8*
store i64 0, i64* %4
%6 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %2
%7 = load i32*, i32** %2
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %38
11: ; preds = %0
br label %12
12: ; preds = %11, %34
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%14 = call i32 @memset(i8* %13, i8 signext 0, i32 256)
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%16 = load i32*, i32** %2
%17 = call i64 @fread(i8* %15, i32 1, i32 3, i32* %16)
store i64 %17, i64* %4
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%19 = load i64, i64* %4
%20 = trunc i64 %19 to i32
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %18, i32 %20)
%22 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%23 = load i64, i64* %4
%24 = icmp ult i64 %23, 3
br i1 %24, label %25, label %34
25: ; preds = %12
%26 = load i32*, i32** %2
%27 = call i64 @feof(i32* %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %33
31: ; preds = %25
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
br label %33
33: ; preds = %31, %29
br label %35
34: ; preds = %12
br label %12
35: ; preds = %33
%36 = load i32*, i32** %2
%37 = call i32 @fclose(i32* %36)
store i32 0, i32* %1
br label %38
38: ; preds = %35, %9
%39 = load i32, i32* %1
ret i32 %39
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [27 x i8] c"buf = [%s], read_len = %d\0A\00"
@.str.3 = [6 x i8] c"fread\00"
@str = [16 x i8] c"feof(fp): false\00"
@str.6 = [15 x i8] c"feof(fp): true\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %20
7: ; preds = %0, %7
%8 = call i32 @memset(i8* %2, i8 signext 0, i32 256)
%9 = call i64 @fread(i8* %2, i32 1, i32 3, i32* %3)
%10 = trunc i64 %9 to i32
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %2, i32 %10)
%12 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%13 = icmp ult i64 %9, 3
br i1 %13, label %14, label %7
14: ; preds = %7
%15 = call i64 @feof(i32* %3)
%16 = icmp eq i64 %15, 0
%17 = select i1 %16, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @str.6, i64 0, i64 0)
%18 = call i32 @puts(i8* dereferenceable(1) %17)
%19 = call i32 @fclose(i32* %3)
br label %20
20: ; preds = %14, %5
%21 = phi i32 [ -1, %5 ], [ 0, %14 ]
ret i32 %21
}