#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 M_PI;
float adsr_envelope(int, int, int, int, double, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(float *, int, int, int /*<<< orphan*/ *);
float sin(int);
int main(void) {
  FILE *file = fopen("example.raw", "wb");
  for (int i = 0; i < 4800 + 4800 + 12000 + 12000; i++) {
    float sample = sin(2 * M_PI * 1000 * i / 48000);
    sample *= adsr_envelope(4800, 4800, 12000, 12000, 0.50, i);
    fwrite(&sample, sizeof(float), 1, file);
  }
  fclose(file);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xea2(%rip),%rdi
lea    0xea7(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x8340,-0x14(%rbp)
jge    1202 <main+0xb2>
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
shl    $0x1,%eax
imul   $0x3e8,%eax,%eax
imul   -0x14(%rbp),%eax
mov    $0xbb80,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
call   1040 <sin@plt>
movss  %xmm0,-0x18(%rbp)
mov    -0x14(%rbp),%r8d
mov    $0x12c0,%esi
mov    $0x2ee0,%ecx
movsd  0xe36(%rip),%xmm0
mov    %esi,%edi
mov    %ecx,%edx
call   1060 <adsr_envelope@plt>
mulss  -0x18(%rbp),%xmm0
movss  %xmm0,-0x18(%rbp)
mov    -0x10(%rbp),%rcx
lea    -0x18(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    117d <main+0x2d>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
lea    0xea5(%rip),%rdi
lea    0xeaa(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%r15
xor    %ebx,%ebx
mov    0x2e6d(%rip),%r12
lea    0xc(%rsp),%r14
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r12),%eax
imul   %ebx,%eax
cltq
imul   $0x57619f1,%rax,%rdi
mov    %rdi,%rax
shr    $0x3f,%rax
sar    $0x2a,%rdi
add    %eax,%edi
call   1040 <sin@plt>
movss  %xmm0,0x8(%rsp)
mov    $0x12c0,%edi
mov    $0x12c0,%esi
mov    $0x2ee0,%edx
mov    $0x2ee0,%ecx
movsd  0xe2c(%rip),%xmm0
mov    %ebp,%r8d
call   1060 <adsr_envelope@plt>
mulss  0x8(%rsp),%xmm0
movss  %xmm0,0xc(%rsp)
mov    %r14,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1070 <fwrite@plt>
add    $0x1,%ebp
add    $0x7d0,%ebx
cmp    $0x8340,%ebp
jne    1190 <main+0x40>
mov    %r15,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"example.raw\00"
@.str.1 = [3 x i8] c"wb\00"
@M_PI = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca float
store i32 0, i32* %1
%5 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %2
store i32 0, i32* %3
br label %6
6: ; preds = %23, %0
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 33600
br i1 %8, label %9, label %26
9: ; preds = %6
%10 = load i32, i32* @M_PI
%11 = mul nsw i32 2, %10
%12 = mul nsw i32 %11, 1000
%13 = load i32, i32* %3
%14 = mul nsw i32 %12, %13
%15 = sdiv i32 %14, 48000
%16 = call float @sin(i32 %15)
store float %16, float* %4
%17 = load i32, i32* %3
%18 = call float @adsr_envelope(i32 4800, i32 4800, i32 12000, i32 12000, double 5.000000e-01, i32 %17)
%19 = load float, float* %4
%20 = fmul float %19, %18
store float %20, float* %4
%21 = load i32*, i32** %2
%22 = call i32 @fwrite(float* %4, i32 4, i32 1, i32* %21)
br label %23
23: ; preds = %9
%24 = load i32, i32* %3
%25 = add nsw i32 %24, 1
store i32 %25, i32* %3
br label %6
26: ; preds = %6
%27 = load i32*, i32** %2
%28 = call i32 @fclose(i32* %27)
%29 = load i32, i32* %1
ret i32 %29
}

LLVM IR (-O2):
@.str = [12 x i8] c"example.raw\00"
@.str.1 = [3 x i8] c"wb\00"
@M_PI = global i32 0
define i32 @main() {
%1 = alloca float
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%3 = bitcast float* %1 to i8*
br label %6
4: ; preds = %6
%5 = tail call i32 @fclose(i32* %2)
ret i32 0
6: ; preds = %0, %6
%7 = phi i32 [ 0, %0 ], [ %16, %6 ]
%8 = load i32, i32* @M_PI
%9 = mul nuw nsw i32 %7, 2000
%10 = mul i32 %9, %8
%11 = sdiv i32 %10, 48000
%12 = tail call float @sin(i32 %11)
%13 = tail call float @adsr_envelope(i32 4800, i32 4800, i32 12000, i32 12000, double 5.000000e-01, i32 %7)
%14 = fmul float %12, %13
store float %14, float* %1
%15 = call i32 @fwrite(float* %1, i32 4, i32 1, i32* %2)
%16 = add nuw nsw i32 %7, 1
%17 = icmp eq i32 %16, 33600
br i1 %17, label %4, label %6
}