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

/* Variables and functions */
double M_PI;
int sin(double);
void init_buf(signed short buf[], int n) {
  int k;
  for (k = 0; k < 10000 - 1; k++)
    buf[k] = 2047 * sin(2.0 * M_PI * k / 1000);
  buf[10000 - 1] = 0;
}


x86 assembly (clang, -O0):
<init_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0x270f,-0x10(%rbp)
jge    1183 <init_buf+0x73>
movsd  0xecd(%rip),%xmm0
mov    0x2ea6(%rip),%rax
mulsd  (%rax),%xmm0
cvtsi2sdl -0x10(%rbp),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xea9(%rip),%xmm1
divsd  %xmm1,%xmm0
call   1030 <sin@plt>
imul   $0x7ff,%eax,%eax
mov    %ax,%dx
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %dx,(%rax,%rcx,2)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1126 <init_buf+0x16>
mov    -0x8(%rbp),%rax
movw   $0x0,0x4e1e(%rax)
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_buf>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
xor    %r12d,%r12d
mov    0x2ec3(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nop
movsd  (%r14),%xmm1
addsd  %xmm1,%xmm1
xorps  %xmm0,%xmm0
cvtsi2sd %r12d,%xmm0
mulsd  %xmm1,%xmm0
divsd  0xeb3(%rip),%xmm0
call   1030 <sin@plt>
mov    %eax,%ecx
shl    $0xb,%ecx
sub    %eax,%ecx
mov    %cx,(%r15,%r12,2)
lea    0x1(%r12),%rbx
cmp    $0x270f,%rbx
je     11a3 <init_buf+0x93>
movsd  (%r14),%xmm1
addsd  %xmm1,%xmm1
xorps  %xmm0,%xmm0
cvtsi2sd %ebx,%xmm0
mulsd  %xmm1,%xmm0
divsd  0xe78(%rip),%xmm0
call   1030 <sin@plt>
mov    %eax,%ecx
shl    $0xb,%ecx
sub    %eax,%ecx
mov    %cx,0x2(%r15,%r12,2)
add    $0x1,%rbx
mov    %rbx,%r12
jmp    1130 <init_buf+0x20>
movw   $0x0,0x4e1e(%r15)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@M_PI = global double 0.000000e+00
define void @init_buf(i16* %0, i32 %1) {
%3 = alloca i16*
%4 = alloca i32
%5 = alloca i32
store i16* %0, i16** %3
store i32 %1, i32* %4
store i32 0, i32* %5
br label %6
6: ; preds = %23, %2
%7 = load i32, i32* %5
%8 = icmp slt i32 %7, 9999
br i1 %8, label %9, label %26
9: ; preds = %6
%10 = load double, double* @M_PI
%11 = fmul double 2.000000e+00, %10
%12 = load i32, i32* %5
%13 = sitofp i32 %12 to double
%14 = fmul double %11, %13
%15 = fdiv double %14, 1.000000e+03
%16 = call i32 @sin(double %15)
%17 = mul nsw i32 2047, %16
%18 = trunc i32 %17 to i16
%19 = load i16*, i16** %3
%20 = load i32, i32* %5
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i16, i16* %19, i64 %21
store i16 %18, i16* %22
br label %23
23: ; preds = %9
%24 = load i32, i32* %5
%25 = add nsw i32 %24, 1
store i32 %25, i32* %5
br label %6
26: ; preds = %6
%27 = load i16*, i16** %3
%28 = getelementptr inbounds i16, i16* %27, i64 9999
store i16 0, i16* %28
ret void
}

LLVM IR (-O2):
@M_PI = global double 0.000000e+00
define void @init_buf(i16* nocapture writeonly %0, i32 %1) {
br label %3
3: ; preds = %17, %2
%4 = phi i64 [ 0, %2 ], [ %28, %17 ]
%5 = load double, double* @M_PI
%6 = fmul double %5, 2.000000e+00
%7 = trunc i64 %4 to i32
%8 = sitofp i32 %7 to double
%9 = fmul double %6, %8
%10 = fdiv double %9, 1.000000e+03
%11 = tail call i32 @sin(double %10)
%12 = trunc i32 %11 to i16
%13 = mul i16 %12, 2047
%14 = getelementptr inbounds i16, i16* %0, i64 %4
store i16 %13, i16* %14
%15 = or i64 %4, 1
%16 = icmp eq i64 %15, 9999
br i1 %16, label %29, label %17
17: ; preds = %3
%18 = load double, double* @M_PI
%19 = fmul double %18, 2.000000e+00
%20 = trunc i64 %15 to i32
%21 = sitofp i32 %20 to double
%22 = fmul double %19, %21
%23 = fdiv double %22, 1.000000e+03
%24 = tail call i32 @sin(double %23)
%25 = trunc i32 %24 to i16
%26 = mul i16 %25, 2047
%27 = getelementptr inbounds i16, i16* %0, i64 %15
store i16 %26, i16* %27
%28 = add nuw nsw i64 %4, 2
br label %3
29: ; preds = %3
%30 = getelementptr inbounds i16, i16* %0, i64 9999
store i16 0, i16* %30
ret void
}