#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 int i8254_gettick();
void i8254_delay(unsigned int n) {
  unsigned int cur_tick, initial_tick;
  int remaining;
  const unsigned long timer_rval = 1193182 / 100;
  initial_tick = i8254_gettick();
  remaining = (unsigned long long)n * 1193182 / 1000000;
  while (remaining > 1) {
    cur_tick = i8254_gettick();
    if (cur_tick > initial_tick)
      remaining -= timer_rval - (cur_tick - initial_tick);
    else
      remaining -= initial_tick - cur_tick;
    initial_tick = cur_tick;
  }
}


x86 assembly (clang, -O0):
<i8254_delay>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x2e9b,-0x18(%rbp)
mov    $0x0,%al
call   1030 <i8254_gettick@plt>
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%eax
imul   $0x1234de,%rax,%rax
mov    $0xf4240,%ecx
xor    %edx,%edx
div    %rcx
mov    %eax,-0x10(%rbp)
cmpl   $0x1,-0x10(%rbp)
jle    119c <i8254_delay+0x8c>
mov    $0x0,%al
call   1030 <i8254_gettick@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jbe    1183 <i8254_delay+0x73>
mov    -0x8(%rbp),%eax
sub    -0xc(%rbp),%eax
mov    %eax,%eax
mov    $0x2e9b,%ecx
sub    %rax,%rcx
movslq -0x10(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x10(%rbp)
jmp    1191 <i8254_delay+0x81>
mov    -0xc(%rbp),%ecx
sub    -0x8(%rbp),%ecx
mov    -0x10(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    1144 <i8254_delay+0x34>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i8254_delay>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
xor    %eax,%eax
call   1030 <i8254_gettick@plt>
mov    %eax,%ebp
mov    %ebx,%eax
imul   $0x1234de,%rax,%rax
movabs $0x431bde82d7b634db,%rcx
de 1b 43
mul    %rcx
mov    %rdx,%rbx
shr    $0x12,%rbx
cmp    $0x2,%ebx
jl     1161 <i8254_delay+0x51>
xor    %eax,%eax
call   1030 <i8254_gettick@plt>
mov    %eax,%ecx
sub    %ebp,%ecx
add    $0xffffd165,%ecx
mov    %eax,%edx
sub    %ebp,%edx
cmova  %ecx,%edx
add    %edx,%ebx
mov    %eax,%ebp
cmp    $0x1,%ebx
jg     1140 <i8254_delay+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define void @i8254_delay(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
store i32 %0, i32* %2
store i64 11931, i64* %6
%7 = call i32 (...) @i8254_gettick()
store i32 %7, i32* %4
%8 = load i32, i32* %2
%9 = zext i32 %8 to i64
%10 = mul i64 %9, 1193182
%11 = udiv i64 %10, 1000000
%12 = trunc i64 %11 to i32
store i32 %12, i32* %5
br label %13
13: ; preds = %37, %1
%14 = load i32, i32* %5
%15 = icmp sgt i32 %14, 1
br i1 %15, label %16, label %39
16: ; preds = %13
%17 = call i32 (...) @i8254_gettick()
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = load i32, i32* %4
%20 = icmp ugt i32 %18, %19
br i1 %20, label %21, label %31
21: ; preds = %16
%22 = load i32, i32* %3
%23 = load i32, i32* %4
%24 = sub i32 %22, %23
%25 = zext i32 %24 to i64
%26 = sub i64 11931, %25
%27 = load i32, i32* %5
%28 = sext i32 %27 to i64
%29 = sub i64 %28, %26
%30 = trunc i64 %29 to i32
store i32 %30, i32* %5
br label %37
31: ; preds = %16
%32 = load i32, i32* %4
%33 = load i32, i32* %3
%34 = sub i32 %32, %33
%35 = load i32, i32* %5
%36 = sub i32 %35, %34
store i32 %36, i32* %5
br label %37
37: ; preds = %31, %21
%38 = load i32, i32* %3
store i32 %38, i32* %4
br label %13
39: ; preds = %13
ret void
}

LLVM IR (-O2):
define void @i8254_delay(i32 %0) {
%2 = tail call i32 (...) @i8254_gettick()
%3 = zext i32 %0 to i64
%4 = mul nuw nsw i64 %3, 1193182
%5 = udiv i64 %4, 1000000
%6 = trunc i64 %5 to i32
%7 = icmp sgt i32 %6, 1
br i1 %7, label %8, label %18
8: ; preds = %1, %8
%9 = phi i32 [ %11, %8 ], [ %2, %1 ]
%10 = phi i32 [ %16, %8 ], [ %6, %1 ]
%11 = tail call i32 (...) @i8254_gettick()
%12 = icmp ugt i32 %11, %9
%13 = sub i32 %11, %9
%14 = add i32 %13, -11931
%15 = select i1 %12, i32 %14, i32 %13
%16 = add i32 %15, %10
%17 = icmp sgt i32 %16, 1
br i1 %17, label %8, label %18
18: ; preds = %8, %1
ret void
}