#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*/ iopl(int);
int /*<<< orphan*/ outb(int, int);
int /*<<< orphan*/ usleep(int);
int speaker(unsigned int freq, unsigned int delay) {
  static int flag = 0;
  static int bit;
  if (flag == 0) {
    flag = 1;
    iopl(3);
  }
  outb(0xb6, 0x43);
  outb((freq & 0xff), 0x42);
  outb((freq >> 8), 0x42);
  bit = inb(0x61);
  outb(3 | bit, 0x61);
  usleep(10000 * delay);
  outb(0xfc | bit, 0x61);
}


x86 assembly (clang, -O0):
<speaker>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,0x2eef(%rip)
jne    116f <speaker+0x2f>
movl   $0x1,0x2edf(%rip)
mov    $0x3,%edi
call   1030 <iopl@plt>
mov    $0xb6,%edi
mov    $0x43,%esi
call   1040 <outb@plt>
mov    -0x8(%rbp),%edi
and    $0xff,%edi
mov    $0x42,%esi
call   1040 <outb@plt>
mov    -0x8(%rbp),%edi
shr    $0x8,%edi
mov    $0x42,%esi
call   1040 <outb@plt>
mov    $0x61,%edi
call   1050 <inb@plt>
mov    %eax,0x2e97(%rip)
mov    0x2e91(%rip),%edi
or     $0x3,%edi
mov    $0x61,%esi
call   1040 <outb@plt>
imul   $0x2710,-0xc(%rbp),%edi
call   1060 <usleep@plt>
mov    0x2e72(%rip),%edi
or     $0xfc,%edi
mov    $0x61,%esi
call   1040 <outb@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<speaker>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
cmpb   $0x0,0x2ef6(%rip)
jne    1161 <speaker+0x21>
movb   $0x1,0x2eed(%rip)
mov    $0x3,%edi
call   1030 <iopl@plt>
mov    $0xb6,%edi
mov    $0x43,%esi
call   1040 <outb@plt>
movzbl %bpl,%edi
mov    $0x42,%esi
call   1040 <outb@plt>
shr    $0x8,%ebp
mov    %ebp,%edi
mov    $0x42,%esi
call   1040 <outb@plt>
mov    $0x61,%edi
call   1050 <inb@plt>
mov    %eax,0x2eab(%rip)
or     $0x3,%eax
mov    %eax,%edi
mov    $0x61,%esi
call   1040 <outb@plt>
imul   $0x2710,%ebx,%edi
call   1060 <usleep@plt>
mov    $0xfc,%edi
or     0x2e86(%rip),%edi
mov    $0x61,%esi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <outb@plt>

LLVM IR (-O0):
@speaker.flag = internal global i32 0
@speaker.bit = internal global i32 0
define i32 @speaker(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%6 = load i32, i32* @speaker.flag
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
store i32 1, i32* @speaker.flag
%9 = call i32 @iopl(i32 3)
br label %10
10: ; preds = %8, %2
%11 = call i32 @outb(i32 182, i32 67)
%12 = load i32, i32* %4
%13 = and i32 %12, 255
%14 = call i32 @outb(i32 %13, i32 66)
%15 = load i32, i32* %4
%16 = lshr i32 %15, 8
%17 = call i32 @outb(i32 %16, i32 66)
%18 = call i32 @inb(i32 97)
store i32 %18, i32* @speaker.bit
%19 = load i32, i32* @speaker.bit
%20 = or i32 3, %19
%21 = call i32 @outb(i32 %20, i32 97)
%22 = load i32, i32* %5
%23 = mul i32 10000, %22
%24 = call i32 @usleep(i32 %23)
%25 = load i32, i32* @speaker.bit
%26 = or i32 252, %25
%27 = call i32 @outb(i32 %26, i32 97)
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
@speaker.flag = internal unnamed_addr global i1 false
@speaker.bit = internal unnamed_addr global i32 0
define i32 @speaker(i32 %0, i32 %1) {
%3 = load i1, i1* @speaker.flag
br i1 %3, label %6, label %4
4: ; preds = %2
store i1 true, i1* @speaker.flag
%5 = tail call i32 @iopl(i32 3)
br label %6
6: ; preds = %4, %2
%7 = tail call i32 @outb(i32 182, i32 67)
%8 = and i32 %0, 255
%9 = tail call i32 @outb(i32 %8, i32 66)
%10 = lshr i32 %0, 8
%11 = tail call i32 @outb(i32 %10, i32 66)
%12 = tail call i32 @inb(i32 97)
store i32 %12, i32* @speaker.bit
%13 = or i32 %12, 3
%14 = tail call i32 @outb(i32 %13, i32 97)
%15 = mul i32 %1, 10000
%16 = tail call i32 @usleep(i32 %15)
%17 = load i32, i32* @speaker.bit
%18 = or i32 %17, 252
%19 = tail call i32 @outb(i32 %18, i32 97)
ret i32 undef
}