#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 */
typedef int uint8_t;
typedef int uint32_t;

/* Variables and functions */
int inp(int);
void timer_wait(uint32_t usec_wait) {
  uint32_t cycles;
  uint8_t old_val;
  uint8_t cur_val;
  cycles = usec_wait / 15;
  old_val = inp(0x61) & 0x10;
  while (cycles--) {
    do {
      cur_val = inp(0x61) & 0x10;
    } while (cur_val != old_val);
    old_val = cur_val;
  }
}


x86 assembly (clang, -O0):
<timer_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    $0xf,%ecx
cltd
idiv   %ecx
mov    %eax,-0x8(%rbp)
mov    $0x61,%edi
call   1030 <inp@plt>
and    $0x10,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x8(%rbp)
cmp    $0x0,%eax
je     1179 <timer_wait+0x69>
jmp    1152 <timer_wait+0x42>
mov    $0x61,%edi
call   1030 <inp@plt>
and    $0x10,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jne    1152 <timer_wait+0x42>
mov    -0x10(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    1139 <timer_wait+0x29>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timer_wait>:
push   %r15
push   %r14
push   %rbx
mov    %edi,%r14d
mov    $0x61,%edi
call   1030 <inp@plt>
mov    %eax,%ebx
lea    0xe(%r14),%eax
cmp    $0x1d,%eax
jb     1166 <timer_wait+0x56>
movslq %r14d,%rax
imul   $0xffffffff88888889,%rax,%r15
shr    $0x20,%r15
add    %r14d,%r15d
mov    %r15d,%eax
shr    $0x1f,%eax
sar    $0x3,%r15d
add    %eax,%r15d
nopl   0x0(%rax,%rax,1)
mov    $0x61,%edi
call   1030 <inp@plt>
xor    %ebx,%eax
test   $0x10,%al
jne    1150 <timer_wait+0x40>
add    $0xffffffff,%r15d
jne    1150 <timer_wait+0x40>
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @timer_wait(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = sdiv i32 %6, 15
store i32 %7, i32* %3
%8 = call i32 @inp(i32 97)
%9 = and i32 %8, 16
store i32 %9, i32* %4
br label %10
10: ; preds = %22, %1
%11 = load i32, i32* %3
%12 = add nsw i32 %11, -1
store i32 %12, i32* %3
%13 = icmp ne i32 %11, 0
br i1 %13, label %14, label %24
14: ; preds = %10
br label %15
15: ; preds = %18, %14
%16 = call i32 @inp(i32 97)
%17 = and i32 %16, 16
store i32 %17, i32* %5
br label %18
18: ; preds = %15
%19 = load i32, i32* %5
%20 = load i32, i32* %4
%21 = icmp ne i32 %19, %20
br i1 %21, label %15, label %22
22: ; preds = %18
%23 = load i32, i32* %5
store i32 %23, i32* %4
br label %10
24: ; preds = %10
ret void
}

LLVM IR (-O2):
define void @timer_wait(i32 %0) {
%2 = tail call i32 @inp(i32 97)
%3 = add i32 %0, 14
%4 = icmp ult i32 %3, 29
br i1 %4, label %17, label %5
5: ; preds = %1
%6 = sdiv i32 %0, 15
br label %10
7: ; preds = %12
%8 = add nsw i32 %11, -1
%9 = icmp eq i32 %8, 0
br i1 %9, label %17, label %10
10: ; preds = %5, %7
%11 = phi i32 [ %8, %7 ], [ %6, %5 ]
br label %12
12: ; preds = %10, %12
%13 = tail call i32 @inp(i32 97)
%14 = xor i32 %13, %2
%15 = and i32 %14, 16
%16 = icmp eq i32 %15, 0
br i1 %16, label %7, label %12
17: ; preds = %7, %1
ret void
}