#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*/ uint8_t;
struct stat {
  int st_size;
};

/* Variables and functions */
int O_RDONLY;
int /*<<< orphan*/ check(int, char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fstat(int, struct stat *);
int open(char *, int);
int /*<<< orphan*/ perror_exit(char *);
int read(int, int /*<<< orphan*/ *, int);
int load_file(char *name, uint8_t *data, int size) {
  struct stat stat;
  int fd, rsize;
  check(NULL != name, "input file name is not specified");
  fd = open(name, O_RDONLY | 0);
  if (fd < 0)
    perror_exit("open()");
  fstat(fd, &stat);
  if (stat.st_size < size)
    size = stat.st_size;
  rsize = read(fd, data, size);
  if (rsize < 0)
    perror_exit("read()");
  check(rsize == size, "cannot fully read file");
  close(fd);
  return rsize;
}


x86 assembly (clang, -O0):
<load_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
xor    %eax,%eax
cmp    -0x8(%rbp),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
lea    0xe78(%rip),%rsi
call   1070 <check@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
or     $0x0,%esi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11bb <load_file+0x5b>
lea    0xe6b(%rip),%rdi
call   1060 <perror_exit@plt>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1080 <fstat@plt>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11d9 <load_file+0x79>
mov    -0x18(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1201 <load_file+0xa1>
lea    0xe2c(%rip),%rdi
call   1060 <perror_exit@plt>
mov    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
lea    0xe19(%rip),%rsi
call   1070 <check@plt>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
mov    %rdi,%rbp
xor    %edi,%edi
test   %rbp,%rbp
setne  %dil
lea    0xe81(%rip),%rsi
call   1070 <check@plt>
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1050 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    11a8 <load_file+0x48>
lea    0xe7e(%rip),%rdi
call   1060 <perror_exit@plt>
mov    %rsp,%rsi
mov    %r15d,%edi
call   1080 <fstat@plt>
mov    (%rsp),%eax
cmp    %ebx,%eax
cmovl  %eax,%ebx
mov    %r15d,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11da <load_file+0x7a>
lea    0xe53(%rip),%rdi
call   1060 <perror_exit@plt>
xor    %edi,%edi
cmp    %ebx,%ebp
sete   %dil
lea    0xe46(%rip),%rsi
call   1070 <check@plt>
mov    %r15d,%edi
call   1030 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [33 x i8] c"input file name is not specified\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@.str.2 = [7 x i8] c"read()\00"
@.str.3 = [23 x i8] c"cannot fully read file\00"
define i32 @load_file(i8* %0, i32* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%10 = load i8*, i8** %4
%11 = icmp ne i8* null, %10
%12 = zext i1 %11 to i32
%13 = call i32 @check(i32 %12, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%14 = load i8*, i8** %4
%15 = load i32, i32* @O_RDONLY
%16 = or i32 %15, 0
%17 = call i32 @open(i8* %14, i32 %16)
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %3
%21 = call i32 @perror_exit(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %3
%23 = load i32, i32* %8
%24 = call i32 @fstat(i32 %23, %struct.stat* %7)
%25 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%26 = load i32, i32* %25
%27 = load i32, i32* %6
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %32
29: ; preds = %22
%30 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%31 = load i32, i32* %30
store i32 %31, i32* %6
br label %32
32: ; preds = %29, %22
%33 = load i32, i32* %8
%34 = load i32*, i32** %5
%35 = load i32, i32* %6
%36 = call i32 @read(i32 %33, i32* %34, i32 %35)
store i32 %36, i32* %9
%37 = load i32, i32* %9
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %32
%40 = call i32 @perror_exit(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %41
41: ; preds = %39, %32
%42 = load i32, i32* %9
%43 = load i32, i32* %6
%44 = icmp eq i32 %42, %43
%45 = zext i1 %44 to i32
%46 = call i32 @check(i32 %45, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%47 = load i32, i32* %8
%48 = call i32 @close(i32 %47)
%49 = load i32, i32* %9
ret i32 %49
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [33 x i8] c"input file name is not specified\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@.str.2 = [7 x i8] c"read()\00"
@.str.3 = [23 x i8] c"cannot fully read file\00"
define i32 @load_file(i8* %0, i32* nocapture %1, i32 %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = icmp ne i8* %0, null
%7 = zext i1 %6 to i32
%8 = tail call i32 @check(i32 %7, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* %0, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = tail call i32 @perror_exit(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %3
%15 = call i32 @fstat(i32 %10, %struct.stat* %4)
%16 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%17 = load i32, i32* %16
%18 = icmp slt i32 %17, %2
%19 = select i1 %18, i32 %17, i32 %2
%20 = tail call i32 @read(i32 %10, i32* %1, i32 %19)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %14
%23 = tail call i32 @perror_exit(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %22, %14
%25 = icmp eq i32 %20, %19
%26 = zext i1 %25 to i32
%27 = tail call i32 @check(i32 %26, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%28 = tail call i32 @close(i32 %10)
ret i32 %20
}