#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;
typedef int uint32_t;
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 *const, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *const);
int fread(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int new_blob(int);
int /*<<< orphan*/ printf(char *, char const *const);
int /*<<< orphan*/ *sector_to_memory(int);
int /*<<< orphan*/ stderr;
uint32_t add_file(const char *const file, uint32_t *size) {
  FILE *fp;
  printf("Adding '%s' to filesystem image.\n", file);
  fp = fopen(file, "r");
  if (!fp) {
    fprintf(stderr, "Cannot open file '%s' for read!\n", file);
    return 0;
  }
  fseek(fp, 0, SEEK_END);
  *size = ftell(fp);
  fseek(fp, 0, SEEK_SET);
  if (*size > 0x0FFFFFFF) {
    fprintf(stderr, "File '%s' too big for the filesystem!\n", file);
    return 0;
  }
  uint32_t blob = new_blob(*size);
  uint8_t *data = sector_to_memory(blob);
  int read = fread(data, 1, *size, fp);
  if (read < 0) {
    fprintf(stderr, "Cannot add all contents of file '%s' to filesystem!\n",
            file);
    fclose(fp);
    return 0;
  }
  fclose(fp);
  return blob;
}


x86 assembly (clang, -O0):
<add_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe55(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    0xe67(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f4 <add_file+0x64>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe41(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12de <add_file+0x14e>
mov    -0x20(%rbp),%rdi
mov    0x2de9(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rdi
mov    0x2dac(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x18(%rbp),%rax
cmpl   $0xfffffff,(%rax)
jle    1262 <add_file+0xd2>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdf4(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12de <add_file+0x14e>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1080 <new_blob@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   10b0 <sector_to_memory@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    12cf <add_file+0x13f>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdb7(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    12de <add_file+0x14e>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbp
mov    %rdi,%r14
lea    0xe5c(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
lea    0xe6f(%rip),%rsi
mov    %r14,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     121c <add_file+0x8c>
mov    %rax,%rbx
mov    0x2e1e(%rip),%rax
mov    (%rax),%edx
xor    %r15d,%r15d
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %eax,0x0(%rbp)
mov    0x2de5(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    0x0(%rbp),%edi
cmp    $0x10000000,%edi
jl     1236 <add_file+0xa6>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0xe33(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
jmp    1289 <add_file+0xf9>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
lea    0xdf8(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
jmp    1286 <add_file+0xf6>
call   1080 <new_blob@plt>
mov    %eax,%r15d
mov    %eax,%edi
call   10b0 <sector_to_memory@plt>
mov    0x0(%rbp),%edx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %rbx,%rcx
call   1030 <fread@plt>
test   %eax,%eax
js     1266 <add_file+0xd6>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    1289 <add_file+0xf9>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
lea    0xdf6(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [34 x i8] c"Adding '%s' to filesystem image.\0A\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [33 x i8] c"Cannot open file '%s' for read!\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [39 x i8] c"File '%s' too big for the filesystem!\0A\00"
@.str.4 = [53 x i8] c"Cannot add all contents of file '%s' to filesystem!\0A\00"
define i32 @add_file(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%10 = load i8*, i8** %4
%11 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = load i8*, i8** %4
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %6
%14 = load i32*, i32** %6
%15 = icmp ne i32* %14, null
br i1 %15, label %20, label %16
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %4
%19 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %18)
store i32 0, i32* %3
br label %60
20: ; preds = %2
%21 = load i32*, i32** %6
%22 = load i32, i32* @SEEK_END
%23 = call i32 @fseek(i32* %21, i32 0, i32 %22)
%24 = load i32*, i32** %6
%25 = call i32 @ftell(i32* %24)
%26 = load i32*, i32** %5
store i32 %25, i32* %26
%27 = load i32*, i32** %6
%28 = load i32, i32* @SEEK_SET
%29 = call i32 @fseek(i32* %27, i32 0, i32 %28)
%30 = load i32*, i32** %5
%31 = load i32, i32* %30
%32 = icmp sgt i32 %31, 268435455
br i1 %32, label %33, label %37
33: ; preds = %20
%34 = load i32, i32* @stderr
%35 = load i8*, i8** %4
%36 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %35)
store i32 0, i32* %3
br label %60
37: ; preds = %20
%38 = load i32*, i32** %5
%39 = load i32, i32* %38
%40 = call i32 @new_blob(i32 %39)
store i32 %40, i32* %7
%41 = load i32, i32* %7
%42 = call i32* @sector_to_memory(i32 %41)
store i32* %42, i32** %8
%43 = load i32*, i32** %8
%44 = load i32*, i32** %5
%45 = load i32, i32* %44
%46 = load i32*, i32** %6
%47 = call i32 @fread(i32* %43, i32 1, i32 %45, i32* %46)
store i32 %47, i32* %9
%48 = load i32, i32* %9
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %56
50: ; preds = %37
%51 = load i32, i32* @stderr
%52 = load i8*, i8** %4
%53 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0), i8* %52)
%54 = load i32*, i32** %6
%55 = call i32 @fclose(i32* %54)
store i32 0, i32* %3
br label %60
56: ; preds = %37
%57 = load i32*, i32** %6
%58 = call i32 @fclose(i32* %57)
%59 = load i32, i32* %7
store i32 %59, i32* %3
br label %60
60: ; preds = %56, %50, %33, %16
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
@.str = [34 x i8] c"Adding '%s' to filesystem image.\0A\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [33 x i8] c"Cannot open file '%s' for read!\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [39 x i8] c"File '%s' too big for the filesystem!\0A\00"
@.str.4 = [53 x i8] c"Cannot add all contents of file '%s' to filesystem!\0A\00"
define i32 @add_file(i8* %0, i32* nocapture %1) {
%3 = tail call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %0)
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %32
9: ; preds = %2
%10 = load i32, i32* @SEEK_END
%11 = tail call i32 @fseek(i32* %4, i32 0, i32 %10)
%12 = tail call i32 @ftell(i32* %4)
store i32 %12, i32* %1
%13 = load i32, i32* @SEEK_SET
%14 = tail call i32 @fseek(i32* %4, i32 0, i32 %13)
%15 = load i32, i32* %1
%16 = icmp sgt i32 %15, 268435455
br i1 %16, label %17, label %20
17: ; preds = %9
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %32
20: ; preds = %9
%21 = tail call i32 @new_blob(i32 %15)
%22 = tail call i32* @sector_to_memory(i32 %21)
%23 = load i32, i32* %1
%24 = tail call i32 @fread(i32* %22, i32 1, i32 %23, i32* %4)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %20
%27 = load i32, i32* @stderr
%28 = tail call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0), i8* %0)
%29 = tail call i32 @fclose(i32* %4)
br label %32
30: ; preds = %20
%31 = tail call i32 @fclose(i32* %4)
br label %32
32: ; preds = %26, %30, %17, %6
%33 = phi i32 [ 0, %17 ], [ 0, %6 ], [ 0, %26 ], [ %21, %30 ]
ret i32 %33
}