#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 */
unsigned char inb(unsigned int);
int /*<<< orphan*/ smbus_delay();
int smbus_wait(unsigned smbus_io_base) {
  unsigned long loops = (1000);
  unsigned char val;
  do {
    smbus_delay();
    val = inb(smbus_io_base + 0x01);
    if ((val & (0x01 << 6)) != 0)
      break;
    if (val & ((0x01 << 5) | (0x01 << 4))) {
      return -1;
    }
  } while (--loops);
  return loops ? 0 : -2;
}


x86 assembly (clang, -O0):
<smbus_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movq   $0x3e8,-0x10(%rbp)
mov    $0x0,%al
call   1040 <smbus_delay@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
call   1030 <inb@plt>
mov    %al,-0x11(%rbp)
movzbl -0x11(%rbp),%eax
and    $0x40,%eax
cmp    $0x0,%eax
je     115d <smbus_wait+0x3d>
jmp    1194 <smbus_wait+0x74>
movzbl -0x11(%rbp),%eax
and    $0x30,%eax
cmp    $0x0,%eax
je     1179 <smbus_wait+0x59>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a9 <smbus_wait+0x89>
jmp    117e <smbus_wait+0x5e>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1133 <smbus_wait+0x13>
mov    -0x10(%rbp),%rdx
mov    $0xfffffffe,%eax
xor    %ecx,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smbus_wait>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%r14d
add    $0x1,%r14d
mov    $0xfffffffffffffc18,%rbx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %eax,%eax
call   1040 <smbus_delay@plt>
mov    %r14d,%edi
call   1030 <inb@plt>
test   $0x40,%al
jne    1168 <smbus_wait+0x48>
test   $0x30,%al
jne    1163 <smbus_wait+0x43>
inc    %rbx
jne    1140 <smbus_wait+0x20>
mov    $0xfffffffe,%ebp
jmp    1168 <smbus_wait+0x48>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @smbus_wait(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
%5 = alloca i8
store i32 %0, i32* %3
store i64 1000, i64* %4
br label %6
6: ; preds = %23, %1
%7 = call i32 (...) @smbus_delay()
%8 = load i32, i32* %3
%9 = add i32 %8, 1
%10 = call zeroext i8 @inb(i32 %9)
store i8 %10, i8* %5
%11 = load i8, i8* %5
%12 = zext i8 %11 to i32
%13 = and i32 %12, 64
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %6
br label %27
16: ; preds = %6
%17 = load i8, i8* %5
%18 = zext i8 %17 to i32
%19 = and i32 %18, 48
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %16
store i32 -1, i32* %2
br label %32
22: ; preds = %16
br label %23
23: ; preds = %22
%24 = load i64, i64* %4
%25 = add i64 %24, -1
store i64 %25, i64* %4
%26 = icmp ne i64 %25, 0
br i1 %26, label %6, label %27
27: ; preds = %23, %15
%28 = load i64, i64* %4
%29 = icmp ne i64 %28, 0
%30 = zext i1 %29 to i64
%31 = select i1 %29, i32 0, i32 -2
store i32 %31, i32* %2
br label %32
32: ; preds = %27, %21
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
define i32 @smbus_wait(i32 %0) {
%2 = add i32 %0, 1
br label %3
3: ; preds = %13, %1
%4 = phi i64 [ 1000, %1 ], [ %14, %13 ]
%5 = tail call i32 (...) @smbus_delay()
%6 = tail call zeroext i8 @inb(i32 %2)
%7 = zext i8 %6 to i32
%8 = and i32 %7, 64
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %16
10: ; preds = %3
%11 = and i32 %7, 48
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = add nsw i64 %4, -1
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %3
16: ; preds = %3, %13, %10
%17 = phi i32 [ -1, %10 ], [ 0, %3 ], [ -2, %13 ]
ret i32 %17
}