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

/* Variables and functions */
size_t const UINT32_MAX;
int flash_erase(struct bladerf *, int, int);
int flash_write(struct bladerf *, int /*<<< orphan*/ *, int, size_t);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int, size_t);
scalar_t__ realloc(int /*<<< orphan*/ *, size_t);
int verify_flash(struct bladerf *, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                 int, size_t);
int flash_write_fx3_fw(struct bladerf *dev, uint8_t **image, size_t len) {
  int status;
  uint8_t *readback_buf;
  uint8_t *padded_image;
  uint32_t padded_image_len;
  const uint32_t page_size = 256;
  const uint32_t padding_len =
      (len % page_size == 0) ? 0 : page_size - (len % page_size);
  if (len >= (UINT32_MAX - padding_len)) {
    return (-3);
  }
  padded_image_len = (uint32_t)len + padding_len;
  readback_buf = (uint8_t *)malloc(padded_image_len);
  if (readback_buf == NULL) {
    return (-4);
  }
  padded_image = (uint8_t *)realloc(*image, padded_image_len);
  if (padded_image == NULL) {
    status = (-4);
    goto error;
  }
  *image = padded_image;
  memset(padded_image + len, 0xFF, padded_image_len - len);
  status = flash_erase(dev, (((0x00000000) / (64 * 1024))),
                       (((0x00030000) / (64 * 1024))));
  if (status != 0) {
    do {
      do {
      } while (0);
    } while (0);
    goto error;
  }
  padded_image_len /= 256;
  status =
      flash_write(dev, padded_image, (((0x00000000) / 256)), padded_image_len);
  if (status < 0) {
    do {
      do {
      } while (0);
    } while (0);
    goto error;
  }
  status = verify_flash(dev, readback_buf, padded_image, (((0x00000000) / 256)),
                        padded_image_len);
  if (status != 0) {
    do {
      do {
      } while (0);
    } while (0);
  }
error:
  free(readback_buf);
  return status;
}


x86 assembly (clang, -O0):
<flash_write_fx3_fw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x100,-0x48(%rbp)
mov    -0x20(%rbp),%rax
and    $0xff,%rax
cmp    $0x0,%rax
jne    11ab <flash_write_fx3_fw+0x3b>
xor    %eax,%eax
mov    %rax,-0x58(%rbp)
jmp    11c2 <flash_write_fx3_fw+0x52>
mov    -0x20(%rbp),%rcx
and    $0xff,%rcx
mov    $0x100,%eax
sub    %rcx,%rax
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2e23(%rip),%rcx
mov    (%rcx),%rcx
sub    -0x50(%rbp),%rcx
cmp    %rcx,%rax
jb     11f1 <flash_write_fx3_fw+0x81>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1344 <flash_write_fx3_fw+0x1d4>
mov    -0x20(%rbp),%rax
add    -0x50(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1221 <flash_write_fx3_fw+0xb1>
movl   $0xfffffffc,-0x4(%rbp)
jmp    1344 <flash_write_fx3_fw+0x1d4>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x40(%rbp),%rsi
call   1070 <realloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    124c <flash_write_fx3_fw+0xdc>
movl   $0xfffffffc,-0x24(%rbp)
jmp    1335 <flash_write_fx3_fw+0x1c5>
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x40(%rbp),%rdx
sub    -0x20(%rbp),%rdx
mov    $0xff,%esi
call   1040 <memset@plt>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x3,%edx
call   1080 <flash_erase@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     12ae <flash_write_fx3_fw+0x13e>
jmp    129a <flash_write_fx3_fw+0x12a>
jmp    129f <flash_write_fx3_fw+0x12f>
jmp    12a4 <flash_write_fx3_fw+0x134>
jmp    12a9 <flash_write_fx3_fw+0x139>
jmp    1335 <flash_write_fx3_fw+0x1c5>
mov    -0x40(%rbp),%rax
shr    $0x8,%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%rcx
xor    %edx,%edx
call   1090 <flash_write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    12f3 <flash_write_fx3_fw+0x183>
jmp    12df <flash_write_fx3_fw+0x16f>
jmp    12e4 <flash_write_fx3_fw+0x174>
jmp    12e9 <flash_write_fx3_fw+0x179>
jmp    12ee <flash_write_fx3_fw+0x17e>
jmp    1335 <flash_write_fx3_fw+0x1c5>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x40(%rbp),%r8
xor    %ecx,%ecx
call   1050 <verify_flash@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1330 <flash_write_fx3_fw+0x1c0>
jmp    131c <flash_write_fx3_fw+0x1ac>
jmp    1321 <flash_write_fx3_fw+0x1b1>
jmp    1326 <flash_write_fx3_fw+0x1b6>
jmp    132b <flash_write_fx3_fw+0x1bb>
jmp    1330 <flash_write_fx3_fw+0x1c0>
jmp    1335 <flash_write_fx3_fw+0x1c5>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flash_write_fx3_fw>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbx
movzbl %bl,%eax
mov    $0x100,%ebp
sub    %rax,%rbp
test   %rax,%rax
cmove  %rax,%rbp
mov    %rbp,%rax
neg    %rax
mov    $0xfffffffd,%r14d
cmp    %rdx,%rax
jbe    125c <flash_write_fx3_fw+0xec>
mov    %rsi,%r15
mov    %rdi,%r12
lea    (%rbx,%rbp,1),%r13
mov    %r13,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     1244 <flash_write_fx3_fw+0xd4>
mov    %rax,%r14
mov    %r12,0x10(%rsp)
mov    (%r15),%rdi
mov    %r13,%rsi
call   1070 <realloc@plt>
test   %rax,%rax
mov    %r14,0x8(%rsp)
je     124c <flash_write_fx3_fw+0xdc>
mov    %rax,%r12
mov    %rax,(%r15)
lea    (%rax,%rbx,4),%rdi
mov    $0xff,%esi
mov    %rbp,%rdx
call   1040 <memset@plt>
mov    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x3,%edx
call   1080 <flash_erase@plt>
mov    %eax,%r14d
test   %eax,%eax
jne    1252 <flash_write_fx3_fw+0xe2>
shr    $0x8,%r13
mov    %rbx,%rdi
mov    %r12,%rsi
xor    %edx,%edx
mov    %r13,%rcx
call   1090 <flash_write@plt>
test   %eax,%eax
js     123f <flash_write_fx3_fw+0xcf>
mov    %rbx,%rdi
mov    0x8(%rsp),%rsi
mov    %r12,%rdx
xor    %ecx,%ecx
mov    %r13,%r8
call   1050 <verify_flash@plt>
mov    %eax,%r14d
jmp    1252 <flash_write_fx3_fw+0xe2>
mov    $0xfffffffc,%r14d
jmp    125c <flash_write_fx3_fw+0xec>
mov    $0xfffffffc,%r14d
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.bladerf = type { i32 }
@UINT32_MAX = constant i64 0
define i32 @flash_write_fx3_fw(%struct.bladerf* %0, i32** %1, i64 %2) {
%4 = alloca i32
%5 = alloca %struct.bladerf*
%6 = alloca i32**
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
store %struct.bladerf* %0, %struct.bladerf** %5
store i32** %1, i32*** %6
store i64 %2, i64* %7
store i64 256, i64* %12
%14 = load i64, i64* %7
%15 = urem i64 %14, 256
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %3
br label %22
18: ; preds = %3
%19 = load i64, i64* %7
%20 = urem i64 %19, 256
%21 = sub i64 256, %20
br label %22
22: ; preds = %18, %17
%23 = phi i64 [ 0, %17 ], [ %21, %18 ]
store i64 %23, i64* %13
%24 = load i64, i64* %7
%25 = load i64, i64* @UINT32_MAX
%26 = load i64, i64* %13
%27 = sub i64 %25, %26
%28 = icmp uge i64 %24, %27
br i1 %28, label %29, label %30
29: ; preds = %22
store i32 -3, i32* %4
br label %100
30: ; preds = %22
%31 = load i64, i64* %7
%32 = load i64, i64* %13
%33 = add i64 %31, %32
store i64 %33, i64* %11
%34 = load i64, i64* %11
%35 = call i64 @malloc(i64 %34)
%36 = inttoptr i64 %35 to i32*
store i32* %36, i32** %9
%37 = load i32*, i32** %9
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %40
39: ; preds = %30
store i32 -4, i32* %4
br label %100
40: ; preds = %30
%41 = load i32**, i32*** %6
%42 = load i32*, i32** %41
%43 = load i64, i64* %11
%44 = call i64 @realloc(i32* %42, i64 %43)
%45 = inttoptr i64 %44 to i32*
store i32* %45, i32** %10
%46 = load i32*, i32** %10
%47 = icmp eq i32* %46, null
br i1 %47, label %48, label %49
48: ; preds = %40
store i32 -4, i32* %8
br label %96
49: ; preds = %40
%50 = load i32*, i32** %10
%51 = load i32**, i32*** %6
store i32* %50, i32** %51
%52 = load i32*, i32** %10
%53 = load i64, i64* %7
%54 = getelementptr inbounds i32, i32* %52, i64 %53
%55 = load i64, i64* %11
%56 = load i64, i64* %7
%57 = sub i64 %55, %56
%58 = call i32 @memset(i32* %54, i32 255, i64 %57)
%59 = load %struct.bladerf*, %struct.bladerf** %5
%60 = call i32 @flash_erase(%struct.bladerf* %59, i32 0, i32 3)
store i32 %60, i32* %8
%61 = load i32, i32* %8
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %68
63: ; preds = %49
br label %64
64: ; preds = %63
br label %65
65: ; preds = %64
br label %66
66: ; preds = %65
br label %67
67: ; preds = %66
br label %96
68: ; preds = %49
%69 = load i64, i64* %11
%70 = udiv i64 %69, 256
store i64 %70, i64* %11
%71 = load %struct.bladerf*, %struct.bladerf** %5
%72 = load i32*, i32** %10
%73 = load i64, i64* %11
%74 = call i32 @flash_write(%struct.bladerf* %71, i32* %72, i32 0, i64 %73)
store i32 %74, i32* %8
%75 = load i32, i32* %8
%76 = icmp slt i32 %75, 0
br i1 %76, label %77, label %82
77: ; preds = %68
br label %78
78: ; preds = %77
br label %79
79: ; preds = %78
br label %80
80: ; preds = %79
br label %81
81: ; preds = %80
br label %96
82: ; preds = %68
%83 = load %struct.bladerf*, %struct.bladerf** %5
%84 = load i32*, i32** %9
%85 = load i32*, i32** %10
%86 = load i64, i64* %11
%87 = call i32 @verify_flash(%struct.bladerf* %83, i32* %84, i32* %85, i32 0, i64 %86)
store i32 %87, i32* %8
%88 = load i32, i32* %8
%89 = icmp ne i32 %88, 0
br i1 %89, label %90, label %95
90: ; preds = %82
br label %91
91: ; preds = %90
br label %92
92: ; preds = %91
br label %93
93: ; preds = %92
br label %94
94: ; preds = %93
br label %95
95: ; preds = %94, %82
br label %96
96: ; preds = %95, %81, %67, %48
%97 = load i32*, i32** %9
%98 = call i32 @free(i32* %97)
%99 = load i32, i32* %8
store i32 %99, i32* %4
br label %100
100: ; preds = %96, %39, %29
%101 = load i32, i32* %4
ret i32 %101
}

LLVM IR (-O2):
%struct.bladerf = type { i32 }
@UINT32_MAX = constant i64 0
define i32 @flash_write_fx3_fw(%struct.bladerf* %0, i32** nocapture %1, i64 %2) {
%4 = and i64 %2, 255
%5 = icmp eq i64 %4, 0
%6 = sub nuw nsw i64 256, %4
%7 = select i1 %5, i64 0, i64 %6
%8 = sub nsw i64 0, %7
%9 = icmp ugt i64 %8, %2
br i1 %9, label %10, label %34
10: ; preds = %3
%11 = add i64 %7, %2
%12 = tail call i64 @malloc(i64 %11)
%13 = inttoptr i64 %12 to i32*
%14 = icmp eq i64 %12, 0
br i1 %14, label %34, label %15
15: ; preds = %10
%16 = load i32*, i32** %1
%17 = tail call i64 @realloc(i32* %16, i64 %11)
%18 = inttoptr i64 %17 to i32*
%19 = icmp eq i64 %17, 0
br i1 %19, label %31, label %20
20: ; preds = %15
store i32* %18, i32** %1
%21 = getelementptr inbounds i32, i32* %18, i64 %2
%22 = tail call i32 @memset(i32* %21, i32 255, i64 %7)
%23 = tail call i32 @flash_erase(%struct.bladerf* %0, i32 0, i32 3)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %20
%26 = lshr i64 %11, 8
%27 = tail call i32 @flash_write(%struct.bladerf* %0, i32* %18, i32 0, i64 %26)
%28 = icmp slt i32 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %25
%30 = tail call i32 @verify_flash(%struct.bladerf* %0, i32* %13, i32* %18, i32 0, i64 %26)
br label %31
31: ; preds = %15, %25, %20, %29
%32 = phi i32 [ %23, %20 ], [ %27, %25 ], [ %30, %29 ], [ -4, %15 ]
%33 = tail call i32 @free(i32* %13)
br label %34
34: ; preds = %10, %3, %31
%35 = phi i32 [ %32, %31 ], [ -3, %3 ], [ -4, %10 ]
ret i32 %35
}