#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 doubleToFixed(double);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ perror(char *);
double sin(int);
int main(int argc, char *argv[]) {
  const int numberOfSamples = 1230;
  int i;
  double sample;
  FILE *fd = fopen("stimulus.txt", "wb");
  if (!fd) {
    perror("Unable to open stimulus.txt");
    return 1;
  }
  for (i = 0; i < numberOfSamples; i++) {
    sample = sin(2 * 3.1415926535 * 60000 * i / 47953.17073);
    fprintf(fd, "0x%.4x\n", doubleToFixed(sample));
  }
  fclose(fd);
  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)
movl   $0x4ce,-0x14(%rbp)
lea    0xe9e(%rip),%rdi
lea    0xe85(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b7 <main+0x57>
lea    0xe6d(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1238 <main+0xd8>
movl   $0x0,-0x18(%rbp)
cmpl   $0x4ce,-0x18(%rbp)
jge    1228 <main+0xc8>
cvtsi2sdl -0x18(%rbp),%xmm1
movsd  0xe30(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  0xe1c(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edi
call   1060 <sin@plt>
movsd  %xmm0,-0x20(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
movsd  -0x20(%rbp),%xmm0
call   1040 <doubleToFixed@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
lea    0xe1a(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11be <main+0x5e>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xebb(%rip),%rdi
lea    0xea2(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11fd <main+0x9d>
mov    %rax,%rbx
xorpd  %xmm0,%xmm0
mov    $0x4ce,%ebp
lea    0xea4(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsd  %xmm0,0x8(%rsp)
movsd  0x8(%rsp),%xmm0
mulsd  0xe4c(%rip),%xmm0
divsd  0xe4c(%rip),%xmm0
cvttsd2si %xmm0,%edi
call   1060 <sin@plt>
call   1040 <doubleToFixed@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1050 <fprintf@plt>
movsd  0x8(%rsp),%xmm0
addsd  0xe2b(%rip),%xmm0
add    $0xffffffff,%ebp
jne    11a0 <main+0x40>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe17(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
jmp    11f4 <main+0x94>

LLVM IR (-O0):
@.str = [13 x i8] c"stimulus.txt\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [28 x i8] c"Unable to open stimulus.txt\00"
@.str.3 = [8 x i8] c"0x%.4x\0A\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 double
%9 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1230, i32* %6
%10 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %9
%11 = load i32*, i32** %9
%12 = icmp ne i32* %11, null
br i1 %12, label %15, label %13
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %36
15: ; preds = %2
store i32 0, i32* %7
br label %16
16: ; preds = %30, %15
%17 = load i32, i32* %7
%18 = icmp slt i32 %17, 1230
br i1 %18, label %19, label %33
19: ; preds = %16
%20 = load i32, i32* %7
%21 = sitofp i32 %20 to double
%22 = fmul double 0x4117027C794317AD, %21
%23 = fdiv double %22, 0x40E76A25769EC2CE
%24 = fptosi double %23 to i32
%25 = call double @sin(i32 %24)
store double %25, double* %8
%26 = load i32*, i32** %9
%27 = load double, double* %8
%28 = call i32 @doubleToFixed(double %27)
%29 = call i32 @fprintf(i32* %26, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %28)
br label %30
30: ; preds = %19
%31 = load i32, i32* %7
%32 = add nsw i32 %31, 1
store i32 %32, i32* %7
br label %16
33: ; preds = %16
%34 = load i32*, i32** %9
%35 = call i32 @fclose(i32* %34)
store i32 0, i32* %3
br label %36
36: ; preds = %33, %13
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
@.str = [13 x i8] c"stimulus.txt\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [28 x i8] c"Unable to open stimulus.txt\00"
@.str.3 = [8 x i8] c"0x%.4x\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %20
7: ; preds = %2, %7
%8 = phi i32 [ %16, %7 ], [ 0, %2 ]
%9 = sitofp i32 %8 to double
%10 = fmul double %9, 0x4117027C794317AD
%11 = fdiv double %10, 0x40E76A25769EC2CE
%12 = fptosi double %11 to i32
%13 = tail call double @sin(i32 %12)
%14 = tail call i32 @doubleToFixed(double %13)
%15 = tail call i32 @fprintf(i32* %3, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %14)
%16 = add nuw nsw i32 %8, 1
%17 = icmp eq i32 %16, 1230
br i1 %17, label %18, label %7
18: ; preds = %7
%19 = tail call i32 @fclose(i32* %3)
br label %20
20: ; preds = %18, %5
%21 = phi i32 [ 0, %18 ], [ 1, %5 ]
ret i32 %21
}