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


x86 assembly (clang, -O0):
<write_sbl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
sub    $0x81000,%rax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe94(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11a7 <write_sbl+0x67>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe70(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1212 <write_sbl+0xd2>
mov    -0x10(%rbp),%rdi
add    $0x81000,%rdi
mov    -0x24(%rbp),%edx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fwrite@plt>
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
mov    %eax,-0x38(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x24(%rbp),%eax
jne    11e8 <write_sbl+0xa8>
cmpl   $0x0,-0x38(%rbp)
je     120b <write_sbl+0xcb>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1212 <write_sbl+0xd2>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_sbl>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
lea    0xea8(%rip),%rsi
mov    %rdx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11b9 <write_sbl+0x79>
mov    %rax,%rbx
add    $0xfff7f000,%r15d
add    $0x81000,%r12
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %rax,%rcx
call   1060 <fwrite@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
cmp    %r15d,%ebp
jne    11a0 <write_sbl+0x60>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
je     11d8 <write_sbl+0x98>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
lea    0xe67(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    11d3 <write_sbl+0x93>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
lea    0xe3b(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%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 = [24 x i8] c"Failed writing to /sbl\0A\00"
define i32 @write_sbl(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 = load i64, i64* %6
%13 = sub i64 %12, 528384
%14 = trunc i64 %13 to i32
store i32 %14, i32* %8
%15 = load i8*, i8** %7
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %9
%17 = load i32*, i32** %9
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %23
19: ; preds = %3
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %7
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %21)
store i32 -1, i32* %4
br label %41
23: ; preds = %3
%24 = load i8*, i8** %5
%25 = getelementptr inbounds i8, i8* %24, i64 528384
%26 = load i32, i32* %8
%27 = load i32*, i32** %9
%28 = call i32 @fwrite(i8* %25, i32 1, i32 %26, i32* %27)
store i32 %28, i32* %10
%29 = load i32*, i32** %9
%30 = call i32 @fclose(i32* %29)
store i32 %30, i32* %11
%31 = load i32, i32* %10
%32 = load i32, i32* %8
%33 = icmp ne i32 %31, %32
br i1 %33, label %37, label %34
34: ; preds = %23
%35 = load i32, i32* %11
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %34, %23
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %41
40: ; preds = %34
store i32 0, i32* %4
br label %41
41: ; preds = %40, %37, %19
%42 = load i32, i32* %4
ret i32 %42
}

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 = [24 x i8] c"Failed writing to /sbl\0A\00"
define i32 @write_sbl(i8* nocapture %0, i64 %1, i8* %2) {
%4 = trunc i64 %1 to i32
%5 = add i32 %4, -528384
%6 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %3
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %2)
br label %21
11: ; preds = %3
%12 = getelementptr inbounds i8, i8* %0, i64 528384
%13 = tail call i32 @fwrite(i8* %12, i32 1, i32 %5, i32* %6)
%14 = tail call i32 @fclose(i32* %6)
%15 = icmp ne i32 %13, %5
%16 = icmp ne i32 %14, 0
%17 = or i1 %15, %16
br i1 %17, label %18, label %21
18: ; preds = %11
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %18, %11, %8
%22 = phi i32 [ -1, %8 ], [ -1, %18 ], [ 0, %11 ]
ret i32 %22
}