#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 get_ticks();
int in_byte(int);
int waitfor(int mask, int val, int timeout) {
  int t = get_ticks();
  while (((get_ticks() - t) * 1000 / 100) < timeout)
    if ((in_byte(0x1F7) & mask) == val)
      return 1;
  return 0;
}


x86 assembly (clang, -O0):
<waitfor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    $0x0,%al
call   1040 <get_ticks@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1040 <get_ticks@plt>
sub    -0x14(%rbp),%eax
imul   $0x3e8,%eax,%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
cmp    -0x10(%rbp),%eax
jge    1183 <waitfor+0x63>
mov    $0x1f7,%edi
call   1030 <in_byte@plt>
and    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jne    117e <waitfor+0x5e>
movl   $0x1,-0x4(%rbp)
jmp    118a <waitfor+0x6a>
jmp    113b <waitfor+0x1b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waitfor>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r12d
mov    %esi,%r15d
mov    %edi,%ebx
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <get_ticks@plt>
mov    %eax,%ebp
nopl   0x0(%rax)
xor    %eax,%eax
call   1040 <get_ticks@plt>
sub    %ebp,%eax
add    %eax,%eax
lea    (%rax,%rax,4),%eax
cmp    %r12d,%eax
jge    116a <waitfor+0x4a>
mov    $0x1f7,%edi
call   1030 <in_byte@plt>
and    %ebx,%eax
cmp    %r15d,%eax
jne    1140 <waitfor+0x20>
mov    $0x1,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @waitfor(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%9 = call i32 (...) @get_ticks()
store i32 %9, i32* %8
br label %10
10: ; preds = %25, %3
%11 = call i32 (...) @get_ticks()
%12 = load i32, i32* %8
%13 = sub nsw i32 %11, %12
%14 = mul nsw i32 %13, 1000
%15 = sdiv i32 %14, 100
%16 = load i32, i32* %7
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %26
18: ; preds = %10
%19 = call i32 @in_byte(i32 503)
%20 = load i32, i32* %5
%21 = and i32 %19, %20
%22 = load i32, i32* %6
%23 = icmp eq i32 %21, %22
br i1 %23, label %24, label %25
24: ; preds = %18
store i32 1, i32* %4
br label %27
25: ; preds = %18
br label %10
26: ; preds = %10
store i32 0, i32* %4
br label %27
27: ; preds = %26, %24
%28 = load i32, i32* %4
ret i32 %28
}

LLVM IR (-O2):
define i32 @waitfor(i32 %0, i32 %1, i32 %2) {
%4 = tail call i32 (...) @get_ticks()
br label %5
5: ; preds = %10, %3
%6 = tail call i32 (...) @get_ticks()
%7 = sub nsw i32 %6, %4
%8 = mul nsw i32 %7, 10
%9 = icmp slt i32 %8, %2
br i1 %9, label %10, label %14
10: ; preds = %5
%11 = tail call i32 @in_byte(i32 503)
%12 = and i32 %11, %0
%13 = icmp eq i32 %12, %1
br i1 %13, label %14, label %5
14: ; preds = %5, %10
%15 = phi i32 [ 1, %10 ], [ 0, %5 ]
ret i32 %15
}