#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 scalar_t__ uint32_t;
struct fx3_firmware {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int execute_fw_from_bootloader(void *, int /*<<< orphan*/);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ fx3_fw_entry_point(struct fx3_firmware *);
int fx3_fw_next_section(struct fx3_firmware *, scalar_t__ *,
                        int /*<<< orphan*/ **, scalar_t__ *);
int /*<<< orphan*/ *malloc(int);
scalar_t__ uint_min(scalar_t__, int);
int write_and_verify_fw_chunk(void *, scalar_t__, int /*<<< orphan*/ *,
                              scalar_t__, int /*<<< orphan*/ *);
int write_fw_to_bootloader(void *driver, struct fx3_firmware *fw) {
  int status = 0;
  uint32_t to_write;
  uint32_t data_len;
  uint32_t addr;
  uint8_t *data;
  bool got_section;
  uint8_t *readback = malloc(4096);
  if (readback == NULL) {
    return (-4);
  }
  do {
    got_section = fx3_fw_next_section(fw, &addr, &data, &data_len);
    if (got_section) {
      assert(data_len != 0);
      do {
        to_write = uint_min(data_len, 4096);
        status =
            write_and_verify_fw_chunk(driver, addr, data, to_write, readback);
        data_len -= to_write;
        addr += to_write;
        data += to_write;
      } while (data_len != 0 && status == 0);
    }
  } while (got_section && status == 0);
  if (status == 0) {
    status = execute_fw_from_bootloader(driver, fx3_fw_entry_point(fw));
  }
  free(readback);
  return status;
}


x86 assembly (clang, -O0):
<write_fw_to_bootloader>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    $0x1000,%edi
call   1080 <malloc@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    11bc <write_fw_to_bootloader+0x3c>
movl   $0xfffffffc,-0x4(%rbp)
jmp    12d9 <write_fw_to_bootloader+0x159>
jmp    11c1 <write_fw_to_bootloader+0x41>
mov    -0x18(%rbp),%rdi
lea    -0x38(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1050 <fx3_fw_next_section@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     1278 <write_fw_to_bootloader+0xf8>
cmpq   $0x0,-0x30(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x30(%rbp),%rdi
mov    $0x1000,%esi
call   1040 <uint_min@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x50(%rbp),%r8
call   1090 <write_and_verify_fw_chunk@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
shl    $0x2,%rax
add    -0x40(%rbp),%rax
mov    %rax,-0x40(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x30(%rbp)
mov    %al,-0x51(%rbp)
je     1268 <write_fw_to_bootloader+0xe8>
cmpl   $0x0,-0x1c(%rbp)
sete   %al
mov    %al,-0x51(%rbp)
mov    -0x51(%rbp),%al
test   $0x1,%al
jne    11f5 <write_fw_to_bootloader+0x75>
jmp    1278 <write_fw_to_bootloader+0xf8>
jmp    127d <write_fw_to_bootloader+0xfd>
xor    %eax,%eax
cmpl   $0x0,-0x44(%rbp)
mov    %al,-0x52(%rbp)
je     1296 <write_fw_to_bootloader+0x116>
cmpl   $0x0,-0x1c(%rbp)
sete   %al
mov    %al,-0x52(%rbp)
mov    -0x52(%rbp),%al
test   $0x1,%al
jne    11c1 <write_fw_to_bootloader+0x41>
cmpl   $0x0,-0x1c(%rbp)
jne    12ca <write_fw_to_bootloader+0x14a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x18(%rbp),%rdi
call   10a0 <fx3_fw_entry_point@plt>
mov    -0x60(%rbp),%rdi
mov    %eax,%esi
call   1060 <execute_fw_from_bootloader@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_fw_to_bootloader>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %rdi,%rbx
mov    $0x1000,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1245 <write_fw_to_bootloader+0xc5>
mov    %rax,%r14
mov    %rsp,%rbp
jmp    11b9 <write_fw_to_bootloader+0x39>
nop
test   %r12d,%r12d
jne    1262 <write_fw_to_bootloader+0xe2>
mov    %r15,%rdi
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    %rbp,%rcx
call   1050 <fx3_fw_next_section@plt>
test   %eax,%eax
je     124d <write_fw_to_bootloader+0xcd>
xor    %edi,%edi
cmpq   $0x0,(%rsp)
setne  %dil
call   1070 <assert@plt>
mov    (%rsp),%rdi
cs nopw 0x0(%rax,%rax,1)
mov    $0x1000,%esi
call   1040 <uint_min@plt>
mov    %rax,%r13
mov    0x10(%rsp),%rsi
mov    0x8(%rsp),%rdx
mov    %rbx,%rdi
mov    %rax,%rcx
mov    %r14,%r8
call   1090 <write_and_verify_fw_chunk@plt>
add    %r13,0x10(%rsp)
lea    0x0(,%r13,4),%rcx
add    %rcx,0x8(%rsp)
mov    %eax,%r12d
mov    (%rsp),%rdi
sub    %r13,%rdi
mov    %rdi,(%rsp)
je     11b0 <write_fw_to_bootloader+0x30>
test   %r12d,%r12d
je     11f0 <write_fw_to_bootloader+0x70>
jmp    11b0 <write_fw_to_bootloader+0x30>
mov    $0xfffffffc,%r12d
jmp    126a <write_fw_to_bootloader+0xea>
mov    %r15,%rdi
call   10a0 <fx3_fw_entry_point@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1060 <execute_fw_from_bootloader@plt>
mov    %eax,%r12d
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.fx3_firmware = type { i32 }
define i32 @write_fw_to_bootloader(i8* %0, %struct.fx3_firmware* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.fx3_firmware*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32*
store i8* %0, i8** %4
store %struct.fx3_firmware* %1, %struct.fx3_firmware** %5
store i32 0, i32* %6
%13 = call i32* @malloc(i32 4096)
store i32* %13, i32** %12
%14 = load i32*, i32** %12
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 -4, i32* %3
br label %76
17: ; preds = %2
br label %18
18: ; preds = %62, %17
%19 = load %struct.fx3_firmware*, %struct.fx3_firmware** %5
%20 = call i32 @fx3_fw_next_section(%struct.fx3_firmware* %19, i64* %9, i32** %10, i64* %8)
store i32 %20, i32* %11
%21 = load i32, i32* %11
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %55
23: ; preds = %18
%24 = load i64, i64* %8
%25 = icmp ne i64 %24, 0
%26 = zext i1 %25 to i32
%27 = call i32 @assert(i32 %26)
br label %28
28: ; preds = %52, %23
%29 = load i64, i64* %8
%30 = call i64 @uint_min(i64 %29, i32 4096)
store i64 %30, i64* %7
%31 = load i8*, i8** %4
%32 = load i64, i64* %9
%33 = load i32*, i32** %10
%34 = load i64, i64* %7
%35 = load i32*, i32** %12
%36 = call i32 @write_and_verify_fw_chunk(i8* %31, i64 %32, i32* %33, i64 %34, i32* %35)
store i32 %36, i32* %6
%37 = load i64, i64* %7
%38 = load i64, i64* %8
%39 = sub nsw i64 %38, %37
store i64 %39, i64* %8
%40 = load i64, i64* %7
%41 = load i64, i64* %9
%42 = add nsw i64 %41, %40
store i64 %42, i64* %9
%43 = load i64, i64* %7
%44 = load i32*, i32** %10
%45 = getelementptr inbounds i32, i32* %44, i64 %43
store i32* %45, i32** %10
br label %46
46: ; preds = %28
%47 = load i64, i64* %8
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %46
%50 = load i32, i32* %6
%51 = icmp eq i32 %50, 0
br label %52
52: ; preds = %49, %46
%53 = phi i1 [ false, %46 ], [ %51, %49 ]
br i1 %53, label %28, label %54
54: ; preds = %52
br label %55
55: ; preds = %54, %18
br label %56
56: ; preds = %55
%57 = load i32, i32* %11
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %56
%60 = load i32, i32* %6
%61 = icmp eq i32 %60, 0
br label %62
62: ; preds = %59, %56
%63 = phi i1 [ false, %56 ], [ %61, %59 ]
br i1 %63, label %18, label %64
64: ; preds = %62
%65 = load i32, i32* %6
%66 = icmp eq i32 %65, 0
br i1 %66, label %67, label %72
67: ; preds = %64
%68 = load i8*, i8** %4
%69 = load %struct.fx3_firmware*, %struct.fx3_firmware** %5
%70 = call i32 @fx3_fw_entry_point(%struct.fx3_firmware* %69)
%71 = call i32 @execute_fw_from_bootloader(i8* %68, i32 %70)
store i32 %71, i32* %6
br label %72
72: ; preds = %67, %64
%73 = load i32*, i32** %12
%74 = call i32 @free(i32* %73)
%75 = load i32, i32* %6
store i32 %75, i32* %3
br label %76
76: ; preds = %72, %16
%77 = load i32, i32* %3
ret i32 %77
}

LLVM IR (-O2):
%struct.fx3_firmware = type { i32 }
define i32 @write_fw_to_bootloader(i8* %0, %struct.fx3_firmware* %1) {
%3 = alloca i64
%4 = alloca i64
%5 = alloca i32*
%6 = bitcast i64* %3 to i8*
%7 = bitcast i64* %4 to i8*
%8 = bitcast i32** %5 to i8*
%9 = tail call i32* @malloc(i32 4096)
%10 = icmp eq i32* %9, null
br i1 %10, label %42, label %11
11: ; preds = %2, %35
%12 = call i32 @fx3_fw_next_section(%struct.fx3_firmware* %1, i64* %4, i32** %5, i64* %3)
%13 = icmp eq i32 %12, 0
br i1 %13, label %36, label %14
14: ; preds = %11
%15 = load i64, i64* %3
%16 = icmp ne i64 %15, 0
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = load i64, i64* %3
br label %20
20: ; preds = %20, %14
%21 = phi i64 [ %27, %20 ], [ %19, %14 ]
%22 = call i64 @uint_min(i64 %21, i32 4096)
%23 = load i64, i64* %4
%24 = load i32*, i32** %5
%25 = call i32 @write_and_verify_fw_chunk(i8* %0, i64 %23, i32* %24, i64 %22, i32* %9)
%26 = load i64, i64* %3
%27 = sub nsw i64 %26, %22
store i64 %27, i64* %3
%28 = load i64, i64* %4
%29 = add nsw i64 %28, %22
store i64 %29, i64* %4
%30 = load i32*, i32** %5
%31 = getelementptr inbounds i32, i32* %30, i64 %22
store i32* %31, i32** %5
%32 = icmp ne i64 %27, 0
%33 = icmp eq i32 %25, 0
%34 = select i1 %32, i1 %33, i1 false
br i1 %34, label %20, label %35
35: ; preds = %20
br i1 %33, label %11, label %39
36: ; preds = %11
%37 = call i32 @fx3_fw_entry_point(%struct.fx3_firmware* %1)
%38 = call i32 @execute_fw_from_bootloader(i8* %0, i32 %37)
br label %39
39: ; preds = %35, %36
%40 = phi i32 [ %38, %36 ], [ %25, %35 ]
%41 = call i32 @free(i32* %9)
br label %42
42: ; preds = %2, %39
%43 = phi i32 [ %40, %39 ], [ -4, %2 ]
ret i32 %43
}