#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 fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fwrite(char const *, int, int, int /*<<< orphan*/ *);
int get_xloader_offset();
int /*<<< orphan*/ stderr;
int write_xloader(const char *image_data, size_t image_size,
                  const char *xloader_loc) {
  int xloader_offset = get_xloader_offset();
  if (xloader_offset < 0) {
    return -1;
  }
  xloader_offset += 0x1000;
  FILE *xloader = fopen(xloader_loc, "r+b");
  if (xloader == NULL) {
    fprintf(stderr, "Could not open %s\n", xloader_loc);
    return -1;
  }
  int written = fwrite(image_data + xloader_offset, 1, 0x20000, xloader);
  int close_status = fclose(xloader);
  if (written != 0x20000 || close_status != 0) {
    fprintf(stderr, "Failed writing to /xloader\n");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_xloader>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    $0x0,%al
call   1040 <get_xloader_offset@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1184 <write_xloader+0x34>
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <write_xloader+0xf3>
mov    -0x24(%rbp),%eax
add    $0x1000,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe66(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11d5 <write_xloader+0x85>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe42(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <write_xloader+0xf3>
mov    -0x10(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
mov    $0x20000,%edx
call   1070 <fwrite@plt>
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x20000,-0x34(%rbp)
jne    1219 <write_xloader+0xc9>
cmpl   $0x0,-0x38(%rbp)
je     123c <write_xloader+0xec>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
lea    0xdee(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <write_xloader+0xf3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_xloader>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rdi,%r15
xor    %eax,%eax
call   1040 <get_xloader_offset@plt>
mov    $0xffffffff,%r12d
test   %eax,%eax
js     11fd <write_xloader+0xad>
mov    %eax,%ebp
lea    0xe84(%rip),%rsi
mov    %r14,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11e3 <write_xloader+0x93>
mov    %rax,%rbx
mov    %ebp,%eax
lea    (%rax,%r15,1),%rdi
add    $0x1000,%rdi
mov    $0x1,%esi
mov    $0x20000,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
cmp    $0x20000,%ebp
jne    11c4 <write_xloader+0x74>
xor    %r12d,%r12d
test   %eax,%eax
je     11fd <write_xloader+0xad>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
lea    0xe43(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%r12d
jmp    11fd <write_xloader+0xad>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
lea    0xe11(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"r+b\00"
@stderr = global i32 0
@.str.1 = [19 x i8] c"Could not open %s\0A\00"
@.str.2 = [28 x i8] c"Failed writing to /xloader\0A\00"
define i32 @write_xloader(i8* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%12 = call i32 (...) @get_xloader_offset()
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 -1, i32* %4
br label %45
16: ; preds = %3
%17 = load i32, i32* %8
%18 = add nsw i32 %17, 4096
store i32 %18, i32* %8
%19 = load i8*, i8** %7
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32* %20, i32** %9
%21 = load i32*, i32** %9
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %27
23: ; preds = %16
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %7
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %25)
store i32 -1, i32* %4
br label %45
27: ; preds = %16
%28 = load i8*, i8** %5
%29 = load i32, i32* %8
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = load i32*, i32** %9
%33 = call i32 @fwrite(i8* %31, i32 1, i32 131072, i32* %32)
store i32 %33, i32* %10
%34 = load i32*, i32** %9
%35 = call i32 @fclose(i32* %34)
store i32 %35, i32* %11
%36 = load i32, i32* %10
%37 = icmp ne i32 %36, 131072
br i1 %37, label %41, label %38
38: ; preds = %27
%39 = load i32, i32* %11
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %38, %27
%42 = load i32, i32* @stderr
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %45
44: ; preds = %38
store i32 0, i32* %4
br label %45
45: ; preds = %44, %41, %23, %15
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
@.str = [4 x i8] c"r+b\00"
@stderr = global i32 0
@.str.1 = [19 x i8] c"Could not open %s\0A\00"
@.str.2 = [28 x i8] c"Failed writing to /xloader\0A\00"
define i32 @write_xloader(i8* nocapture %0, i64 %1, i8* %2) {
%4 = tail call i32 (...) @get_xloader_offset()
%5 = icmp slt i32 %4, 0
br i1 %5, label %24, label %6
6: ; preds = %3
%7 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %2)
br label %24
12: ; preds = %6
%13 = add nuw nsw i32 %4, 4096
%14 = zext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %0, i64 %14
%16 = tail call i32 @fwrite(i8* %15, i32 1, i32 131072, i32* %7)
%17 = tail call i32 @fclose(i32* %7)
%18 = icmp ne i32 %16, 131072
%19 = icmp ne i32 %17, 0
%20 = or i1 %18, %19
br i1 %20, label %21, label %24
21: ; preds = %12
%22 = load i32, i32* @stderr
%23 = tail call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %9, %12, %21, %3
%25 = phi i32 [ -1, %3 ], [ -1, %9 ], [ -1, %21 ], [ 0, %12 ]
ret i32 %25
}