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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
scalar_t__ fstat(int, struct stat *);
scalar_t__ malloc(int);
int open(char const *, int /*<<< orphan*/);
int read(int, char *, int);
int load_filemap_contents(const char *file, char **area, int *size) {
  struct stat st;
  char *p = NULL;
  int status = 0;
  int fd = -1;
  status = -2;
  if ((fd = open(file, 0)) < 0)
    goto err;
  status = -3;
  if (fstat(fd, &st) < 0)
    goto err;
  status = -1;
  if ((p = (char *)malloc(st.st_size)) == NULL)
    goto err;
  status = -4;
  if (read(fd, p, st.st_size) != st.st_size)
    goto err;
  close(fd);
  *area = p;
  *size = st.st_size;
  return 0;
err:
  if (fd != -1)
    close(fd);
  if (p != NULL)
    free(p);
  return status;
}


x86 assembly (clang, -O0):
<load_filemap_contents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0xffffffff,-0x38(%rbp)
movl   $0xfffffffe,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1070 <open@plt>
mov    %eax,-0x38(%rbp)
cmp    $0x0,%eax
jge    11ad <load_filemap_contents+0x4d>
jmp    123d <load_filemap_contents+0xdd>
movl   $0xfffffffd,-0x34(%rbp)
mov    -0x38(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0x0,%rax
jge    11cf <load_filemap_contents+0x6f>
jmp    123d <load_filemap_contents+0xdd>
movl   $0xffffffff,-0x34(%rbp)
mov    -0x28(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11f1 <load_filemap_contents+0x91>
jmp    123d <load_filemap_contents+0xdd>
movl   $0xfffffffc,-0x34(%rbp)
mov    -0x38(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1050 <read@plt>
cmp    -0x28(%rbp),%eax
je     1215 <load_filemap_contents+0xb5>
jmp    123d <load_filemap_contents+0xdd>
mov    -0x38(%rbp),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1269 <load_filemap_contents+0x109>
cmpl   $0xffffffff,-0x38(%rbp)
je     124f <load_filemap_contents+0xef>
mov    -0x38(%rbp),%edi
call   1040 <close@plt>
cmpq   $0x0,-0x30(%rbp)
je     1263 <load_filemap_contents+0x103>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_filemap_contents>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
xor    %esi,%esi
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11c4 <load_filemap_contents+0x64>
mov    %rsp,%rsi
mov    %ebx,%edi
call   1080 <fstat@plt>
test   %rax,%rax
js     11d0 <load_filemap_contents+0x70>
mov    (%rsp),%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11d7 <load_filemap_contents+0x77>
mov    %rax,%r12
mov    %ebx,%edi
mov    %rax,%rsi
mov    %ebp,%edx
call   1050 <read@plt>
mov    %eax,%r13d
mov    %ebx,%edi
call   1040 <close@plt>
cmp    %ebp,%r13d
jne    11f4 <load_filemap_contents+0x94>
mov    %r12,(%r15)
mov    %ebp,(%r14)
xor    %ebp,%ebp
jmp    11e3 <load_filemap_contents+0x83>
mov    $0xfffffffe,%ebp
cmp    $0xffffffff,%ebx
jne    11dc <load_filemap_contents+0x7c>
jmp    11e3 <load_filemap_contents+0x83>
mov    $0xfffffffd,%ebp
jmp    11dc <load_filemap_contents+0x7c>
mov    $0xffffffff,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12,%rdi
call   1030 <free@plt>
mov    $0xfffffffc,%ebp
jmp    11e3 <load_filemap_contents+0x83>

LLVM IR (-O0):
%struct.stat = type { i32 }
define i32 @load_filemap_contents(i8* %0, i8** %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca %struct.stat
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i32* %2, i32** %7
store i8* null, i8** %9
store i32 0, i32* %10
store i32 -1, i32* %11
store i32 -2, i32* %10
%12 = load i8*, i8** %5
%13 = call i32 @open(i8* %12, i32 0)
store i32 %13, i32* %11
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %3
br label %46
16: ; preds = %3
store i32 -3, i32* %10
%17 = load i32, i32* %11
%18 = call i64 @fstat(i32 %17, %struct.stat* %8)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %16
br label %46
21: ; preds = %16
store i32 -1, i32* %10
%22 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i64 @malloc(i32 %23)
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %9
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %28
27: ; preds = %21
br label %46
28: ; preds = %21
store i32 -4, i32* %10
%29 = load i32, i32* %11
%30 = load i8*, i8** %9
%31 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @read(i32 %29, i8* %30, i32 %32)
%34 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%35 = load i32, i32* %34
%36 = icmp ne i32 %33, %35
br i1 %36, label %37, label %38
37: ; preds = %28
br label %46
38: ; preds = %28
%39 = load i32, i32* %11
%40 = call i32 @close(i32 %39)
%41 = load i8*, i8** %9
%42 = load i8**, i8*** %6
store i8* %41, i8** %42
%43 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%44 = load i32, i32* %43
%45 = load i32*, i32** %7
store i32 %44, i32* %45
store i32 0, i32* %4
br label %60
46: ; preds = %37, %27, %20, %15
%47 = load i32, i32* %11
%48 = icmp ne i32 %47, -1
br i1 %48, label %49, label %52
49: ; preds = %46
%50 = load i32, i32* %11
%51 = call i32 @close(i32 %50)
br label %52
52: ; preds = %49, %46
%53 = load i8*, i8** %9
%54 = icmp ne i8* %53, null
br i1 %54, label %55, label %58
55: ; preds = %52
%56 = load i8*, i8** %9
%57 = call i32 @free(i8* %56)
br label %58
58: ; preds = %55, %52
%59 = load i32, i32* %10
store i32 %59, i32* %4
br label %60
60: ; preds = %58, %38
%61 = load i32, i32* %4
ret i32 %61
}

LLVM IR (-O2):
%struct.stat = type { i32 }
define i32 @load_filemap_contents(i8* nocapture %0, i8** nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = tail call i32 @open(i8* %0, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %22, label %8
8: ; preds = %3
%9 = call i64 @fstat(i32 %6, %struct.stat* %4)
%10 = icmp slt i64 %9, 0
br i1 %10, label %26, label %11
11: ; preds = %8
%12 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%13 = load i32, i32* %12
%14 = tail call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to i8*
%16 = icmp eq i64 %14, 0
br i1 %16, label %26, label %17
17: ; preds = %11
%18 = tail call i32 @read(i32 %6, i8* %15, i32 %13)
%19 = icmp eq i32 %18, %13
%20 = tail call i32 @close(i32 %6)
br i1 %19, label %21, label %29
21: ; preds = %17
store i8* %15, i8** %1
store i32 %13, i32* %2
br label %31
22: ; preds = %3
%23 = icmp eq i32 %6, -1
br i1 %23, label %31, label %24
24: ; preds = %22
%25 = tail call i32 @close(i32 %6)
br label %31
26: ; preds = %11, %8
%27 = phi i32 [ -3, %8 ], [ -1, %11 ]
%28 = tail call i32 @close(i32 %6)
br label %31
29: ; preds = %17
%30 = tail call i32 @free(i8* %15)
br label %31
31: ; preds = %22, %26, %24, %29, %21
%32 = phi i32 [ 0, %21 ], [ -4, %29 ], [ -2, %24 ], [ %27, %26 ], [ -2, %22 ]
ret i32 %32
}