#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*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  FILE *fp;
  size_t len;
  char buff[1024];
  fp = fopen("/etc/hosts", "r");
  if (!fp) {
    printf("fopen error\n");
    return 1;
  }
  while ((len = fread(buff, 1, 1024, fp))) {
    buff[len] = '\0';
    printf("%s", buff);
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xea7(%rip),%rdi
lea    0xeab(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    118e <main+0x4e>
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11f1 <main+0xb1>
jmp    1193 <main+0x53>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1030 <fread@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11e1 <main+0xa1>
mov    -0x18(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
lea    -0x420(%rbp),%rsi
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1193 <main+0x53>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x400,%rsp
lea    0xe9d(%rip),%rdi
lea    0xeae(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11d7 <main+0x87>
mov    %rax,%r15
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     11cb <main+0x7b>
lea    0xe73(%rip),%r14
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
movb   $0x0,(%rsp,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
test   %rax,%rax
jne    11a0 <main+0x50>
mov    %r15,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
jmp    11e8 <main+0x98>
lea    0xe30(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/etc/hosts\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [13 x i8] c"fopen error\0A\00"
@.str.3 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i64
%4 = alloca [1024 x i8]
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 ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %2
%6 = load i32*, i32** %2
%7 = icmp ne i32* %6, null
br i1 %7, label %10, label %8
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %24
10: ; preds = %0
br label %11
11: ; preds = %16, %10
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%13 = load i32*, i32** %2
%14 = call i64 @fread(i8* %12, i32 1, i32 1024, i32* %13)
store i64 %14, i64* %3
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %11
%17 = load i64, i64* %3
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %17
store i8 0, i8* %18
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %19)
br label %11
21: ; preds = %11
%22 = load i32*, i32** %2
%23 = call i32 @fclose(i32* %22)
store i32 0, i32* %1
br label %24
24: ; preds = %21, %8
%25 = load i32, i32* %1
ret i32 %25
}

LLVM IR (-O2):
@.str = [11 x i8] c"/etc/hosts\00"
@.str.1 = [2 x i8] c"r\00"
@.str.3 = [3 x i8] c"%s\00"
@str = [12 x i8] c"fopen error\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = 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))
%4 = icmp eq i32* %3, null
br i1 %4, label %8, label %5
5: ; preds = %0
%6 = call i64 @fread(i8* %2, i32 1, i32 1024, i32* %3)
%7 = icmp eq i64 %6, 0
br i1 %7, label %16, label %10
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %18
10: ; preds = %5, %10
%11 = phi i64 [ %14, %10 ], [ %6, %5 ]
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %11
store i8 0, i8* %12
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %2)
%14 = call i64 @fread(i8* %2, i32 1, i32 1024, i32* %3)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %10
16: ; preds = %10, %5
%17 = call i32 @fclose(i32* %3)
br label %18
18: ; preds = %16, %8
%19 = phi i32 [ 0, %16 ], [ 1, %8 ]
ret i32 %19
}