#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*/ uint8_t;
struct stat {
  int /*<<< orphan*/ st_size;
  int /*<<< orphan*/ member_0;
};
typedef int /*<<< orphan*/ ssize_t;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(int /*<<< orphan*/);
int /*<<< orphan*/ mp4tree_print(int /*<<< orphan*/ *, int /*<<< orphan*/,
                                 int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
scalar_t__ stat(char const *, struct stat *);
int process_file(const char *filename) {
  uint8_t *buf = NULL;
  struct stat sb = {0};
  int fd = -1;
  ssize_t len = 0;
  printf("Reading file %s\n", filename);
  if (stat(filename, &sb) < 0) {
    perror("stat");
    exit(EXIT_FAILURE);
  }
  buf = malloc(sb.st_size);
  if (buf == NULL) {
    printf("Failed to allocate memory\n");
    exit(EXIT_FAILURE);
  }
  fd = open(filename, 0);
  if (fd < 0) {
    perror("open");
    goto errout;
  }
  len = read(fd, buf, sb.st_size);
  printf("Read %zd bytes \n", len);
  if (len < 0) {
    perror("read");
    goto errout;
  }
  printf("File Content:\n");
  mp4tree_print(buf, len, 0);
  if (buf != NULL)
    free(buf);
  return EXIT_SUCCESS;
errout:
  if (buf != NULL)
    free(buf);
  return EXIT_FAILURE;
}


x86 assembly (clang, -O0):
<process_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
movl   $0xffffffff,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jge    1215 <process_file+0x75>
lea    0xe0f(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x20(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1248 <process_file+0xa8>
lea    0xde3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   10a0 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1271 <process_file+0xd1>
lea    0xdca(%rip),%rdi
call   10b0 <perror@plt>
jmp    12f0 <process_file+0x150>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1060 <read@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%esi
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x28(%rbp)
jge    12af <process_file+0x10f>
lea    0xda2(%rip),%rdi
call   10b0 <perror@plt>
jmp    12f0 <process_file+0x150>
lea    0xd96(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%esi
xor    %edx,%edx
call   1080 <mp4tree_print@plt>
cmpq   $0x0,-0x18(%rbp)
je     12df <process_file+0x13f>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    0x2cf2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1310 <process_file+0x170>
cmpq   $0x0,-0x18(%rbp)
je     1304 <process_file+0x164>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    0x2cd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<process_file>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
movq   $0x0,(%rsp)
lea    0xe4a(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1070 <stat@plt>
test   %rax,%rax
js     1268 <process_file+0xc8>
mov    (%rsp),%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     1276 <process_file+0xd6>
mov    %rax,%r14
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <open@plt>
test   %eax,%eax
js     124a <process_file+0xaa>
mov    (%rsp),%edx
mov    %eax,%edi
mov    %r14,%rsi
call   1060 <read@plt>
mov    %eax,%ebx
lea    0xe0f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
test   %ebx,%ebx
js     1253 <process_file+0xb3>
lea    0xe11(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
mov    %ebx,%esi
xor    %edx,%edx
call   1080 <mp4tree_print@plt>
mov    0x2da0(%rip),%rbx
mov    %r14,%rdi
call   1030 <free@plt>
mov    (%rbx),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdc5(%rip),%rdi
jmp    125a <process_file+0xba>
lea    0xdd2(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d7a(%rip),%rbx
jmp    1238 <process_file+0x98>
lea    0xda2(%rip),%rdi
call   10b0 <perror@plt>
jmp    1282 <process_file+0xe2>
lea    0xdc2(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@.str = [17 x i8] c"Reading file %s\0A\00"
@.str.1 = [5 x i8] c"stat\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [27 x i8] c"Failed to allocate memory\0A\00"
@.str.3 = [5 x i8] c"open\00"
@.str.4 = [17 x i8] c"Read %zd bytes \0A\00"
@.str.5 = [5 x i8] c"read\00"
@.str.6 = [15 x i8] c"File Content:\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @process_file(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.stat
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32* null, i32** %4
%8 = bitcast %struct.stat* %5 to i8*
store i32 -1, i32* %6
store i32 0, i32* %7
%9 = load i8*, i8** %3
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = load i8*, i8** %3
%12 = call i64 @stat(i8* %11, %struct.stat* %5)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %1
%19 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i32* @malloc(i32 %20)
store i32* %21, i32** %4
%22 = load i32*, i32** %4
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %28
24: ; preds = %18
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %18
%29 = load i8*, i8** %3
%30 = call i32 @open(i8* %29, i32 0)
store i32 %30, i32* %6
%31 = load i32, i32* %6
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %59
35: ; preds = %28
%36 = load i32, i32* %6
%37 = load i32*, i32** %4
%38 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%39 = load i32, i32* %38
%40 = call i32 @read(i32 %36, i32* %37, i32 %39)
store i32 %40, i32* %7
%41 = load i32, i32* %7
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %41)
%43 = load i32, i32* %7
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %35
%46 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %59
47: ; preds = %35
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%49 = load i32*, i32** %4
%50 = load i32, i32* %7
%51 = call i32 @mp4tree_print(i32* %49, i32 %50, i32 0)
%52 = load i32*, i32** %4
%53 = icmp ne i32* %52, null
br i1 %53, label %54, label %57
54: ; preds = %47
%55 = load i32*, i32** %4
%56 = call i32 @free(i32* %55)
br label %57
57: ; preds = %54, %47
%58 = load i32, i32* @EXIT_SUCCESS
store i32 %58, i32* %2
br label %67
59: ; preds = %45, %33
%60 = load i32*, i32** %4
%61 = icmp ne i32* %60, null
br i1 %61, label %62, label %65
62: ; preds = %59
%63 = load i32*, i32** %4
%64 = call i32 @free(i32* %63)
br label %65
65: ; preds = %62, %59
%66 = load i32, i32* @EXIT_FAILURE
store i32 %66, i32* %2
br label %67
67: ; preds = %65, %57
%68 = load i32, i32* %2
ret i32 %68
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@.str = [17 x i8] c"Reading file %s\0A\00"
@.str.1 = [5 x i8] c"stat\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [5 x i8] c"open\00"
@.str.4 = [17 x i8] c"Read %zd bytes \0A\00"
@.str.5 = [5 x i8] c"read\00"
@EXIT_SUCCESS = global i32 0
@str = [14 x i8] c"File Content:\00"
@str.7 = [26 x i8] c"Failed to allocate memory\00"
define i32 @process_file(i8* %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to %struct.stat*
%4 = bitcast i64* %2 to i8*
store i64 0, i64* %2
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %0)
%6 = call i64 @stat(i8* %0, %struct.stat* %3)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %1
%13 = bitcast i64* %2 to i32*
%14 = load i32, i32* %13
%15 = call i32* @malloc(i32 %14)
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.7, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %12
%22 = call i32 @open(i8* %0, i32 0)
%23 = icmp slt i32 %22, 0
br i1 %23, label %32, label %24
24: ; preds = %21
%25 = load i32, i32* %13
%26 = call i32 @read(i32 %22, i32* %15, i32 %25)
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %26)
%28 = icmp slt i32 %26, 0
br i1 %28, label %32, label %29
29: ; preds = %24
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%31 = call i32 @mp4tree_print(i32* %15, i32 %26, i32 0)
br label %35
32: ; preds = %24, %21
%33 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), %21 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), %24 ]
%34 = call i32 @perror(i8* %33)
br label %35
35: ; preds = %32, %29
%36 = phi i32* [ @EXIT_FAILURE, %32 ], [ @EXIT_SUCCESS, %29 ]
%37 = call i32 @free(i32* %15)
%38 = load i32, i32* %36
ret i32 %38
}