#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*/ semi_close(int);
int semi_flen(int);
int semi_open(char const *, int);
scalar_t__ semi_read(int, void *, int);
int /*<<< orphan*/ semi_write0(char const *);
int semi_load_file(void **dest, unsigned *size, char const *filename) {
  int result = -1;
  int fd = -1;
  int filesize;
  fd = semi_open(filename, 1);
  if (fd == -1) {
    semi_write0("Cannot open file: ");
    goto out;
  }
  filesize = semi_flen(fd);
  if (filesize == -1) {
    semi_write0("Cannot get file size for: ");
    goto out;
  }
  if (semi_read(fd, *dest, filesize)) {
    semi_write0("Could not read: ");
    goto out;
  }
  result = 0;
  *dest = (char *)*dest + filesize;
out:
  if (fd != -1)
    semi_close(fd);
  if (result) {
    semi_write0(filename);
    semi_write0("\n");
  } else if (size)
    *size = filesize;
  return result;
}


x86 assembly (clang, -O0):
<semi_load_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x1,%esi
call   1050 <semi_open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    119e <semi_load_file+0x4e>
lea    0xe6c(%rip),%rdi
call   1060 <semi_write0@plt>
jmp    120d <semi_load_file+0xbd>
mov    -0x20(%rbp),%edi
call   1040 <semi_flen@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11c4 <semi_load_file+0x74>
lea    0xe59(%rip),%rdi
call   1060 <semi_write0@plt>
jmp    120d <semi_load_file+0xbd>
mov    -0x20(%rbp),%edi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
mov    -0x24(%rbp),%edx
call   1070 <semi_read@plt>
cmp    $0x0,%rax
je     11f1 <semi_load_file+0xa1>
lea    0xe47(%rip),%rdi
call   1060 <semi_write0@plt>
jmp    120d <semi_load_file+0xbd>
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
movslq -0x24(%rbp),%rax
add    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
cmpl   $0xffffffff,-0x20(%rbp)
je     121f <semi_load_file+0xcf>
mov    -0x20(%rbp),%edi
call   1030 <semi_close@plt>
cmpl   $0x0,-0x1c(%rbp)
je     1243 <semi_load_file+0xf3>
mov    -0x18(%rbp),%rdi
call   1060 <semi_write0@plt>
lea    0xe06(%rip),%rdi
call   1060 <semi_write0@plt>
jmp    125c <semi_load_file+0x10c>
cmpq   $0x0,-0x10(%rbp)
je     1257 <semi_load_file+0x107>
mov    -0x24(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    125c <semi_load_file+0x10c>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<semi_load_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rdx,%rdi
mov    $0x1,%esi
call   1050 <semi_open@plt>
cmp    $0xffffffff,%eax
je     119d <semi_load_file+0x4d>
mov    %eax,%ebp
mov    %eax,%edi
call   1040 <semi_flen@plt>
cmp    $0xffffffff,%eax
je     11ab <semi_load_file+0x5b>
mov    %eax,%ebx
mov    (%r15),%rsi
mov    %ebp,%edi
mov    %eax,%edx
call   1070 <semi_read@plt>
test   %rax,%rax
je     11e0 <semi_load_file+0x90>
lea    0xe93(%rip),%rdi
jmp    11b2 <semi_load_file+0x62>
lea    0xe5c(%rip),%rdi
call   1060 <semi_write0@plt>
jmp    11be <semi_load_file+0x6e>
lea    0xe61(%rip),%rdi
call   1060 <semi_write0@plt>
mov    %ebp,%edi
call   1030 <semi_close@plt>
mov    %r12,%rdi
call   1060 <semi_write0@plt>
lea    0xe72(%rip),%rdi
call   1060 <semi_write0@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
movslq %ebx,%rax
add    %rax,(%r15)
mov    %ebp,%edi
call   1030 <semi_close@plt>
xor    %eax,%eax
test   %r14,%r14
je     11d7 <semi_load_file+0x87>
mov    %ebx,(%r14)
jmp    11d7 <semi_load_file+0x87>

LLVM IR (-O0):
@.str = [19 x i8] c"Cannot open file: \00"
@.str.1 = [27 x i8] c"Cannot get file size for: \00"
@.str.2 = [17 x i8] c"Could not read: \00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @semi_load_file(i8** %0, i32* %1, i8* %2) {
%4 = alloca i8**
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8** %0, i8*** %4
store i32* %1, i32** %5
store i8* %2, i8** %6
store i32 -1, i32* %7
store i32 -1, i32* %8
%10 = load i8*, i8** %6
%11 = call i32 @semi_open(i8* %10, i32 1)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = call i32 @semi_write0(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %39
16: ; preds = %3
%17 = load i32, i32* %8
%18 = call i32 @semi_flen(i32 %17)
store i32 %18, i32* %9
%19 = load i32, i32* %9
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @semi_write0(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %39
23: ; preds = %16
%24 = load i32, i32* %8
%25 = load i8**, i8*** %4
%26 = load i8*, i8** %25
%27 = load i32, i32* %9
%28 = call i64 @semi_read(i32 %24, i8* %26, i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %23
%31 = call i32 @semi_write0(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %39
32: ; preds = %23
store i32 0, i32* %7
%33 = load i8**, i8*** %4
%34 = load i8*, i8** %33
%35 = load i32, i32* %9
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
%38 = load i8**, i8*** %4
store i8* %37, i8** %38
br label %39
39: ; preds = %32, %30, %21, %14
%40 = load i32, i32* %8
%41 = icmp ne i32 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = load i32, i32* %8
%44 = call i32 @semi_close(i32 %43)
br label %45
45: ; preds = %42, %39
%46 = load i32, i32* %7
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %45
%49 = load i8*, i8** %6
%50 = call i32 @semi_write0(i8* %49)
%51 = call i32 @semi_write0(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %59
52: ; preds = %45
%53 = load i32*, i32** %5
%54 = icmp ne i32* %53, null
br i1 %54, label %55, label %58
55: ; preds = %52
%56 = load i32, i32* %9
%57 = load i32*, i32** %5
store i32 %56, i32* %57
br label %58
58: ; preds = %55, %52
br label %59
59: ; preds = %58, %48
%60 = load i32, i32* %7
ret i32 %60
}

LLVM IR (-O2):
@.str = [19 x i8] c"Cannot open file: \00"
@.str.1 = [27 x i8] c"Cannot get file size for: \00"
@.str.2 = [17 x i8] c"Could not read: \00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @semi_load_file(i8** nocapture %0, i32* writeonly %1, i8* %2) {
%4 = tail call i32 @semi_open(i8* %2, i32 1)
%5 = icmp eq i32 %4, -1
br i1 %5, label %13, label %6
6: ; preds = %3
%7 = tail call i32 @semi_flen(i32 %4)
%8 = icmp eq i32 %7, -1
br i1 %8, label %15, label %9
9: ; preds = %6
%10 = load i8*, i8** %0
%11 = tail call i64 @semi_read(i32 %4, i8* %10, i32 %7)
%12 = icmp eq i64 %11, 0
br i1 %12, label %22, label %15
13: ; preds = %3
%14 = tail call i32 @semi_write0(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %19
15: ; preds = %9, %6
%16 = phi i8* [ getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), %6 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), %9 ]
%17 = tail call i32 @semi_write0(i8* %16)
%18 = tail call i32 @semi_close(i32 %4)
br label %19
19: ; preds = %15, %13
%20 = tail call i32 @semi_write0(i8* %2)
%21 = tail call i32 @semi_write0(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %29
22: ; preds = %9
%23 = load i8*, i8** %0
%24 = sext i32 %7 to i64
%25 = getelementptr inbounds i8, i8* %23, i64 %24
store i8* %25, i8** %0
%26 = tail call i32 @semi_close(i32 %4)
%27 = icmp eq i32* %1, null
br i1 %27, label %29, label %28
28: ; preds = %22
store i32 %7, i32* %1
br label %29
29: ; preds = %22, %28, %19
%30 = phi i32 [ 0, %22 ], [ 0, %28 ], [ -1, %19 ]
ret i32 %30
}