#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*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int fwrite(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;
  int written_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;
    } else {
      written_len = fwrite("XYZ", 1, 3, fp);
      printf("written_len = %d\n", written_len);
      perror("fwrite");
      if (ferror(fp)) {
        printf("ferror true!\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   1070 <memset@plt>
movq   $0x0,-0x118(%rbp)
movl   $0x0,-0x11c(%rbp)
lea    0xe2f(%rip),%rdi
lea    0xe31(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1204 <main+0x74>
lea    0xe0d(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132e <main+0x19e>
jmp    1209 <main+0x79>
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
lea    -0x110(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x3,%edx
call   1040 <fread@plt>
mov    %rax,-0x118(%rbp)
lea    -0x110(%rbp),%rsi
mov    -0x118(%rbp),%rax
mov    %eax,%edx
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xdc5(%rip),%rdi
call   10a0 <perror@plt>
cmpq   $0x3,-0x118(%rbp)
03
jae    12ae <main+0x11e>
mov    -0x10(%rbp),%rdi
call   1080 <feof@plt>
cmp    $0x0,%rax
je     129b <main+0x10b>
lea    0xd9e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12a9 <main+0x119>
lea    0xd9b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    131e <main+0x18e>
mov    -0x10(%rbp),%rcx
lea    0xd95(%rip),%rdi
mov    $0x1,%esi
mov    $0x3,%edx
call   10b0 <fwrite@plt>
mov    %eax,-0x11c(%rbp)
mov    -0x11c(%rbp),%esi
lea    0xd77(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xd7b(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1314 <main+0x184>
lea    0xd63(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    131e <main+0x18e>
jmp    1319 <main+0x189>
jmp    1209 <main+0x79>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%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    0xdde(%rip),%rdi
lea    0xde0(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     131f <main+0x17f>
mov    %rax,%rbx
mov    %rsp,%r14
lea    0xdc8(%rip),%r12
lea    0xddc(%rip),%r13
lea    0xdf8(%rip),%r15
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1080 <memset@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x3,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
mov    %rax,%rbp
mov    %r12,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r13,%rdi
call   10b0 <perror@plt>
cmp    $0x2,%rbp
jbe    12de <main+0x13e>
lea    0xd90(%rip),%rdi
mov    $0x1,%esi
mov    $0x3,%edx
mov    %rbx,%rcx
call   10c0 <fwrite@plt>
lea    0xd7b(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xd7d(%rip),%rdi
call   10b0 <perror@plt>
mov    %rbx,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
je     1252 <main+0xb2>
jmp    12fb <main+0x15b>
mov    %rbx,%rdi
call   1090 <feof@plt>
test   %rax,%rax
lea    0xd67(%rip),%rax
lea    0xd70(%rip),%r15
cmove  %rax,%r15
mov    %r15,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcda(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%eax
jmp    130d <main+0x16d>

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"
@.str.6 = [4 x i8] c"XYZ\00"
@.str.7 = [18 x i8] c"written_len = %d\0A\00"
@.str.8 = [7 x i8] c"fwrite\00"
@.str.9 = [14 x i8] c"ferror true!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [256 x i8]
%4 = alloca i64
%5 = alloca i32
store i32 0, i32* %1
%6 = bitcast [256 x i8]* %3 to i8*
store i64 0, i64* %4
store i32 0, i32* %5
%7 = 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* %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 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %51
12: ; preds = %0
br label %13
13: ; preds = %12, %47
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%15 = call i32 @memset(i8* %14, i8 signext 0, i32 256)
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%17 = load i32*, i32** %2
%18 = call i64 @fread(i8* %16, i32 1, i32 3, i32* %17)
store i64 %18, i64* %4
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%20 = load i64, i64* %4
%21 = trunc i64 %20 to i32
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %19, i32 %21)
%23 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%24 = load i64, i64* %4
%25 = icmp ult i64 %24, 3
br i1 %25, label %26, label %35
26: ; preds = %13
%27 = load i32*, i32** %2
%28 = call i64 @feof(i32* %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %34
32: ; preds = %26
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
br label %34
34: ; preds = %32, %30
br label %48
35: ; preds = %13
%36 = load i32*, i32** %2
%37 = call i32 @fwrite(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 1, i32 3, i32* %36)
store i32 %37, i32* %5
%38 = load i32, i32* %5
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), i32 %38)
%40 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
%41 = load i32*, i32** %2
%42 = call i64 @ferror(i32* %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %35
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.9, i64 0, i64 0))
br label %48
46: ; preds = %35
br label %47
47: ; preds = %46
br label %13
48: ; preds = %44, %34
%49 = load i32*, i32** %2
%50 = call i32 @fclose(i32* %49)
store i32 0, i32* %1
br label %51
51: ; preds = %48, %10
%52 = load i32, i32* %1
ret i32 %52
}

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.6 = [4 x i8] c"XYZ\00"
@.str.7 = [18 x i8] c"written_len = %d\0A\00"
@.str.8 = [7 x i8] c"fwrite\00"
@str = [13 x i8] c"ferror true!\00"
@str.10 = [16 x i8] c"feof(fp): false\00"
@str.11 = [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 %28
7: ; preds = %0, %18
%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 %18
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.10, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @str.11, i64 0, i64 0)
br label %24
18: ; preds = %7
%19 = call i32 @fwrite(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 1, i32 3, i32* %3)
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), i32 %19)
%21 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
%22 = call i64 @ferror(i32* %3)
%23 = icmp eq i64 %22, 0
br i1 %23, label %7, label %24
24: ; preds = %18, %14
%25 = phi i8* [ %17, %14 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0), %18 ]
%26 = call i32 @puts(i8* dereferenceable(1) %25)
%27 = call i32 @fclose(i32* %3)
br label %28
28: ; preds = %24, %5
%29 = phi i32 [ -1, %5 ], [ 0, %24 ]
ret i32 %29
}