#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 fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
int ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ stderr;
int main() {
  const char filename[] = "test.txt";
  char buffer[16];
  int r;
  FILE *fp;
  fp = fopen(filename, "r");
  if (fp == NULL) {
    fprintf(stderr, "Unable to open %s\n", filename);
    return (1);
  }
  r = fread(buffer, 1, 16, fp);
  if (r != 16) {
    if (feof(fp))
      printf("Read %d bytes from %s\n", r, filename);
    else {
      r = ferror(fp);
      printf("Error: ferror() returned %d\n", r);
    }
  }
  r = fclose(fp);
  if (r != 0) {
    fprintf(stderr, "Unable to close file\n");
    return (1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe7a(%rip),%rax
mov    %rax,-0xd(%rbp)
mov    0xe78(%rip),%al
mov    %al,-0x5(%rbp)
lea    -0xd(%rbp),%rdi
lea    0xe6b(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11d9 <main+0x69>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
lea    -0xd(%rbp),%rdx
lea    0xe45(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    128c <main+0x11c>
lea    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
mov    $0x10,%edx
call   1040 <fread@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x10,-0x24(%rbp)
je     124c <main+0xdc>
mov    -0x30(%rbp),%rdi
call   1080 <feof@plt>
cmp    $0x0,%rax
je     122a <main+0xba>
mov    -0x24(%rbp),%esi
lea    -0xd(%rbp),%rdx
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1247 <main+0xd7>
mov    -0x30(%rbp),%rdi
call   1030 <ferror@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    124c <main+0xdc>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1285 <main+0x115>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
lea    0xde0(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    128c <main+0x11c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
movabs $0x7478742e74736574,%rax
74 78 74
mov    %rax,(%rsp)
movb   $0x0,0x8(%rsp)
lea    0xe79(%rip),%rsi
mov    %rsp,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     11e0 <main+0x70>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x10,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
cmp    $0x10,%eax
je     1214 <main+0xa4>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1080 <feof@plt>
test   %rax,%rax
je     11fc <main+0x8c>
lea    0xe4c(%rip),%rdi
mov    %rsp,%rdx
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1214 <main+0xa4>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
mov    %rsp,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1237 <main+0xc7>
mov    %rbx,%rdi
call   1030 <ferror@plt>
lea    0xe2a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
test   %eax,%eax
je     123e <main+0xce>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
lea    0xe22(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%eax
jmp    1240 <main+0xd0>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.filename = [9 x i8] c"test.txt\00"
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [19 x i8] c"Unable to open %s\0A\00"
@.str.2 = [23 x i8] c"Read %d bytes from %s\0A\00"
@.str.3 = [29 x i8] c"Error: ferror() returned %d\0A\00"
@.str.4 = [22 x i8] c"Unable to close file\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [9 x i8]
%3 = alloca [16 x i8]
%4 = alloca i32
%5 = alloca i32*
store i32 0, i32* %1
%6 = bitcast [9 x i8]* %2 to i8*
%7 = getelementptr inbounds [9 x i8], [9 x i8]* %2, i64 0, i64 0
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %5
%9 = load i32*, i32** %5
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %15
11: ; preds = %0
%12 = load i32, i32* @stderr
%13 = getelementptr inbounds [9 x i8], [9 x i8]* %2, i64 0, i64 0
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %13)
store i32 1, i32* %1
br label %44
15: ; preds = %0
%16 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%17 = load i32*, i32** %5
%18 = call i32 @fread(i8* %16, i32 1, i32 16, i32* %17)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = icmp ne i32 %19, 16
br i1 %20, label %21, label %35
21: ; preds = %15
%22 = load i32*, i32** %5
%23 = call i64 @feof(i32* %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = load i32, i32* %4
%27 = getelementptr inbounds [9 x i8], [9 x i8]* %2, i64 0, i64 0
%28 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %26, i8* %27)
br label %34
29: ; preds = %21
%30 = load i32*, i32** %5
%31 = call i32 @ferror(i32* %30)
store i32 %31, i32* %4
%32 = load i32, i32* %4
%33 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %32)
br label %34
34: ; preds = %29, %25
br label %35
35: ; preds = %34, %15
%36 = load i32*, i32** %5
%37 = call i32 @fclose(i32* %36)
store i32 %37, i32* %4
%38 = load i32, i32* %4
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %35
%41 = load i32, i32* @stderr
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %44
43: ; preds = %35
store i32 0, i32* %1
br label %44
44: ; preds = %43, %40, %11
%45 = load i32, i32* %1
ret i32 %45
}

LLVM IR (-O2):
@__const.main.filename = [9 x i8] c"test.txt\00"
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [19 x i8] c"Unable to open %s\0A\00"
@.str.2 = [23 x i8] c"Read %d bytes from %s\0A\00"
@.str.3 = [29 x i8] c"Error: ferror() returned %d\0A\00"
@.str.4 = [22 x i8] c"Unable to close file\0A\00"
define i32 @main() {
%1 = alloca [9 x i8]
%2 = alloca [16 x i8]
%3 = getelementptr inbounds [9 x i8], [9 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%5 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %3)
br label %27
10: ; preds = %0
%11 = call i32 @fread(i8* %4, i32 1, i32 16, i32* %5)
%12 = icmp eq i32 %11, 16
br i1 %12, label %21, label %13
13: ; preds = %10
%14 = tail call i64 @feof(i32* %5)
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %11, i8* %3)
br label %21
18: ; preds = %13
%19 = tail call i32 @ferror(i32* %5)
%20 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %16, %18, %10
%22 = call i32 @fclose(i32* %5)
%23 = icmp eq i32 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %27
27: ; preds = %21, %24, %7
%28 = phi i32 [ 1, %7 ], [ 1, %24 ], [ 0, %21 ]
ret i32 %28
}