#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char const *, int /*<<< orphan*/);
unsigned int read(int, void *, unsigned int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
char *strerror(int /*<<< orphan*/);
int file2buff(const char *input_file, const char *buff, unsigned int buff_len) {
  int fd;
  if (!input_file) {
    fprintf(stderr, "ERROR: "
                    "Invalid input_file\n");
    return -1;
  }
  if (!buff) {
    fprintf(stderr, "ERROR: "
                    "Invalid buff\n");
    return -1;
  }
  fd = open(input_file, O_RDONLY);
  if (fd == -1) {
    fprintf(stderr,
            "ERROR: "
            "Fail to open %s (%s)\n",
            input_file, strerror(errno));
    return -1;
  }
  fprintf(stdout, "Reading %s\n", input_file);
  if (read(fd, (void *)buff, buff_len) != buff_len) {
    fprintf(stderr,
            "ERROR: "
            "Fail to read %s (%s)\n",
            input_file, strerror(errno));
    close(fd);
    return -1;
  } else {
    close(fd);
    return 0;
  }
}


x86 assembly (clang, -O0):
<file2buff>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1191 <file2buff+0x41>
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
lea    0xe82(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b4 <file2buff+0x164>
cmpq   $0x0,-0x18(%rbp)
jne    11bf <file2buff+0x6f>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
lea    0xe6f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b4 <file2buff+0x164>
mov    -0x10(%rbp),%rdi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1224 <file2buff+0xd4>
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
lea    0xe1f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b4 <file2buff+0x164>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe15(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <read@plt>
cmp    -0x1c(%rbp),%eax
je     12a5 <file2buff+0x155>
mov    0x2d7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xdcf(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b4 <file2buff+0x164>
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file2buff>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdi,%rdi
je     11bd <file2buff+0x6d>
mov    %rsi,%rbx
test   %rsi,%rsi
je     11cf <file2buff+0x7f>
mov    %edx,%r15d
mov    %rdi,%r12
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11e8 <file2buff+0x98>
mov    %eax,%r14d
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
lea    0xebc(%rip),%rsi
xor    %ebp,%ebp
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1040 <read@plt>
cmp    %r15d,%eax
jne    1217 <file2buff+0xc7>
mov    %r14d,%edi
call   1030 <close@plt>
jmp    1251 <file2buff+0x101>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
lea    0xe33(%rip),%rsi
jmp    11df <file2buff+0x8f>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    124c <file2buff+0xfc>
mov    0x2de9(%rip),%rax
mov    (%rax),%ebx
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe2a(%rip),%rsi
mov    %ebx,%edi
mov    %r12,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    124c <file2buff+0xfc>
mov    0x2dba(%rip),%rax
mov    (%rax),%ebx
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe24(%rip),%rsi
mov    %ebx,%edi
mov    %r12,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r14d,%edi
call   1030 <close@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [27 x i8] c"ERROR: Invalid input_file\0A\00"
@.str.1 = [21 x i8] c"ERROR: Invalid buff\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [29 x i8] c"ERROR: Fail to open %s (%s)\0A\00"
@errno = global i32 0
@stdout = global i32 0
@.str.3 = [12 x i8] c"Reading %s\0A\00"
@.str.4 = [29 x i8] c"ERROR: Fail to read %s (%s)\0A\00"
define i32 @file2buff(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i8*, i8** %5
%10 = icmp ne i8* %9, null
br i1 %10, label %14, label %11
11: ; preds = %3
%12 = load i32, i32* @stderr
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %53
14: ; preds = %3
%15 = load i8*, i8** %6
%16 = icmp ne i8* %15, null
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %53
20: ; preds = %14
%21 = load i8*, i8** %5
%22 = load i32, i32* @O_RDONLY
%23 = call i32 @open(i8* %21, i32 %22)
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %32
26: ; preds = %20
%27 = load i32, i32* @stderr
%28 = load i8*, i8** %5
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %28, i8* %30)
store i32 -1, i32* %4
br label %53
32: ; preds = %20
%33 = load i32, i32* @stdout
%34 = load i8*, i8** %5
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %34)
%36 = load i32, i32* %8
%37 = load i8*, i8** %6
%38 = load i32, i32* %7
%39 = call i32 @read(i32 %36, i8* %37, i32 %38)
%40 = load i32, i32* %7
%41 = icmp ne i32 %39, %40
br i1 %41, label %42, label %50
42: ; preds = %32
%43 = load i32, i32* @stderr
%44 = load i8*, i8** %5
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %44, i8* %46)
%48 = load i32, i32* %8
%49 = call i32 @close(i32 %48)
store i32 -1, i32* %4
br label %53
50: ; preds = %32
%51 = load i32, i32* %8
%52 = call i32 @close(i32 %51)
store i32 0, i32* %4
br label %53
53: ; preds = %50, %42, %26, %17, %11
%54 = load i32, i32* %4
ret i32 %54
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [27 x i8] c"ERROR: Invalid input_file\0A\00"
@.str.1 = [21 x i8] c"ERROR: Invalid buff\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [29 x i8] c"ERROR: Fail to open %s (%s)\0A\00"
@errno = global i32 0
@stdout = global i32 0
@.str.3 = [12 x i8] c"Reading %s\0A\00"
@.str.4 = [29 x i8] c"ERROR: Fail to read %s (%s)\0A\00"
define i32 @file2buff(i8* %0, i8* %1, i32 %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %5, label %8
5: ; preds = %3
%6 = load i32, i32* @stderr
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %35
8: ; preds = %3
%9 = icmp eq i8* %1, null
br i1 %9, label %10, label %13
10: ; preds = %8
%11 = load i32, i32* @stderr
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %35
13: ; preds = %8
%14 = load i32, i32* @O_RDONLY
%15 = tail call i32 @open(i8* %0, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %22
17: ; preds = %13
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = tail call i8* @strerror(i32 %19)
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %20)
br label %35
22: ; preds = %13
%23 = load i32, i32* @stdout
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%25 = tail call i32 @read(i32 %15, i8* %1, i32 %2)
%26 = icmp eq i32 %25, %2
br i1 %26, label %33, label %27
27: ; preds = %22
%28 = load i32, i32* @stderr
%29 = load i32, i32* @errno
%30 = tail call i8* @strerror(i32 %29)
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %30)
%32 = tail call i32 @close(i32 %15)
br label %35
33: ; preds = %22
%34 = tail call i32 @close(i32 %15)
br label %35
35: ; preds = %33, %27, %17, %10, %5
%36 = phi i32 [ -1, %17 ], [ -1, %27 ], [ 0, %33 ], [ -1, %10 ], [ -1, %5 ]
ret i32 %36
}