#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 tml_doc {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
long fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
struct tml_doc *tml_parse_in_memory(char *, long);
struct tml_doc *tml_parse_file(const char *filename) {
  long int fsize;
  FILE *fp = fopen(filename, "rb");
  if (!fp)
    return NULL;
  fseek(fp, 0, SEEK_END);
  fsize = ftell(fp);
  rewind(fp);
  char *buff = malloc(sizeof(char) * fsize);
  if (!buff) {
    fclose(fp);
    return NULL;
  }
  if (fsize != fread(buff, 1, fsize, fp)) {
    fclose(fp);
    free(buff);
    return NULL;
  }
  struct tml_doc *data = tml_parse_in_memory(buff, fsize);
  fclose(fp);
  free(buff);
  return data;
}


x86 assembly (clang, -O0):
<tml_parse_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c8 <tml_parse_file+0x38>
movq   $0x0,-0x8(%rbp)
jmp    129e <tml_parse_file+0x10e>
mov    -0x20(%rbp),%rdi
mov    0x2e15(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x18(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1226 <tml_parse_file+0x96>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    129e <tml_parse_file+0x10e>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
cmp    %rcx,%rax
je     1273 <tml_parse_file+0xe3>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    129e <tml_parse_file+0x10e>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <tml_parse_in_memory@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tml_parse_file>:
push   %r15
push   %r14
push   %rbx
lea    0xe64(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1238 <tml_parse_file+0xa8>
mov    %rax,%rbx
mov    0x2e34(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1060 <rewind@plt>
mov    %r15d,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     121e <tml_parse_file+0x8e>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    %rax,%r15
jne    1228 <tml_parse_file+0x98>
mov    %r14,%rdi
mov    %r15,%rsi
call   1070 <tml_parse_in_memory@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rax
jmp    123a <tml_parse_file+0xaa>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    1238 <tml_parse_file+0xa8>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.tml_doc = type { i32 }
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
define %struct.tml_doc* @tml_parse_file(i8* %0) {
%2 = alloca %struct.tml_doc*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca %struct.tml_doc*
store i8* %0, i8** %3
%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** %5
%10 = load i32*, i32** %5
%11 = icmp ne i32* %10, null
br i1 %11, label %13, label %12
12: ; preds = %1
store %struct.tml_doc* null, %struct.tml_doc** %2
br label %51
13: ; preds = %1
%14 = load i32*, i32** %5
%15 = load i32, i32* @SEEK_END
%16 = call i32 @fseek(i32* %14, i32 0, i32 %15)
%17 = load i32*, i32** %5
%18 = call i64 @ftell(i32* %17)
store i64 %18, i64* %4
%19 = load i32*, i32** %5
%20 = call i32 @rewind(i32* %19)
%21 = load i64, i64* %4
%22 = mul i64 1, %21
%23 = trunc i64 %22 to i32
%24 = call i8* @malloc(i32 %23)
store i8* %24, i8** %6
%25 = load i8*, i8** %6
%26 = icmp ne i8* %25, null
br i1 %26, label %30, label %27
27: ; preds = %13
%28 = load i32*, i32** %5
%29 = call i32 @fclose(i32* %28)
store %struct.tml_doc* null, %struct.tml_doc** %2
br label %51
30: ; preds = %13
%31 = load i64, i64* %4
%32 = load i8*, i8** %6
%33 = load i64, i64* %4
%34 = load i32*, i32** %5
%35 = call i64 @fread(i8* %32, i32 1, i64 %33, i32* %34)
%36 = icmp ne i64 %31, %35
br i1 %36, label %37, label %42
37: ; preds = %30
%38 = load i32*, i32** %5
%39 = call i32 @fclose(i32* %38)
%40 = load i8*, i8** %6
%41 = call i32 @free(i8* %40)
store %struct.tml_doc* null, %struct.tml_doc** %2
br label %51
42: ; preds = %30
%43 = load i8*, i8** %6
%44 = load i64, i64* %4
%45 = call %struct.tml_doc* @tml_parse_in_memory(i8* %43, i64 %44)
store %struct.tml_doc* %45, %struct.tml_doc** %7
%46 = load i32*, i32** %5
%47 = call i32 @fclose(i32* %46)
%48 = load i8*, i8** %6
%49 = call i32 @free(i8* %48)
%50 = load %struct.tml_doc*, %struct.tml_doc** %7
store %struct.tml_doc* %50, %struct.tml_doc** %2
br label %51
51: ; preds = %42, %37, %27, %12
%52 = load %struct.tml_doc*, %struct.tml_doc** %2
ret %struct.tml_doc* %52
}

LLVM IR (-O2):
%struct.tml_doc = type { i32 }
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
define %struct.tml_doc* @tml_parse_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 %24, label %4
4: ; preds = %1
%5 = load i32, i32* @SEEK_END
%6 = tail call i32 @fseek(i32* %2, i32 0, i32 %5)
%7 = tail call i64 @ftell(i32* %2)
%8 = tail call i32 @rewind(i32* %2)
%9 = trunc i64 %7 to i32
%10 = tail call i8* @malloc(i32 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %4
%13 = tail call i32 @fclose(i32* %2)
br label %24
14: ; preds = %4
%15 = tail call i64 @fread(i8* %10, i32 1, i64 %7, i32* %2)
%16 = icmp eq i64 %7, %15
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = tail call i32 @fclose(i32* %2)
%19 = tail call i32 @free(i8* %10)
br label %24
20: ; preds = %14
%21 = tail call %struct.tml_doc* @tml_parse_in_memory(i8* %10, i64 %7)
%22 = tail call i32 @fclose(i32* %2)
%23 = tail call i32 @free(i8* %10)
br label %24
24: ; preds = %12, %17, %20, %1
%25 = phi %struct.tml_doc* [ null, %1 ], [ null, %17 ], [ %21, %20 ], [ null, %12 ]
ret %struct.tml_doc* %25
}