#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 ao_sym {
  int dummy;
};
struct ao_hex_image {
  int dummy;
};
struct ao_hex_file {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ ao_hex_file_free(struct ao_hex_file *);
struct ao_hex_file *ao_hex_file_read(int /*<<< orphan*/ *, char *);
struct ao_hex_image *ao_hex_image_create(struct ao_hex_file *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
struct ao_sym *load_symbols(struct ao_hex_file *, int *);
struct ao_hex_image *ao_hex_load(char *filename, struct ao_sym **symbols,
                                 int *num_symbolsp) {
  FILE *file;
  struct ao_hex_file *hex_file;
  struct ao_hex_image *hex_image;
  file = fopen(filename, "r");
  if (!file)
    return NULL;
  hex_file = ao_hex_file_read(file, filename);
  fclose(file);
  if (!hex_file)
    return NULL;
  hex_image = ao_hex_image_create(hex_file);
  if (!hex_image)
    return NULL;
  if (symbols)
    *symbols = load_symbols(hex_file, num_symbolsp);
  ao_hex_file_free(hex_file);
  return hex_image;
}


x86 assembly (clang, -O0):
<ao_hex_load>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe81(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a0 <ao_hex_load+0x40>
movq   $0x0,-0x8(%rbp)
jmp    122a <ao_hex_load+0xca>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <ao_hex_file_read@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
cmpq   $0x0,-0x30(%rbp)
jne    11d2 <ao_hex_load+0x72>
movq   $0x0,-0x8(%rbp)
jmp    122a <ao_hex_load+0xca>
mov    -0x30(%rbp),%rdi
call   1040 <ao_hex_image_create@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11f7 <ao_hex_load+0x97>
movq   $0x0,-0x8(%rbp)
jmp    122a <ao_hex_load+0xca>
cmpq   $0x0,-0x18(%rbp)
je     1219 <ao_hex_load+0xb9>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <load_symbols@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rdi
call   1030 <ao_hex_file_free@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ao_hex_load>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
lea    0xe88(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11cd <ao_hex_load+0x6d>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
call   1060 <ao_hex_file_read@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1050 <fclose@plt>
test   %r12,%r12
je     11cd <ao_hex_load+0x6d>
mov    %r12,%rdi
call   1040 <ao_hex_image_create@plt>
test   %rax,%rax
je     11cd <ao_hex_load+0x6d>
mov    %rax,%rbx
test   %r15,%r15
je     11c3 <ao_hex_load+0x63>
mov    %r12,%rdi
mov    %r14,%rsi
call   1070 <load_symbols@plt>
mov    %rax,(%r15)
mov    %r12,%rdi
call   1030 <ao_hex_file_free@plt>
jmp    11cf <ao_hex_load+0x6f>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.ao_hex_image = type { i32 }
%struct.ao_sym = type { i32 }
%struct.ao_hex_file = type { i32 }
@.str = [2 x i8] c"r\00"
define %struct.ao_hex_image* @ao_hex_load(i8* %0, %struct.ao_sym** %1, i32* %2) {
%4 = alloca %struct.ao_hex_image*
%5 = alloca i8*
%6 = alloca %struct.ao_sym**
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca %struct.ao_hex_file*
%10 = alloca %struct.ao_hex_image*
store i8* %0, i8** %5
store %struct.ao_sym** %1, %struct.ao_sym*** %6
store i32* %2, i32** %7
%11 = load i8*, i8** %5
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %8
%13 = load i32*, i32** %8
%14 = icmp ne i32* %13, null
br i1 %14, label %16, label %15
15: ; preds = %3
store %struct.ao_hex_image* null, %struct.ao_hex_image** %4
br label %43
16: ; preds = %3
%17 = load i32*, i32** %8
%18 = load i8*, i8** %5
%19 = call %struct.ao_hex_file* @ao_hex_file_read(i32* %17, i8* %18)
store %struct.ao_hex_file* %19, %struct.ao_hex_file** %9
%20 = load i32*, i32** %8
%21 = call i32 @fclose(i32* %20)
%22 = load %struct.ao_hex_file*, %struct.ao_hex_file** %9
%23 = icmp ne %struct.ao_hex_file* %22, null
br i1 %23, label %25, label %24
24: ; preds = %16
store %struct.ao_hex_image* null, %struct.ao_hex_image** %4
br label %43
25: ; preds = %16
%26 = load %struct.ao_hex_file*, %struct.ao_hex_file** %9
%27 = call %struct.ao_hex_image* @ao_hex_image_create(%struct.ao_hex_file* %26)
store %struct.ao_hex_image* %27, %struct.ao_hex_image** %10
%28 = load %struct.ao_hex_image*, %struct.ao_hex_image** %10
%29 = icmp ne %struct.ao_hex_image* %28, null
br i1 %29, label %31, label %30
30: ; preds = %25
store %struct.ao_hex_image* null, %struct.ao_hex_image** %4
br label %43
31: ; preds = %25
%32 = load %struct.ao_sym**, %struct.ao_sym*** %6
%33 = icmp ne %struct.ao_sym** %32, null
br i1 %33, label %34, label %39
34: ; preds = %31
%35 = load %struct.ao_hex_file*, %struct.ao_hex_file** %9
%36 = load i32*, i32** %7
%37 = call %struct.ao_sym* @load_symbols(%struct.ao_hex_file* %35, i32* %36)
%38 = load %struct.ao_sym**, %struct.ao_sym*** %6
store %struct.ao_sym* %37, %struct.ao_sym** %38
br label %39
39: ; preds = %34, %31
%40 = load %struct.ao_hex_file*, %struct.ao_hex_file** %9
%41 = call i32 @ao_hex_file_free(%struct.ao_hex_file* %40)
%42 = load %struct.ao_hex_image*, %struct.ao_hex_image** %10
store %struct.ao_hex_image* %42, %struct.ao_hex_image** %4
br label %43
43: ; preds = %39, %30, %24, %15
%44 = load %struct.ao_hex_image*, %struct.ao_hex_image** %4
ret %struct.ao_hex_image* %44
}

LLVM IR (-O2):
%struct.ao_hex_image = type { i32 }
%struct.ao_sym = type { i32 }
%struct.ao_hex_file = type { i32 }
@.str = [2 x i8] c"r\00"
define %struct.ao_hex_image* @ao_hex_load(i8* %0, %struct.ao_sym** writeonly %1, i32* %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %19, label %6
6: ; preds = %3
%7 = tail call %struct.ao_hex_file* @ao_hex_file_read(i32* %4, i8* %0)
%8 = tail call i32 @fclose(i32* %4)
%9 = icmp eq %struct.ao_hex_file* %7, null
br i1 %9, label %19, label %10
10: ; preds = %6
%11 = tail call %struct.ao_hex_image* @ao_hex_image_create(%struct.ao_hex_file* %7)
%12 = icmp eq %struct.ao_hex_image* %11, null
br i1 %12, label %19, label %13
13: ; preds = %10
%14 = icmp eq %struct.ao_sym** %1, null
br i1 %14, label %17, label %15
15: ; preds = %13
%16 = tail call %struct.ao_sym* @load_symbols(%struct.ao_hex_file* %7, i32* %2)
store %struct.ao_sym* %16, %struct.ao_sym** %1
br label %17
17: ; preds = %15, %13
%18 = tail call i32 @ao_hex_file_free(%struct.ao_hex_file* %7)
br label %19
19: ; preds = %10, %6, %3, %17
%20 = phi %struct.ao_hex_image* [ %11, %17 ], [ null, %3 ], [ null, %6 ], [ null, %10 ]
ret %struct.ao_hex_image* %20
}