#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*/ assert(int /*<<< orphan*/);
float atof(char *);
double cos(float);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(short *, int, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, char *);
int main(int argc, char *argv[]) {
  FILE *f;
  int i, n;
  float freq, length;
  short *buf;
  if (argc != 4) {
    printf("usage: %s outputFile frequencyHz lengthSecs\n", argv[0]);
    exit(1);
  }
  f = fopen(argv[1], "wb");
  freq = atof(argv[2]);
  length = atof(argv[3]);
  n = length * 8000.0;
  buf = (short *)malloc(sizeof(short) * n);
  assert(buf != NULL);
  for (i = 0; i < n; i++)
    buf[i] = 32767.0 * cos(freq * i * (6.283185307 / 8000.0));
  fwrite(buf, sizeof(short), n, f);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     11bd <main+0x3d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe6a(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe79(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1030 <atof@plt>
movss  %xmm0,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1030 <atof@plt>
movss  %xmm0,-0x28(%rbp)
movss  -0x28(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xdf6(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x20(%rbp)
movslq -0x20(%rbp),%rax
shl    $0x1,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    12a1 <main+0x121>
movss  -0x24(%rbp),%xmm0
cvtsi2ssl -0x1c(%rbp),%xmm1
mulss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xda1(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
call   1070 <cos@plt>
movsd  0xd94(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %ax,%dx
mov    -0x30(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dx,(%rax,%rcx,2)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1241 <main+0xc1>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x2,%esi
call   10a0 <fwrite@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x4,%edi
jne    12ef <main+0x16f>
mov    0x8(%rbx),%rdi
lea    0xea3(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r14
mov    0x10(%rbx),%rdi
call   1030 <atof@plt>
movss  %xmm0,0x4(%rsp)
mov    0x18(%rbx),%rdi
call   1030 <atof@plt>
cvtss2sd %xmm0,%xmm0
mulsd  0xe32(%rip),%xmm0
cvttsd2si %xmm0,%r15d
lea    (%r15,%r15,1),%edi
call   1060 <malloc@plt>
mov    %rax,%r12
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1050 <assert@plt>
test   %r15d,%r15d
jle    12cb <main+0x14b>
mov    %r15d,%r13d
cmp    $0x1,%r15d
jne    1206 <main+0x86>
xor    %ebx,%ebx
jmp    128b <main+0x10b>
mov    %r13d,%ebp
and    $0xfffffffe,%ebp
xor    %ebx,%ebx
xchg   %ax,%ax
xorps  %xmm0,%xmm0
cvtsi2ss %ebx,%xmm0
mulss  0x4(%rsp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xddf(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
call   1070 <cos@plt>
movsd  0xdd2(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %ax,(%r12,%rbx,2)
lea    0x1(%rbx),%eax
xorps  %xmm0,%xmm0
cvtsi2ss %eax,%xmm0
mulss  0x4(%rsp),%xmm0
cvtss2sd %xmm0,%xmm0
mulsd  0xda1(%rip),%xmm0
cvtsd2ss %xmm0,%xmm0
call   1070 <cos@plt>
mulsd  0xd98(%rip),%xmm0
cvttsd2si %xmm0,%eax
mov    %ax,0x2(%r12,%rbx,2)
add    $0x2,%rbx
cmp    %rbp,%rbx
jne    1210 <main+0x90>
test   $0x1,%r13b
je     12cb <main+0x14b>
xorps  %xmm0,%xmm0
cvtsi2ss %ebx,%xmm0
movss  0x4(%rsp),%xmm1
mulss  %xmm0,%xmm1
xorps  %xmm0,%xmm0
cvtss2sd %xmm1,%xmm0
mulsd  0xd57(%rip),%xmm0
cvtsd2ss %xmm0,%xmm0
call   1070 <cos@plt>
mulsd  0xd4e(%rip),%xmm0
cvttsd2si %xmm0,%eax
mov    %ax,(%r12,%rbx,2)
mov    %r12,%rdi
mov    $0x2,%esi
mov    %r15d,%edx
mov    %r14,%rcx
call   10a0 <fwrite@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rbx),%rsi
lea    0xd1f(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [45 x i8] c"usage: %s outputFile frequencyHz lengthSecs\0A\00"
@.str.1 = [3 x i8] c"wb\00"
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 float
%10 = alloca float
%11 = alloca i16*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 4
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32* @fopen(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %24, i32** %6
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 2
%27 = load i8*, i8** %26
%28 = call float @atof(i8* %27)
store float %28, float* %9
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 3
%31 = load i8*, i8** %30
%32 = call float @atof(i8* %31)
store float %32, float* %10
%33 = load float, float* %10
%34 = fpext float %33 to double
%35 = fmul double %34, 8.000000e+03
%36 = fptosi double %35 to i32
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = sext i32 %37 to i64
%39 = mul i64 2, %38
%40 = trunc i64 %39 to i32
%41 = call i64 @malloc(i32 %40)
%42 = inttoptr i64 %41 to i16*
store i16* %42, i16** %11
%43 = load i16*, i16** %11
%44 = icmp ne i16* %43, null
%45 = zext i1 %44 to i32
%46 = call i32 @assert(i32 %45)
store i32 0, i32* %7
br label %47
47: ; preds = %66, %20
%48 = load i32, i32* %7
%49 = load i32, i32* %8
%50 = icmp slt i32 %48, %49
br i1 %50, label %51, label %69
51: ; preds = %47
%52 = load float, float* %9
%53 = load i32, i32* %7
%54 = sitofp i32 %53 to float
%55 = fmul float %52, %54
%56 = fpext float %55 to double
%57 = fmul double %56, 0x3F49BC65B68848FA
%58 = fptrunc double %57 to float
%59 = call double @cos(float %58)
%60 = fmul double 3.276700e+04, %59
%61 = fptosi double %60 to i16
%62 = load i16*, i16** %11
%63 = load i32, i32* %7
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i16, i16* %62, i64 %64
store i16 %61, i16* %65
br label %66
66: ; preds = %51
%67 = load i32, i32* %7
%68 = add nsw i32 %67, 1
store i32 %68, i32* %7
br label %47
69: ; preds = %47
%70 = load i16*, i16** %11
%71 = load i32, i32* %8
%72 = load i32*, i32** %6
%73 = call i32 @fwrite(i16* %70, i32 2, i32 %71, i32* %72)
ret i32 0
}

LLVM IR (-O2):
@.str = [45 x i8] c"usage: %s outputFile frequencyHz lengthSecs\0A\00"
@.str.1 = [3 x i8] c"wb\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 4
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8* %5)
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call float @atof(i8* %13)
%15 = getelementptr inbounds i8*, i8** %1, i64 3
%16 = load i8*, i8** %15
%17 = tail call float @atof(i8* %16)
%18 = fpext float %17 to double
%19 = fmul double %18, 8.000000e+03
%20 = fptosi double %19 to i32
%21 = shl i32 %20, 1
%22 = tail call i64 @malloc(i32 %21)
%23 = inttoptr i64 %22 to i16*
%24 = icmp ne i64 %22, 0
%25 = zext i1 %24 to i32
%26 = tail call i32 @assert(i32 %25)
%27 = icmp sgt i32 %20, 0
br i1 %27, label %28, label %75
28: ; preds = %8
%29 = zext i32 %20 to i64
%30 = and i64 %29, 1
%31 = icmp eq i32 %20, 1
br i1 %31, label %61, label %32
32: ; preds = %28
%33 = and i64 %29, 4294967294
br label %34
34: ; preds = %34, %32
%35 = phi i64 [ 0, %32 ], [ %58, %34 ]
%36 = phi i64 [ 0, %32 ], [ %59, %34 ]
%37 = trunc i64 %35 to i32
%38 = sitofp i32 %37 to float
%39 = fmul float %14, %38
%40 = fpext float %39 to double
%41 = fmul double %40, 0x3F49BC65B68848FA
%42 = fptrunc double %41 to float
%43 = tail call double @cos(float %42)
%44 = fmul double %43, 3.276700e+04
%45 = fptosi double %44 to i16
%46 = getelementptr inbounds i16, i16* %23, i64 %35
store i16 %45, i16* %46
%47 = or i64 %35, 1
%48 = trunc i64 %47 to i32
%49 = sitofp i32 %48 to float
%50 = fmul float %14, %49
%51 = fpext float %50 to double
%52 = fmul double %51, 0x3F49BC65B68848FA
%53 = fptrunc double %52 to float
%54 = tail call double @cos(float %53)
%55 = fmul double %54, 3.276700e+04
%56 = fptosi double %55 to i16
%57 = getelementptr inbounds i16, i16* %23, i64 %47
store i16 %56, i16* %57
%58 = add nuw nsw i64 %35, 2
%59 = add i64 %36, 2
%60 = icmp eq i64 %59, %33
br i1 %60, label %61, label %34
61: ; preds = %34, %28
%62 = phi i64 [ 0, %28 ], [ %58, %34 ]
%63 = icmp eq i64 %30, 0
br i1 %63, label %75, label %64
64: ; preds = %61
%65 = trunc i64 %62 to i32
%66 = sitofp i32 %65 to float
%67 = fmul float %14, %66
%68 = fpext float %67 to double
%69 = fmul double %68, 0x3F49BC65B68848FA
%70 = fptrunc double %69 to float
%71 = tail call double @cos(float %70)
%72 = fmul double %71, 3.276700e+04
%73 = fptosi double %72 to i16
%74 = getelementptr inbounds i16, i16* %23, i64 %62
store i16 %73, i16* %74
br label %75
75: ; preds = %64, %61, %8
%76 = tail call i32 @fwrite(i16* %23, i32 2, i32 %20, i32* %11)
ret i32 0
}