short _FLASH_waitWhileBusy(unsigned long timeout) {
  *(volatile unsigned short *)(0x0400000) = 0x70;
  while (timeout-- > 0) {
    if ((*(volatile unsigned short *)(0x0400000) & 0x0080) ||
        (*(volatile unsigned short *)(0x0400000) & 0x8000)) {
      if (((*(volatile unsigned short *)(0x0400000) & 0x007F) != 0) ||
          ((*(volatile unsigned short *)(0x0400000) & 0x7F00) != 0)) {
        *(volatile unsigned short *)(0x0400000) = 0xFF;
        return -1;
      } else {
        *(volatile unsigned short *)(0x0400000) = 0xFF;
        return 0;
      }
    }
  }
  *(volatile unsigned short *)(0x0400000) = 0xFF;
  return 1;
}


x86 assembly (clang, -O0):
<_FLASH_waitWhileBusy>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    $0x400000,%eax
movw   $0x70,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x10(%rbp)
cmp    $0x0,%rax
jbe    11bc <_FLASH_waitWhileBusy+0xbc>
mov    $0x400000,%eax
mov    (%rax),%ax
movzwl %ax,%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    115d <_FLASH_waitWhileBusy+0x5d>
mov    $0x400000,%eax
mov    (%rax),%ax
movzwl %ax,%eax
and    $0x8000,%eax
cmp    $0x0,%eax
je     11b7 <_FLASH_waitWhileBusy+0xb7>
mov    $0x400000,%eax
mov    (%rax),%ax
movzwl %ax,%eax
and    $0x7f,%eax
cmp    $0x0,%eax
jne    118d <_FLASH_waitWhileBusy+0x8d>
mov    $0x400000,%eax
mov    (%rax),%ax
movzwl %ax,%eax
and    $0x7f00,%eax
cmp    $0x0,%eax
je     11a2 <_FLASH_waitWhileBusy+0xa2>
mov    $0x400000,%eax
movw   $0xff,(%rax)
movw   $0xffff,-0x2(%rbp)
jmp    11cc <_FLASH_waitWhileBusy+0xcc>
mov    $0x400000,%eax
movw   $0xff,(%rax)
movw   $0x0,-0x2(%rbp)
jmp    11cc <_FLASH_waitWhileBusy+0xcc>
jmp    1112 <_FLASH_waitWhileBusy+0x12>
mov    $0x400000,%eax
movw   $0xff,(%rax)
movw   $0x1,-0x2(%rbp)
movswl -0x2(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<_FLASH_waitWhileBusy>:
movw   $0x70,0x400000
add    $0x1,%rdi
xchg   %ax,%ax
add    $0xffffffffffffffff,%rdi
je     1149 <_FLASH_waitWhileBusy+0x49>
movzwl 0x400000,%eax
test   $0x80,%al
jne    112d <_FLASH_waitWhileBusy+0x2d>
cmpw   $0x0,0x400000
jns    1110 <_FLASH_waitWhileBusy+0x10>
movzwl 0x400000,%ecx
mov    $0xffff,%ax
test   $0x7f,%cl
je     1158 <_FLASH_waitWhileBusy+0x58>
movw   $0xff,0x400000
ret
mov    $0x1,%ax
movw   $0xff,0x400000
ret
movzwl 0x400000,%eax
and    $0x7f00,%eax
neg    %ax
sbb    %eax,%eax
movw   $0xff,0x400000
ret

LLVM IR (-O0):
define signext i16 @_FLASH_waitWhileBusy(i64 %0) {
%2 = alloca i16
%3 = alloca i64
store i64 %0, i64* %3
store volatile i16 112, i16* inttoptr (i64 4194304 to i16*)
br label %4
4: ; preds = %30, %1
%5 = load i64, i64* %3
%6 = add i64 %5, -1
store i64 %6, i64* %3
%7 = icmp ugt i64 %5, 0
br i1 %7, label %8, label %31
8: ; preds = %4
%9 = load volatile i16, i16* inttoptr (i64 4194304 to i16*)
%10 = zext i16 %9 to i32
%11 = and i32 %10, 128
%12 = icmp ne i32 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %8
%14 = load volatile i16, i16* inttoptr (i64 4194304 to i16*)
%15 = zext i16 %14 to i32
%16 = and i32 %15, 32768
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %30
18: ; preds = %13, %8
%19 = load volatile i16, i16* inttoptr (i64 4194304 to i16*)
%20 = zext i16 %19 to i32
%21 = and i32 %20, 127
%22 = icmp ne i32 %21, 0
br i1 %22, label %28, label %23
23: ; preds = %18
%24 = load volatile i16, i16* inttoptr (i64 4194304 to i16*)
%25 = zext i16 %24 to i32
%26 = and i32 %25, 32512
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %23, %18
store volatile i16 255, i16* inttoptr (i64 4194304 to i16*)
store i16 -1, i16* %2
br label %32
29: ; preds = %23
store volatile i16 255, i16* inttoptr (i64 4194304 to i16*)
store i16 0, i16* %2
br label %32
30: ; preds = %13
br label %4
31: ; preds = %4
store volatile i16 255, i16* inttoptr (i64 4194304 to i16*)
store i16 1, i16* %2
br label %32
32: ; preds = %31, %29, %28
%33 = load i16, i16* %2
ret i16 %33
}

LLVM IR (-O2):
define signext i16 @_FLASH_waitWhileBusy(i64 %0) {
store volatile i16 112, i16* inttoptr (i64 4194304 to i16*)
br label %2
2: ; preds = %10, %1
%3 = phi i64 [ %0, %1 ], [ %4, %10 ]
%4 = add i64 %3, -1
%5 = icmp eq i64 %3, 0
br i1 %5, label %22, label %6
6: ; preds = %2
%7 = load volatile i16, i16* inttoptr (i64 4194304 to i16*)
%8 = and i16 %7, 128
%9 = icmp eq i16 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %6
%11 = load volatile i16, i16* inttoptr (i64 4194304 to i16*)
%12 = icmp sgt i16 %11, -1
br i1 %12, label %2, label %13
13: ; preds = %10, %6
%14 = load volatile i16, i16* inttoptr (i64 4194304 to i16*)
%15 = and i16 %14, 127
%16 = icmp eq i16 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %13
%18 = load volatile i16, i16* inttoptr (i64 4194304 to i16*)
%19 = and i16 %18, 32512
%20 = icmp ne i16 %19, 0
%21 = sext i1 %20 to i16
br label %22
22: ; preds = %2, %17, %13
%23 = phi i16 [ -1, %13 ], [ %21, %17 ], [ 1, %2 ]
store volatile i16 255, i16* inttoptr (i64 4194304 to i16*)
ret i16 %23
}