#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, unsigned char);
int /*<<< orphan*/ timer_wait(int);
void beep(unsigned int wait_time, unsigned int times) {
  unsigned char tempA = inb(0x61);
  unsigned char tempB = (inb(0x61) & 0xFC);
  unsigned int count;
  for (count = 0; count < times; count++) {
    if (tempA != (tempA | 3)) {
      outb(0x61, tempA | 3);
    }
    timer_wait(wait_time);
    outb(0x61, tempB);
    timer_wait(1);
  }
}


x86 assembly (clang, -O0):
<beep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    $0x61,%edi
call   1040 <inb@plt>
mov    %al,-0x9(%rbp)
mov    $0x61,%edi
call   1040 <inb@plt>
and    $0xfc,%eax
mov    %al,-0xa(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
jae    11c5 <beep+0x95>
movzbl -0x9(%rbp),%eax
movzbl -0x9(%rbp),%ecx
or     $0x3,%ecx
cmp    %ecx,%eax
je     1197 <beep+0x67>
movzbl -0x9(%rbp),%eax
or     $0x3,%eax
mov    $0x61,%edi
movzbl %al,%esi
call   1030 <outb@plt>
mov    -0x4(%rbp),%edi
call   1050 <timer_wait@plt>
mov    $0x61,%edi
movzbl -0xa(%rbp),%esi
call   1030 <outb@plt>
mov    $0x1,%edi
call   1050 <timer_wait@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1164 <beep+0x34>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<beep>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%r14d
mov    $0x61,%edi
call   1040 <inb@plt>
mov    %eax,%ebp
mov    $0x61,%edi
call   1040 <inb@plt>
test   %ebx,%ebx
je     11a8 <beep+0x78>
and    $0xfc,%al
movzbl %bpl,%r12d
mov    %r12d,%ebp
or     $0x3,%ebp
movzbl %al,%r13d
movzbl %bpl,%r15d
jmp    1194 <beep+0x64>
mov    %r14d,%edi
call   1050 <timer_wait@plt>
mov    $0x61,%edi
mov    %r13d,%esi
call   1030 <outb@plt>
mov    $0x1,%edi
call   1050 <timer_wait@plt>
add    $0xffffffff,%ebx
je     11a8 <beep+0x78>
cmp    %ebp,%r12d
je     1170 <beep+0x40>
mov    $0x61,%edi
mov    %r15d,%esi
call   1030 <outb@plt>
jmp    1170 <beep+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @beep(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
%6 = alloca i8
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = call i32 @inb(i32 97)
%9 = trunc i32 %8 to i8
store i8 %9, i8* %5
%10 = call i32 @inb(i32 97)
%11 = and i32 %10, 252
%12 = trunc i32 %11 to i8
store i8 %12, i8* %6
store i32 0, i32* %7
br label %13
13: ; preds = %36, %2
%14 = load i32, i32* %7
%15 = load i32, i32* %4
%16 = icmp ult i32 %14, %15
br i1 %16, label %17, label %39
17: ; preds = %13
%18 = load i8, i8* %5
%19 = zext i8 %18 to i32
%20 = load i8, i8* %5
%21 = zext i8 %20 to i32
%22 = or i32 %21, 3
%23 = icmp ne i32 %19, %22
br i1 %23, label %24, label %30
24: ; preds = %17
%25 = load i8, i8* %5
%26 = zext i8 %25 to i32
%27 = or i32 %26, 3
%28 = trunc i32 %27 to i8
%29 = call i32 @outb(i32 97, i8 zeroext %28)
br label %30
30: ; preds = %24, %17
%31 = load i32, i32* %3
%32 = call i32 @timer_wait(i32 %31)
%33 = load i8, i8* %6
%34 = call i32 @outb(i32 97, i8 zeroext %33)
%35 = call i32 @timer_wait(i32 1)
br label %36
36: ; preds = %30
%37 = load i32, i32* %7
%38 = add i32 %37, 1
store i32 %38, i32* %7
br label %13
39: ; preds = %13
ret void
}

LLVM IR (-O2):
define void @beep(i32 %0, i32 %1) {
%3 = tail call i32 @inb(i32 97)
%4 = tail call i32 @inb(i32 97)
%5 = trunc i32 %4 to i8
%6 = and i8 %5, -4
%7 = icmp eq i32 %1, 0
br i1 %7, label %23, label %8
8: ; preds = %2
%9 = and i32 %3, 255
%10 = or i32 %9, 3
%11 = icmp eq i32 %9, %10
%12 = trunc i32 %10 to i8
br label %13
13: ; preds = %8, %17
%14 = phi i32 [ 0, %8 ], [ %21, %17 ]
br i1 %11, label %17, label %15
15: ; preds = %13
%16 = tail call i32 @outb(i32 97, i8 zeroext %12)
br label %17
17: ; preds = %15, %13
%18 = tail call i32 @timer_wait(i32 %0)
%19 = tail call i32 @outb(i32 97, i8 zeroext %6)
%20 = tail call i32 @timer_wait(i32 1)
%21 = add nuw i32 %14, 1
%22 = icmp eq i32 %21, %1
br i1 %22, label %23, label %13
23: ; preds = %17, %2
ret void
}