#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 SHRT_MAX;
int SHRT_MIN;
int /*<<< orphan*/ fwrite(short *, int, int, int /*<<< orphan*/);
int sin(int);
int /*<<< orphan*/ stdout;
int strtol(char *, int /*<<< orphan*/ *, int);
int main(int argc, char **argv) {
  int hz = 441.0;
  int r = 0;
  int n;
  short sample[2];
  if (argc > 1)
    hz = strtol(argv[1], NULL, 10);
  for (;;) {
    n = sin(hz * 2 * 3.141592653549793238462643383279 * r / 44100) * 32768 *
        0.8;
    if (n > SHRT_MAX)
      n = SHRT_MAX;
    if (n < SHRT_MIN)
      n = SHRT_MIN;
    sample[0] = n;
    sample[1] = n;
    fwrite(sample, sizeof(short), 2, stdout);
    r++;
    if (r > 44100)
      r = 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1b9,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1177 <main+0x47>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %eax,-0x14(%rbp)
jmp    117c <main+0x4c>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
cvtsi2sd %eax,%xmm0
movsd  0xe82(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvtsi2sdl -0x18(%rbp),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xe65(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edi
call   1030 <sin@plt>
shl    $0xf,%eax
cvtsi2sd %eax,%xmm0
movsd  0xe41(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2e04(%rip),%rcx
cmp    (%rcx),%eax
jle    11e8 <main+0xb8>
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2dee(%rip),%rcx
cmp    (%rcx),%eax
jge    1206 <main+0xd6>
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %ax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
mov    %ax,-0x1e(%rbp)
lea    -0x20(%rbp),%rdi
mov    0x2db1(%rip),%rax
mov    (%rax),%ecx
mov    $0x2,%edx
mov    %edx,%esi
call   1050 <fwrite@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0xac44,-0x18(%rbp)
jle    124a <main+0x11a>
movl   $0x0,-0x18(%rbp)
jmp    117c <main+0x4c>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jl     1163 <main+0x33>
mov    0x8(%rsi),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1040 <strtol@plt>
add    %eax,%eax
cvtsi2sd %eax,%xmm1
mulsd  0xea7(%rip),%xmm1
jmp    116b <main+0x3b>
movsd  0xe95(%rip),%xmm1
xor    %r14d,%r14d
mov    0x2e63(%rip),%r15
mov    0x2e64(%rip),%r12
mov    0x2e4d(%rip),%r13
lea    0xc(%rsp),%rbx
xor    %ebp,%ebp
movsd  %xmm1,0x10(%rsp)
xorps  %xmm0,%xmm0
cvtsi2sd %ebp,%xmm0
mulsd  %xmm1,%xmm0
divsd  0xe6d(%rip),%xmm0
cvttsd2si %xmm0,%edi
call   1030 <sin@plt>
shl    $0xf,%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
mulsd  0xe5a(%rip),%xmm0
cvttsd2si %xmm0,%eax
mov    (%r15),%ecx
cmp    %eax,%ecx
cmovl  %ecx,%eax
mov    (%r12),%ecx
cmp    %ecx,%eax
cmovl  %ecx,%eax
mov    %ax,0xc(%rsp)
mov    %ax,0xe(%rsp)
mov    0x0(%r13),%ecx
mov    %rbx,%rdi
mov    $0x2,%esi
mov    $0x2,%edx
call   1050 <fwrite@plt>
movsd  0x10(%rsp),%xmm1
cmp    $0xac44,%ebp
lea    0x1(%rbp),%eax
cmovge %r14d,%eax
mov    %eax,%ebp
jmp    1190 <main+0x60>

LLVM IR (-O0):
@SHRT_MAX = global i32 0
@SHRT_MIN = global i32 0
@stdout = 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 i32
%9 = alloca [2 x i16]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 441, i32* %6
store i32 0, i32* %7
%10 = load i32, i32* %4
%11 = icmp sgt i32 %10, 1
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call i32 @strtol(i8* %15, i32* null, i32 10)
store i32 %16, i32* %6
br label %17
17: ; preds = %12, %2
br label %18
18: ; preds = %59, %17
%19 = load i32, i32* %6
%20 = mul nsw i32 %19, 2
%21 = sitofp i32 %20 to double
%22 = fmul double %21, 0x400921FB5442CD40
%23 = load i32, i32* %7
%24 = sitofp i32 %23 to double
%25 = fmul double %22, %24
%26 = fdiv double %25, 4.410000e+04
%27 = fptosi double %26 to i32
%28 = call i32 @sin(i32 %27)
%29 = mul nsw i32 %28, 32768
%30 = sitofp i32 %29 to double
%31 = fmul double %30, 8.000000e-01
%32 = fptosi double %31 to i32
store i32 %32, i32* %8
%33 = load i32, i32* %8
%34 = load i32, i32* @SHRT_MAX
%35 = icmp sgt i32 %33, %34
br i1 %35, label %36, label %38
36: ; preds = %18
%37 = load i32, i32* @SHRT_MAX
store i32 %37, i32* %8
br label %38
38: ; preds = %36, %18
%39 = load i32, i32* %8
%40 = load i32, i32* @SHRT_MIN
%41 = icmp slt i32 %39, %40
br i1 %41, label %42, label %44
42: ; preds = %38
%43 = load i32, i32* @SHRT_MIN
store i32 %43, i32* %8
br label %44
44: ; preds = %42, %38
%45 = load i32, i32* %8
%46 = trunc i32 %45 to i16
%47 = getelementptr inbounds [2 x i16], [2 x i16]* %9, i64 0, i64 0
store i16 %46, i16* %47
%48 = load i32, i32* %8
%49 = trunc i32 %48 to i16
%50 = getelementptr inbounds [2 x i16], [2 x i16]* %9, i64 0, i64 1
store i16 %49, i16* %50
%51 = getelementptr inbounds [2 x i16], [2 x i16]* %9, i64 0, i64 0
%52 = load i32, i32* @stdout
%53 = call i32 @fwrite(i16* %51, i32 2, i32 2, i32 %52)
%54 = load i32, i32* %7
%55 = add nsw i32 %54, 1
store i32 %55, i32* %7
%56 = load i32, i32* %7
%57 = icmp sgt i32 %56, 44100
br i1 %57, label %58, label %59
58: ; preds = %44
store i32 0, i32* %7
br label %59
59: ; preds = %58, %44
br label %18
}

LLVM IR (-O2):
@SHRT_MAX = global i32 0
@SHRT_MIN = global i32 0
@stdout = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x i16]
%4 = bitcast [2 x i16]* %3 to i8*
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %13
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @strtol(i8* nocapture %8, i32* null, i32 10)
%10 = shl i32 %9, 1
%11 = sitofp i32 %10 to double
%12 = fmul double %11, 0x400921FB5442CD40
br label %13
13: ; preds = %6, %2
%14 = phi double [ %12, %6 ], [ 0x40A5A5C4FA1389CA, %2 ]
%15 = getelementptr inbounds [2 x i16], [2 x i16]* %3, i64 0, i64 0
%16 = getelementptr inbounds [2 x i16], [2 x i16]* %3, i64 0, i64 1
br label %17
17: ; preds = %17, %13
%18 = phi i32 [ 0, %13 ], [ %39, %17 ]
%19 = sitofp i32 %18 to double
%20 = fmul double %14, %19
%21 = fdiv double %20, 4.410000e+04
%22 = fptosi double %21 to i32
%23 = call i32 @sin(i32 %22)
%24 = shl nsw i32 %23, 15
%25 = sitofp i32 %24 to double
%26 = fmul double %25, 8.000000e-01
%27 = fptosi double %26 to i32
%28 = load i32, i32* @SHRT_MAX
%29 = icmp slt i32 %28, %27
%30 = select i1 %29, i32 %28, i32 %27
%31 = load i32, i32* @SHRT_MIN
%32 = icmp slt i32 %30, %31
%33 = select i1 %32, i32 %31, i32 %30
%34 = trunc i32 %33 to i16
store i16 %34, i16* %15
store i16 %34, i16* %16
%35 = load i32, i32* @stdout
%36 = call i32 @fwrite(i16* %15, i32 2, i32 2, i32 %35)
%37 = add nsw i32 %18, 1
%38 = icmp sgt i32 %18, 44099
%39 = select i1 %38, i32 0, i32 %37
br label %17
}