#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 /*<<< orphan*/ fill_fpga_metadata_page(int /*<<< orphan*/ *, size_t);
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_fpga_bitstream(struct bladerf *dev, uint8_t **bitstream,
                               size_t len) {
  int status;
  uint8_t *readback_buf;
  uint8_t *padded_bitstream;
  uint8_t metadata[256];
  uint32_t padded_bitstream_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_bitstream_len = (uint32_t)len + padding_len;
  fill_fpga_metadata_page(metadata, len);
  readback_buf = (uint8_t *)malloc(padded_bitstream_len);
  if (readback_buf == NULL) {
    return (-4);
  }
  padded_bitstream = (uint8_t *)realloc(*bitstream, padded_bitstream_len);
  if (padded_bitstream == NULL) {
    status = (-4);
    goto error;
  }
  *bitstream = padded_bitstream;
  memset(padded_bitstream + len, 0xFF, padded_bitstream_len - len);
  status = flash_erase(dev, (((0x00040000) / (64 * 1024))),
                       (((0x00370000) / (64 * 1024))));
  if (status != 0) {
    do {
      do {
      } while (0);
    } while (0);
    goto error;
  }
  status = flash_write(dev, metadata, (((0x00040000) / 256)), 1);
  if (status != 0) {
    do {
      do {
      } while (0);
    } while (0);
    goto error;
  }
  padded_bitstream_len /= 256;
  status = flash_write(dev, padded_bitstream, (((0x00040000) / 256)) + 1,
                       padded_bitstream_len);
  if (status != 0) {
    do {
      do {
      } while (0);
    } while (0);
    goto error;
  }
  status = verify_flash(dev, readback_buf, metadata, (((0x00040000) / 256)), 1);
  if (status != 0) {
    do {
      do {
      } while (0);
    } while (0);
    goto error;
  }
  status = verify_flash(dev, readback_buf, padded_bitstream,
                        (((0x00040000) / 256)) + 1, padded_bitstream_len);
  if (status != 0) {
    do {
      do {
      } while (0);
    } while (0);
    goto error;
  }
error:
  free(readback_buf);
  return status;
}


x86 assembly (clang, -O0):
<flash_write_fpga_bitstream>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x100,-0x450(%rbp)
mov    -0x20(%rbp),%rax
and    $0xff,%rax
cmp    $0x0,%rax
jne    11c4 <flash_write_fpga_bitstream+0x44>
xor    %eax,%eax
mov    %rax,-0x460(%rbp)
jmp    11de <flash_write_fpga_bitstream+0x5e>
mov    -0x20(%rbp),%rcx
and    $0xff,%rcx
mov    $0x100,%eax
sub    %rcx,%rax
mov    %rax,-0x460(%rbp)
mov    -0x460(%rbp),%rax
mov    %rax,-0x458(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2e01(%rip),%rcx
mov    (%rcx),%rcx
sub    -0x458(%rbp),%rcx
cmp    %rcx,%rax
jb     1216 <flash_write_fpga_bitstream+0x96>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1422 <flash_write_fpga_bitstream+0x2a2>
mov    -0x20(%rbp),%rax
add    -0x458(%rbp),%rax
mov    %rax,-0x448(%rbp)
lea    -0x440(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <fill_fpga_metadata_page@plt>
mov    -0x448(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    125f <flash_write_fpga_bitstream+0xdf>
movl   $0xfffffffc,-0x4(%rbp)
jmp    1422 <flash_write_fpga_bitstream+0x2a2>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x448(%rbp),%rsi
call   1080 <realloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    128d <flash_write_fpga_bitstream+0x10d>
movl   $0xfffffffc,-0x24(%rbp)
jmp    1413 <flash_write_fpga_bitstream+0x293>
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    -0x448(%rbp),%rdx
sub    -0x20(%rbp),%rdx
mov    $0xff,%esi
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    $0x4,%esi
mov    $0x37,%edx
call   1090 <flash_erase@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     12f5 <flash_write_fpga_bitstream+0x175>
jmp    12e1 <flash_write_fpga_bitstream+0x161>
jmp    12e6 <flash_write_fpga_bitstream+0x166>
jmp    12eb <flash_write_fpga_bitstream+0x16b>
jmp    12f0 <flash_write_fpga_bitstream+0x170>
jmp    1413 <flash_write_fpga_bitstream+0x293>
mov    -0x10(%rbp),%rdi
lea    -0x440(%rbp),%rsi
mov    $0x400,%edx
mov    $0x1,%ecx
call   10a0 <flash_write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1335 <flash_write_fpga_bitstream+0x1b5>
jmp    1321 <flash_write_fpga_bitstream+0x1a1>
jmp    1326 <flash_write_fpga_bitstream+0x1a6>
jmp    132b <flash_write_fpga_bitstream+0x1ab>
jmp    1330 <flash_write_fpga_bitstream+0x1b0>
jmp    1413 <flash_write_fpga_bitstream+0x293>
mov    -0x448(%rbp),%rax
shr    $0x8,%rax
mov    %rax,-0x448(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x448(%rbp),%rcx
mov    $0x401,%edx
call   10a0 <flash_write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1386 <flash_write_fpga_bitstream+0x206>
jmp    1372 <flash_write_fpga_bitstream+0x1f2>
jmp    1377 <flash_write_fpga_bitstream+0x1f7>
jmp    137c <flash_write_fpga_bitstream+0x1fc>
jmp    1381 <flash_write_fpga_bitstream+0x201>
jmp    1413 <flash_write_fpga_bitstream+0x293>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
lea    -0x440(%rbp),%rdx
mov    $0x400,%ecx
mov    $0x1,%r8d
call   1060 <verify_flash@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     13cb <flash_write_fpga_bitstream+0x24b>
jmp    13b7 <flash_write_fpga_bitstream+0x237>
jmp    13bc <flash_write_fpga_bitstream+0x23c>
jmp    13c1 <flash_write_fpga_bitstream+0x241>
jmp    13c6 <flash_write_fpga_bitstream+0x246>
jmp    1413 <flash_write_fpga_bitstream+0x293>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x448(%rbp),%r8
mov    $0x401,%ecx
call   1060 <verify_flash@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     140e <flash_write_fpga_bitstream+0x28e>
jmp    13fa <flash_write_fpga_bitstream+0x27a>
jmp    13ff <flash_write_fpga_bitstream+0x27f>
jmp    1404 <flash_write_fpga_bitstream+0x284>
jmp    1409 <flash_write_fpga_bitstream+0x289>
jmp    1413 <flash_write_fpga_bitstream+0x293>
jmp    1413 <flash_write_fpga_bitstream+0x293>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x460,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flash_write_fpga_bitstream>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%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    12bc <flash_write_fpga_bitstream+0x13c>
mov    %rsi,%r12
mov    %rdi,%r14
lea    (%rbx,%rbp,1),%r13
mov    %rsp,%rdi
mov    %rbx,%rsi
call   1040 <fill_fpga_metadata_page@plt>
mov    %r13,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     12a6 <flash_write_fpga_bitstream+0x126>
mov    %rax,%r15
mov    (%r12),%rdi
mov    %r13,%rsi
call   1080 <realloc@plt>
test   %rax,%rax
je     12ae <flash_write_fpga_bitstream+0x12e>
mov    %rax,(%r12)
mov    %rax,%r12
lea    (%rax,%rbx,4),%rdi
mov    $0xff,%esi
mov    %rbp,%rdx
call   1050 <memset@plt>
mov    %r14,%rbx
mov    %r14,%rdi
mov    $0x4,%esi
mov    $0x37,%edx
call   1090 <flash_erase@plt>
mov    %eax,%r14d
test   %eax,%eax
jne    12b4 <flash_write_fpga_bitstream+0x134>
mov    %rsp,%rsi
mov    $0x1,%ecx
mov    %rbx,%rdi
mov    $0x400,%edx
call   10a0 <flash_write@plt>
mov    %eax,%r14d
test   %eax,%eax
jne    12b4 <flash_write_fpga_bitstream+0x134>
shr    $0x8,%r13
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x401,%edx
mov    %r13,%rcx
call   10a0 <flash_write@plt>
mov    %eax,%r14d
test   %eax,%eax
jne    12b4 <flash_write_fpga_bitstream+0x134>
mov    %rsp,%rdx
mov    $0x1,%r8d
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x400,%ecx
call   1060 <verify_flash@plt>
mov    %eax,%r14d
test   %eax,%eax
jne    12b4 <flash_write_fpga_bitstream+0x134>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    $0x401,%ecx
mov    %r13,%r8
call   1060 <verify_flash@plt>
mov    %eax,%r14d
jmp    12b4 <flash_write_fpga_bitstream+0x134>
mov    $0xfffffffc,%r14d
jmp    12bc <flash_write_fpga_bitstream+0x13c>
mov    $0xfffffffc,%r14d
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14d,%eax
add    $0x408,%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_fpga_bitstream(%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 [256 x i32]
%12 = alloca i64
%13 = alloca i64
%14 = alloca i64
store %struct.bladerf* %0, %struct.bladerf** %5
store i32** %1, i32*** %6
store i64 %2, i64* %7
store i64 256, i64* %13
%15 = load i64, i64* %7
%16 = urem i64 %15, 256
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %3
br label %23
19: ; preds = %3
%20 = load i64, i64* %7
%21 = urem i64 %20, 256
%22 = sub i64 256, %21
br label %23
23: ; preds = %19, %18
%24 = phi i64 [ 0, %18 ], [ %22, %19 ]
store i64 %24, i64* %14
%25 = load i64, i64* %7
%26 = load i64, i64* @UINT32_MAX
%27 = load i64, i64* %14
%28 = sub i64 %26, %27
%29 = icmp uge i64 %25, %28
br i1 %29, label %30, label %31
30: ; preds = %23
store i32 -3, i32* %4
br label %127
31: ; preds = %23
%32 = load i64, i64* %7
%33 = load i64, i64* %14
%34 = add i64 %32, %33
store i64 %34, i64* %12
%35 = getelementptr inbounds [256 x i32], [256 x i32]* %11, i64 0, i64 0
%36 = load i64, i64* %7
%37 = call i32 @fill_fpga_metadata_page(i32* %35, i64 %36)
%38 = load i64, i64* %12
%39 = call i64 @malloc(i64 %38)
%40 = inttoptr i64 %39 to i32*
store i32* %40, i32** %9
%41 = load i32*, i32** %9
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %44
43: ; preds = %31
store i32 -4, i32* %4
br label %127
44: ; preds = %31
%45 = load i32**, i32*** %6
%46 = load i32*, i32** %45
%47 = load i64, i64* %12
%48 = call i64 @realloc(i32* %46, i64 %47)
%49 = inttoptr i64 %48 to i32*
store i32* %49, i32** %10
%50 = load i32*, i32** %10
%51 = icmp eq i32* %50, null
br i1 %51, label %52, label %53
52: ; preds = %44
store i32 -4, i32* %8
br label %123
53: ; preds = %44
%54 = load i32*, i32** %10
%55 = load i32**, i32*** %6
store i32* %54, i32** %55
%56 = load i32*, i32** %10
%57 = load i64, i64* %7
%58 = getelementptr inbounds i32, i32* %56, i64 %57
%59 = load i64, i64* %12
%60 = load i64, i64* %7
%61 = sub i64 %59, %60
%62 = call i32 @memset(i32* %58, i32 255, i64 %61)
%63 = load %struct.bladerf*, %struct.bladerf** %5
%64 = call i32 @flash_erase(%struct.bladerf* %63, i32 4, i32 55)
store i32 %64, i32* %8
%65 = load i32, i32* %8
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %72
67: ; preds = %53
br label %68
68: ; preds = %67
br label %69
69: ; preds = %68
br label %70
70: ; preds = %69
br label %71
71: ; preds = %70
br label %123
72: ; preds = %53
%73 = load %struct.bladerf*, %struct.bladerf** %5
%74 = getelementptr inbounds [256 x i32], [256 x i32]* %11, i64 0, i64 0
%75 = call i32 @flash_write(%struct.bladerf* %73, i32* %74, i32 1024, i64 1)
store i32 %75, i32* %8
%76 = load i32, i32* %8
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %83
78: ; preds = %72
br label %79
79: ; preds = %78
br label %80
80: ; preds = %79
br label %81
81: ; preds = %80
br label %82
82: ; preds = %81
br label %123
83: ; preds = %72
%84 = load i64, i64* %12
%85 = udiv i64 %84, 256
store i64 %85, i64* %12
%86 = load %struct.bladerf*, %struct.bladerf** %5
%87 = load i32*, i32** %10
%88 = load i64, i64* %12
%89 = call i32 @flash_write(%struct.bladerf* %86, i32* %87, i32 1025, i64 %88)
store i32 %89, i32* %8
%90 = load i32, i32* %8
%91 = icmp ne i32 %90, 0
br i1 %91, label %92, label %97
92: ; preds = %83
br label %93
93: ; preds = %92
br label %94
94: ; preds = %93
br label %95
95: ; preds = %94
br label %96
96: ; preds = %95
br label %123
97: ; preds = %83
%98 = load %struct.bladerf*, %struct.bladerf** %5
%99 = load i32*, i32** %9
%100 = getelementptr inbounds [256 x i32], [256 x i32]* %11, i64 0, i64 0
%101 = call i32 @verify_flash(%struct.bladerf* %98, i32* %99, i32* %100, i32 1024, i64 1)
store i32 %101, i32* %8
%102 = load i32, i32* %8
%103 = icmp ne i32 %102, 0
br i1 %103, label %104, label %109
104: ; preds = %97
br label %105
105: ; preds = %104
br label %106
106: ; preds = %105
br label %107
107: ; preds = %106
br label %108
108: ; preds = %107
br label %123
109: ; preds = %97
%110 = load %struct.bladerf*, %struct.bladerf** %5
%111 = load i32*, i32** %9
%112 = load i32*, i32** %10
%113 = load i64, i64* %12
%114 = call i32 @verify_flash(%struct.bladerf* %110, i32* %111, i32* %112, i32 1025, i64 %113)
store i32 %114, i32* %8
%115 = load i32, i32* %8
%116 = icmp ne i32 %115, 0
br i1 %116, label %117, label %122
117: ; preds = %109
br label %118
118: ; preds = %117
br label %119
119: ; preds = %118
br label %120
120: ; preds = %119
br label %121
121: ; preds = %120
br label %123
122: ; preds = %109
br label %123
123: ; preds = %122, %121, %108, %96, %82, %71, %52
%124 = load i32*, i32** %9
%125 = call i32 @free(i32* %124)
%126 = load i32, i32* %8
store i32 %126, i32* %4
br label %127
127: ; preds = %123, %43, %30
%128 = load i32, i32* %4
ret i32 %128
}

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