#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 /*<<< orphan*/ flash_clear_status(int);
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 *, int, ...);
void erase_block(int addr16b) {
  int j, status;
  unlock_block_for_writes(addr16b);
  flash_clear_status(addr16b);
  flash_write(addr16b, 0x20);
  flash_write(addr16b, 0xD0);
  status = flash_read(addr16b);
  for (j = 0; ((status & 0x80) == 0) && (j < 10000000); j++)
    status = flash_read(addr16b);
  if ((status & 0x80) == 0)
    warnx("Error on erase - flash is busy even after %1d status checks, "
          "status=0x%02x\n",
          j, status);
  status = flash_read_status(addr16b);
  if ((status & (1 << 3)) != 0)
    warnx("Vpp voltage droop error during erease, aborted, status=0x%02x\n",
          status);
  if ((status & (3 << 4)) != 0)
    warnx("Command sequence error during erase, aborted, status=0x%02x\n",
          status);
  if ((status & (1 << 1)) != 0)
    warnx("Block locked during erase process, aborted, status=0x%02x\n",
          status);
  if ((status & (1 << 5)) != 0)
    warnx("Erase failed at addr16b 0x%08x, status=0x%02x\n", addr16b << 1,
          status);
  lock_block_to_prevent_writes(addr16b);
  flash_clear_status(addr16b);
  flash_read_mode(addr16b);
  flash_read_mode(addr16b);
  j = flash_read(addr16b);
  if (j != 0xffff)
    warnx("Erase appears to have happened but read back 0x%04x but expecting "
          "0xffff",
          j);
}


x86 assembly (clang, -O0):
<erase_block>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1060 <unlock_block_for_writes@plt>
mov    -0x4(%rbp),%edi
call   1090 <flash_clear_status@plt>
mov    -0x4(%rbp),%edi
mov    $0x20,%esi
call   10a0 <flash_write@plt>
mov    -0x4(%rbp),%edi
mov    $0xd0,%esi
call   10a0 <flash_write@plt>
mov    -0x4(%rbp),%edi
call   1070 <flash_read@plt>
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0xc(%rbp),%ecx
and    $0x80,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0xd(%rbp)
jne    11eb <erase_block+0x6b>
cmpl   $0x989680,-0x8(%rbp)
setl   %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
test   $0x1,%al
jne    11fb <erase_block+0x7b>
jmp    1214 <erase_block+0x94>
mov    -0x4(%rbp),%edi
call   1070 <flash_read@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11c7 <erase_block+0x47>
mov    -0xc(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    1239 <erase_block+0xb9>
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1050 <warnx@plt>
mov    -0x4(%rbp),%edi
call   1030 <flash_read_status@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
and    $0x8,%eax
cmp    $0x0,%eax
je     1264 <erase_block+0xe4>
mov    -0xc(%rbp),%esi
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1050 <warnx@plt>
mov    -0xc(%rbp),%eax
and    $0x30,%eax
cmp    $0x0,%eax
je     1284 <erase_block+0x104>
mov    -0xc(%rbp),%esi
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1050 <warnx@plt>
mov    -0xc(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     12a4 <erase_block+0x124>
mov    -0xc(%rbp),%esi
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1050 <warnx@plt>
mov    -0xc(%rbp),%eax
and    $0x20,%eax
cmp    $0x0,%eax
je     12ca <erase_block+0x14a>
mov    -0x4(%rbp),%esi
shl    $0x1,%esi
mov    -0xc(%rbp),%edx
lea    0xe40(%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   1090 <flash_clear_status@plt>
mov    -0x4(%rbp),%edi
call   1080 <flash_read_mode@plt>
mov    -0x4(%rbp),%edi
call   1080 <flash_read_mode@plt>
mov    -0x4(%rbp),%edi
call   1070 <flash_read@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffff,-0x8(%rbp)
je     1313 <erase_block+0x193>
mov    -0x8(%rbp),%esi
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1050 <warnx@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<erase_block>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
call   1060 <unlock_block_for_writes@plt>
mov    %ebx,%edi
call   1090 <flash_clear_status@plt>
mov    %ebx,%edi
mov    $0x20,%esi
call   10a0 <flash_write@plt>
mov    %ebx,%edi
mov    $0xd0,%esi
call   10a0 <flash_write@plt>
mov    %ebx,%edi
call   1070 <flash_read@plt>
test   $0x80,%al
jne    11ec <erase_block+0x6c>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1070 <flash_read@plt>
lea    0x1(%rbp),%esi
test   $0x80,%al
jne    11d8 <erase_block+0x58>
cmp    $0x98967f,%ebp
mov    %esi,%ebp
jb     11c0 <erase_block+0x40>
test   $0x80,%al
jne    11ec <erase_block+0x6c>
lea    0xe1d(%rip),%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <warnx@plt>
mov    %ebx,%edi
call   1030 <flash_read_status@plt>
mov    %eax,%ebp
test   $0x8,%bpl
je     120b <erase_block+0x8b>
lea    0xe4a(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <warnx@plt>
test   $0x30,%bpl
je     1221 <erase_block+0xa1>
lea    0xe73(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <warnx@plt>
test   $0x2,%bpl
je     1237 <erase_block+0xb7>
lea    0xe9a(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <warnx@plt>
test   $0x20,%bpl
je     1250 <erase_block+0xd0>
lea    (%rbx,%rbx,1),%esi
lea    0xebc(%rip),%rdi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <warnx@plt>
mov    %ebx,%edi
call   1040 <lock_block_to_prevent_writes@plt>
mov    %ebx,%edi
call   1090 <flash_clear_status@plt>
mov    %ebx,%edi
call   1080 <flash_read_mode@plt>
mov    %ebx,%edi
call   1080 <flash_read_mode@plt>
mov    %ebx,%edi
call   1070 <flash_read@plt>
cmp    $0xffff,%eax
jne    1281 <erase_block+0x101>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xeaa(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <warnx@plt>

LLVM IR (-O0):
@.str = [76 x i8] c"Error on erase - flash is busy even after %1d status checks, status=0x%02x\0A\00"
@.str.1 = [63 x i8] c"Vpp voltage droop error during erease, aborted, status=0x%02x\0A\00"
@.str.2 = [61 x i8] c"Command sequence error during erase, aborted, status=0x%02x\0A\00"
@.str.3 = [59 x i8] c"Block locked during erase process, aborted, status=0x%02x\0A\00"
@.str.4 = [47 x i8] c"Erase failed at addr16b 0x%08x, status=0x%02x\0A\00"
@.str.5 = [73 x i8] c"Erase appears to have happened but read back 0x%04x but expecting 0xffff\00"
define void @erase_block(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = call i32 @unlock_block_for_writes(i32 %5)
%7 = load i32, i32* %2
%8 = call i32 @flash_clear_status(i32 %7)
%9 = load i32, i32* %2
%10 = call i32 @flash_write(i32 %9, i32 32)
%11 = load i32, i32* %2
%12 = call i32 @flash_write(i32 %11, i32 208)
%13 = load i32, i32* %2
%14 = call i32 @flash_read(i32 %13)
store i32 %14, i32* %4
store i32 0, i32* %3
br label %15
15: ; preds = %27, %1
%16 = load i32, i32* %4
%17 = and i32 %16, 128
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* %3
%21 = icmp slt i32 %20, 10000000
br label %22
22: ; preds = %19, %15
%23 = phi i1 [ false, %15 ], [ %21, %19 ]
br i1 %23, label %24, label %30
24: ; preds = %22
%25 = load i32, i32* %2
%26 = call i32 @flash_read(i32 %25)
store i32 %26, i32* %4
br label %27
27: ; preds = %24
%28 = load i32, i32* %3
%29 = add nsw i32 %28, 1
store i32 %29, i32* %3
br label %15
30: ; preds = %22
%31 = load i32, i32* %4
%32 = and i32 %31, 128
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = load i32, i32* %3
%36 = load i32, i32* %4
%37 = call i32 (i8*, i32, ...) @warnx(i8* getelementptr inbounds ([76 x i8], [76 x i8]* @.str, i64 0, i64 0), i32 %35, i32 %36)
br label %38
38: ; preds = %34, %30
%39 = load i32, i32* %2
%40 = call i32 @flash_read_status(i32 %39)
store i32 %40, i32* %4
%41 = load i32, i32* %4
%42 = and i32 %41, 8
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %38
%45 = load i32, i32* %4
%46 = call i32 (i8*, i32, ...) @warnx(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.1, i64 0, i64 0), i32 %45)
br label %47
47: ; preds = %44, %38
%48 = load i32, i32* %4
%49 = and i32 %48, 48
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %47
%52 = load i32, i32* %4
%53 = call i32 (i8*, i32, ...) @warnx(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.2, i64 0, i64 0), i32 %52)
br label %54
54: ; preds = %51, %47
%55 = load i32, i32* %4
%56 = and i32 %55, 2
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %54
%59 = load i32, i32* %4
%60 = call i32 (i8*, i32, ...) @warnx(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.3, i64 0, i64 0), i32 %59)
br label %61
61: ; preds = %58, %54
%62 = load i32, i32* %4
%63 = and i32 %62, 32
%64 = icmp ne i32 %63, 0
br i1 %64, label %65, label %70
65: ; preds = %61
%66 = load i32, i32* %2
%67 = shl i32 %66, 1
%68 = load i32, i32* %4
%69 = call i32 (i8*, i32, ...) @warnx(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i32 %67, i32 %68)
br label %70
70: ; preds = %65, %61
%71 = load i32, i32* %2
%72 = call i32 @lock_block_to_prevent_writes(i32 %71)
%73 = load i32, i32* %2
%74 = call i32 @flash_clear_status(i32 %73)
%75 = load i32, i32* %2
%76 = call i32 @flash_read_mode(i32 %75)
%77 = load i32, i32* %2
%78 = call i32 @flash_read_mode(i32 %77)
%79 = load i32, i32* %2
%80 = call i32 @flash_read(i32 %79)
store i32 %80, i32* %3
%81 = load i32, i32* %3
%82 = icmp ne i32 %81, 65535
br i1 %82, label %83, label %86
83: ; preds = %70
%84 = load i32, i32* %3
%85 = call i32 (i8*, i32, ...) @warnx(i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str.5, i64 0, i64 0), i32 %84)
br label %86
86: ; preds = %83, %70
ret void
}

LLVM IR (-O2):
@.str = [76 x i8] c"Error on erase - flash is busy even after %1d status checks, status=0x%02x\0A\00"
@.str.1 = [63 x i8] c"Vpp voltage droop error during erease, aborted, status=0x%02x\0A\00"
@.str.2 = [61 x i8] c"Command sequence error during erase, aborted, status=0x%02x\0A\00"
@.str.3 = [59 x i8] c"Block locked during erase process, aborted, status=0x%02x\0A\00"
@.str.4 = [47 x i8] c"Erase failed at addr16b 0x%08x, status=0x%02x\0A\00"
@.str.5 = [73 x i8] c"Erase appears to have happened but read back 0x%04x but expecting 0xffff\00"
define void @erase_block(i32 %0) {
%2 = tail call i32 @unlock_block_for_writes(i32 %0)
%3 = tail call i32 @flash_clear_status(i32 %0)
%4 = tail call i32 @flash_write(i32 %0, i32 32)
%5 = tail call i32 @flash_write(i32 %0, i32 208)
%6 = tail call i32 @flash_read(i32 %0)
%7 = and i32 %6, 128
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %20
9: ; preds = %1, %9
%10 = phi i32 [ %12, %9 ], [ 0, %1 ]
%11 = tail call i32 @flash_read(i32 %0)
%12 = add nuw nsw i32 %10, 1
%13 = and i32 %11, 128
%14 = icmp eq i32 %13, 0
%15 = icmp ult i32 %10, 9999999
%16 = select i1 %14, i1 %15, i1 false
br i1 %16, label %9, label %17
17: ; preds = %9
br i1 %14, label %18, label %20
18: ; preds = %17
%19 = tail call i32 (i8*, i32, ...) @warnx(i8* getelementptr inbounds ([76 x i8], [76 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %11)
br label %20
20: ; preds = %1, %18, %17
%21 = tail call i32 @flash_read_status(i32 %0)
%22 = and i32 %21, 8
%23 = icmp eq i32 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %20
%25 = tail call i32 (i8*, i32, ...) @warnx(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.1, i64 0, i64 0), i32 %21)
br label %26
26: ; preds = %24, %20
%27 = and i32 %21, 48
%28 = icmp eq i32 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %26
%30 = tail call i32 (i8*, i32, ...) @warnx(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.2, i64 0, i64 0), i32 %21)
br label %31
31: ; preds = %29, %26
%32 = and i32 %21, 2
%33 = icmp eq i32 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %31
%35 = tail call i32 (i8*, i32, ...) @warnx(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.3, i64 0, i64 0), i32 %21)
br label %36
36: ; preds = %34, %31
%37 = and i32 %21, 32
%38 = icmp eq i32 %37, 0
br i1 %38, label %42, label %39
39: ; preds = %36
%40 = shl i32 %0, 1
%41 = tail call i32 (i8*, i32, ...) @warnx(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i32 %40, i32 %21)
br label %42
42: ; preds = %39, %36
%43 = tail call i32 @lock_block_to_prevent_writes(i32 %0)
%44 = tail call i32 @flash_clear_status(i32 %0)
%45 = tail call i32 @flash_read_mode(i32 %0)
%46 = tail call i32 @flash_read_mode(i32 %0)
%47 = tail call i32 @flash_read(i32 %0)
%48 = icmp eq i32 %47, 65535
br i1 %48, label %51, label %49
49: ; preds = %42
%50 = tail call i32 (i8*, i32, ...) @warnx(i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str.5, i64 0, i64 0), i32 %47)
br label %51
51: ; preds = %49, %42
ret void
}