#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*/ sample_f;

/* Variables and functions */
int /*<<< orphan*/ STDOUT_FILENO;
scalar_t__ get_sample(double, long, long const);
int /*<<< orphan*/ write(int /*<<< orphan*/, float *, int);
void calculate_sine_of(double *frequencies, int freq_quantity, double amplitude,
                       long int samples, const long int rate) {
  double sample = 0.0;
  float sample_f = 0.0;
  for (long int i = 0; i < samples; i++) {
    for (int j = 0; j < freq_quantity; j++) {
      sample += get_sample(frequencies[j], i, rate);
    }
    sample = sample / freq_quantity * amplitude;
    sample_f = (float)sample;
    write(STDOUT_FILENO, &sample_f, sizeof(sample_f));
  }
}


x86 assembly (clang, -O0):
<calculate_sine_of>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movsd  %xmm0,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x34(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x20(%rbp),%rax
jge    11fa <calculate_sine_of+0xda>
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11ac <calculate_sine_of+0x8c>
mov    -0x8(%rbp),%rax
movslq -0x44(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <get_sample@plt>
cvtsi2sd %rax,%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1169 <calculate_sine_of+0x49>
movsd  -0x30(%rbp),%xmm0
cvtsi2sdl -0xc(%rbp),%xmm1
divsd  %xmm1,%xmm0
mulsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x34(%rbp)
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
lea    -0x34(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    1154 <calculate_sine_of+0x34>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calculate_sine_of>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
movsd  %xmm0,0x18(%rsp)
mov    %rdx,0x20(%rsp)
test   %rdx,%rdx
jle    1200 <calculate_sine_of+0xe0>
mov    %rcx,%r12
mov    %esi,%r15d
mov    %rdi,%rbp
xorps  %xmm0,%xmm0
cvtsi2sd %esi,%xmm0
movsd  %xmm0,0x10(%rsp)
mov    %esi,%r13d
xorpd  %xmm0,%xmm0
movsd  %xmm0,(%rsp)
xor    %ebx,%ebx
jmp    11b3 <calculate_sine_of+0x93>
nopl   0x0(%rax,%rax,1)
movsd  (%rsp),%xmm0
divsd  0x10(%rsp),%xmm0
mulsd  0x18(%rsp),%xmm0
movsd  %xmm0,(%rsp)
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,0xc(%rsp)
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
add    $0x1,%rbx
cmp    0x20(%rsp),%rbx
je     1200 <calculate_sine_of+0xe0>
test   %r15d,%r15d
jle    1170 <calculate_sine_of+0x50>
xor    %r14d,%r14d
movsd  (%rsp),%xmm0
movsd  %xmm0,(%rsp)
movsd  0x0(%rbp,%r14,8),%xmm0
mov    %rbx,%rdi
mov    %r12,%rsi
call   1040 <get_sample@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movsd  (%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rsp)
movsd  (%rsp),%xmm0
add    $0x1,%r14
cmp    %r14,%r13
jne    11c0 <calculate_sine_of+0xa0>
jmp    1175 <calculate_sine_of+0x55>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
define void @calculate_sine_of(double* %0, i32 %1, double %2, i64 %3, i64 %4) {
%6 = alloca double*
%7 = alloca i32
%8 = alloca double
%9 = alloca i64
%10 = alloca i64
%11 = alloca double
%12 = alloca float
%13 = alloca i64
%14 = alloca i32
store double* %0, double** %6
store i32 %1, i32* %7
store double %2, double* %8
store i64 %3, i64* %9
store i64 %4, i64* %10
store double 0.000000e+00, double* %11
store float 0.000000e+00, float* %12
store i64 0, i64* %13
br label %15
15: ; preds = %50, %5
%16 = load i64, i64* %13
%17 = load i64, i64* %9
%18 = icmp slt i64 %16, %17
br i1 %18, label %19, label %53
19: ; preds = %15
store i32 0, i32* %14
br label %20
20: ; preds = %36, %19
%21 = load i32, i32* %14
%22 = load i32, i32* %7
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %39
24: ; preds = %20
%25 = load double*, double** %6
%26 = load i32, i32* %14
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds double, double* %25, i64 %27
%29 = load double, double* %28
%30 = load i64, i64* %13
%31 = load i64, i64* %10
%32 = call i64 @get_sample(double %29, i64 %30, i64 %31)
%33 = sitofp i64 %32 to double
%34 = load double, double* %11
%35 = fadd double %34, %33
store double %35, double* %11
br label %36
36: ; preds = %24
%37 = load i32, i32* %14
%38 = add nsw i32 %37, 1
store i32 %38, i32* %14
br label %20
39: ; preds = %20
%40 = load double, double* %11
%41 = load i32, i32* %7
%42 = sitofp i32 %41 to double
%43 = fdiv double %40, %42
%44 = load double, double* %8
%45 = fmul double %43, %44
store double %45, double* %11
%46 = load double, double* %11
%47 = fptrunc double %46 to float
store float %47, float* %12
%48 = load i32, i32* @STDOUT_FILENO
%49 = call i32 @write(i32 %48, float* %12, i32 4)
br label %50
50: ; preds = %39
%51 = load i64, i64* %13
%52 = add nsw i64 %51, 1
store i64 %52, i64* %13
br label %15
53: ; preds = %15
ret void
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
define void @calculate_sine_of(double* nocapture %0, i32 %1, double %2, i64 %3, i64 %4) {
%6 = alloca float
%7 = bitcast float* %6 to i8*
%8 = icmp sgt i64 %3, 0
br i1 %8, label %9, label %16
9: ; preds = %5
%10 = icmp sgt i32 %1, 0
%11 = sitofp i32 %1 to double
%12 = zext i32 %1 to i64
br label %13
13: ; preds = %9, %17
%14 = phi i64 [ 0, %9 ], [ %24, %17 ]
%15 = phi double [ 0.000000e+00, %9 ], [ %20, %17 ]
br i1 %10, label %26, label %17
16: ; preds = %17, %5
ret void
17: ; preds = %26, %13
%18 = phi double [ %15, %13 ], [ %33, %26 ]
%19 = fdiv double %18, %11
%20 = fmul double %19, %2
%21 = fptrunc double %20 to float
store float %21, float* %6
%22 = load i32, i32* @STDOUT_FILENO
%23 = call i32 @write(i32 %22, float* %6, i32 4)
%24 = add nuw nsw i64 %14, 1
%25 = icmp eq i64 %24, %3
br i1 %25, label %16, label %13
26: ; preds = %13, %26
%27 = phi i64 [ %34, %26 ], [ 0, %13 ]
%28 = phi double [ %33, %26 ], [ %15, %13 ]
%29 = getelementptr inbounds double, double* %0, i64 %27
%30 = load double, double* %29
%31 = tail call i64 @get_sample(double %30, i64 %14, i64 %4)
%32 = sitofp i64 %31 to double
%33 = fadd double %28, %32
%34 = add nuw nsw i64 %27, 1
%35 = icmp eq i64 %34, %12
br i1 %35, label %17, label %26
}