#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 const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
unsigned int read_whole_file(const char *fname, char *buffer, int buffer_size) {
  memset(buffer, 0, buffer_size);
  FILE *f = fopen(fname, "rb");
  if (f == NULL) {
    fprintf(stderr, "Cannot open %s!\n", fname);
    return -1;
  }
  int read_byte_count = fread(buffer, 1, buffer_size - 1, f);
  fclose(f);
  if (read_byte_count < 0) {
    fprintf(stderr, "Couldn't read %s\n", fname);
    return -1;
  }
  while (buffer[read_byte_count - 1] == '\n') {
    buffer[--read_byte_count] = 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_whole_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    0xe84(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b5 <read_whole_file+0x65>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe5f(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1247 <read_whole_file+0xf7>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
sub    $0x1,%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x0,-0x2c(%rbp)
jge    1208 <read_whole_file+0xb8>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe1d(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1247 <read_whole_file+0xf7>
jmp    120d <read_whole_file+0xbd>
mov    -0x18(%rbp),%rax
mov    -0x2c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1240 <read_whole_file+0xf0>
mov    -0x18(%rbp),%rax
mov    -0x2c(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x2c(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    120d <read_whole_file+0xbd>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_whole_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
lea    0xe90(%rip),%rsi
mov    %r14,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11d1 <read_whole_file+0x81>
mov    %rax,%rbx
add    $0xffffffff,%ebp
mov    %r15,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %ebp,%ebp
js     11da <read_whole_file+0x8a>
movslq %ebp,%rcx
xor    %eax,%eax
cmpb   $0xa,-0x1(%rcx,%r15,1)
jne    11f7 <read_whole_file+0xa7>
add    %r15,%rcx
add    $0xffffffffffffffff,%rcx
mov    %ebp,%edx
add    %r15,%rdx
add    $0xfffffffffffffffe,%rdx
movb   $0x0,(%rcx)
cmpb   $0xa,(%rdx)
mov    %rdx,%rcx
lea    -0x1(%rdx),%rdx
je     11c0 <read_whole_file+0x70>
jmp    11f7 <read_whole_file+0xa7>
lea    0xe2b(%rip),%rsi
jmp    11e1 <read_whole_file+0x91>
lea    0xe33(%rip),%rsi
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"Cannot open %s!\0A\00"
@.str.2 = [18 x i8] c"Couldn't read %s\0A\00"
define i32 @read_whole_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %6
%11 = load i32, i32* %7
%12 = call i32 @memset(i8* %10, i32 0, i32 %11)
%13 = load i8*, i8** %5
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %8
%15 = load i32*, i32** %8
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %21
17: ; preds = %3
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %5
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %19)
store i32 -1, i32* %4
br label %52
21: ; preds = %3
%22 = load i8*, i8** %6
%23 = load i32, i32* %7
%24 = sub nsw i32 %23, 1
%25 = load i32*, i32** %8
%26 = call i32 @fread(i8* %22, i32 1, i32 %24, i32* %25)
store i32 %26, i32* %9
%27 = load i32*, i32** %8
%28 = call i32 @fclose(i32* %27)
%29 = load i32, i32* %9
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %21
%32 = load i32, i32* @stderr
%33 = load i8*, i8** %5
%34 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %33)
store i32 -1, i32* %4
br label %52
35: ; preds = %21
br label %36
36: ; preds = %45, %35
%37 = load i8*, i8** %6
%38 = load i32, i32* %9
%39 = sub nsw i32 %38, 1
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %37, i64 %40
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp eq i32 %43, 10
br i1 %44, label %45, label %51
45: ; preds = %36
%46 = load i8*, i8** %6
%47 = load i32, i32* %9
%48 = add nsw i32 %47, -1
store i32 %48, i32* %9
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %46, i64 %49
store i8 0, i8* %50
br label %36
51: ; preds = %36
store i32 0, i32* %4
br label %52
52: ; preds = %51, %31, %17
%53 = load i32, i32* %4
ret i32 %53
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"Cannot open %s!\0A\00"
@.str.2 = [18 x i8] c"Couldn't read %s\0A\00"
define i32 @read_whole_file(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32 @memset(i8* %1, i32 0, i32 %2)
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %28, label %7
7: ; preds = %3
%8 = add nsw i32 %2, -1
%9 = tail call i32 @fread(i8* %1, i32 1, i32 %8, i32* %5)
%10 = tail call i32 @fclose(i32* %5)
%11 = icmp slt i32 %9, 0
br i1 %11, label %28, label %12
12: ; preds = %7
%13 = add nsw i32 %9, -1
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %1, i64 %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 10
br i1 %17, label %18, label %32
18: ; preds = %12
%19 = zext i32 %9 to i64
%20 = add nsw i64 %19, -1
br label %21
21: ; preds = %18, %21
%22 = phi i64 [ %20, %18 ], [ %24, %21 ]
%23 = phi i8* [ %15, %18 ], [ %25, %21 ]
store i8 0, i8* %23
%24 = add i64 %22, -1
%25 = getelementptr inbounds i8, i8* %1, i64 %24
%26 = load i8, i8* %25
%27 = icmp eq i8 %26, 10
br i1 %27, label %21, label %32
28: ; preds = %7, %3
%29 = phi i8* [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), %3 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), %7 ]
%30 = load i32, i32* @stderr
%31 = tail call i32 @fprintf(i32 %30, i8* %29, i8* %0)
br label %32
32: ; preds = %21, %28, %12
%33 = phi i32 [ 0, %12 ], [ -1, %28 ], [ 0, %21 ]
ret i32 %33
}