#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 stat {
  scalar_t__ st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ fstat(int /*<<< orphan*/, struct stat *);
char *malloc(scalar_t__);
char *read_file(const char *file) {
  FILE *f = fopen(file, "rb");
  if (!f)
    return 0;
  struct stat st;
  if (fstat(fileno(f), &st) != 0) {
    fclose(f);
    return 0;
  }
  char *data = malloc(st.st_size);
  if (!data) {
    fclose(f);
    return 0;
  }
  if (fread(data, 1, st.st_size, f) != (size_t)st.st_size) {
    fclose(f);
    free(data);
    return 0;
  }
  fclose(f);
  return data;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe79(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a8 <read_file+0x38>
movq   $0x0,-0x8(%rbp)
jmp    125a <read_file+0xea>
mov    -0x18(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
lea    -0x20(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0x0,%rax
je     11dc <read_file+0x6c>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    125a <read_file+0xea>
mov    -0x20(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    120a <read_file+0x9a>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    125a <read_file+0xea>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
cmp    -0x20(%rbp),%rax
je     1249 <read_file+0xd9>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    125a <read_file+0xea>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xe81(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11ab <read_file+0x3b>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <fileno@plt>
mov    %rsp,%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
test   %rax,%rax
je     11bd <read_file+0x4d>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    (%rsp),%r15
mov    %r15,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     11a3 <read_file+0x33>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1050 <fclose@plt>
cmp    %r15,%r12
je     11ae <read_file+0x3e>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    11ab <read_file+0x3b>

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

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [3 x i8] c"rb\00"
define noalias i8* @read_file(i8* nocapture %0) {
%2 = alloca %struct.stat
%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 %27, label %5
5: ; preds = %1
%6 = bitcast %struct.stat* %2 to i8*
%7 = tail call i32 @fileno(i32* %3)
%8 = call i64 @fstat(i32 %7, %struct.stat* %2)
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %5
%11 = tail call i32 @fclose(i32* %3)
br label %25
12: ; preds = %5
%13 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%14 = load i64, i64* %13
%15 = tail call i8* @malloc(i64 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = tail call i32 @fclose(i32* %3)
br label %25
19: ; preds = %12
%20 = tail call i64 @fread(i8* %15, i32 1, i64 %14, i32* %3)
%21 = icmp eq i64 %20, %14
%22 = tail call i32 @fclose(i32* %3)
br i1 %21, label %25, label %23
23: ; preds = %19
%24 = tail call i32 @free(i8* %15)
br label %25
25: ; preds = %19, %17, %23, %10
%26 = phi i8* [ null, %10 ], [ null, %23 ], [ null, %17 ], [ %15, %19 ]
br label %27
27: ; preds = %1, %25
%28 = phi i8* [ %26, %25 ], [ null, %1 ]
ret i8* %28
}