#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 flash_read(int);
int /*<<< orphan*/ flash_read_mode(int);
int flash_read_status(int);
int /*<<< orphan*/ flash_write(int, int);
int /*<<< orphan*/ lock_block_to_prevent_writes(int);
int /*<<< orphan*/ unlock_block_for_writes(int);
int /*<<< orphan*/ warnx(char *, ...);
void block_write(int addr16b, int *data) {
  int j, f, status;
  unlock_block_for_writes(addr16b);
  flash_write(addr16b, 0xe8);
  if ((flash_read(addr16b) & 0x80) == 0)
    warnx("block_write to flash failed - block write not supported by device?");
  flash_write(addr16b, 0x1f);
  for (j = 0; j < 0x20; j++)
    flash_write(addr16b + j, data[j]);
  flash_write(addr16b, 0xd0);
  status = flash_read(addr16b);
  for (j = 0; ((status & 0x80) == 0) && (j < 0xfff); j++)
    status = flash_read(addr16b);
  if ((status & 0x80) == 0)
    warnx("ERROR on block-write - flash is busy even after 0x%x checks\n", j);
  status = flash_read_status(addr16b);
  if ((status & (1 << 3)) != 0)
    warnx(
        "Vpp voltage droop error during block-write, aborted, status=0x%02x\n",
        status);
  if ((status & (3 << 4)) != 0)
    warnx("Command sequence error during block-write, aborted, status=0x%02x\n",
          status);
  if ((status & (1 << 1)) != 0)
    warnx("Block locked during block-write process, aborted, status=0x%02x\n",
          status);
  if ((status & (1 << 5)) != 0)
    warnx("Block-write failed at offset 0x%08x, status=0x%02x\n", addr16b << 1,
          status);
  lock_block_to_prevent_writes(addr16b);
  flash_read_mode(addr16b);
}


x86 assembly (clang, -O0):
<block_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%edi
call   1060 <unlock_block_for_writes@plt>
mov    -0x4(%rbp),%edi
mov    $0xe8,%esi
call   1090 <flash_write@plt>
mov    -0x4(%rbp),%edi
call   1070 <flash_read@plt>
and    $0x80,%eax
cmp    $0x0,%eax
jne    11b8 <block_write+0x48>
lea    0xe4f(%rip),%rdi
mov    $0x0,%al
call   1050 <warnx@plt>
mov    -0x4(%rbp),%edi
mov    $0x1f,%esi
call   1090 <flash_write@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x20,-0x14(%rbp)
jge    11fa <block_write+0x8a>
mov    -0x4(%rbp),%edi
add    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
call   1090 <flash_write@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11cc <block_write+0x5c>
mov    -0x4(%rbp),%edi
mov    $0xd0,%esi
call   1090 <flash_write@plt>
mov    -0x4(%rbp),%edi
call   1070 <flash_read@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x1c(%rbp),%ecx
and    $0x80,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x1d(%rbp)
jne    123d <block_write+0xcd>
cmpl   $0xfff,-0x14(%rbp)
setl   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    124d <block_write+0xdd>
jmp    1266 <block_write+0xf6>
mov    -0x4(%rbp),%edi
call   1070 <flash_read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1219 <block_write+0xa9>
mov    -0x1c(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    1288 <block_write+0x118>
mov    -0x14(%rbp),%esi
lea    0xdc2(%rip),%rdi
mov    $0x0,%al
call   1050 <warnx@plt>
mov    -0x4(%rbp),%edi
call   1030 <flash_read_status@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
and    $0x8,%eax
cmp    $0x0,%eax
je     12b3 <block_write+0x143>
mov    -0x1c(%rbp),%esi
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1050 <warnx@plt>
mov    -0x1c(%rbp),%eax
and    $0x30,%eax
cmp    $0x0,%eax
je     12d3 <block_write+0x163>
mov    -0x1c(%rbp),%esi
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1050 <warnx@plt>
mov    -0x1c(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     12f3 <block_write+0x183>
mov    -0x1c(%rbp),%esi
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1050 <warnx@plt>
mov    -0x1c(%rbp),%eax
and    $0x20,%eax
cmp    $0x0,%eax
je     1319 <block_write+0x1a9>
mov    -0x4(%rbp),%esi
shl    $0x1,%esi
mov    -0x1c(%rbp),%edx
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1050 <warnx@plt>
mov    -0x4(%rbp),%edi
call   1040 <lock_block_to_prevent_writes@plt>
mov    -0x4(%rbp),%edi
call   1080 <flash_read_mode@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<block_write>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%r14d
call   1060 <unlock_block_for_writes@plt>
mov    %r14d,%edi
mov    $0xe8,%esi
call   1090 <flash_write@plt>
mov    %r14d,%edi
call   1070 <flash_read@plt>
mov    %r14d,%r12d
test   $0x80,%al
jne    11ad <block_write+0x3d>
lea    0xe5a(%rip),%rdi
xor    %eax,%eax
call   1050 <warnx@plt>
mov    %r14d,%edi
mov    $0x1f,%esi
call   1090 <flash_write@plt>
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    (%r15,%rbx,4),%esi
lea    (%r12,%rbx,1),%edi
call   1090 <flash_write@plt>
add    $0x1,%rbx
cmp    $0x20,%rbx
jne    11c0 <block_write+0x50>
mov    %r12d,%edi
mov    $0xd0,%esi
call   1090 <flash_write@plt>
mov    %r14d,%edi
call   1070 <flash_read@plt>
test   $0x80,%al
jne    122b <block_write+0xbb>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14d,%edi
call   1070 <flash_read@plt>
lea    0x1(%rbx),%esi
test   $0x80,%al
jne    1219 <block_write+0xa9>
cmp    $0xffe,%ebx
mov    %esi,%ebx
jb     1200 <block_write+0x90>
test   $0x80,%al
jne    122b <block_write+0xbb>
lea    0xe1f(%rip),%rdi
xor    %eax,%eax
call   1050 <warnx@plt>
mov    %r14d,%edi
call   1030 <flash_read_status@plt>
mov    %eax,%ebx
test   $0x8,%bl
je     124a <block_write+0xda>
lea    0xe3f(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <warnx@plt>
test   $0x30,%bl
je     125f <block_write+0xef>
lea    0xe6e(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <warnx@plt>
test   $0x2,%bl
je     1274 <block_write+0x104>
lea    0xe9c(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <warnx@plt>
test   $0x20,%bl
je     128d <block_write+0x11d>
lea    (%r14,%r14,1),%esi
lea    0xec4(%rip),%rdi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <warnx@plt>
mov    %r14d,%edi
call   1040 <lock_block_to_prevent_writes@plt>
mov    %r14d,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1080 <flash_read_mode@plt>

LLVM IR (-O0):
@.str = [67 x i8] c"block_write to flash failed - block write not supported by device?\00"
@.str.1 = [61 x i8] c"ERROR on block-write - flash is busy even after 0x%x checks\0A\00"
@.str.2 = [68 x i8] c"Vpp voltage droop error during block-write, aborted, status=0x%02x\0A\00"
@.str.3 = [67 x i8] c"Command sequence error during block-write, aborted, status=0x%02x\0A\00"
@.str.4 = [65 x i8] c"Block locked during block-write process, aborted, status=0x%02x\0A\00"
@.str.5 = [52 x i8] c"Block-write failed at offset 0x%08x, status=0x%02x\0A\00"
define void @block_write(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i32* %1, i32** %4
%8 = load i32, i32* %3
%9 = call i32 @unlock_block_for_writes(i32 %8)
%10 = load i32, i32* %3
%11 = call i32 @flash_write(i32 %10, i32 232)
%12 = load i32, i32* %3
%13 = call i32 @flash_read(i32 %12)
%14 = and i32 %13, 128
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %2
%19 = load i32, i32* %3
%20 = call i32 @flash_write(i32 %19, i32 31)
store i32 0, i32* %5
br label %21
21: ; preds = %34, %18
%22 = load i32, i32* %5
%23 = icmp slt i32 %22, 32
br i1 %23, label %24, label %37
24: ; preds = %21
%25 = load i32, i32* %3
%26 = load i32, i32* %5
%27 = add nsw i32 %25, %26
%28 = load i32*, i32** %4
%29 = load i32, i32* %5
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32, i32* %28, i64 %30
%32 = load i32, i32* %31
%33 = call i32 @flash_write(i32 %27, i32 %32)
br label %34
34: ; preds = %24
%35 = load i32, i32* %5
%36 = add nsw i32 %35, 1
store i32 %36, i32* %5
br label %21
37: ; preds = %21
%38 = load i32, i32* %3
%39 = call i32 @flash_write(i32 %38, i32 208)
%40 = load i32, i32* %3
%41 = call i32 @flash_read(i32 %40)
store i32 %41, i32* %7
store i32 0, i32* %5
br label %42
42: ; preds = %54, %37
%43 = load i32, i32* %7
%44 = and i32 %43, 128
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = load i32, i32* %5
%48 = icmp slt i32 %47, 4095
br label %49
49: ; preds = %46, %42
%50 = phi i1 [ false, %42 ], [ %48, %46 ]
br i1 %50, label %51, label %57
51: ; preds = %49
%52 = load i32, i32* %3
%53 = call i32 @flash_read(i32 %52)
store i32 %53, i32* %7
br label %54
54: ; preds = %51
%55 = load i32, i32* %5
%56 = add nsw i32 %55, 1
store i32 %56, i32* %5
br label %42
57: ; preds = %49
%58 = load i32, i32* %7
%59 = and i32 %58, 128
%60 = icmp eq i32 %59, 0
br i1 %60, label %61, label %64
61: ; preds = %57
%62 = load i32, i32* %5
%63 = call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0), i32 %62)
br label %64
64: ; preds = %61, %57
%65 = load i32, i32* %3
%66 = call i32 @flash_read_status(i32 %65)
store i32 %66, i32* %7
%67 = load i32, i32* %7
%68 = and i32 %67, 8
%69 = icmp ne i32 %68, 0
br i1 %69, label %70, label %73
70: ; preds = %64
%71 = load i32, i32* %7
%72 = call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.2, i64 0, i64 0), i32 %71)
br label %73
73: ; preds = %70, %64
%74 = load i32, i32* %7
%75 = and i32 %74, 48
%76 = icmp ne i32 %75, 0
br i1 %76, label %77, label %80
77: ; preds = %73
%78 = load i32, i32* %7
%79 = call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.3, i64 0, i64 0), i32 %78)
br label %80
80: ; preds = %77, %73
%81 = load i32, i32* %7
%82 = and i32 %81, 2
%83 = icmp ne i32 %82, 0
br i1 %83, label %84, label %87
84: ; preds = %80
%85 = load i32, i32* %7
%86 = call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.4, i64 0, i64 0), i32 %85)
br label %87
87: ; preds = %84, %80
%88 = load i32, i32* %7
%89 = and i32 %88, 32
%90 = icmp ne i32 %89, 0
br i1 %90, label %91, label %96
91: ; preds = %87
%92 = load i32, i32* %3
%93 = shl i32 %92, 1
%94 = load i32, i32* %7
%95 = call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.5, i64 0, i64 0), i32 %93, i32 %94)
br label %96
96: ; preds = %91, %87
%97 = load i32, i32* %3
%98 = call i32 @lock_block_to_prevent_writes(i32 %97)
%99 = load i32, i32* %3
%100 = call i32 @flash_read_mode(i32 %99)
ret void
}

LLVM IR (-O2):
@.str = [67 x i8] c"block_write to flash failed - block write not supported by device?\00"
@.str.1 = [61 x i8] c"ERROR on block-write - flash is busy even after 0x%x checks\0A\00"
@.str.2 = [68 x i8] c"Vpp voltage droop error during block-write, aborted, status=0x%02x\0A\00"
@.str.3 = [67 x i8] c"Command sequence error during block-write, aborted, status=0x%02x\0A\00"
@.str.4 = [65 x i8] c"Block locked during block-write process, aborted, status=0x%02x\0A\00"
@.str.5 = [52 x i8] c"Block-write failed at offset 0x%08x, status=0x%02x\0A\00"
define void @block_write(i32 %0, i32* nocapture %1) {
%3 = tail call i32 @unlock_block_for_writes(i32 %0)
%4 = tail call i32 @flash_write(i32 %0, i32 232)
%5 = tail call i32 @flash_read(i32 %0)
%6 = and i32 %5, 128
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = tail call i32 @flash_write(i32 %0, i32 31)
br label %12
12: ; preds = %10, %12
%13 = phi i64 [ 0, %10 ], [ %19, %12 ]
%14 = getelementptr inbounds i32, i32* %1, i64 %13
%15 = load i32, i32* %14
%16 = trunc i64 %13 to i32
%17 = add i32 %16, %0
%18 = tail call i32 @flash_write(i32 %17, i32 %15)
%19 = add nuw nsw i64 %13, 1
%20 = icmp eq i64 %19, 32
br i1 %20, label %21, label %12
21: ; preds = %12
%22 = tail call i32 @flash_write(i32 %0, i32 208)
%23 = tail call i32 @flash_read(i32 %0)
%24 = and i32 %23, 128
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %37
26: ; preds = %21, %26
%27 = phi i32 [ %29, %26 ], [ 0, %21 ]
%28 = tail call i32 @flash_read(i32 %0)
%29 = add nuw nsw i32 %27, 1
%30 = and i32 %28, 128
%31 = icmp eq i32 %30, 0
%32 = icmp ult i32 %27, 4094
%33 = select i1 %31, i1 %32, i1 false
br i1 %33, label %26, label %34
34: ; preds = %26
br i1 %31, label %35, label %37
35: ; preds = %34
%36 = tail call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0), i32 %29)
br label %37
37: ; preds = %21, %35, %34
%38 = tail call i32 @flash_read_status(i32 %0)
%39 = and i32 %38, 8
%40 = icmp eq i32 %39, 0
br i1 %40, label %43, label %41
41: ; preds = %37
%42 = tail call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.2, i64 0, i64 0), i32 %38)
br label %43
43: ; preds = %41, %37
%44 = and i32 %38, 48
%45 = icmp eq i32 %44, 0
br i1 %45, label %48, label %46
46: ; preds = %43
%47 = tail call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.3, i64 0, i64 0), i32 %38)
br label %48
48: ; preds = %46, %43
%49 = and i32 %38, 2
%50 = icmp eq i32 %49, 0
br i1 %50, label %53, label %51
51: ; preds = %48
%52 = tail call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.4, i64 0, i64 0), i32 %38)
br label %53
53: ; preds = %51, %48
%54 = and i32 %38, 32
%55 = icmp eq i32 %54, 0
br i1 %55, label %59, label %56
56: ; preds = %53
%57 = shl i32 %0, 1
%58 = tail call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.5, i64 0, i64 0), i32 %57, i32 %38)
br label %59
59: ; preds = %56, %53
%60 = tail call i32 @lock_block_to_prevent_writes(i32 %0)
%61 = tail call i32 @flash_read_mode(i32 %0)
ret void
}