#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_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 single_write(int offset, int data) {
  int j;
  int status;
  unlock_block_for_writes(offset);
  flash_write(offset, 0x40);
  flash_write(offset, data);
  status = flash_read_status(offset);
  for (j = 0; ((status & 0x80) == 0) && (j < 0xfff); j++)
    status = flash_read_status(offset);
  if ((status & 0x80) == 0)
    warnx("ERROR on write - flash is busy even after 0x%x checks\n", j);
  status = flash_read_status(offset);
  if ((status & (1 << 3)) != 0)
    warnx("Vpp voltage droop error during write, aborted, status=0x%02x\n",
          status);
  if ((status & (3 << 4)) != 0)
    warnx("Command sequence error during write, aborted, status=0x%02x\n",
          status);
  if ((status & (1 << 1)) != 0)
    warnx("Block locked during write process, aborted, status=0x%02x\n",
          status);
  if ((status & (1 << 5)) != 0)
    warnx("write failed at offset 0x%08x, status=0x%02x\n", offset << 1,
          status);
  lock_block_to_prevent_writes(offset);
  flash_read_mode(offset);
}


x86 assembly (clang, -O0):
<single_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
call   1060 <unlock_block_for_writes@plt>
mov    -0x4(%rbp),%edi
mov    $0x40,%esi
call   1080 <flash_write@plt>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1080 <flash_write@plt>
mov    -0x4(%rbp),%edi
call   1030 <flash_read_status@plt>
mov    %eax,-0x10(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x10(%rbp),%ecx
and    $0x80,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x11(%rbp)
jne    11c4 <single_write+0x64>
cmpl   $0xfff,-0xc(%rbp)
setl   %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    11d4 <single_write+0x74>
jmp    11ed <single_write+0x8d>
mov    -0x4(%rbp),%edi
call   1030 <flash_read_status@plt>
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11a0 <single_write+0x40>
mov    -0x10(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    120f <single_write+0xaf>
mov    -0xc(%rbp),%esi
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1050 <warnx@plt>
mov    -0x4(%rbp),%edi
call   1030 <flash_read_status@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
and    $0x8,%eax
cmp    $0x0,%eax
je     123a <single_write+0xda>
mov    -0x10(%rbp),%esi
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1050 <warnx@plt>
mov    -0x10(%rbp),%eax
and    $0x30,%eax
cmp    $0x0,%eax
je     125a <single_write+0xfa>
mov    -0x10(%rbp),%esi
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1050 <warnx@plt>
mov    -0x10(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     127a <single_write+0x11a>
mov    -0x10(%rbp),%esi
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1050 <warnx@plt>
mov    -0x10(%rbp),%eax
and    $0x20,%eax
cmp    $0x0,%eax
je     12a0 <single_write+0x140>
mov    -0x4(%rbp),%esi
shl    $0x1,%esi
mov    -0x10(%rbp),%edx
lea    0xe54(%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   1070 <flash_read_mode@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<single_write>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%ebx
call   1060 <unlock_block_for_writes@plt>
mov    %ebx,%edi
mov    $0x40,%esi
call   1080 <flash_write@plt>
mov    %ebx,%edi
mov    %ebp,%esi
call   1080 <flash_write@plt>
mov    %ebx,%edi
call   1030 <flash_read_status@plt>
test   $0x80,%al
jne    11ba <single_write+0x5a>
xor    %ebp,%ebp
xchg   %ax,%ax
mov    %ebx,%edi
call   1030 <flash_read_status@plt>
lea    0x1(%rbp),%esi
test   $0x80,%al
jne    11a8 <single_write+0x48>
cmp    $0xffe,%ebp
mov    %esi,%ebp
jb     1190 <single_write+0x30>
test   $0x80,%al
jne    11ba <single_write+0x5a>
lea    0xe4d(%rip),%rdi
xor    %eax,%eax
call   1050 <warnx@plt>
mov    %ebx,%edi
call   1030 <flash_read_status@plt>
mov    %eax,%ebp
test   $0x8,%bpl
je     11d9 <single_write+0x79>
lea    0xe67(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <warnx@plt>
test   $0x30,%bpl
je     11ef <single_write+0x8f>
lea    0xe8f(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <warnx@plt>
test   $0x2,%bpl
je     1205 <single_write+0xa5>
lea    0xeb6(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <warnx@plt>
test   $0x20,%bpl
je     121e <single_write+0xbe>
lea    (%rbx,%rbx,1),%esi
lea    0xed8(%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
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1070 <flash_read_mode@plt>

LLVM IR (-O0):
@.str = [55 x i8] c"ERROR on write - flash is busy even after 0x%x checks\0A\00"
@.str.1 = [62 x i8] c"Vpp voltage droop error during write, aborted, status=0x%02x\0A\00"
@.str.2 = [61 x i8] c"Command sequence error during write, aborted, status=0x%02x\0A\00"
@.str.3 = [59 x i8] c"Block locked during write process, aborted, status=0x%02x\0A\00"
@.str.4 = [46 x i8] c"write failed at offset 0x%08x, status=0x%02x\0A\00"
define void @single_write(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = load i32, i32* %3
%8 = call i32 @unlock_block_for_writes(i32 %7)
%9 = load i32, i32* %3
%10 = call i32 @flash_write(i32 %9, i32 64)
%11 = load i32, i32* %3
%12 = load i32, i32* %4
%13 = call i32 @flash_write(i32 %11, i32 %12)
%14 = load i32, i32* %3
%15 = call i32 @flash_read_status(i32 %14)
store i32 %15, i32* %6
store i32 0, i32* %5
br label %16
16: ; preds = %28, %2
%17 = load i32, i32* %6
%18 = and i32 %17, 128
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i32, i32* %5
%22 = icmp slt i32 %21, 4095
br label %23
23: ; preds = %20, %16
%24 = phi i1 [ false, %16 ], [ %22, %20 ]
br i1 %24, label %25, label %31
25: ; preds = %23
%26 = load i32, i32* %3
%27 = call i32 @flash_read_status(i32 %26)
store i32 %27, i32* %6
br label %28
28: ; preds = %25
%29 = load i32, i32* %5
%30 = add nsw i32 %29, 1
store i32 %30, i32* %5
br label %16
31: ; preds = %23
%32 = load i32, i32* %6
%33 = and i32 %32, 128
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = load i32, i32* %5
%37 = call i32 (i8*, i32, ...) @warnx(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %35, %31
%39 = load i32, i32* %3
%40 = call i32 @flash_read_status(i32 %39)
store i32 %40, i32* %6
%41 = load i32, i32* %6
%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* %6
%46 = call i32 (i8*, i32, ...) @warnx(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0), i32 %45)
br label %47
47: ; preds = %44, %38
%48 = load i32, i32* %6
%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* %6
%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* %6
%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* %6
%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* %6
%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* %3
%67 = shl i32 %66, 1
%68 = load i32, i32* %6
%69 = call i32 (i8*, i32, ...) @warnx(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), i32 %67, i32 %68)
br label %70
70: ; preds = %65, %61
%71 = load i32, i32* %3
%72 = call i32 @lock_block_to_prevent_writes(i32 %71)
%73 = load i32, i32* %3
%74 = call i32 @flash_read_mode(i32 %73)
ret void
}

LLVM IR (-O2):
@.str = [55 x i8] c"ERROR on write - flash is busy even after 0x%x checks\0A\00"
@.str.1 = [62 x i8] c"Vpp voltage droop error during write, aborted, status=0x%02x\0A\00"
@.str.2 = [61 x i8] c"Command sequence error during write, aborted, status=0x%02x\0A\00"
@.str.3 = [59 x i8] c"Block locked during write process, aborted, status=0x%02x\0A\00"
@.str.4 = [46 x i8] c"write failed at offset 0x%08x, status=0x%02x\0A\00"
define void @single_write(i32 %0, i32 %1) {
%3 = tail call i32 @unlock_block_for_writes(i32 %0)
%4 = tail call i32 @flash_write(i32 %0, i32 64)
%5 = tail call i32 @flash_write(i32 %0, i32 %1)
%6 = tail call i32 @flash_read_status(i32 %0)
%7 = and i32 %6, 128
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %20
9: ; preds = %2, %9
%10 = phi i32 [ %12, %9 ], [ 0, %2 ]
%11 = tail call i32 @flash_read_status(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, 4094
%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 ([55 x i8], [55 x i8]* @.str, i64 0, i64 0), i32 %12)
br label %20
20: ; preds = %2, %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 ([62 x i8], [62 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 ([46 x i8], [46 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_read_mode(i32 %0)
ret void
}