#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*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  FILE *fp;
  int i;
  int buffer[4];
  if ((fp = fopen("myfile.txt", "rb")) == NULL) {
    printf("cant open the file");
    exit(0);
  }
  if (fread(buffer, sizeof(int), 4, fp) != 4) {
    printf("file read error\n");
    exit(0);
  }
  for (i = 0; i < 4; i++)
    printf("%d\n", buffer[i]);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
lea    0xeae(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1185 <main+0x45>
lea    0xe97(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x4,%edx
mov    %edx,%esi
call   1030 <fread@plt>
cmp    $0x4,%eax
je     11b7 <main+0x77>
lea    0xe78(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x4,-0x14(%rbp)
jge    11ec <main+0xac>
movslq -0x14(%rbp),%rax
mov    -0x30(%rbp,%rax,4),%esi
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11be <main+0x7e>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xea4(%rip),%rdi
lea    0xea8(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11cd <main+0x7d>
mov    %rsp,%rdi
mov    $0x4,%esi
mov    $0x4,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
cmp    $0x4,%eax
jne    11e2 <main+0x92>
mov    (%rsp),%esi
lea    0xe90(%rip),%rbx
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x4(%rsp),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rsp),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0xc(%rsp),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe3a(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
lea    0xe3c(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"myfile.txt\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [19 x i8] c"cant open the file\00"
@.str.3 = [17 x i8] c"file read error\0A\00"
@.str.4 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca [4 x i32]
store i32 0, i32* %1
%5 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %2
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%9 = call i32 @exit(i32 0)
unreachable
10: ; preds = %0
%11 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 0
%12 = load i32*, i32** %2
%13 = call i32 @fread(i32* %11, i32 4, i32 4, i32* %12)
%14 = icmp ne i32 %13, 4
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%17 = call i32 @exit(i32 0)
unreachable
18: ; preds = %10
store i32 0, i32* %3
br label %19
19: ; preds = %28, %18
%20 = load i32, i32* %3
%21 = icmp slt i32 %20, 4
br i1 %21, label %22, label %31
22: ; preds = %19
%23 = load i32, i32* %3
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 %24
%26 = load i32, i32* %25
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %22
%29 = load i32, i32* %3
%30 = add nsw i32 %29, 1
store i32 %30, i32* %3
br label %19
31: ; preds = %19
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"myfile.txt\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [19 x i8] c"cant open the file\00"
@.str.4 = [4 x i8] c"%d\0A\00"
@str = [16 x i8] c"file read error\00"
define i32 @main() {
%1 = alloca [4 x i32]
%2 = bitcast [4 x i32]* %1 to i8*
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 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 %8
5: ; preds = %0
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%7 = tail call i32 @exit(i32 0)
unreachable
8: ; preds = %0
%9 = getelementptr inbounds [4 x i32], [4 x i32]* %1, i64 0, i64 0
%10 = call i32 @fread(i32* %9, i32 4, i32 4, i32* %3)
%11 = icmp eq i32 %10, 4
br i1 %11, label %12, label %24
12: ; preds = %8
%13 = load i32, i32* %9
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %13)
%15 = getelementptr inbounds [4 x i32], [4 x i32]* %1, i64 0, i64 1
%16 = load i32, i32* %15
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %16)
%18 = getelementptr inbounds [4 x i32], [4 x i32]* %1, i64 0, i64 2
%19 = load i32, i32* %18
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %19)
%21 = getelementptr inbounds [4 x i32], [4 x i32]* %1, i64 0, i64 3
%22 = load i32, i32* %21
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %22)
ret i32 0
24: ; preds = %8
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%26 = tail call i32 @exit(i32 0)
unreachable
}