#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*/ bbm_read(void *, int, unsigned char *);
signed int WaitForXfer(void *handle) {
  signed int i;
  signed int res = 0;
  unsigned char status;
  i = 100 * 20000;
  do {
    bbm_read(handle, 0x0034, &status);
    i--;
  } while ((i > 0) && (status & 0x02));
  if (status & 0x02) {
    res = 4;
  } else {
    bbm_read(handle, 0x0034, &status);
    if (status & 0x80)
      res = 2;
    else
      res = 0;
  }
  return res;
}


x86 assembly (clang, -O0):
<WaitForXfer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x1e8480,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x34,%esi
lea    -0x11(%rbp),%rdx
call   1030 <bbm_read@plt>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0xc(%rbp)
mov    %al,-0x12(%rbp)
jle    1164 <WaitForXfer+0x54>
movzbl -0x11(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x12(%rbp)
mov    -0x12(%rbp),%al
test   $0x1,%al
jne    112a <WaitForXfer+0x1a>
movzbl -0x11(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     118b <WaitForXfer+0x7b>
movl   $0x4,-0x10(%rbp)
jmp    11c7 <WaitForXfer+0xb7>
mov    -0x8(%rbp),%rdi
mov    $0x34,%esi
lea    -0x11(%rbp),%rdx
call   1030 <bbm_read@plt>
movzbl -0x11(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
je     11bb <WaitForXfer+0xab>
movl   $0x2,-0x10(%rbp)
jmp    11c2 <WaitForXfer+0xb2>
movl   $0x0,-0x10(%rbp)
jmp    11c7 <WaitForXfer+0xb7>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<WaitForXfer>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    $0x1e8480,%ebp
lea    0xf(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    $0x34,%esi
mov    %r14,%rdx
call   1030 <bbm_read@plt>
movzbl 0xf(%rsp),%ecx
cmp    $0x2,%ebp
jb     1153 <WaitForXfer+0x43>
add    $0xffffffff,%ebp
mov    %ecx,%eax
and    $0x2,%al
jne    1130 <WaitForXfer+0x20>
mov    $0x4,%eax
test   $0x2,%cl
jne    117a <WaitForXfer+0x6a>
lea    0xf(%rsp),%rdx
mov    %rbx,%rdi
mov    $0x34,%esi
call   1030 <bbm_read@plt>
movzbl 0xf(%rsp),%eax
shr    $0x6,%eax
and    $0x2,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @WaitForXfer(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
store i8* %0, i8** %2
store i32 0, i32* %4
store i32 2000000, i32* %3
br label %6
6: ; preds = %19, %1
%7 = load i8*, i8** %2
%8 = call i32 @bbm_read(i8* %7, i32 52, i8* %5)
%9 = load i32, i32* %3
%10 = add nsw i32 %9, -1
store i32 %10, i32* %3
br label %11
11: ; preds = %6
%12 = load i32, i32* %3
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %11
%15 = load i8, i8* %5
%16 = zext i8 %15 to i32
%17 = and i32 %16, 2
%18 = icmp ne i32 %17, 0
br label %19
19: ; preds = %14, %11
%20 = phi i1 [ false, %11 ], [ %18, %14 ]
br i1 %20, label %6, label %21
21: ; preds = %19
%22 = load i8, i8* %5
%23 = zext i8 %22 to i32
%24 = and i32 %23, 2
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %21
store i32 4, i32* %4
br label %37
27: ; preds = %21
%28 = load i8*, i8** %2
%29 = call i32 @bbm_read(i8* %28, i32 52, i8* %5)
%30 = load i8, i8* %5
%31 = zext i8 %30 to i32
%32 = and i32 %31, 128
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %27
store i32 2, i32* %4
br label %36
35: ; preds = %27
store i32 0, i32* %4
br label %36
36: ; preds = %35, %34
br label %37
37: ; preds = %36, %26
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
define i32 @WaitForXfer(i8* %0) {
%2 = alloca i8
br label %3
3: ; preds = %3, %1
%4 = phi i32 [ 2000000, %1 ], [ %6, %3 ]
%5 = call i32 @bbm_read(i8* %0, i32 52, i8* %2)
%6 = add nsw i32 %4, -1
%7 = icmp ugt i32 %4, 1
%8 = load i8, i8* %2
%9 = and i8 %8, 2
%10 = icmp ne i8 %9, 0
%11 = select i1 %7, i1 %10, i1 false
br i1 %11, label %3, label %12
12: ; preds = %3
br i1 %10, label %19, label %13
13: ; preds = %12
%14 = call i32 @bbm_read(i8* %0, i32 52, i8* %2)
%15 = load i8, i8* %2
%16 = lshr i8 %15, 6
%17 = and i8 %16, 2
%18 = zext i8 %17 to i32
br label %19
19: ; preds = %13, %12
%20 = phi i32 [ 4, %12 ], [ %18, %13 ]
ret i32 %20
}