#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*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ *fopen(char *, char *);
int fread(void *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
void *malloc(int);
void *read_buffer(char *file_name, int *psize) {
  FILE *f;
  void *buf;
  int size, alloc_size, read_size;
  f = fopen(file_name, "rb");
  if (!f)
    return NULL;
  fseek(f, 0, SEEK_END);
  size = ftell(f);
  alloc_size = size ? size : 1;
  fseek(f, 0, SEEK_SET);
  buf = malloc(alloc_size);
  if (!buf)
    return NULL;
  read_size = fread(buf, 1, size, f);
  if (psize)
    *psize = read_size;
  return buf;
}


x86 assembly (clang, -O0):
<read_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe95(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    118c <read_buffer+0x3c>
movq   $0x0,-0x8(%rbp)
jmp    1240 <read_buffer+0xf0>
mov    -0x20(%rbp),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1040 <ftell@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     11c1 <read_buffer+0x71>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x38(%rbp)
jmp    11ce <read_buffer+0x7e>
mov    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    11ce <read_buffer+0x7e>
mov    -0x38(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
mov    -0x30(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    120c <read_buffer+0xbc>
movq   $0x0,-0x8(%rbp)
jmp    1240 <read_buffer+0xf0>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x34(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1238 <read_buffer+0xe8>
mov    -0x34(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
lea    0xe9e(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11d6 <read_buffer+0x86>
mov    %rax,%rbx
mov    0x2e72(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <fseek@plt>
mov    %rbx,%rdi
call   1040 <ftell@plt>
mov    %eax,%r12d
test   %eax,%eax
mov    $0x1,%ebp
cmovne %eax,%ebp
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <fseek@plt>
mov    %ebp,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11d6 <read_buffer+0x86>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r12d,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
test   %r14,%r14
je     11d9 <read_buffer+0x89>
mov    %eax,(%r14)
jmp    11d9 <read_buffer+0x89>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i8* @read_buffer(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%11 = load i8*, i8** %4
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %6
%13 = load i32*, i32** %6
%14 = icmp ne i32* %13, null
br i1 %14, label %16, label %15
15: ; preds = %2
store i8* null, i8** %3
br label %49
16: ; preds = %2
%17 = load i32*, i32** %6
%18 = load i32, i32* @SEEK_END
%19 = call i32 @fseek(i32* %17, i32 0, i32 %18)
%20 = load i32*, i32** %6
%21 = call i32 @ftell(i32* %20)
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = load i32, i32* %8
br label %27
26: ; preds = %16
br label %27
27: ; preds = %26, %24
%28 = phi i32 [ %25, %24 ], [ 1, %26 ]
store i32 %28, i32* %9
%29 = load i32*, i32** %6
%30 = load i32, i32* @SEEK_SET
%31 = call i32 @fseek(i32* %29, i32 0, i32 %30)
%32 = load i32, i32* %9
%33 = call i8* @malloc(i32 %32)
store i8* %33, i8** %7
%34 = load i8*, i8** %7
%35 = icmp ne i8* %34, null
br i1 %35, label %37, label %36
36: ; preds = %27
store i8* null, i8** %3
br label %49
37: ; preds = %27
%38 = load i8*, i8** %7
%39 = load i32, i32* %8
%40 = load i32*, i32** %6
%41 = call i32 @fread(i8* %38, i32 1, i32 %39, i32* %40)
store i32 %41, i32* %10
%42 = load i32*, i32** %5
%43 = icmp ne i32* %42, null
br i1 %43, label %44, label %47
44: ; preds = %37
%45 = load i32, i32* %10
%46 = load i32*, i32** %5
store i32 %45, i32* %46
br label %47
47: ; preds = %44, %37
%48 = load i8*, i8** %7
store i8* %48, i8** %3
br label %49
49: ; preds = %47, %36, %15
%50 = load i8*, i8** %3
ret i8* %50
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define noalias i8* @read_buffer(i8* nocapture %0, i32* writeonly %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %19, label %5
5: ; preds = %2
%6 = load i32, i32* @SEEK_END
%7 = tail call i32 @fseek(i32* %3, i32 0, i32 %6)
%8 = tail call i32 @ftell(i32* %3)
%9 = icmp eq i32 %8, 0
%10 = select i1 %9, i32 1, i32 %8
%11 = load i32, i32* @SEEK_SET
%12 = tail call i32 @fseek(i32* %3, i32 0, i32 %11)
%13 = tail call i8* @malloc(i32 %10)
%14 = icmp eq i8* %13, null
br i1 %14, label %19, label %15
15: ; preds = %5
%16 = tail call i32 @fread(i8* %13, i32 1, i32 %8, i32* %3)
%17 = icmp eq i32* %1, null
br i1 %17, label %19, label %18
18: ; preds = %15
store i32 %16, i32* %1
br label %19
19: ; preds = %15, %18, %5, %2
%20 = phi i8* [ null, %2 ], [ null, %5 ], [ %13, %18 ], [ %13, %15 ]
ret i8* %20
}