#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int file_size(char *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(void *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(void *);
scalar_t__ malloc(int);
void *file_alloc(char *fname, int *pn) {
  void *b;
  int n;
  FILE *fp;
  n = file_size(fname);
  if (n < 0)
    return NULL;
  if (pn)
    *pn = n;
  b = (void *)malloc(n + 1);
  fp = fopen(fname, "rb");
  if (fread(b, 1, n, fp) < n) {
    free(b);
    b = NULL;
  }
  fclose(fp);
  return b;
}


x86 assembly (clang, -O0):
<file_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <file_size@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1193 <file_alloc+0x33>
movq   $0x0,-0x8(%rbp)
jmp    120a <file_alloc+0xaa>
cmpq   $0x0,-0x18(%rbp)
je     11a7 <file_alloc+0x47>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe3f(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
cmp    -0x24(%rbp),%eax
jge    11f9 <file_alloc+0x99>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_alloc>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1060 <file_size@plt>
test   %eax,%eax
js     11c6 <file_alloc+0x66>
mov    %eax,%ebx
test   %r14,%r14
je     117e <file_alloc+0x1e>
mov    %ebx,(%r14)
lea    0x1(%rbx),%edi
call   1070 <malloc@plt>
mov    %rax,%r14
lea    0xe70(%rip),%rsi
mov    %r15,%rdi
call   1080 <fopen@plt>
mov    %rax,%r15
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
cmp    %ebx,%eax
jge    11bc <file_alloc+0x5c>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r15,%rdi
call   1050 <fclose@plt>
jmp    11c9 <file_alloc+0x69>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
define i8* @file_alloc(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
store i8* %0, i8** %4
store i32* %1, i32** %5
%9 = load i8*, i8** %4
%10 = call i32 @file_size(i8* %9)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i8* null, i8** %3
br label %40
14: ; preds = %2
%15 = load i32*, i32** %5
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* %7
%19 = load i32*, i32** %5
store i32 %18, i32* %19
br label %20
20: ; preds = %17, %14
%21 = load i32, i32* %7
%22 = add nsw i32 %21, 1
%23 = call i64 @malloc(i32 %22)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %6
%25 = load i8*, i8** %4
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %26, i32** %8
%27 = load i8*, i8** %6
%28 = load i32, i32* %7
%29 = load i32*, i32** %8
%30 = call i32 @fread(i8* %27, i32 1, i32 %28, i32* %29)
%31 = load i32, i32* %7
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %36
33: ; preds = %20
%34 = load i8*, i8** %6
%35 = call i32 @free(i8* %34)
store i8* null, i8** %6
br label %36
36: ; preds = %33, %20
%37 = load i32*, i32** %8
%38 = call i32 @fclose(i32* %37)
%39 = load i8*, i8** %6
store i8* %39, i8** %3
br label %40
40: ; preds = %36, %13
%41 = load i8*, i8** %3
ret i8* %41
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
define i8* @file_alloc(i8* %0, i32* writeonly %1) {
%3 = tail call i32 @file_size(i8* %0)
%4 = icmp slt i32 %3, 0
br i1 %4, label %20, label %5
5: ; preds = %2
%6 = icmp eq i32* %1, null
br i1 %6, label %8, label %7
7: ; preds = %5
store i32 %3, i32* %1
br label %8
8: ; preds = %7, %5
%9 = add nuw nsw i32 %3, 1
%10 = tail call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to i8*
%12 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @fread(i8* %11, i32 1, i32 %3, i32* %12)
%14 = icmp slt i32 %13, %3
br i1 %14, label %15, label %17
15: ; preds = %8
%16 = tail call i32 @free(i8* %11)
br label %17
17: ; preds = %15, %8
%18 = phi i8* [ null, %15 ], [ %11, %8 ]
%19 = tail call i32 @fclose(i32* %12)
br label %20
20: ; preds = %2, %17
%21 = phi i8* [ %18, %17 ], [ null, %2 ]
ret i8* %21
}