#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 */
struct ci20_dev {
  int dummy;
};

/* Variables and functions */
int ci20_pin_level(struct ci20_dev *, int /*<<< orphan*/, int, int *);
int wait_ready(struct ci20_dev *dev) {
  int err, level;
  unsigned timeout = 1000;
  do {
    err = ci20_pin_level(dev, 0, 20, &level);
    if (err)
      return err;
  } while (level && timeout--);
  do {
    err = ci20_pin_level(dev, 0, 20, &level);
    if (err)
      return err;
  } while (!level);
  return 0;
}


x86 assembly (clang, -O0):
<wait_ready>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x3e8,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
lea    -0x18(%rbp),%rcx
call   1030 <ci20_pin_level@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     114f <wait_ready+0x3f>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d0 <wait_ready+0xc0>
jmp    1154 <wait_ready+0x44>
xor    %eax,%eax
cmpl   $0x0,-0x18(%rbp)
mov    %al,-0x1d(%rbp)
je     1177 <wait_ready+0x67>
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x1c(%rbp)
cmp    $0x0,%eax
setne  %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1123 <wait_ready+0x13>
jmp    1187 <wait_ready+0x77>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
lea    -0x18(%rbp),%rcx
call   1030 <ci20_pin_level@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11b3 <wait_ready+0xa3>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d0 <wait_ready+0xc0>
jmp    11b8 <wait_ready+0xa8>
cmpl   $0x0,-0x18(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1187 <wait_ready+0x77>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_ready>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r15
mov    $0xfffffc18,%ebx
lea    0xc(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x14,%edx
mov    %r14,%rcx
call   1030 <ci20_pin_level@plt>
test   %eax,%eax
jne    117f <wait_ready+0x6f>
cmpl   $0x0,0xc(%rsp)
je     1156 <wait_ready+0x46>
lea    0x1(%rbx),%eax
test   %ebx,%ebx
mov    %eax,%ebx
jne    1130 <wait_ready+0x20>
lea    0xc(%rsp),%r14
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x14,%edx
mov    %r14,%rcx
call   1030 <ci20_pin_level@plt>
test   %eax,%eax
jne    117f <wait_ready+0x6f>
cmpl   $0x0,0xc(%rsp)
je     1160 <wait_ready+0x50>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.ci20_dev = type { i32 }
define i32 @wait_ready(%struct.ci20_dev* %0) {
%2 = alloca i32
%3 = alloca %struct.ci20_dev*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store %struct.ci20_dev* %0, %struct.ci20_dev** %3
store i32 1000, i32* %6
br label %7
7: ; preds = %22, %1
%8 = load %struct.ci20_dev*, %struct.ci20_dev** %3
%9 = call i32 @ci20_pin_level(%struct.ci20_dev* %8, i32 0, i32 20, i32* %5)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = load i32, i32* %4
store i32 %13, i32* %2
br label %38
14: ; preds = %7
br label %15
15: ; preds = %14
%16 = load i32, i32* %5
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i32, i32* %6
%20 = add i32 %19, -1
store i32 %20, i32* %6
%21 = icmp ne i32 %19, 0
br label %22
22: ; preds = %18, %15
%23 = phi i1 [ false, %15 ], [ %21, %18 ]
br i1 %23, label %7, label %24
24: ; preds = %22
br label %25
25: ; preds = %33, %24
%26 = load %struct.ci20_dev*, %struct.ci20_dev** %3
%27 = call i32 @ci20_pin_level(%struct.ci20_dev* %26, i32 0, i32 20, i32* %5)
store i32 %27, i32* %4
%28 = load i32, i32* %4
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = load i32, i32* %4
store i32 %31, i32* %2
br label %38
32: ; preds = %25
br label %33
33: ; preds = %32
%34 = load i32, i32* %5
%35 = icmp ne i32 %34, 0
%36 = xor i1 %35, true
br i1 %36, label %25, label %37
37: ; preds = %33
store i32 0, i32* %2
br label %38
38: ; preds = %37, %30, %12
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
%struct.ci20_dev = type { i32 }
define i32 @wait_ready(%struct.ci20_dev* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
br label %4
4: ; preds = %8, %1
%5 = phi i32 [ 1000, %1 ], [ %11, %8 ]
%6 = call i32 @ci20_pin_level(%struct.ci20_dev* %0, i32 0, i32 20, i32* %2)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %20
8: ; preds = %4
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, 0
%11 = add nsw i32 %5, -1
%12 = icmp eq i32 %5, 0
%13 = select i1 %10, i1 true, i1 %12
br i1 %13, label %14, label %4
14: ; preds = %8, %17
%15 = call i32 @ci20_pin_level(%struct.ci20_dev* %0, i32 0, i32 20, i32* %2)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* %2
%19 = icmp eq i32 %18, 0
br i1 %19, label %14, label %20
20: ; preds = %4, %17, %14
%21 = phi i32 [ 0, %17 ], [ %15, %14 ], [ %6, %4 ]
ret i32 %21
}