#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 inb(int);
int /*<<< orphan*/ outb(int, int);
void wait(int ms) {
  ms *= 1193;
  outb(0x43, 0x34);
  outb(0x40, 0);
  outb(0x40, 0);
  unsigned short state;
  unsigned short old = 0;
  while (ms > 0) {
    outb(0x43, 0);
    state = inb(0x40);
    state |= inb(0x40) << 8;
    ms -= (unsigned short)(old - state);
    old = state;
  }
}


x86 assembly (clang, -O0):
<wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
imul   $0x4a9,-0x4(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    $0x43,%edi
mov    $0x34,%esi
call   1030 <outb@plt>
mov    $0x40,%edi
xor    %esi,%esi
call   1030 <outb@plt>
mov    $0x40,%edi
xor    %esi,%esi
call   1030 <outb@plt>
movw   $0x0,-0x8(%rbp)
cmpl   $0x0,-0x4(%rbp)
jle    11c1 <wait+0xa1>
mov    $0x43,%edi
xor    %esi,%esi
call   1030 <outb@plt>
mov    $0x40,%edi
call   1040 <inb@plt>
mov    %ax,-0x6(%rbp)
mov    $0x40,%edi
call   1040 <inb@plt>
mov    %eax,%ecx
shl    $0x8,%ecx
movzwl -0x6(%rbp),%eax
or     %ecx,%eax
mov    %ax,-0x6(%rbp)
movzwl -0x8(%rbp),%eax
movzwl -0x6(%rbp),%ecx
sub    %ecx,%eax
movzwl %ax,%ecx
mov    -0x4(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x6(%rbp),%ax
mov    %ax,-0x8(%rbp)
jmp    1162 <wait+0x42>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebx
mov    $0x43,%edi
mov    $0x34,%esi
call   1030 <outb@plt>
xor    %ebp,%ebp
mov    $0x40,%edi
xor    %esi,%esi
call   1030 <outb@plt>
mov    $0x40,%edi
xor    %esi,%esi
call   1030 <outb@plt>
test   %ebx,%ebx
jle    1194 <wait+0x74>
imul   $0x4a9,%ebx,%ebx
nopl   0x0(%rax)
mov    $0x43,%edi
xor    %esi,%esi
call   1030 <outb@plt>
mov    $0x40,%edi
call   1040 <inb@plt>
mov    %eax,%r14d
mov    $0x40,%edi
call   1040 <inb@plt>
shl    $0x8,%eax
or     %r14d,%eax
sub    %eax,%ebp
movzwl %bp,%ecx
mov    %eax,%ebp
sub    %ecx,%ebx
jg     1160 <wait+0x40>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define void @wait(i32 %0) {
%2 = alloca i32
%3 = alloca i16
%4 = alloca i16
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = mul nsw i32 %5, 1193
store i32 %6, i32* %2
%7 = call i32 @outb(i32 67, i32 52)
%8 = call i32 @outb(i32 64, i32 0)
%9 = call i32 @outb(i32 64, i32 0)
store i16 0, i16* %4
br label %10
10: ; preds = %13, %1
%11 = load i32, i32* %2
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %33
13: ; preds = %10
%14 = call i32 @outb(i32 67, i32 0)
%15 = call i32 @inb(i32 64)
%16 = trunc i32 %15 to i16
store i16 %16, i16* %3
%17 = call i32 @inb(i32 64)
%18 = shl i32 %17, 8
%19 = load i16, i16* %3
%20 = zext i16 %19 to i32
%21 = or i32 %20, %18
%22 = trunc i32 %21 to i16
store i16 %22, i16* %3
%23 = load i16, i16* %4
%24 = zext i16 %23 to i32
%25 = load i16, i16* %3
%26 = zext i16 %25 to i32
%27 = sub nsw i32 %24, %26
%28 = trunc i32 %27 to i16
%29 = zext i16 %28 to i32
%30 = load i32, i32* %2
%31 = sub nsw i32 %30, %29
store i32 %31, i32* %2
%32 = load i16, i16* %3
store i16 %32, i16* %4
br label %10
33: ; preds = %10
ret void
}

LLVM IR (-O2):
define void @wait(i32 %0) {
%2 = tail call i32 @outb(i32 67, i32 52)
%3 = tail call i32 @outb(i32 64, i32 0)
%4 = tail call i32 @outb(i32 64, i32 0)
%5 = icmp sgt i32 %0, 0
br i1 %5, label %6, label %20
6: ; preds = %1
%7 = mul nsw i32 %0, 1193
br label %8
8: ; preds = %6, %8
%9 = phi i32 [ %15, %8 ], [ 0, %6 ]
%10 = phi i32 [ %18, %8 ], [ %7, %6 ]
%11 = tail call i32 @outb(i32 67, i32 0)
%12 = tail call i32 @inb(i32 64)
%13 = tail call i32 @inb(i32 64)
%14 = shl i32 %13, 8
%15 = or i32 %14, %12
%16 = sub i32 %9, %15
%17 = and i32 %16, 65535
%18 = sub nsw i32 %10, %17
%19 = icmp sgt i32 %18, 0
br i1 %19, label %8, label %20
20: ; preds = %8, %1
ret void
}