#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
size_t fwrite(short *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int sin(double);
void wav_note(FILE **wav, double duration, double pitch) {
  int rc = 0, i = 0, l = 0;
  short waveform[44100 * 2];
  double time = 0.0;
  duration *= 0.25;
  for (i = 0, l = (int)(44100 * duration); i < l; i++) {
    time = (double)i / 44100;
    waveform[i] = (short)(32000 * sin(pitch * time * 2 * 3.1415));
  }
  if (fwrite(waveform, sizeof(short), (size_t)l, *wav) != (size_t)l) {
    printf("Could not write waveform\n");
    rc = 1;
    goto cleanup;
  }
cleanup:
  if (rc) {
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<wav_note>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2b150,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x2b148(%rbp)
ff
movsd  0xe87(%rip),%xmm0
mulsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
movsd  0xe66(%rip),%xmm0
mulsd  -0x10(%rbp),%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1218 <wav_note+0xd8>
cvtsi2sdl -0x20(%rbp),%xmm0
movsd  0xe41(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x2b148(%rbp)
ff
movsd  -0x18(%rbp),%xmm0
mulsd  -0x2b148(%rbp),%xmm0
ff
movsd  0xe38(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xe24(%rip),%xmm1
mulsd  %xmm1,%xmm0
call   1040 <sin@plt>
imul   $0x7d00,%eax,%eax
mov    %ax,%cx
movslq -0x20(%rbp),%rax
mov    %cx,-0x2b140(%rbp,%rax,2)
ff
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11a6 <wav_note+0x66>
lea    -0x2b140(%rbp),%rdi
movslq -0x24(%rbp),%rdx
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
mov    $0x2,%esi
call   1060 <fwrite@plt>
movslq -0x24(%rbp),%rcx
cmp    %rcx,%rax
je     1259 <wav_note+0x119>
lea    0xdd8(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x1c(%rbp)
jmp    125e <wav_note+0x11e>
jmp    125e <wav_note+0x11e>
cmpl   $0x0,-0x1c(%rbp)
je     1276 <wav_note+0x136>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
add    $0x2b150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wav_note>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x2b120,%rsp
mulsd  0xea9(%rip),%xmm0
mulsd  0xea9(%rip),%xmm0
mov    %rdi,%r14
cvttsd2si %xmm0,%r12d
test   %r12d,%r12d
jle    123f <wav_note+0xff>
mov    %r12d,%r15d
cmp    $0x1,%r12d
jne    1180 <wav_note+0x40>
xor    %ebx,%ebx
jmp    120a <wav_note+0xca>
mov    %r15d,%ebp
and    $0xfffffffe,%ebp
xor    %ebx,%ebx
movsd  %xmm1,0x8(%rsp)
xchg   %ax,%ax
xorps  %xmm0,%xmm0
cvtsi2sd %ebx,%xmm0
movsd  0xe69(%rip),%xmm2
divsd  %xmm2,%xmm0
mulsd  %xmm1,%xmm0
addsd  %xmm0,%xmm0
movsd  0xe5d(%rip),%xmm1
mulsd  %xmm1,%xmm0
call   1040 <sin@plt>
imul   $0x7d00,%eax,%eax
mov    %ax,0x10(%rsp,%rbx,2)
lea    0x1(%rbx),%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
divsd  0xe2f(%rip),%xmm0
mulsd  0x8(%rsp),%xmm0
addsd  %xmm0,%xmm0
mulsd  0xe25(%rip),%xmm0
call   1040 <sin@plt>
movsd  0x8(%rsp),%xmm1
imul   $0x7d00,%eax,%eax
mov    %ax,0x12(%rsp,%rbx,2)
add    $0x2,%rbx
cmp    %rbp,%rbx
jne    1190 <wav_note+0x50>
test   $0x1,%r15b
je     123f <wav_note+0xff>
xorps  %xmm0,%xmm0
cvtsi2sd %ebx,%xmm0
divsd  0xde9(%rip),%xmm0
mulsd  %xmm1,%xmm0
addsd  %xmm0,%xmm0
mulsd  0xde1(%rip),%xmm0
call   1040 <sin@plt>
imul   $0x7d00,%eax,%eax
mov    %ax,0x10(%rsp,%rbx,2)
movslq %r12d,%rbx
mov    (%r14),%rcx
lea    0x10(%rsp),%rdi
mov    $0x2,%esi
mov    %rbx,%rdx
call   1060 <fwrite@plt>
cmp    %rbx,%rax
jne    126c <wav_note+0x12c>
add    $0x2b120,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xda5(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"Could not write waveform\0A\00"
@EXIT_FAILURE = global i32 0
define void @wav_note(i32** %0, double %1, double %2) {
%4 = alloca i32**
%5 = alloca double
%6 = alloca double
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [88200 x i16]
%11 = alloca double
store i32** %0, i32*** %4
store double %1, double* %5
store double %2, double* %6
store i32 0, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
store double 0.000000e+00, double* %11
%12 = load double, double* %5
%13 = fmul double %12, 2.500000e-01
store double %13, double* %5
store i32 0, i32* %8
%14 = load double, double* %5
%15 = fmul double 4.410000e+04, %14
%16 = fptosi double %15 to i32
store i32 %16, i32* %9
br label %17
17: ; preds = %36, %3
%18 = load i32, i32* %8
%19 = load i32, i32* %9
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %39
21: ; preds = %17
%22 = load i32, i32* %8
%23 = sitofp i32 %22 to double
%24 = fdiv double %23, 4.410000e+04
store double %24, double* %11
%25 = load double, double* %6
%26 = load double, double* %11
%27 = fmul double %25, %26
%28 = fmul double %27, 2.000000e+00
%29 = fmul double %28, 3.141500e+00
%30 = call i32 @sin(double %29)
%31 = mul nsw i32 32000, %30
%32 = trunc i32 %31 to i16
%33 = load i32, i32* %8
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [88200 x i16], [88200 x i16]* %10, i64 0, i64 %34
store i16 %32, i16* %35
br label %36
36: ; preds = %21
%37 = load i32, i32* %8
%38 = add nsw i32 %37, 1
store i32 %38, i32* %8
br label %17
39: ; preds = %17
%40 = getelementptr inbounds [88200 x i16], [88200 x i16]* %10, i64 0, i64 0
%41 = load i32, i32* %9
%42 = sext i32 %41 to i64
%43 = load i32**, i32*** %4
%44 = load i32*, i32** %43
%45 = call i64 @fwrite(i16* %40, i32 2, i64 %42, i32* %44)
%46 = load i32, i32* %9
%47 = sext i32 %46 to i64
%48 = icmp ne i64 %45, %47
br i1 %48, label %49, label %51
49: ; preds = %39
%50 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %7
br label %52
51: ; preds = %39
br label %52
52: ; preds = %51, %49
%53 = load i32, i32* %7
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %52
%56 = load i32, i32* @EXIT_FAILURE
%57 = call i32 @exit(i32 %56)
unreachable
58: ; preds = %52
ret void
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@str = [25 x i8] c"Could not write waveform\00"
define void @wav_note(i32** nocapture %0, double %1, double %2) {
%4 = alloca [88200 x i16]
%5 = bitcast [88200 x i16]* %4 to i8*
%6 = fmul double %1, 2.500000e-01
%7 = fmul double %6, 4.410000e+04
%8 = fptosi double %7 to i32
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %57
10: ; preds = %3
%11 = zext i32 %8 to i64
%12 = and i64 %11, 1
%13 = icmp eq i32 %8, 1
br i1 %13, label %43, label %14
14: ; preds = %10
%15 = and i64 %11, 4294967294
br label %16
16: ; preds = %16, %14
%17 = phi i64 [ 0, %14 ], [ %40, %16 ]
%18 = phi i64 [ 0, %14 ], [ %41, %16 ]
%19 = trunc i64 %17 to i32
%20 = sitofp i32 %19 to double
%21 = fdiv double %20, 4.410000e+04
%22 = fmul double %21, %2
%23 = fmul double %22, 2.000000e+00
%24 = fmul double %23, 3.141500e+00
%25 = tail call i32 @sin(double %24)
%26 = trunc i32 %25 to i16
%27 = mul i16 %26, 32000
%28 = getelementptr inbounds [88200 x i16], [88200 x i16]* %4, i64 0, i64 %17
store i16 %27, i16* %28
%29 = or i64 %17, 1
%30 = trunc i64 %29 to i32
%31 = sitofp i32 %30 to double
%32 = fdiv double %31, 4.410000e+04
%33 = fmul double %32, %2
%34 = fmul double %33, 2.000000e+00
%35 = fmul double %34, 3.141500e+00
%36 = tail call i32 @sin(double %35)
%37 = trunc i32 %36 to i16
%38 = mul i16 %37, 32000
%39 = getelementptr inbounds [88200 x i16], [88200 x i16]* %4, i64 0, i64 %29
store i16 %38, i16* %39
%40 = add nuw nsw i64 %17, 2
%41 = add i64 %18, 2
%42 = icmp eq i64 %41, %15
br i1 %42, label %43, label %16
43: ; preds = %16, %10
%44 = phi i64 [ 0, %10 ], [ %40, %16 ]
%45 = icmp eq i64 %12, 0
br i1 %45, label %57, label %46
46: ; preds = %43
%47 = trunc i64 %44 to i32
%48 = sitofp i32 %47 to double
%49 = fdiv double %48, 4.410000e+04
%50 = fmul double %49, %2
%51 = fmul double %50, 2.000000e+00
%52 = fmul double %51, 3.141500e+00
%53 = tail call i32 @sin(double %52)
%54 = trunc i32 %53 to i16
%55 = mul i16 %54, 32000
%56 = getelementptr inbounds [88200 x i16], [88200 x i16]* %4, i64 0, i64 %44
store i16 %55, i16* %56
br label %57
57: ; preds = %46, %43, %3
%58 = getelementptr inbounds [88200 x i16], [88200 x i16]* %4, i64 0, i64 0
%59 = sext i32 %8 to i64
%60 = load i32*, i32** %0
%61 = call i64 @fwrite(i16* %58, i32 2, i64 %59, i32* %60)
%62 = icmp eq i64 %61, %59
br i1 %62, label %67, label %63
63: ; preds = %57
%64 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%65 = load i32, i32* @EXIT_FAILURE
%66 = tail call i32 @exit(i32 %65)
unreachable
67: ; preds = %57
ret void
}