#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*/ uint64_t;
typedef int int32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int);
int /*<<< orphan*/ *my_fopen(char const *, char *);
size_t my_fread(int *, int, int, int /*<<< orphan*/ *);
int *my_malloc(int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int read_file_headers_lhalotree(const char *filename, int32_t *ntrees,
                                int32_t *totnhalos, int32_t **nhalos_per_tree) {
  int status = EXIT_SUCCESS;
  FILE *fp = my_fopen(filename, "r");
  size_t nitems = my_fread(ntrees, sizeof(*ntrees), 1, fp);
  if (nitems != 1) {
    status = EXIT_FAILURE;
  }
  nitems = my_fread(totnhalos, sizeof(*totnhalos), 1, fp);
  if (nitems != 1) {
    status = EXIT_FAILURE;
  }
  *nhalos_per_tree = my_malloc(sizeof(**nhalos_per_tree), (uint64_t)*ntrees);
  if (*nhalos_per_tree == NULL) {
    fprintf(stderr,
            "malloc failed for array containing nhalos per tree. ntrees = %d. "
            "requested size = %zu \n",
            *ntrees, sizeof(**nhalos_per_tree) * (*ntrees));
    status = EXIT_FAILURE;
  } else {
    my_fread(*nhalos_per_tree, sizeof(**nhalos_per_tree), *ntrees, fp);
  }
  fclose(fp);
  return status;
}


x86 assembly (clang, -O0):
<read_file_headers_lhalotree>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe81(%rip),%rsi
call   1030 <my_fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    $0x4,%esi
mov    $0x1,%edx
call   1050 <my_fread@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x1,-0x38(%rbp)
je     11ba <read_file_headers_lhalotree+0x6a>
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    $0x4,%esi
mov    $0x1,%edx
call   1050 <my_fread@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x1,-0x38(%rbp)
je     11ec <read_file_headers_lhalotree+0x9c>
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
mov    $0x4,%edi
call   1040 <my_malloc@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    124d <read_file_headers_lhalotree+0xfd>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
mov    -0x10(%rbp),%rax
movslq (%rax),%rax
shl    $0x2,%rax
mov    %eax,%ecx
lea    0xdcb(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    1268 <read_file_headers_lhalotree+0x118>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
mov    -0x30(%rbp),%rcx
mov    $0x4,%esi
call   1050 <my_fread@plt>
mov    -0x30(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x24(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file_headers_lhalotree>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,0x10(%rsp)
mov    %rdx,%rbp
mov    %rsi,%rbx
mov    0x2e60(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
lea    0xe83(%rip),%rsi
call   1030 <my_fopen@plt>
mov    %rax,%r14
mov    %rbx,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1050 <my_fread@plt>
mov    %rax,0x8(%rsp)
mov    0x2e32(%rip),%r12
mov    (%r12),%r15d
mov    %rbp,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1050 <my_fread@plt>
mov    %rax,%r13
mov    (%r12),%ebp
mov    (%rbx),%esi
mov    $0x4,%edi
call   1040 <my_malloc@plt>
mov    0x10(%rsp),%rcx
mov    %rax,(%rcx)
test   %rax,%rax
je     1207 <read_file_headers_lhalotree+0xb7>
cmpq   $0x1,0x8(%rsp)
cmove  0x4(%rsp),%r15d
cmp    $0x1,%r13
cmove  %r15d,%ebp
mov    (%rbx),%edx
mov    %rax,%rdi
mov    $0x4,%esi
mov    %r14,%rcx
call   1050 <my_fread@plt>
jmp    122e <read_file_headers_lhalotree+0xde>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%edx
lea    0x0(,%rdx,4),%ecx
lea    0xde2(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%ebp
mov    %r14,%rdi
call   1060 <fclose@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EXIT_SUCCESS = global i32 0
@.str = [2 x i8] c"r\00"
@EXIT_FAILURE = global i32 0
@stderr = global i32 0
@.str.1 = [88 x i8] c"malloc failed for array containing nhalos per tree. ntrees = %d. requested size = %zu \0A\00"
define i32 @read_file_headers_lhalotree(i8* %0, i32* %1, i32* %2, i32** %3) {
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32**
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i64
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
store i32** %3, i32*** %8
%12 = load i32, i32* @EXIT_SUCCESS
store i32 %12, i32* %9
%13 = load i8*, i8** %5
%14 = call i32* @my_fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %10
%15 = load i32*, i32** %6
%16 = load i32*, i32** %10
%17 = call i64 @my_fread(i32* %15, i32 4, i32 1, i32* %16)
store i64 %17, i64* %11
%18 = load i64, i64* %11
%19 = icmp ne i64 %18, 1
br i1 %19, label %20, label %22
20: ; preds = %4
%21 = load i32, i32* @EXIT_FAILURE
store i32 %21, i32* %9
br label %22
22: ; preds = %20, %4
%23 = load i32*, i32** %7
%24 = load i32*, i32** %10
%25 = call i64 @my_fread(i32* %23, i32 4, i32 1, i32* %24)
store i64 %25, i64* %11
%26 = load i64, i64* %11
%27 = icmp ne i64 %26, 1
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = load i32, i32* @EXIT_FAILURE
store i32 %29, i32* %9
br label %30
30: ; preds = %28, %22
%31 = load i32*, i32** %6
%32 = load i32, i32* %31
%33 = call i32* @my_malloc(i32 4, i32 %32)
%34 = load i32**, i32*** %8
store i32* %33, i32** %34
%35 = load i32**, i32*** %8
%36 = load i32*, i32** %35
%37 = icmp eq i32* %36, null
br i1 %37, label %38, label %49
38: ; preds = %30
%39 = load i32, i32* @stderr
%40 = load i32*, i32** %6
%41 = load i32, i32* %40
%42 = load i32*, i32** %6
%43 = load i32, i32* %42
%44 = sext i32 %43 to i64
%45 = mul i64 4, %44
%46 = trunc i64 %45 to i32
%47 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([88 x i8], [88 x i8]* @.str.1, i64 0, i64 0), i32 %41, i32 %46)
%48 = load i32, i32* @EXIT_FAILURE
store i32 %48, i32* %9
br label %56
49: ; preds = %30
%50 = load i32**, i32*** %8
%51 = load i32*, i32** %50
%52 = load i32*, i32** %6
%53 = load i32, i32* %52
%54 = load i32*, i32** %10
%55 = call i64 @my_fread(i32* %51, i32 4, i32 %53, i32* %54)
br label %56
56: ; preds = %49, %38
%57 = load i32*, i32** %10
%58 = call i32 @fclose(i32* %57)
%59 = load i32, i32* %9
ret i32 %59
}

LLVM IR (-O2):
@EXIT_SUCCESS = global i32 0
@.str = [2 x i8] c"r\00"
@EXIT_FAILURE = global i32 0
@stderr = global i32 0
@.str.1 = [88 x i8] c"malloc failed for array containing nhalos per tree. ntrees = %d. requested size = %zu \0A\00"
define i32 @read_file_headers_lhalotree(i8* %0, i32* %1, i32* %2, i32** nocapture writeonly %3) {
%5 = load i32, i32* @EXIT_SUCCESS
%6 = tail call i32* @my_fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = tail call i64 @my_fread(i32* %1, i32 4, i32 1, i32* %6)
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i64 @my_fread(i32* %2, i32 4, i32 1, i32* %6)
%10 = load i32, i32* @EXIT_FAILURE
%11 = load i32, i32* %1
%12 = tail call i32* @my_malloc(i32 4, i32 %11)
store i32* %12, i32** %3
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %20
14: ; preds = %4
%15 = load i32, i32* @stderr
%16 = load i32, i32* %1
%17 = shl i32 %16, 2
%18 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([88 x i8], [88 x i8]* @.str.1, i64 0, i64 0), i32 %16, i32 %17)
%19 = load i32, i32* @EXIT_FAILURE
br label %27
20: ; preds = %4
%21 = icmp eq i64 %9, 1
%22 = icmp eq i64 %7, 1
%23 = select i1 %22, i32 %5, i32 %8
%24 = select i1 %21, i32 %23, i32 %10
%25 = load i32, i32* %1
%26 = tail call i64 @my_fread(i32* %12, i32 4, i32 %25, i32* %6)
br label %27
27: ; preds = %20, %14
%28 = phi i32 [ %19, %14 ], [ %24, %20 ]
%29 = tail call i32 @fclose(i32* %6)
ret i32 %28
}