#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*/ fread(char *, int, long, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *malloc(long);
long read_file(FILE *file, char **dst) {
  if (fseek(file, 0, SEEK_END) != 0)
    return -1;
  long len = ftell(file);
  if (fseek(file, 0, SEEK_SET) != 0)
    return -1;
  *dst = malloc(len);
  fread(*dst, 1, len, file);
  return len;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e8d(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
cmp    $0x0,%rax
je     117b <read_file+0x3b>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11e7 <read_file+0xa7>
mov    -0x10(%rbp),%rdi
call   1040 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e45(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
cmp    $0x0,%rax
je     11b3 <read_file+0x73>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11e7 <read_file+0xa7>
mov    -0x20(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e93(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
mov    $0xffffffffffffffff,%r12
test   %rax,%rax
jne    11ae <read_file+0x6e>
mov    %rbx,%rdi
call   1040 <ftell@plt>
mov    %rax,%r15
mov    0x2e5c(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <fseek@plt>
test   %rax,%rax
jne    11ae <read_file+0x6e>
mov    %r15,%rdi
call   1050 <malloc@plt>
mov    %rax,(%r14)
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %r15,%r12
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i64 @read_file(i32* %0, i8** %1) {
%3 = alloca i64
%4 = alloca i32*
%5 = alloca i8**
%6 = alloca i64
store i32* %0, i32** %4
store i8** %1, i8*** %5
%7 = load i32*, i32** %4
%8 = load i32, i32* @SEEK_END
%9 = call i64 @fseek(i32* %7, i32 0, i32 %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i64 -1, i64* %3
br label %30
12: ; preds = %2
%13 = load i32*, i32** %4
%14 = call i64 @ftell(i32* %13)
store i64 %14, i64* %6
%15 = load i32*, i32** %4
%16 = load i32, i32* @SEEK_SET
%17 = call i64 @fseek(i32* %15, i32 0, i32 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %12
store i64 -1, i64* %3
br label %30
20: ; preds = %12
%21 = load i64, i64* %6
%22 = call i8* @malloc(i64 %21)
%23 = load i8**, i8*** %5
store i8* %22, i8** %23
%24 = load i8**, i8*** %5
%25 = load i8*, i8** %24
%26 = load i64, i64* %6
%27 = load i32*, i32** %4
%28 = call i32 @fread(i8* %25, i32 1, i64 %26, i32* %27)
%29 = load i64, i64* %6
store i64 %29, i64* %3
br label %30
30: ; preds = %20, %19, %11
%31 = load i64, i64* %3
ret i64 %31
}

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