#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 *);
int /*<<< orphan*/ ms_wait(int);
int fc8080_scan_status(void *handle) {
  int i, res = 1;
  unsigned char mode = 0, status = 0, sync_status = 0;
  int dlock_cnt;
  bbm_read(handle, 0x0220, &mode);
  if ((mode & 0x01) == 0x01) {
    int slock_cnt, flock_cnt;
    slock_cnt = 200 / 5;
    flock_cnt = 300 / 5;
    dlock_cnt = 500 / 5;
    for (i = 0; i < slock_cnt; i++) {
      if (!ms_wait(5))
        return 1;
      bbm_read(handle, 0x0228, &status);
      if (status & 0x01)
        break;
    }
    if (i == slock_cnt)
      return 1;
    if ((status & 0x02) == 0x00)
      return 1;
    for (i += 1; i < flock_cnt; i++) {
      if (!ms_wait(5))
        return 1;
      bbm_read(handle, 0x0238, &sync_status);
      if (sync_status & 0x01)
        break;
    }
    if (i == flock_cnt)
      return 1;
    for (i += 1; i < dlock_cnt; i++) {
      if (!ms_wait(5))
        return 1;
      bbm_read(handle, 0x0238, &sync_status);
      if (sync_status & 0x20)
        return 0;
    }
  } else {
    dlock_cnt = 500 / 5;
    for (i = 0; i < dlock_cnt; i++) {
      if (!ms_wait(5))
        return 1;
      bbm_read(handle, 0x0238, &sync_status);
      if (sync_status & 0x20)
        return 0;
    }
  }
  return res;
}


x86 assembly (clang, -O0):
<fc8080_scan_status>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x1,-0x18(%rbp)
movb   $0x0,-0x19(%rbp)
movb   $0x0,-0x1a(%rbp)
movb   $0x0,-0x1b(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x220,%esi
lea    -0x19(%rbp),%rdx
call   1040 <bbm_read@plt>
movzbl -0x19(%rbp),%eax
and    $0x1,%eax
cmp    $0x1,%eax
jne    1316 <fc8080_scan_status+0x1f6>
movl   $0x28,-0x24(%rbp)
movl   $0x3c,-0x28(%rbp)
movl   $0x64,-0x20(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    11e2 <fc8080_scan_status+0xc2>
mov    $0x5,%edi
call   1030 <ms_wait@plt>
cmp    $0x0,%eax
jne    11a8 <fc8080_scan_status+0x88>
movl   $0x1,-0x4(%rbp)
jmp    139b <fc8080_scan_status+0x27b>
mov    -0x10(%rbp),%rdi
mov    $0x228,%esi
lea    -0x1a(%rbp),%rdx
call   1040 <bbm_read@plt>
movzbl -0x1a(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     11cf <fc8080_scan_status+0xaf>
jmp    11e2 <fc8080_scan_status+0xc2>
jmp    11d4 <fc8080_scan_status+0xb4>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    117d <fc8080_scan_status+0x5d>
mov    -0x14(%rbp),%eax
cmp    -0x24(%rbp),%eax
jne    11fa <fc8080_scan_status+0xda>
movl   $0x1,-0x4(%rbp)
jmp    139b <fc8080_scan_status+0x27b>
movzbl -0x1a(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
jne    1216 <fc8080_scan_status+0xf6>
movl   $0x1,-0x4(%rbp)
jmp    139b <fc8080_scan_status+0x27b>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    1284 <fc8080_scan_status+0x164>
mov    $0x5,%edi
call   1030 <ms_wait@plt>
cmp    $0x0,%eax
jne    124a <fc8080_scan_status+0x12a>
movl   $0x1,-0x4(%rbp)
jmp    139b <fc8080_scan_status+0x27b>
mov    -0x10(%rbp),%rdi
mov    $0x238,%esi
lea    -0x1b(%rbp),%rdx
call   1040 <bbm_read@plt>
movzbl -0x1b(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1271 <fc8080_scan_status+0x151>
jmp    1284 <fc8080_scan_status+0x164>
jmp    1276 <fc8080_scan_status+0x156>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    121f <fc8080_scan_status+0xff>
mov    -0x14(%rbp),%eax
cmp    -0x28(%rbp),%eax
jne    129c <fc8080_scan_status+0x17c>
movl   $0x1,-0x4(%rbp)
jmp    139b <fc8080_scan_status+0x27b>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    1311 <fc8080_scan_status+0x1f1>
mov    $0x5,%edi
call   1030 <ms_wait@plt>
cmp    $0x0,%eax
jne    12d0 <fc8080_scan_status+0x1b0>
movl   $0x1,-0x4(%rbp)
jmp    139b <fc8080_scan_status+0x27b>
mov    -0x10(%rbp),%rdi
mov    $0x238,%esi
lea    -0x1b(%rbp),%rdx
call   1040 <bbm_read@plt>
movzbl -0x1b(%rbp),%eax
and    $0x20,%eax
cmp    $0x0,%eax
je     12fe <fc8080_scan_status+0x1de>
movl   $0x0,-0x4(%rbp)
jmp    139b <fc8080_scan_status+0x27b>
jmp    1303 <fc8080_scan_status+0x1e3>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    12a5 <fc8080_scan_status+0x185>
jmp    1395 <fc8080_scan_status+0x275>
movl   $0x64,-0x20(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    1390 <fc8080_scan_status+0x270>
mov    $0x5,%edi
call   1030 <ms_wait@plt>
cmp    $0x0,%eax
jne    134f <fc8080_scan_status+0x22f>
movl   $0x1,-0x4(%rbp)
jmp    139b <fc8080_scan_status+0x27b>
mov    -0x10(%rbp),%rdi
mov    $0x238,%esi
lea    -0x1b(%rbp),%rdx
call   1040 <bbm_read@plt>
movzbl -0x1b(%rbp),%eax
and    $0x20,%eax
cmp    $0x0,%eax
je     137d <fc8080_scan_status+0x25d>
movl   $0x0,-0x4(%rbp)
jmp    139b <fc8080_scan_status+0x27b>
jmp    1382 <fc8080_scan_status+0x262>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1324 <fc8080_scan_status+0x204>
jmp    1395 <fc8080_scan_status+0x275>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fc8080_scan_status>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
movb   $0x0,0x7(%rsp)
movb   $0x0,0x6(%rsp)
movb   $0x0,0x5(%rsp)
lea    0x7(%rsp),%rdx
mov    $0x220,%esi
call   1040 <bbm_read@plt>
testb  $0x1,0x7(%rsp)
jne    1197 <fc8080_scan_status+0x77>
mov    $0x64,%ebx
lea    0x5(%rsp),%r14
mov    $0x1,%ebp
xchg   %ax,%ax
mov    $0x5,%edi
call   1030 <ms_wait@plt>
test   %eax,%eax
je     1259 <fc8080_scan_status+0x139>
mov    %r15,%rdi
mov    $0x238,%esi
mov    %r14,%rdx
call   1040 <bbm_read@plt>
testb  $0x20,0x5(%rsp)
jne    1257 <fc8080_scan_status+0x137>
add    $0xffffffff,%ebx
jne    1160 <fc8080_scan_status+0x40>
jmp    1259 <fc8080_scan_status+0x139>
mov    $0xffffff9d,%ebx
lea    0x6(%rsp),%r14
mov    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    $0x5,%edi
call   1030 <ms_wait@plt>
test   %eax,%eax
je     1259 <fc8080_scan_status+0x139>
mov    %r15,%rdi
mov    $0x228,%esi
mov    %r14,%rdx
call   1040 <bbm_read@plt>
movzbl 0x6(%rsp),%eax
test   $0x1,%al
jne    11e5 <fc8080_scan_status+0xc5>
add    $0x1,%ebx
cmp    $0xffffffc5,%ebx
jne    11b0 <fc8080_scan_status+0x90>
jmp    1259 <fc8080_scan_status+0x139>
test   $0x2,%al
je     1259 <fc8080_scan_status+0x139>
lea    0x5(%rsp),%r14
xchg   %ax,%ax
cmp    $0xffffffd8,%ebx
je     1259 <fc8080_scan_status+0x139>
mov    $0x5,%edi
call   1030 <ms_wait@plt>
test   %eax,%eax
je     1259 <fc8080_scan_status+0x139>
mov    %r15,%rdi
mov    $0x238,%esi
mov    %r14,%rdx
call   1040 <bbm_read@plt>
add    $0x1,%ebx
testb  $0x1,0x5(%rsp)
je     11f0 <fc8080_scan_status+0xd0>
mov    %ebx,%eax
add    $0x62,%eax
jae    1259 <fc8080_scan_status+0x139>
neg    %ebx
lea    0x5(%rsp),%r14
mov    $0x5,%edi
call   1030 <ms_wait@plt>
test   %eax,%eax
je     1259 <fc8080_scan_status+0x139>
mov    %r15,%rdi
mov    $0x238,%esi
mov    %r14,%rdx
call   1040 <bbm_read@plt>
testb  $0x20,0x5(%rsp)
jne    1257 <fc8080_scan_status+0x137>
add    $0xffffffff,%ebx
jne    122b <fc8080_scan_status+0x10b>
jmp    1259 <fc8080_scan_status+0x139>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @fc8080_scan_status(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
%7 = alloca i8
%8 = alloca i8
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %3
store i32 1, i32* %5
store i8 0, i8* %6
store i8 0, i8* %7
store i8 0, i8* %8
%12 = load i8*, i8** %3
%13 = call i32 @bbm_read(i8* %12, i32 544, i8* %6)
%14 = load i8, i8* %6
%15 = zext i8 %14 to i32
%16 = and i32 %15, 1
%17 = icmp eq i32 %16, 1
br i1 %17, label %18, label %102
18: ; preds = %1
store i32 40, i32* %10
store i32 60, i32* %11
store i32 100, i32* %9
store i32 0, i32* %4
br label %19
19: ; preds = %36, %18
%20 = load i32, i32* %4
%21 = load i32, i32* %10
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %39
23: ; preds = %19
%24 = call i32 @ms_wait(i32 5)
%25 = icmp ne i32 %24, 0
br i1 %25, label %27, label %26
26: ; preds = %23
store i32 1, i32* %2
br label %126
27: ; preds = %23
%28 = load i8*, i8** %3
%29 = call i32 @bbm_read(i8* %28, i32 552, i8* %7)
%30 = load i8, i8* %7
%31 = zext i8 %30 to i32
%32 = and i32 %31, 1
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %27
br label %39
35: ; preds = %27
br label %36
36: ; preds = %35
%37 = load i32, i32* %4
%38 = add nsw i32 %37, 1
store i32 %38, i32* %4
br label %19
39: ; preds = %34, %19
%40 = load i32, i32* %4
%41 = load i32, i32* %10
%42 = icmp eq i32 %40, %41
br i1 %42, label %43, label %44
43: ; preds = %39
store i32 1, i32* %2
br label %126
44: ; preds = %39
%45 = load i8, i8* %7
%46 = zext i8 %45 to i32
%47 = and i32 %46, 2
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %44
store i32 1, i32* %2
br label %126
50: ; preds = %44
%51 = load i32, i32* %4
%52 = add nsw i32 %51, 1
store i32 %52, i32* %4
br label %53
53: ; preds = %70, %50
%54 = load i32, i32* %4
%55 = load i32, i32* %11
%56 = icmp slt i32 %54, %55
br i1 %56, label %57, label %73
57: ; preds = %53
%58 = call i32 @ms_wait(i32 5)
%59 = icmp ne i32 %58, 0
br i1 %59, label %61, label %60
60: ; preds = %57
store i32 1, i32* %2
br label %126
61: ; preds = %57
%62 = load i8*, i8** %3
%63 = call i32 @bbm_read(i8* %62, i32 568, i8* %8)
%64 = load i8, i8* %8
%65 = zext i8 %64 to i32
%66 = and i32 %65, 1
%67 = icmp ne i32 %66, 0
br i1 %67, label %68, label %69
68: ; preds = %61
br label %73
69: ; preds = %61
br label %70
70: ; preds = %69
%71 = load i32, i32* %4
%72 = add nsw i32 %71, 1
store i32 %72, i32* %4
br label %53
73: ; preds = %68, %53
%74 = load i32, i32* %4
%75 = load i32, i32* %11
%76 = icmp eq i32 %74, %75
br i1 %76, label %77, label %78
77: ; preds = %73
store i32 1, i32* %2
br label %126
78: ; preds = %73
%79 = load i32, i32* %4
%80 = add nsw i32 %79, 1
store i32 %80, i32* %4
br label %81
81: ; preds = %98, %78
%82 = load i32, i32* %4
%83 = load i32, i32* %9
%84 = icmp slt i32 %82, %83
br i1 %84, label %85, label %101
85: ; preds = %81
%86 = call i32 @ms_wait(i32 5)
%87 = icmp ne i32 %86, 0
br i1 %87, label %89, label %88
88: ; preds = %85
store i32 1, i32* %2
br label %126
89: ; preds = %85
%90 = load i8*, i8** %3
%91 = call i32 @bbm_read(i8* %90, i32 568, i8* %8)
%92 = load i8, i8* %8
%93 = zext i8 %92 to i32
%94 = and i32 %93, 32
%95 = icmp ne i32 %94, 0
br i1 %95, label %96, label %97
96: ; preds = %89
store i32 0, i32* %2
br label %126
97: ; preds = %89
br label %98
98: ; preds = %97
%99 = load i32, i32* %4
%100 = add nsw i32 %99, 1
store i32 %100, i32* %4
br label %81
101: ; preds = %81
br label %124
102: ; preds = %1
store i32 100, i32* %9
store i32 0, i32* %4
br label %103
103: ; preds = %120, %102
%104 = load i32, i32* %4
%105 = load i32, i32* %9
%106 = icmp slt i32 %104, %105
br i1 %106, label %107, label %123
107: ; preds = %103
%108 = call i32 @ms_wait(i32 5)
%109 = icmp ne i32 %108, 0
br i1 %109, label %111, label %110
110: ; preds = %107
store i32 1, i32* %2
br label %126
111: ; preds = %107
%112 = load i8*, i8** %3
%113 = call i32 @bbm_read(i8* %112, i32 568, i8* %8)
%114 = load i8, i8* %8
%115 = zext i8 %114 to i32
%116 = and i32 %115, 32
%117 = icmp ne i32 %116, 0
br i1 %117, label %118, label %119
118: ; preds = %111
store i32 0, i32* %2
br label %126
119: ; preds = %111
br label %120
120: ; preds = %119
%121 = load i32, i32* %4
%122 = add nsw i32 %121, 1
store i32 %122, i32* %4
br label %103
123: ; preds = %103
br label %124
124: ; preds = %123, %101
%125 = load i32, i32* %5
store i32 %125, i32* %2
br label %126
126: ; preds = %124, %118, %110, %96, %88, %77, %60, %49, %43, %26
%127 = load i32, i32* %2
ret i32 %127
}

LLVM IR (-O2):
define i32 @fc8080_scan_status(i8* %0) {
%2 = alloca i8
%3 = alloca i8
%4 = alloca i8
store i8 0, i8* %2
store i8 0, i8* %3
store i8 0, i8* %4
%5 = call i32 @bbm_read(i8* %0, i32 544, i8* %2)
%6 = load i8, i8* %2
%7 = and i8 %6, 1
%8 = icmp eq i8 %7, 0
br i1 %8, label %55, label %9
9: ; preds = %1, %18
%10 = phi i32 [ %19, %18 ], [ 0, %1 ]
%11 = call i32 @ms_wait(i32 5)
%12 = icmp eq i32 %11, 0
br i1 %12, label %64, label %13
13: ; preds = %9
%14 = call i32 @bbm_read(i8* %0, i32 552, i8* %3)
%15 = load i8, i8* %3
%16 = and i8 %15, 1
%17 = icmp eq i8 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = add nuw nsw i32 %10, 1
%20 = icmp eq i32 %19, 40
br i1 %20, label %64, label %9
21: ; preds = %13
%22 = and i8 %15, 2
%23 = icmp eq i8 %22, 0
br i1 %23, label %64, label %24
24: ; preds = %21, %31
%25 = phi i32 [ %26, %31 ], [ %10, %21 ]
%26 = add nuw nsw i32 %25, 1
%27 = icmp eq i32 %25, 59
br i1 %27, label %64, label %28
28: ; preds = %24
%29 = call i32 @ms_wait(i32 5)
%30 = icmp eq i32 %29, 0
br i1 %30, label %64, label %31
31: ; preds = %28
%32 = call i32 @bbm_read(i8* %0, i32 568, i8* %4)
%33 = load i8, i8* %4
%34 = and i8 %33, 1
%35 = icmp eq i8 %34, 0
br i1 %35, label %24, label %36
36: ; preds = %31
%37 = icmp ult i32 %25, 98
br i1 %37, label %38, label %64
38: ; preds = %36
%39 = add nuw nsw i32 %25, 2
br label %43
40: ; preds = %47
%41 = add nuw nsw i32 %44, 1
%42 = icmp eq i32 %41, 100
br i1 %42, label %64, label %43
43: ; preds = %38, %40
%44 = phi i32 [ %41, %40 ], [ %39, %38 ]
%45 = call i32 @ms_wait(i32 5)
%46 = icmp eq i32 %45, 0
br i1 %46, label %64, label %47
47: ; preds = %43
%48 = call i32 @bbm_read(i8* %0, i32 568, i8* %4)
%49 = load i8, i8* %4
%50 = and i8 %49, 32
%51 = icmp eq i8 %50, 0
br i1 %51, label %40, label %64
52: ; preds = %59
%53 = add nuw nsw i32 %56, 1
%54 = icmp eq i32 %53, 100
br i1 %54, label %64, label %55
55: ; preds = %1, %52
%56 = phi i32 [ %53, %52 ], [ 0, %1 ]
%57 = call i32 @ms_wait(i32 5)
%58 = icmp eq i32 %57, 0
br i1 %58, label %64, label %59
59: ; preds = %55
%60 = call i32 @bbm_read(i8* %0, i32 568, i8* %4)
%61 = load i8, i8* %4
%62 = and i8 %61, 32
%63 = icmp eq i8 %62, 0
br i1 %63, label %52, label %64
64: ; preds = %9, %18, %28, %24, %40, %43, %47, %52, %59, %55, %36, %21
%65 = phi i32 [ 1, %21 ], [ 1, %36 ], [ 1, %52 ], [ 0, %59 ], [ 1, %55 ], [ 1, %40 ], [ 1, %43 ], [ 0, %47 ], [ 1, %24 ], [ 1, %28 ], [ 1, %18 ], [ 1, %9 ]
ret i32 %65
}