#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;
typedef int /*<<< orphan*/ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int load_file(char const *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ *malloc(int /*<<< orphan*/);
int /*<<< orphan*/ size_file(char const *);
int /*<<< orphan*/ stderr;
uint8_t *alloc_load_file(const char *filename, ssize_t *length) {
  uint8_t *buf = NULL;
  bool success = false;
  if ((filename == NULL) || (length == NULL)) {
    errno = EINVAL;
    fprintf(stderr, "Error (alloc_load_file): invalid parameters\n");
  } else {
    *length = size_file(filename);
    if (length > 0) {
      buf = malloc(*length);
      if (buf == NULL) {
        fprintf(stderr, "Error: Unable to allocate buffer for '%s'\n",
                filename);
      } else {
        success = load_file(filename, buf, *length);
      }
    }
  }
  if (!success) {
    if (buf != NULL) {
      free(buf);
      buf = NULL;
    }
    if (length != NULL) {
      *length = 0;
    }
  }
  return buf;
}


x86 assembly (clang, -O0):
<alloc_load_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1185 <alloc_load_file+0x35>
cmpq   $0x0,-0x10(%rbp)
jne    11b3 <alloc_load_file+0x63>
mov    0x2e44(%rip),%rax
mov    (%rax),%ecx
mov    0x2e43(%rip),%rax
mov    %ecx,(%rax)
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
lea    0xe59(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1229 <alloc_load_file+0xd9>
mov    -0x8(%rbp),%rdi
call   1070 <size_file@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x10(%rbp)
jbe    1224 <alloc_load_file+0xd4>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1209 <alloc_load_file+0xb9>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe30(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    121f <alloc_load_file+0xcf>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
call   1060 <load_file@plt>
mov    %eax,-0x1c(%rbp)
jmp    1224 <alloc_load_file+0xd4>
jmp    1229 <alloc_load_file+0xd9>
cmpl   $0x0,-0x1c(%rbp)
jne    1269 <alloc_load_file+0x119>
cmpq   $0x0,-0x18(%rbp)
je     124f <alloc_load_file+0xff>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1264 <alloc_load_file+0x114>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
jmp    1269 <alloc_load_file+0x119>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alloc_load_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
test   %rdi,%rdi
je     11a0 <alloc_load_file+0x50>
test   %r14,%r14
je     11a0 <alloc_load_file+0x50>
mov    %rdi,%r15
call   1070 <size_file@plt>
mov    %eax,%ebp
mov    %eax,(%r14)
mov    %eax,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11e5 <alloc_load_file+0x95>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %rax,%rsi
mov    %ebp,%edx
call   1060 <load_file@plt>
test   %eax,%eax
jne    11d7 <alloc_load_file+0x87>
mov    %rbx,%rdi
call   1030 <free@plt>
test   %r14,%r14
jne    11ce <alloc_load_file+0x7e>
jmp    11d5 <alloc_load_file+0x85>
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    0x2e28(%rip),%rcx
mov    %eax,(%rcx)
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
lea    0xe3e(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
test   %r14,%r14
je     11d5 <alloc_load_file+0x85>
movl   $0x0,(%r14)
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    0xe38(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
test   %r14,%r14
jne    11ce <alloc_load_file+0x7e>
jmp    11d5 <alloc_load_file+0x85>

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@stderr = global i32 0
@.str = [45 x i8] c"Error (alloc_load_file): invalid parameters\0A\00"
@.str.1 = [43 x i8] c"Error: Unable to allocate buffer for '%s'\0A\00"
define i32* @alloc_load_file(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
store i8* %0, i8** %3
store i32* %1, i32** %4
store i32* null, i32** %5
store i32 0, i32* %6
%7 = load i8*, i8** %3
%8 = icmp eq i8* %7, null
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = load i32*, i32** %4
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %16
12: ; preds = %9, %2
%13 = load i32, i32* @EINVAL
store i32 %13, i32* @errno
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
br label %40
16: ; preds = %9
%17 = load i8*, i8** %3
%18 = call i32 @size_file(i8* %17)
%19 = load i32*, i32** %4
store i32 %18, i32* %19
%20 = load i32*, i32** %4
%21 = icmp ugt i32* %20, null
br i1 %21, label %22, label %39
22: ; preds = %16
%23 = load i32*, i32** %4
%24 = load i32, i32* %23
%25 = call i32* @malloc(i32 %24)
store i32* %25, i32** %5
%26 = load i32*, i32** %5
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %32
28: ; preds = %22
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %3
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i8* %30)
br label %38
32: ; preds = %22
%33 = load i8*, i8** %3
%34 = load i32*, i32** %5
%35 = load i32*, i32** %4
%36 = load i32, i32* %35
%37 = call i32 @load_file(i8* %33, i32* %34, i32 %36)
store i32 %37, i32* %6
br label %38
38: ; preds = %32, %28
br label %39
39: ; preds = %38, %16
br label %40
40: ; preds = %39, %12
%41 = load i32, i32* %6
%42 = icmp ne i32 %41, 0
br i1 %42, label %55, label %43
43: ; preds = %40
%44 = load i32*, i32** %5
%45 = icmp ne i32* %44, null
br i1 %45, label %46, label %49
46: ; preds = %43
%47 = load i32*, i32** %5
%48 = call i32 @free(i32* %47)
store i32* null, i32** %5
br label %49
49: ; preds = %46, %43
%50 = load i32*, i32** %4
%51 = icmp ne i32* %50, null
br i1 %51, label %52, label %54
52: ; preds = %49
%53 = load i32*, i32** %4
store i32 0, i32* %53
br label %54
54: ; preds = %52, %49
br label %55
55: ; preds = %54, %40
%56 = load i32*, i32** %5
ret i32* %56
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@stderr = global i32 0
@.str = [45 x i8] c"Error (alloc_load_file): invalid parameters\0A\00"
@.str.1 = [43 x i8] c"Error: Unable to allocate buffer for '%s'\0A\00"
define i32* @alloc_load_file(i8* %0, i32* %1) {
%3 = icmp eq i8* %0, null
%4 = icmp eq i32* %1, null
%5 = or i1 %3, %4
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i32, i32* @EINVAL
store i32 %7, i32* @errno
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
br label %22
10: ; preds = %2
%11 = tail call i32 @size_file(i8* %0)
store i32 %11, i32* %1
%12 = tail call i32* @malloc(i32 %11)
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %22
17: ; preds = %10
%18 = tail call i32 @load_file(i8* %0, i32* %12, i32 %11)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = tail call i32 @free(i32* %12)
br label %22
22: ; preds = %6, %14, %20
br i1 %4, label %24, label %23
23: ; preds = %22
store i32 0, i32* %1
br label %24
24: ; preds = %22, %23, %17
%25 = phi i32* [ %12, %17 ], [ null, %23 ], [ null, %22 ]
ret i32* %25
}