#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__ const uint32_t;
typedef scalar_t__ uint16_t;
struct bladerf {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int change_setting(struct bladerf *, int);
int restore_post_flash_setting(struct bladerf *);
int write_page(struct bladerf *, scalar_t__ const, int /*<<< orphan*/ const *);
int usb_write_flash_pages(struct bladerf *dev, const uint8_t *buf,
                          uint32_t page_u32, uint32_t count_u32) {
  int status, restore_status;
  uint16_t i;
  size_t n_written;
  const uint16_t page = (uint16_t)page_u32;
  const uint16_t count = (uint16_t)count_u32;
  assert(page == page_u32);
  assert(count == count_u32);
  status = change_setting(dev, 2);
  if (status != 0) {
    return status;
  }
  do {
    do {
    } while (0);
  } while (0);
  n_written = 0;
  for (i = 0; i < count; i++) {
    do {
      do {
      } while (0);
    } while (0);
    status = write_page(dev, page + i, buf + n_written);
    if (status) {
      goto error;
    }
    n_written += 256;
  }
  do {
    do {
    } while (0);
  } while (0);
error:
  restore_status = restore_post_flash_setting(dev);
  if (status != 0) {
    return status;
  } else if (restore_status != 0) {
    return restore_status;
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<usb_write_flash_pages>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x20(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x50(%rbp),%rax
cmp    -0x28(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    $0x2,%esi
call   1030 <change_setting@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     11b8 <usb_write_flash_pages+0x78>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12a5 <usb_write_flash_pages+0x165>
jmp    11bd <usb_write_flash_pages+0x7d>
jmp    11c2 <usb_write_flash_pages+0x82>
jmp    11c7 <usb_write_flash_pages+0x87>
jmp    11cc <usb_write_flash_pages+0x8c>
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x50(%rbp),%rax
jge    124f <usb_write_flash_pages+0x10f>
jmp    11ef <usb_write_flash_pages+0xaf>
jmp    11f4 <usb_write_flash_pages+0xb4>
jmp    11f9 <usb_write_flash_pages+0xb9>
jmp    11fe <usb_write_flash_pages+0xbe>
mov    -0x10(%rbp),%rdi
mov    -0x48(%rbp),%rsi
add    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x40(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdx
call   1040 <write_page@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     1230 <usb_write_flash_pages+0xf0>
jmp    1268 <usb_write_flash_pages+0x128>
mov    -0x40(%rbp),%rax
add    $0x100,%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    11dc <usb_write_flash_pages+0x9c>
jmp    1254 <usb_write_flash_pages+0x114>
jmp    1259 <usb_write_flash_pages+0x119>
jmp    125e <usb_write_flash_pages+0x11e>
jmp    1263 <usb_write_flash_pages+0x123>
jmp    1268 <usb_write_flash_pages+0x128>
mov    -0x10(%rbp),%rdi
call   1060 <restore_post_flash_setting@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     1289 <usb_write_flash_pages+0x149>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12a5 <usb_write_flash_pages+0x165>
cmpl   $0x0,-0x30(%rbp)
je     129e <usb_write_flash_pages+0x15e>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12a5 <usb_write_flash_pages+0x165>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<usb_write_flash_pages>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%r14
mov    $0x1,%edi
call   1050 <assert@plt>
mov    $0x1,%edi
call   1050 <assert@plt>
mov    %r14,%rdi
mov    $0x2,%esi
call   1030 <change_setting@plt>
mov    %eax,%ebp
test   %eax,%eax
je     1186 <usb_write_flash_pages+0x46>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
test   %r15,%r15
jle    11b3 <usb_write_flash_pages+0x73>
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1040 <write_page@plt>
test   %eax,%eax
jne    11c3 <usb_write_flash_pages+0x83>
add    $0x400,%rbx
add    $0x1,%r12
add    $0xffffffffffffffff,%r15
jne    1190 <usb_write_flash_pages+0x50>
mov    %r14,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <restore_post_flash_setting@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1060 <restore_post_flash_setting@plt>
jmp    117b <usb_write_flash_pages+0x3b>

LLVM IR (-O0):
%struct.bladerf = type { i32 }
define i32 @usb_write_flash_pages(%struct.bladerf* %0, i32* %1, i64 %2, i64 %3) {
%5 = alloca i32
%6 = alloca %struct.bladerf*
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i64
%13 = alloca i64
%14 = alloca i64
%15 = alloca i64
store %struct.bladerf* %0, %struct.bladerf** %6
store i32* %1, i32** %7
store i64 %2, i64* %8
store i64 %3, i64* %9
%16 = load i64, i64* %8
store i64 %16, i64* %14
%17 = load i64, i64* %9
store i64 %17, i64* %15
%18 = load i64, i64* %14
%19 = load i64, i64* %8
%20 = icmp eq i64 %18, %19
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
%23 = load i64, i64* %15
%24 = load i64, i64* %9
%25 = icmp eq i64 %23, %24
%26 = zext i1 %25 to i32
%27 = call i32 @assert(i32 %26)
%28 = load %struct.bladerf*, %struct.bladerf** %6
%29 = call i32 @change_setting(%struct.bladerf* %28, i32 2)
store i32 %29, i32* %10
%30 = load i32, i32* %10
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %4
%33 = load i32, i32* %10
store i32 %33, i32* %5
br label %83
34: ; preds = %4
br label %35
35: ; preds = %34
br label %36
36: ; preds = %35
br label %37
37: ; preds = %36
br label %38
38: ; preds = %37
store i64 0, i64* %13
store i64 0, i64* %12
br label %39
39: ; preds = %62, %38
%40 = load i64, i64* %12
%41 = load i64, i64* %15
%42 = icmp slt i64 %40, %41
br i1 %42, label %43, label %65
43: ; preds = %39
br label %44
44: ; preds = %43
br label %45
45: ; preds = %44
br label %46
46: ; preds = %45
br label %47
47: ; preds = %46
%48 = load %struct.bladerf*, %struct.bladerf** %6
%49 = load i64, i64* %14
%50 = load i64, i64* %12
%51 = add nsw i64 %49, %50
%52 = load i32*, i32** %7
%53 = load i64, i64* %13
%54 = getelementptr inbounds i32, i32* %52, i64 %53
%55 = call i32 @write_page(%struct.bladerf* %48, i64 %51, i32* %54)
store i32 %55, i32* %10
%56 = load i32, i32* %10
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %47
br label %70
59: ; preds = %47
%60 = load i64, i64* %13
%61 = add i64 %60, 256
store i64 %61, i64* %13
br label %62
62: ; preds = %59
%63 = load i64, i64* %12
%64 = add nsw i64 %63, 1
store i64 %64, i64* %12
br label %39
65: ; preds = %39
br label %66
66: ; preds = %65
br label %67
67: ; preds = %66
br label %68
68: ; preds = %67
br label %69
69: ; preds = %68
br label %70
70: ; preds = %69, %58
%71 = load %struct.bladerf*, %struct.bladerf** %6
%72 = call i32 @restore_post_flash_setting(%struct.bladerf* %71)
store i32 %72, i32* %11
%73 = load i32, i32* %10
%74 = icmp ne i32 %73, 0
br i1 %74, label %75, label %77
75: ; preds = %70
%76 = load i32, i32* %10
store i32 %76, i32* %5
br label %83
77: ; preds = %70
%78 = load i32, i32* %11
%79 = icmp ne i32 %78, 0
br i1 %79, label %80, label %82
80: ; preds = %77
%81 = load i32, i32* %11
store i32 %81, i32* %5
br label %83
82: ; preds = %77
store i32 0, i32* %5
br label %83
83: ; preds = %82, %80, %75, %32
%84 = load i32, i32* %5
ret i32 %84
}

LLVM IR (-O2):
%struct.bladerf = type { i32 }
define i32 @usb_write_flash_pages(%struct.bladerf* %0, i32* %1, i64 %2, i64 %3) {
%5 = tail call i32 @assert(i32 1)
%6 = tail call i32 @assert(i32 1)
%7 = tail call i32 @change_setting(%struct.bladerf* %0, i32 2)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %26
9: ; preds = %4
%10 = icmp sgt i64 %3, 0
br i1 %10, label %11, label %24
11: ; preds = %9, %18
%12 = phi i64 [ %19, %18 ], [ 0, %9 ]
%13 = phi i64 [ %20, %18 ], [ 0, %9 ]
%14 = add nsw i64 %13, %2
%15 = getelementptr inbounds i32, i32* %1, i64 %12
%16 = tail call i32 @write_page(%struct.bladerf* %0, i64 %14, i32* %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %11
%19 = add i64 %12, 256
%20 = add nuw nsw i64 %13, 1
%21 = icmp eq i64 %20, %3
br i1 %21, label %24, label %11
22: ; preds = %11
%23 = tail call i32 @restore_post_flash_setting(%struct.bladerf* %0)
br label %26
24: ; preds = %18, %9
%25 = tail call i32 @restore_post_flash_setting(%struct.bladerf* %0)
br label %26
26: ; preds = %22, %24, %4
%27 = phi i32 [ %7, %4 ], [ %16, %22 ], [ %25, %24 ]
ret i32 %27
}