#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 fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
char *malloc(size_t);
int read_file(FILE *f, char **dat) {
  if (!f)
    return -1;
  size_t sz;
  fseek(f, 0L, SEEK_END);
  sz = ftell(f);
  fseek(f, 0L, SEEK_SET);
  *dat = malloc(sz);
  if (!*dat) {
    return -1;
  }
  int bytes_read;
  bytes_read = fread(*dat, 1, sz, f);
  if (bytes_read < sz) {
    free(*dat);
    *dat = NULL;
    return -1;
  }
  return bytes_read;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1177 <read_file+0x27>
movl   $0xffffffff,-0x4(%rbp)
jmp    1230 <read_file+0xe0>
mov    -0x10(%rbp),%rdi
mov    0x2e66(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1050 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e33(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11dd <read_file+0x8d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1230 <read_file+0xe0>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    122a <read_file+0xda>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1230 <read_file+0xe0>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0xffffffff,%ebp
test   %rdi,%rdi
je     11d5 <read_file+0x85>
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e7a(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %rbx,%rdi
call   1050 <ftell@plt>
mov    %rax,%r15
mov    0x2e4f(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %r15,%rdi
call   1060 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     11d5 <read_file+0x85>
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %eax,%ebp
cltq
cmp    %rax,%r15
jbe    11d5 <read_file+0x85>
mov    (%r14),%rdi
call   1030 <free@plt>
movq   $0x0,(%r14)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @read_file(i32* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
store i32* %0, i32** %4
store i8** %1, i8*** %5
%8 = load i32*, i32** %4
%9 = icmp ne i32* %8, null
br i1 %9, label %11, label %10
10: ; preds = %2
store i32 -1, i32* %3
br label %44
11: ; preds = %2
%12 = load i32*, i32** %4
%13 = load i32, i32* @SEEK_END
%14 = call i32 @fseek(i32* %12, i64 0, i32 %13)
%15 = load i32*, i32** %4
%16 = call i64 @ftell(i32* %15)
store i64 %16, i64* %6
%17 = load i32*, i32** %4
%18 = load i32, i32* @SEEK_SET
%19 = call i32 @fseek(i32* %17, i64 0, i32 %18)
%20 = load i64, i64* %6
%21 = call i8* @malloc(i64 %20)
%22 = load i8**, i8*** %5
store i8* %21, i8** %22
%23 = load i8**, i8*** %5
%24 = load i8*, i8** %23
%25 = icmp ne i8* %24, null
br i1 %25, label %27, label %26
26: ; preds = %11
store i32 -1, i32* %3
br label %44
27: ; preds = %11
%28 = load i8**, i8*** %5
%29 = load i8*, i8** %28
%30 = load i64, i64* %6
%31 = load i32*, i32** %4
%32 = call i32 @fread(i8* %29, i32 1, i64 %30, i32* %31)
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = sext i32 %33 to i64
%35 = load i64, i64* %6
%36 = icmp ult i64 %34, %35
br i1 %36, label %37, label %42
37: ; preds = %27
%38 = load i8**, i8*** %5
%39 = load i8*, i8** %38
%40 = call i32 @free(i8* %39)
%41 = load i8**, i8*** %5
store i8* null, i8** %41
store i32 -1, i32* %3
br label %44
42: ; preds = %27
%43 = load i32, i32* %7
store i32 %43, i32* %3
br label %44
44: ; preds = %42, %37, %26, %10
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @read_file(i32* %0, i8** nocapture %1) {
%3 = icmp eq i32* %0, null
br i1 %3, label %19, label %4
4: ; preds = %2
%5 = load i32, i32* @SEEK_END
%6 = tail call i32 @fseek(i32* %0, i64 0, i32 %5)
%7 = tail call i64 @ftell(i32* %0)
%8 = load i32, i32* @SEEK_SET
%9 = tail call i32 @fseek(i32* %0, i64 0, i32 %8)
%10 = tail call i8* @malloc(i64 %7)
store i8* %10, i8** %1
%11 = icmp eq i8* %10, null
br i1 %11, label %19, label %12
12: ; preds = %4
%13 = tail call i32 @fread(i8* %10, i32 1, i64 %7, i32* %0)
%14 = sext i32 %13 to i64
%15 = icmp ugt i64 %7, %14
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i8*, i8** %1
%18 = tail call i32 @free(i8* %17)
store i8* null, i8** %1
br label %19
19: ; preds = %4, %12, %16, %2
%20 = phi i32 [ -1, %2 ], [ -1, %4 ], [ -1, %16 ], [ %13, %12 ]
ret i32 %20
}