#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ x264_free(char *);
char *x264_malloc(int);
char *x264_slurp_file(const char *filename) {
  int b_error = 0;
  int i_size;
  char *buf;
  FILE *fh = fopen(filename, "rb");
  if (!fh)
    return NULL;
  b_error |= fseek(fh, 0, SEEK_END) < 0;
  b_error |= (i_size = ftell(fh)) <= 0;
  b_error |= fseek(fh, 0, SEEK_SET) < 0;
  if (b_error)
    return NULL;
  buf = x264_malloc(i_size + 2);
  if (buf == NULL)
    return NULL;
  b_error |= fread(buf, 1, i_size, fh) != i_size;
  if (buf[i_size - 1] != '\n')
    buf[i_size++] = '\n';
  buf[i_size] = 0;
  fclose(fh);
  if (b_error) {
    x264_free(buf);
    return NULL;
  }
  return buf;
}


x86 assembly (clang, -O0):
<x264_slurp_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe72(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11af <x264_slurp_file+0x3f>
movq   $0x0,-0x8(%rbp)
jmp    12e9 <x264_slurp_file+0x179>
mov    -0x28(%rbp),%rdi
mov    0x2e2e(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
cmp    $0x0,%rax
setl   %al
and    $0x1,%al
movzbl %al,%eax
or     -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <ftell@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
setle  %al
and    $0x1,%al
movzbl %al,%eax
or     -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2ddb(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
cmp    $0x0,%rax
setl   %al
and    $0x1,%al
movzbl %al,%eax
or     -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     122f <x264_slurp_file+0xbf>
movq   $0x0,-0x8(%rbp)
jmp    12e9 <x264_slurp_file+0x179>
mov    -0x18(%rbp),%edi
add    $0x2,%edi
call   1030 <x264_malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1256 <x264_slurp_file+0xe6>
movq   $0x0,-0x8(%rbp)
jmp    12e9 <x264_slurp_file+0x179>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
cmp    -0x18(%rbp),%eax
setne  %al
and    $0x1,%al
movzbl %al,%eax
or     -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     12ac <x264_slurp_file+0x13c>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x18(%rbp)
movslq %ecx,%rcx
movb   $0xa,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
cmpl   $0x0,-0x14(%rbp)
je     12e1 <x264_slurp_file+0x171>
mov    -0x20(%rbp),%rdi
call   1090 <x264_free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12e9 <x264_slurp_file+0x179>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<x264_slurp_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
lea    0xe81(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1236 <x264_slurp_file+0xc6>
mov    %rax,%r12
mov    0x2e51(%rip),%rax
mov    (%rax),%edx
xor    %r14d,%r14d
mov    %r12,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %rax,%rbp
shr    $0x3f,%rbp
mov    %r12,%rdi
call   1060 <ftell@plt>
mov    %eax,%r15d
xor    %ebx,%ebx
test   %eax,%eax
setle  %bl
or     %ebp,%ebx
mov    0x2e10(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
shr    $0x3f,%rax
or     %ebx,%eax
jne    1239 <x264_slurp_file+0xc9>
lea    0x2(%r15),%edi
call   1030 <x264_malloc@plt>
test   %rax,%rax
je     1236 <x264_slurp_file+0xc6>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %r12,%rcx
call   1040 <fread@plt>
mov    %eax,%ebp
movslq %r15d,%rax
cmpb   $0xa,-0x1(%rax,%r14,1)
mov    %r15d,%ecx
je     1219 <x264_slurp_file+0xa9>
lea    0x1(%r15),%ecx
movb   $0xa,(%r14,%rax,1)
movslq %ecx,%rax
movb   $0x0,(%r14,%rax,1)
mov    %r12,%rdi
call   1050 <fclose@plt>
cmp    %r15d,%ebp
je     1239 <x264_slurp_file+0xc9>
mov    %r14,%rdi
call   1090 <x264_free@plt>
xor    %r14d,%r14d
mov    %r14,%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* @x264_slurp_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
store i8* %0, i8** %3
store i32 0, i32* %4
%8 = load i8*, i8** %3
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %7
%10 = load i32*, i32** %7
%11 = icmp ne i32* %10, null
br i1 %11, label %13, label %12
12: ; preds = %1
store i8* null, i8** %2
br label %82
13: ; preds = %1
%14 = load i32*, i32** %7
%15 = load i32, i32* @SEEK_END
%16 = call i64 @fseek(i32* %14, i32 0, i32 %15)
%17 = icmp slt i64 %16, 0
%18 = zext i1 %17 to i32
%19 = load i32, i32* %4
%20 = or i32 %19, %18
store i32 %20, i32* %4
%21 = load i32*, i32** %7
%22 = call i32 @ftell(i32* %21)
store i32 %22, i32* %5
%23 = icmp sle i32 %22, 0
%24 = zext i1 %23 to i32
%25 = load i32, i32* %4
%26 = or i32 %25, %24
store i32 %26, i32* %4
%27 = load i32*, i32** %7
%28 = load i32, i32* @SEEK_SET
%29 = call i64 @fseek(i32* %27, i32 0, i32 %28)
%30 = icmp slt i64 %29, 0
%31 = zext i1 %30 to i32
%32 = load i32, i32* %4
%33 = or i32 %32, %31
store i32 %33, i32* %4
%34 = load i32, i32* %4
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %13
store i8* null, i8** %2
br label %82
37: ; preds = %13
%38 = load i32, i32* %5
%39 = add nsw i32 %38, 2
%40 = call i8* @x264_malloc(i32 %39)
store i8* %40, i8** %6
%41 = load i8*, i8** %6
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %44
43: ; preds = %37
store i8* null, i8** %2
br label %82
44: ; preds = %37
%45 = load i8*, i8** %6
%46 = load i32, i32* %5
%47 = load i32*, i32** %7
%48 = call i32 @fread(i8* %45, i32 1, i32 %46, i32* %47)
%49 = load i32, i32* %5
%50 = icmp ne i32 %48, %49
%51 = zext i1 %50 to i32
%52 = load i32, i32* %4
%53 = or i32 %52, %51
store i32 %53, i32* %4
%54 = load i8*, i8** %6
%55 = load i32, i32* %5
%56 = sub nsw i32 %55, 1
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %54, i64 %57
%59 = load i8, i8* %58
%60 = sext i8 %59 to i32
%61 = icmp ne i32 %60, 10
br i1 %61, label %62, label %68
62: ; preds = %44
%63 = load i8*, i8** %6
%64 = load i32, i32* %5
%65 = add nsw i32 %64, 1
store i32 %65, i32* %5
%66 = sext i32 %64 to i64
%67 = getelementptr inbounds i8, i8* %63, i64 %66
store i8 10, i8* %67
br label %68
68: ; preds = %62, %44
%69 = load i8*, i8** %6
%70 = load i32, i32* %5
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8, i8* %69, i64 %71
store i8 0, i8* %72
%73 = load i32*, i32** %7
%74 = call i32 @fclose(i32* %73)
%75 = load i32, i32* %4
%76 = icmp ne i32 %75, 0
br i1 %76, label %77, label %80
77: ; preds = %68
%78 = load i8*, i8** %6
%79 = call i32 @x264_free(i8* %78)
store i8* null, i8** %2
br label %82
80: ; preds = %68
%81 = load i8*, i8** %6
store i8* %81, i8** %2
br label %82
82: ; preds = %80, %77, %43, %36, %12
%83 = load i8*, i8** %2
ret i8* %83
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i8* @x264_slurp_file(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %42, label %4
4: ; preds = %1
%5 = load i32, i32* @SEEK_END
%6 = tail call i64 @fseek(i32* %2, i32 0, i32 %5)
%7 = lshr i64 %6, 63
%8 = trunc i64 %7 to i32
%9 = tail call i32 @ftell(i32* %2)
%10 = icmp slt i32 %9, 1
%11 = zext i1 %10 to i32
%12 = or i32 %11, %8
%13 = load i32, i32* @SEEK_SET
%14 = tail call i64 @fseek(i32* %2, i32 0, i32 %13)
%15 = lshr i64 %14, 63
%16 = trunc i64 %15 to i32
%17 = or i32 %12, %16
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %42
19: ; preds = %4
%20 = add nsw i32 %9, 2
%21 = tail call i8* @x264_malloc(i32 %20)
%22 = icmp eq i8* %21, null
br i1 %22, label %42, label %23
23: ; preds = %19
%24 = tail call i32 @fread(i8* %21, i32 1, i32 %9, i32* %2)
%25 = icmp eq i32 %24, %9
%26 = add nsw i32 %9, -1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %21, i64 %27
%29 = load i8, i8* %28
%30 = icmp eq i8 %29, 10
br i1 %30, label %35, label %31
31: ; preds = %23
%32 = add nsw i32 %9, 1
%33 = sext i32 %9 to i64
%34 = getelementptr inbounds i8, i8* %21, i64 %33
store i8 10, i8* %34
br label %35
35: ; preds = %31, %23
%36 = phi i32 [ %32, %31 ], [ %9, %23 ]
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %21, i64 %37
store i8 0, i8* %38
%39 = tail call i32 @fclose(i32* %2)
br i1 %25, label %42, label %40
40: ; preds = %35
%41 = tail call i32 @x264_free(i8* %21)
br label %42
42: ; preds = %35, %19, %4, %1, %40
%43 = phi i8* [ null, %40 ], [ null, %1 ], [ null, %4 ], [ null, %19 ], [ %21, %35 ]
ret i8* %43
}