#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 EIO;
int ETIMEDOUT;
int /*<<< orphan*/ printf(char *, int);
int spi_read(int, char *, int);
int /*<<< orphan*/ usleep(int);
int wait_rdy(int fd) {
  int i = 0, ret;
  char temp[2] = {0x05, 0};
  do {
    ++i;
    usleep(50000);
    if ((ret = spi_read(fd, temp, 2)) < 0)
      goto outrdy;
  } while ((temp[1] & (1 << 4)) && i < 10);
  if (temp[1] & (1 << 4)) {
    printf("Error erasing page: timeout %u\n", i);
    return -ETIMEDOUT;
  } else
    return 0;
outrdy:
  return -EIO;
}


x86 assembly (clang, -O0):
<wait_rdy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    0xeb7(%rip),%ax
mov    %ax,-0x12(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    $0xc350,%edi
call   1050 <usleep@plt>
mov    -0x8(%rbp),%edi
lea    -0x12(%rbp),%rsi
mov    $0x2,%edx
call   1040 <spi_read@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    1182 <wait_rdy+0x52>
jmp    11ef <wait_rdy+0xbf>
jmp    1187 <wait_rdy+0x57>
movsbl -0x11(%rbp),%ecx
and    $0x10,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x13(%rbp)
je     11a6 <wait_rdy+0x76>
cmpl   $0xa,-0xc(%rbp)
setl   %al
mov    %al,-0x13(%rbp)
mov    -0x13(%rbp),%al
test   $0x1,%al
jne    114d <wait_rdy+0x1d>
movsbl -0x11(%rbp),%eax
and    $0x10,%eax
cmp    $0x0,%eax
je     11e3 <wait_rdy+0xb3>
mov    -0xc(%rbp),%esi
lea    0xe37(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
mov    0x2dff(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11fd <wait_rdy+0xcd>
movl   $0x0,-0x4(%rbp)
jmp    11fd <wait_rdy+0xcd>
xor    %eax,%eax
mov    0x2de8(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_rdy>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
movw   $0x5,0xe(%rsp)
xor    %ebx,%ebx
lea    0xe(%rsp),%r14
nopl   0x0(%rax,%rax,1)
mov    $0xc350,%edi
call   1050 <usleep@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x2,%edx
call   1040 <spi_read@plt>
test   %eax,%eax
js     119d <wait_rdy+0x6d>
movzbl 0xf(%rsp),%ecx
lea    0x1(%rbx),%esi
test   $0x10,%cl
je     1181 <wait_rdy+0x51>
cmp    $0x9,%ebx
mov    %esi,%ebx
jb     1150 <wait_rdy+0x20>
xor    %eax,%eax
test   $0x10,%cl
je     11a8 <wait_rdy+0x78>
lea    0xe71(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e3d(%rip),%rcx
jmp    11a4 <wait_rdy+0x74>
mov    0x2e3c(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@__const.wait_rdy.temp = [2 x i8] c"\05\00"
@.str = [32 x i8] c"Error erasing page: timeout %u\0A\00"
@ETIMEDOUT = global i32 0
@EIO = global i32 0
define i32 @wait_rdy(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [2 x i8]
store i32 %0, i32* %3
store i32 0, i32* %4
%7 = bitcast [2 x i8]* %6 to i8*
br label %8
8: ; preds = %27, %1
%9 = load i32, i32* %4
%10 = add nsw i32 %9, 1
store i32 %10, i32* %4
%11 = call i32 @usleep(i32 50000)
%12 = load i32, i32* %3
%13 = getelementptr inbounds [2 x i8], [2 x i8]* %6, i64 0, i64 0
%14 = call i32 @spi_read(i32 %12, i8* %13, i32 2)
store i32 %14, i32* %5
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %8
br label %41
17: ; preds = %8
br label %18
18: ; preds = %17
%19 = getelementptr inbounds [2 x i8], [2 x i8]* %6, i64 0, i64 1
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = and i32 %21, 16
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = load i32, i32* %4
%26 = icmp slt i32 %25, 10
br label %27
27: ; preds = %24, %18
%28 = phi i1 [ false, %18 ], [ %26, %24 ]
br i1 %28, label %8, label %29
29: ; preds = %27
%30 = getelementptr inbounds [2 x i8], [2 x i8]* %6, i64 0, i64 1
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = and i32 %32, 16
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %29
%36 = load i32, i32* %4
%37 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %36)
%38 = load i32, i32* @ETIMEDOUT
%39 = sub nsw i32 0, %38
store i32 %39, i32* %2
br label %44
40: ; preds = %29
store i32 0, i32* %2
br label %44
41: ; preds = %16
%42 = load i32, i32* @EIO
%43 = sub nsw i32 0, %42
store i32 %43, i32* %2
br label %44
44: ; preds = %41, %40, %35
%45 = load i32, i32* %2
ret i32 %45
}

LLVM IR (-O2):
@.str = [32 x i8] c"Error erasing page: timeout %u\0A\00"
@ETIMEDOUT = global i32 0
@EIO = global i32 0
define i32 @wait_rdy(i32 %0) {
%2 = alloca i16
%3 = bitcast i16* %2 to [2 x i8]*
%4 = bitcast i16* %2 to i8*
store i16 5, i16* %2
%5 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 1
br label %6
6: ; preds = %12, %1
%7 = phi i32 [ 0, %1 ], [ %8, %12 ]
%8 = add nuw nsw i32 %7, 1
%9 = call i32 @usleep(i32 50000)
%10 = call i32 @spi_read(i32 %0, i8* %4, i32 2)
%11 = icmp slt i32 %10, 0
br i1 %11, label %23, label %12
12: ; preds = %6
%13 = load i8, i8* %5
%14 = and i8 %13, 16
%15 = icmp ne i8 %14, 0
%16 = icmp ult i32 %7, 9
%17 = select i1 %15, i1 %16, i1 false
br i1 %17, label %6, label %18
18: ; preds = %12
br i1 %15, label %19, label %26
19: ; preds = %18
%20 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %8)
%21 = load i32, i32* @ETIMEDOUT
%22 = sub nsw i32 0, %21
br label %26
23: ; preds = %6
%24 = load i32, i32* @EIO
%25 = sub nsw i32 0, %24
br label %26
26: ; preds = %18, %23, %19
%27 = phi i32 [ %25, %23 ], [ %22, %19 ], [ 0, %18 ]
ret i32 %27
}