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


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x3ea0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11a0 <main+0x40>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe7f(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe83(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1030 <atof@plt>
movss  %xmm0,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x1f40,-0x1c(%rbp)
jge    1232 <main+0xd2>
movss  -0x20(%rbp),%xmm0
cvtsi2ssl -0x1c(%rbp),%xmm1
mulss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe08(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
call   1050 <cos@plt>
movsd  0xdfb(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %ax,%cx
movslq -0x1c(%rbp),%rax
mov    %cx,-0x3ea0(%rbp,%rax,2)
ff
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11d1 <main+0x71>
lea    -0x3ea0(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x2,%esi
mov    $0x1f40,%edx
call   1080 <fwrite@plt>
xor    %eax,%eax
add    $0x3ea0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x3e98,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    1242 <main+0xe2>
mov    0x8(%rbx),%rdi
lea    0xeb1(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r14
mov    0x10(%rbx),%rdi
call   1030 <atof@plt>
movss  %xmm0,0xc(%rsp)
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
xorps  %xmm0,%xmm0
cvtsi2ss %ebx,%xmm0
mulss  0xc(%rsp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe47(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
call   1050 <cos@plt>
movsd  0xe3a(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %ax,0x10(%rsp,%rbx,2)
lea    0x1(%rbx),%eax
xorps  %xmm0,%xmm0
cvtsi2ss %eax,%xmm0
mulss  0xc(%rsp),%xmm0
cvtss2sd %xmm0,%xmm0
mulsd  0xe09(%rip),%xmm0
cvtsd2ss %xmm0,%xmm0
call   1050 <cos@plt>
mulsd  0xe00(%rip),%xmm0
cvttsd2si %xmm0,%eax
mov    %ax,0x12(%rsp,%rbx,2)
add    $0x2,%rbx
cmp    $0x1f40,%rbx
jne    11a0 <main+0x40>
lea    0x10(%rsp),%rdi
mov    $0x2,%esi
mov    $0x1f40,%edx
mov    %r14,%rcx
call   1080 <fwrite@plt>
xor    %eax,%eax
add    $0x3e98,%rsp
pop    %rbx
pop    %r14
ret
mov    (%rbx),%rsi
lea    0xdc4(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [34 x i8] c"usage: %s outputFile frequencyHz\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 float
%9 = alloca [8000 x i16]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 3
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %22, i32** %6
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
%26 = call float @atof(i8* %25)
store float %26, float* %8
store i32 0, i32* %7
br label %27
27: ; preds = %44, %18
%28 = load i32, i32* %7
%29 = icmp slt i32 %28, 8000
br i1 %29, label %30, label %47
30: ; preds = %27
%31 = load float, float* %8
%32 = load i32, i32* %7
%33 = sitofp i32 %32 to float
%34 = fmul float %31, %33
%35 = fpext float %34 to double
%36 = fmul double %35, 0x3F49BC65B68848FA
%37 = fptrunc double %36 to float
%38 = call double @cos(float %37)
%39 = fmul double 1.000000e+03, %38
%40 = fptosi double %39 to i16
%41 = load i32, i32* %7
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [8000 x i16], [8000 x i16]* %9, i64 0, i64 %42
store i16 %40, i16* %43
br label %44
44: ; preds = %30
%45 = load i32, i32* %7
%46 = add nsw i32 %45, 1
store i32 %46, i32* %7
br label %27
47: ; preds = %27
%48 = getelementptr inbounds [8000 x i16], [8000 x i16]* %9, i64 0, i64 0
%49 = load i32*, i32** %6
%50 = call i32 @fwrite(i16* %48, i32 2, i32 8000, i32* %49)
ret i32 0
}

LLVM IR (-O2):
@.str = [34 x i8] c"usage: %s outputFile frequencyHz\0A\00"
@.str.1 = [3 x i8] c"wb\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [8000 x i16]
%4 = bitcast [8000 x i16]* %3 to i8*
%5 = icmp eq i32 %0, 3
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = tail call float @atof(i8* %15)
br label %17
17: ; preds = %17, %10
%18 = phi i64 [ 0, %10 ], [ %40, %17 ]
%19 = trunc i64 %18 to i32
%20 = sitofp i32 %19 to float
%21 = fmul float %16, %20
%22 = fpext float %21 to double
%23 = fmul double %22, 0x3F49BC65B68848FA
%24 = fptrunc double %23 to float
%25 = tail call double @cos(float %24)
%26 = fmul double %25, 1.000000e+03
%27 = fptosi double %26 to i16
%28 = getelementptr inbounds [8000 x i16], [8000 x i16]* %3, i64 0, i64 %18
store i16 %27, i16* %28
%29 = or i64 %18, 1
%30 = trunc i64 %29 to i32
%31 = sitofp i32 %30 to float
%32 = fmul float %16, %31
%33 = fpext float %32 to double
%34 = fmul double %33, 0x3F49BC65B68848FA
%35 = fptrunc double %34 to float
%36 = tail call double @cos(float %35)
%37 = fmul double %36, 1.000000e+03
%38 = fptosi double %37 to i16
%39 = getelementptr inbounds [8000 x i16], [8000 x i16]* %3, i64 0, i64 %29
store i16 %38, i16* %39
%40 = add nuw nsw i64 %18, 2
%41 = icmp eq i64 %40, 8000
br i1 %41, label %42, label %17
42: ; preds = %17
%43 = getelementptr inbounds [8000 x i16], [8000 x i16]* %3, i64 0, i64 0
%44 = call i32 @fwrite(i16* %43, i32 2, i32 8000, i32* %13)
ret i32 0
}