#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 open(unsigned char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int, void *, unsigned long long);
int fileread(const unsigned char *fname, void *buf, unsigned long long len) {
  int fd = 0;
  if ((fd = open(fname, O_RDONLY)) < 0) {
    perror("open");
    return 0x80001104;
  }
  if (read(fd, buf, len) < 0) {
    perror("read");
    return 0x80001105;
  }
  return 0;
}


x86 assembly (clang, -O0):
<fileread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1181 <fileread+0x51>
lea    0xe90(%rip),%rdi
call   1050 <perror@plt>
movl   $0x80001104,-0x4(%rbp)
jmp    11ba <fileread+0x8a>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <read@plt>
cmp    $0x0,%rax
jge    11b3 <fileread+0x83>
lea    0xe63(%rip),%rdi
call   1050 <perror@plt>
movl   $0x80001105,-0x4(%rbp)
jmp    11ba <fileread+0x8a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fileread>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    0x2ea7(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
test   %eax,%eax
js     116a <fileread+0x3a>
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <read@plt>
xor    %ebx,%ebx
test   %rax,%rax
js     1178 <fileread+0x48>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    $0x80001104,%ebx
lea    0xe8a(%rip),%rdi
jmp    1184 <fileread+0x54>
mov    $0x80001105,%ebx
lea    0xe81(%rip),%rdi
call   1050 <perror@plt>
jmp    1160 <fileread+0x30>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
@.str.1 = [5 x i8] c"read\00"
define i32 @fileread(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 0, i32* %8
%9 = load i8*, i8** %5
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %8
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -2147479292, i32* %4
br label %24
15: ; preds = %3
%16 = load i32, i32* %8
%17 = load i8*, i8** %6
%18 = load i64, i64* %7
%19 = call i64 @read(i32 %16, i8* %17, i64 %18)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -2147479291, i32* %4
br label %24
23: ; preds = %15
store i32 0, i32* %4
br label %24
24: ; preds = %23, %21, %13
%25 = load i32, i32* %4
ret i32 %25
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
@.str.1 = [5 x i8] c"read\00"
define i32 @fileread(i8* nocapture %0, i8* nocapture %1, i64 %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 %10, label %7
7: ; preds = %3
%8 = tail call i64 @read(i32 %5, i8* %1, i64 %2)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %7, %3
%11 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %3 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %7 ]
%12 = phi i32 [ -2147479292, %3 ], [ -2147479291, %7 ]
%13 = tail call i32 @perror(i8* %11)
br label %14
14: ; preds = %10, %7
%15 = phi i32 [ 0, %7 ], [ %12, %10 ]
ret i32 %15
}