#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 */
struct timespec {
  int member_0;
  int tv_sec;
  int tv_nsec;
  int /*<<< orphan*/ member_1;
};

/* Variables and functions */
int /*<<< orphan*/ KIOCSOUND;
int /*<<< orphan*/ O_WRONLY;
int atoi(char *);
void *floor(int);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ nanosleep(struct timespec *, int /*<<< orphan*/ *);
int open(char *, int /*<<< orphan*/);
int pow(int, float);
int main(int argc, char *argv[]) {
  int console = open("/dev/tty0", O_WRONLY);
  int key = 40;
  struct timespec duration = {1, 0};
  struct timespec delay = {0, 0};
  if (argc >= 2) {
    key = atoi(argv[1]);
  }
  if (argc >= 3) {
    int temp_duration = atoi(argv[2]);
    duration.tv_sec = floor(temp_duration / 1000);
    temp_duration -= duration.tv_sec * 1000;
    duration.tv_nsec = temp_duration * 1000000;
  }
  if (argc >= 4) {
    int temp_delay = atoi(argv[3]);
    delay.tv_sec = floor(temp_delay / 1000);
    temp_delay -= delay.tv_sec * 1000;
    delay.tv_nsec = temp_delay * 1000000;
  }
  ioctl(console, KIOCSOUND,
        (int)(1193180 / (pow(2, (float)(key - 49) / 12) * 440)));
  nanosleep(&duration, NULL);
  ioctl(console, KIOCSOUND, 0);
  nanosleep(&delay, NULL);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
lea    0xe6e(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
movl   $0x28,-0x18(%rbp)
mov    0xe64(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe61(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
cmpl   $0x2,-0x8(%rbp)
jl     11e5 <main+0x75>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x3,-0x8(%rbp)
jl     122d <main+0xbd>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
call   1090 <floor@plt>
mov    %eax,-0x24(%rbp)
imul   $0x3e8,-0x24(%rbp),%ecx
mov    -0x3c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x3c(%rbp)
imul   $0xf4240,-0x3c(%rbp),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x4,-0x8(%rbp)
jl     1275 <main+0x105>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
call   1090 <floor@plt>
mov    %eax,-0x34(%rbp)
imul   $0x3e8,-0x34(%rbp),%ecx
mov    -0x40(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x40(%rbp)
imul   $0xf4240,-0x40(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    0x2d66(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%eax
sub    $0x31,%eax
cvtsi2ss %eax,%xmm0
movss  0xd67(%rip),%xmm1
divss  %xmm1,%xmm0
mov    $0x2,%edi
call   1070 <pow@plt>
mov    -0x48(%rbp),%edi
mov    -0x44(%rbp),%esi
imul   $0x1b8,%eax,%ecx
mov    $0x1234dc,%eax
cltd
idiv   %ecx
mov    %eax,%edx
call   1050 <ioctl@plt>
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <nanosleep@plt>
mov    -0x14(%rbp),%edi
mov    0x2d0f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <ioctl@plt>
lea    -0x38(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <nanosleep@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r15
mov    %edi,%ebp
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
lea    0xe89(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%r14d
movups 0xe86(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
cmp    $0x2,%ebp
jl     1249 <main+0xe9>
mov    0x8(%r15),%rdi
call   1070 <atoi@plt>
add    $0xffffffcf,%eax
xorps  %xmm0,%xmm0
cvtsi2ss %eax,%xmm0
cmp    $0x2,%ebp
je     1251 <main+0xf1>
movss  %xmm0,0xc(%rsp)
mov    0x10(%r15),%rdi
call   1070 <atoi@plt>
movslq %eax,%rbx
imul   $0x10624dd3,%rbx,%rdi
mov    %rdi,%rax
shr    $0x3f,%rax
sar    $0x26,%rdi
add    %eax,%edi
call   1080 <floor@plt>
mov    %eax,0x24(%rsp)
imul   $0xfffffc18,%eax,%eax
add    %eax,%ebx
imul   $0xf4240,%ebx,%eax
mov    %eax,0x28(%rsp)
cmp    $0x4,%ebp
jb     1241 <main+0xe1>
mov    0x18(%r15),%rdi
call   1070 <atoi@plt>
movslq %eax,%rbx
imul   $0x10624dd3,%rbx,%rdi
mov    %rdi,%rax
shr    $0x3f,%rax
sar    $0x26,%rdi
add    %eax,%edi
call   1080 <floor@plt>
mov    %eax,0x14(%rsp)
imul   $0xfffffc18,%eax,%eax
add    %eax,%ebx
imul   $0xf4240,%ebx,%eax
mov    %eax,0x18(%rsp)
movss  0xc(%rsp),%xmm0
jmp    1251 <main+0xf1>
movss  0xdaf(%rip),%xmm0
mov    0x2d90(%rip),%rbp
mov    0x0(%rbp),%ebx
divss  0xda1(%rip),%xmm0
mov    $0x2,%edi
call   1060 <pow@plt>
imul   $0x1b8,%eax,%ecx
mov    $0x1234dc,%eax
xor    %edx,%edx
idiv   %ecx
mov    %r14d,%edi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <ioctl@plt>
lea    0x20(%rsp),%rdi
xor    %esi,%esi
call   1030 <nanosleep@plt>
mov    0x0(%rbp),%esi
mov    %r14d,%edi
xor    %edx,%edx
call   1040 <ioctl@plt>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1030 <nanosleep@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32, i32, i32 }
@.str = [10 x i8] c"/dev/tty0\00"
@O_WRONLY = global i32 0
@__const.main.duration = %struct.timespec { i32 1, i32 0, i32 0, i32 0 }
@KIOCSOUND = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.timespec
%9 = alloca %struct.timespec
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @O_WRONLY
%13 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* %6
store i32 40, i32* %7
%14 = bitcast %struct.timespec* %8 to i8*
%15 = bitcast %struct.timespec* %9 to i8*
%16 = load i32, i32* %4
%17 = icmp sge i32 %16, 2
br i1 %17, label %18, label %23
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32 @atoi(i8* %21)
store i32 %22, i32* %7
br label %23
23: ; preds = %18, %2
%24 = load i32, i32* %4
%25 = icmp sge i32 %24, 3
br i1 %25, label %26, label %44
26: ; preds = %23
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = call i32 @atoi(i8* %29)
store i32 %30, i32* %10
%31 = load i32, i32* %10
%32 = sdiv i32 %31, 1000
%33 = call i8* @floor(i32 %32)
%34 = ptrtoint i8* %33 to i32
%35 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 1
store i32 %34, i32* %35
%36 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 1
%37 = load i32, i32* %36
%38 = mul nsw i32 %37, 1000
%39 = load i32, i32* %10
%40 = sub nsw i32 %39, %38
store i32 %40, i32* %10
%41 = load i32, i32* %10
%42 = mul nsw i32 %41, 1000000
%43 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 2
store i32 %42, i32* %43
br label %44
44: ; preds = %26, %23
%45 = load i32, i32* %4
%46 = icmp sge i32 %45, 4
br i1 %46, label %47, label %65
47: ; preds = %44
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 3
%50 = load i8*, i8** %49
%51 = call i32 @atoi(i8* %50)
store i32 %51, i32* %11
%52 = load i32, i32* %11
%53 = sdiv i32 %52, 1000
%54 = call i8* @floor(i32 %53)
%55 = ptrtoint i8* %54 to i32
%56 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 1
store i32 %55, i32* %56
%57 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 1
%58 = load i32, i32* %57
%59 = mul nsw i32 %58, 1000
%60 = load i32, i32* %11
%61 = sub nsw i32 %60, %59
store i32 %61, i32* %11
%62 = load i32, i32* %11
%63 = mul nsw i32 %62, 1000000
%64 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 2
store i32 %63, i32* %64
br label %65
65: ; preds = %47, %44
%66 = load i32, i32* %6
%67 = load i32, i32* @KIOCSOUND
%68 = load i32, i32* %7
%69 = sub nsw i32 %68, 49
%70 = sitofp i32 %69 to float
%71 = fdiv float %70, 1.200000e+01
%72 = call i32 @pow(i32 2, float %71)
%73 = mul nsw i32 %72, 440
%74 = sdiv i32 1193180, %73
%75 = call i32 @ioctl(i32 %66, i32 %67, i32 %74)
%76 = call i32 @nanosleep(%struct.timespec* %8, i32* null)
%77 = load i32, i32* %6
%78 = load i32, i32* @KIOCSOUND
%79 = call i32 @ioctl(i32 %77, i32 %78, i32 0)
%80 = call i32 @nanosleep(%struct.timespec* %9, i32* null)
ret i32 0
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32, i32, i32 }
@.str = [10 x i8] c"/dev/tty0\00"
@O_WRONLY = global i32 0
@__const.main.duration = %struct.timespec { i32 1, i32 0, i32 0, i32 0 }
@KIOCSOUND = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = load i32, i32* @O_WRONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = bitcast %struct.timespec* %3 to i8*
%8 = bitcast %struct.timespec* %4 to i8*
%9 = icmp sgt i32 %0, 1
br i1 %9, label %10, label %44
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32 @atoi(i8* %12)
%14 = add i32 %13, -49
%15 = sitofp i32 %14 to float
%16 = icmp eq i32 %0, 2
br i1 %16, label %44, label %17
17: ; preds = %10
%18 = getelementptr inbounds i8*, i8** %1, i64 2
%19 = load i8*, i8** %18
%20 = tail call i32 @atoi(i8* %19)
%21 = sdiv i32 %20, 1000
%22 = tail call i8* @floor(i32 %21)
%23 = ptrtoint i8* %22 to i64
%24 = trunc i64 %23 to i32
%25 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
store i32 %24, i32* %25
%26 = mul i32 %24, -1000
%27 = add i32 %26, %20
%28 = mul nsw i32 %27, 1000000
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 2
store i32 %28, i32* %29
%30 = icmp ugt i32 %0, 3
br i1 %30, label %31, label %44
31: ; preds = %17
%32 = getelementptr inbounds i8*, i8** %1, i64 3
%33 = load i8*, i8** %32
%34 = tail call i32 @atoi(i8* %33)
%35 = sdiv i32 %34, 1000
%36 = tail call i8* @floor(i32 %35)
%37 = ptrtoint i8* %36 to i64
%38 = trunc i64 %37 to i32
%39 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 1
store i32 %38, i32* %39
%40 = mul i32 %38, -1000
%41 = add i32 %40, %34
%42 = mul nsw i32 %41, 1000000
%43 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 2
store i32 %42, i32* %43
br label %44
44: ; preds = %2, %10, %31, %17
%45 = phi float [ %15, %31 ], [ %15, %17 ], [ %15, %10 ], [ -9.000000e+00, %2 ]
%46 = load i32, i32* @KIOCSOUND
%47 = fdiv float %45, 1.200000e+01
%48 = tail call i32 @pow(i32 2, float %47)
%49 = mul nsw i32 %48, 440
%50 = sdiv i32 1193180, %49
%51 = tail call i32 @ioctl(i32 %6, i32 %46, i32 %50)
%52 = call i32 @nanosleep(%struct.timespec* %3, i32* null)
%53 = load i32, i32* @KIOCSOUND
%54 = call i32 @ioctl(i32 %6, i32 %53, i32 0)
%55 = call i32 @nanosleep(%struct.timespec* %4, i32* null)
ret i32 0
}