#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;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ error(char *, char *);
int open(char *, int /*<<< orphan*/);
int read(int, int /*<<< orphan*/ *, int);
void load_file(char *name, uint8_t *data, int size) {
  int f, n;
  f = open(name, O_RDONLY);
  if (f < 0)
    error("cannot open firmware file %s", name);
  n = read(f, data, size);
  close(f);
  if (size == n)
    error("firmware file %s is too big", name);
}


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)
mov    -0x8(%rbp),%rdi
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1182 <load_file+0x42>
mov    -0x8(%rbp),%rsi
lea    0xe83(%rip),%rdi
call   1050 <error@plt>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
mov    -0x14(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    11b8 <load_file+0x78>
mov    -0x8(%rbp),%rsi
lea    0xe6a(%rip),%rdi
call   1050 <error@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r12d
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e90(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1174 <load_file+0x34>
lea    0xe94(%rip),%rdi
mov    %r15,%rsi
call   1050 <error@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r12d,%edx
call   1040 <read@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1030 <close@plt>
cmp    %r12d,%ebp
jne    11a6 <load_file+0x66>
lea    0xe87(%rip),%rdi
mov    %r15,%rsi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <error@plt>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [29 x i8] c"cannot open firmware file %s\00"
@.str.1 = [28 x i8] c"firmware file %s is too big\00"
define void @load_file(i8* %0, i32* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = load i8*, i8** %4
%16 = call i32 @error(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %15)
br label %17
17: ; preds = %14, %3
%18 = load i32, i32* %7
%19 = load i32*, i32** %5
%20 = load i32, i32* %6
%21 = call i32 @read(i32 %18, i32* %19, i32 %20)
store i32 %21, i32* %8
%22 = load i32, i32* %7
%23 = call i32 @close(i32 %22)
%24 = load i32, i32* %6
%25 = load i32, i32* %8
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %30
27: ; preds = %17
%28 = load i8*, i8** %4
%29 = call i32 @error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %28)
br label %30
30: ; preds = %27, %17
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [29 x i8] c"cannot open firmware file %s\00"
@.str.1 = [28 x i8] c"firmware file %s is too big\00"
define void @load_file(i8* %0, i32* nocapture %1, i32 %2) {
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 @error(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %9
9: ; preds = %7, %3
%10 = tail call i32 @read(i32 %5, i32* %1, i32 %2)
%11 = tail call i32 @close(i32 %5)
%12 = icmp eq i32 %10, %2
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 @error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %15
15: ; preds = %13, %9
ret void
}