#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 */

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, size_t);
int /*<<< orphan*/ stderr;
scalar_t__ write_pit_partition_table(char const *, size_t);
scalar_t__ write_sbl(char const *, size_t, char const *);
scalar_t__ write_xloader(char const *, size_t, char const *);
int update_bootloader(const char *image_data, size_t image_size,
                      const char *xloader_loc, const char *sbl_loc) {
  if (image_size < (0x1000 + (0x20000 * 4))) {
    fprintf(stderr, "image size %d is too small\n", image_size);
    return -1;
  }
  if (write_pit_partition_table(image_data, image_size) < 0) {
    return -1;
  }
  if (write_xloader(image_data, image_size, xloader_loc) < 0) {
    return -1;
  }
  if (write_sbl(image_data, image_size, sbl_loc) < 0) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<update_bootloader>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
cmpq   $0x81000,-0x18(%rbp)
jae    118b <update_bootloader+0x4b>
mov    0x2e73(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe86(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1203 <update_bootloader+0xc3>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <write_pit_partition_table@plt>
cmp    $0x0,%rax
jge    11ae <update_bootloader+0x6e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1203 <update_bootloader+0xc3>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write_xloader@plt>
cmp    $0x0,%rax
jge    11d5 <update_bootloader+0x95>
movl   $0xffffffff,-0x4(%rbp)
jmp    1203 <update_bootloader+0xc3>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <write_sbl@plt>
cmp    $0x0,%rax
jge    11fc <update_bootloader+0xbc>
movl   $0xffffffff,-0x4(%rbp)
jmp    1203 <update_bootloader+0xc3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<update_bootloader>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%rbx
cmp    $0x80fff,%rsi
ja     1175 <update_bootloader+0x35>
mov    0x2e84(%rip),%rax
mov    (%rax),%edi
lea    0xe9b(%rip),%rsi
mov    %rbx,%rdx
call   1050 <fprintf@plt>
mov    $0xffffffff,%r12d
jmp    11b9 <update_bootloader+0x79>
mov    %rcx,%r14
mov    %rdx,%r13
mov    %rdi,%r15
mov    %rbx,%rsi
call   1060 <write_pit_partition_table@plt>
mov    $0xffffffff,%r12d
test   %rax,%rax
js     11b9 <update_bootloader+0x79>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
call   1030 <write_xloader@plt>
test   %rax,%rax
js     11b9 <update_bootloader+0x79>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <write_sbl@plt>
mov    %rax,%r12
sar    $0x3f,%r12
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [28 x i8] c"image size %d is too small\0A\00"
define i32 @update_bootloader(i8* %0, i64 %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i8* %3, i8** %9
%10 = load i64, i64* %7
%11 = icmp ult i64 %10, 528384
br i1 %11, label %12, label %16
12: ; preds = %4
%13 = load i32, i32* @stderr
%14 = load i64, i64* %7
%15 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i64 %14)
store i32 -1, i32* %5
br label %37
16: ; preds = %4
%17 = load i8*, i8** %6
%18 = load i64, i64* %7
%19 = call i64 @write_pit_partition_table(i8* %17, i64 %18)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %16
store i32 -1, i32* %5
br label %37
22: ; preds = %16
%23 = load i8*, i8** %6
%24 = load i64, i64* %7
%25 = load i8*, i8** %8
%26 = call i64 @write_xloader(i8* %23, i64 %24, i8* %25)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %22
store i32 -1, i32* %5
br label %37
29: ; preds = %22
%30 = load i8*, i8** %6
%31 = load i64, i64* %7
%32 = load i8*, i8** %9
%33 = call i64 @write_sbl(i8* %30, i64 %31, i8* %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %29
store i32 -1, i32* %5
br label %37
36: ; preds = %29
store i32 0, i32* %5
br label %37
37: ; preds = %36, %35, %28, %21, %12
%38 = load i32, i32* %5
ret i32 %38
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"image size %d is too small\0A\00"
define i32 @update_bootloader(i8* %0, i64 %1, i8* %2, i8* %3) {
%5 = icmp ult i64 %1, 528384
br i1 %5, label %6, label %9
6: ; preds = %4
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i64 %1)
br label %19
9: ; preds = %4
%10 = tail call i64 @write_pit_partition_table(i8* %0, i64 %1)
%11 = icmp slt i64 %10, 0
br i1 %11, label %19, label %12
12: ; preds = %9
%13 = tail call i64 @write_xloader(i8* %0, i64 %1, i8* %2)
%14 = icmp slt i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %12
%16 = tail call i64 @write_sbl(i8* %0, i64 %1, i8* %3)
%17 = ashr i64 %16, 63
%18 = trunc i64 %17 to i32
br label %19
19: ; preds = %15, %12, %9, %6
%20 = phi i32 [ -1, %6 ], [ -1, %9 ], [ -1, %12 ], [ %18, %15 ]
ret i32 %20
}