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

/* Variables and functions */
int /*<<< orphan*/ fputc(unsigned int, int /*<<< orphan*/ *);
scalar_t__ sin(int);
void write_sound(FILE *wav_file, unsigned int wav_sample_rate,
                 unsigned int wav_volume, unsigned int number_samples,
                 double frequency) {
  int t;
  if (!wav_file)
    return;
  for (t = 0; t < number_samples; t++) {
    uint8_t c;
    c = (uint8_t)(sin(t * 2 * 3.14159265358979323846 / wav_sample_rate *
                      frequency) +
                  1) *
        wav_volume;
    fputc(c, wav_file);
  }
}


x86 assembly (clang, -O0):
<write_sound>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
movsd  %xmm0,-0x20(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    114a <write_sound+0x2a>
jmp    11b1 <write_sound+0x91>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    11b1 <write_sound+0x91>
mov    -0x24(%rbp),%eax
add    %eax,%eax
cvtsi2sd %eax,%xmm0
movsd  0xe92(%rip),%xmm1
mulsd  %xmm1,%xmm0
mov    -0xc(%rbp),%eax
cvtsi2sd %rax,%xmm1
divsd  %xmm1,%xmm0
mulsd  -0x20(%rbp),%xmm0
cvttsd2si %xmm0,%edi
call   1040 <sin@plt>
add    $0x1,%rax
imul   -0x10(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x8(%rbp),%rsi
call   1030 <fputc@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1151 <write_sound+0x31>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_sound>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
test   %rdi,%rdi
je     118f <write_sound+0x6f>
mov    %ecx,%ebx
test   %ecx,%ecx
je     118f <write_sound+0x6f>
mov    %edx,%r14d
mov    %rdi,%r15
mov    %esi,%eax
cvtsi2sd %rax,%xmm1
movsd  %xmm1,0x8(%rsp)
xor    %ebp,%ebp
movsd  %xmm0,0x10(%rsp)
xorps  %xmm1,%xmm1
cvtsi2sd %ebp,%xmm1
mulsd  0xea1(%rip),%xmm1
divsd  0x8(%rsp),%xmm1
mulsd  %xmm0,%xmm1
cvttsd2si %xmm1,%edi
call   1040 <sin@plt>
lea    0x1(%rax),%edi
imul   %r14d,%edi
mov    %r15,%rsi
call   1030 <fputc@plt>
movsd  0x10(%rsp),%xmm0
add    $0x2,%ebp
add    $0xffffffff,%ebx
jne    1150 <write_sound+0x30>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @write_sound(i32* %0, i32 %1, i32 %2, i32 %3, double %4) {
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca double
%11 = alloca i32
%12 = alloca i32
store i32* %0, i32** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store double %4, double* %10
%13 = load i32*, i32** %6
%14 = icmp ne i32* %13, null
br i1 %14, label %16, label %15
15: ; preds = %5
br label %43
16: ; preds = %5
store i32 0, i32* %11
br label %17
17: ; preds = %40, %16
%18 = load i32, i32* %11
%19 = load i32, i32* %9
%20 = icmp ult i32 %18, %19
br i1 %20, label %21, label %43
21: ; preds = %17
%22 = load i32, i32* %11
%23 = mul nsw i32 %22, 2
%24 = sitofp i32 %23 to double
%25 = fmul double %24, 0x400921FB54442D18
%26 = load i32, i32* %7
%27 = uitofp i32 %26 to double
%28 = fdiv double %25, %27
%29 = load double, double* %10
%30 = fmul double %28, %29
%31 = fptosi double %30 to i32
%32 = call i64 @sin(i32 %31)
%33 = add nsw i64 %32, 1
%34 = trunc i64 %33 to i32
%35 = load i32, i32* %8
%36 = mul i32 %34, %35
store i32 %36, i32* %12
%37 = load i32, i32* %12
%38 = load i32*, i32** %6
%39 = call i32 @fputc(i32 %37, i32* %38)
br label %40
40: ; preds = %21
%41 = load i32, i32* %11
%42 = add nsw i32 %41, 1
store i32 %42, i32* %11
br label %17
43: ; preds = %15, %17
ret void
}

LLVM IR (-O2):
define void @write_sound(i32* %0, i32 %1, i32 %2, i32 %3, double %4) {
%6 = icmp ne i32* %0, null
%7 = icmp ne i32 %3, 0
%8 = and i1 %6, %7
br i1 %8, label %9, label %26
9: ; preds = %5
%10 = uitofp i32 %1 to double
br label %11
11: ; preds = %9, %11
%12 = phi i32 [ 0, %9 ], [ %24, %11 ]
%13 = shl nuw nsw i32 %12, 1
%14 = sitofp i32 %13 to double
%15 = fmul double %14, 0x400921FB54442D18
%16 = fdiv double %15, %10
%17 = fmul double %16, %4
%18 = fptosi double %17 to i32
%19 = tail call i64 @sin(i32 %18)
%20 = trunc i64 %19 to i32
%21 = add i32 %20, 1
%22 = mul i32 %21, %2
%23 = tail call i32 @fputc(i32 %22, i32* %0)
%24 = add nuw i32 %12, 1
%25 = icmp eq i32 %24, %3
br i1 %25, label %26, label %11
26: ; preds = %11, %5
ret void
}