#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 */
int BGBMID_SincPolate(short *, int, int, int);
int BGBMID_ScaleSampleSinc(short *d, int dl, short *s, int sl) {
  double r;
  int i, j;
  r = (((double)sl) / ((double)dl));
  for (i = 0; i < dl; i++) {
    j = BGBMID_SincPolate(s, sl, i * r, 64);
    if (j > 32767)
      j = 32767;
    if (j < -32768)
      j = -32768;
    d[i] = j;
  }
  return (0);
}


x86 assembly (clang, -O0):
<BGBMID_ScaleSampleSinc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
cvtsi2sdl -0x1c(%rbp),%xmm0
cvtsi2sdl -0xc(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11b6 <BGBMID_ScaleSampleSinc+0xa6>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
cvtsi2sdl -0x2c(%rbp),%xmm0
mulsd  -0x28(%rbp),%xmm0
cvttsd2si %xmm0,%edx
mov    $0x40,%ecx
call   1030 <BGBMID_SincPolate@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x7fff,-0x30(%rbp)
jle    1182 <BGBMID_ScaleSampleSinc+0x72>
movl   $0x7fff,-0x30(%rbp)
cmpl   $0xffff8000,-0x30(%rbp)
jge    1196 <BGBMID_ScaleSampleSinc+0x86>
movl   $0xffff8000,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    %ax,%dx
mov    -0x8(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %dx,(%rax,%rcx,2)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1140 <BGBMID_ScaleSampleSinc+0x30>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BGBMID_ScaleSampleSinc>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %esi,%esi
jle    1199 <BGBMID_ScaleSampleSinc+0x89>
mov    %ecx,%r15d
mov    %rdx,%r14
mov    %rdi,%r12
cvtsi2sd %ecx,%xmm1
cvtsi2sd %esi,%xmm0
divsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
mov    %esi,%ebp
xor    %ebx,%ebx
jmp    115e <BGBMID_ScaleSampleSinc+0x4e>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ax,(%r12,%rbx,2)
add    $0x1,%rbx
cmp    %rbx,%rbp
je     1199 <BGBMID_ScaleSampleSinc+0x89>
xorps  %xmm0,%xmm0
cvtsi2sd %ebx,%xmm0
mulsd  0x8(%rsp),%xmm0
cvttsd2si %xmm0,%edx
mov    %r14,%rdi
mov    %r15d,%esi
mov    $0x40,%ecx
call   1030 <BGBMID_SincPolate@plt>
cmp    $0x7fff,%eax
jl     118b <BGBMID_ScaleSampleSinc+0x7b>
mov    $0x7fff,%eax
cmp    $0xffff8001,%eax
jge    1150 <BGBMID_ScaleSampleSinc+0x40>
mov    $0x8000,%eax
jmp    1150 <BGBMID_ScaleSampleSinc+0x40>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @BGBMID_ScaleSampleSinc(i16* %0, i32 %1, i16* %2, i32 %3) {
%5 = alloca i16*
%6 = alloca i32
%7 = alloca i16*
%8 = alloca i32
%9 = alloca double
%10 = alloca i32
%11 = alloca i32
store i16* %0, i16** %5
store i32 %1, i32* %6
store i16* %2, i16** %7
store i32 %3, i32* %8
%12 = load i32, i32* %8
%13 = sitofp i32 %12 to double
%14 = load i32, i32* %6
%15 = sitofp i32 %14 to double
%16 = fdiv double %13, %15
store double %16, double* %9
store i32 0, i32* %10
br label %17
17: ; preds = %44, %4
%18 = load i32, i32* %10
%19 = load i32, i32* %6
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %47
21: ; preds = %17
%22 = load i16*, i16** %7
%23 = load i32, i32* %8
%24 = load i32, i32* %10
%25 = sitofp i32 %24 to double
%26 = load double, double* %9
%27 = fmul double %25, %26
%28 = fptosi double %27 to i32
%29 = call i32 @BGBMID_SincPolate(i16* %22, i32 %23, i32 %28, i32 64)
store i32 %29, i32* %11
%30 = load i32, i32* %11
%31 = icmp sgt i32 %30, 32767
br i1 %31, label %32, label %33
32: ; preds = %21
store i32 32767, i32* %11
br label %33
33: ; preds = %32, %21
%34 = load i32, i32* %11
%35 = icmp slt i32 %34, -32768
br i1 %35, label %36, label %37
36: ; preds = %33
store i32 -32768, i32* %11
br label %37
37: ; preds = %36, %33
%38 = load i32, i32* %11
%39 = trunc i32 %38 to i16
%40 = load i16*, i16** %5
%41 = load i32, i32* %10
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i16, i16* %40, i64 %42
store i16 %39, i16* %43
br label %44
44: ; preds = %37
%45 = load i32, i32* %10
%46 = add nsw i32 %45, 1
store i32 %46, i32* %10
br label %17
47: ; preds = %17
ret i32 0
}

LLVM IR (-O2):
define i32 @BGBMID_ScaleSampleSinc(i16* nocapture writeonly %0, i32 %1, i16* %2, i32 %3) {
%5 = sitofp i32 %3 to double
%6 = sitofp i32 %1 to double
%7 = fdiv double %5, %6
%8 = icmp sgt i32 %1, 0
br i1 %8, label %9, label %26
9: ; preds = %4
%10 = zext i32 %1 to i64
br label %11
11: ; preds = %9, %11
%12 = phi i64 [ 0, %9 ], [ %24, %11 ]
%13 = trunc i64 %12 to i32
%14 = sitofp i32 %13 to double
%15 = fmul double %7, %14
%16 = fptosi double %15 to i32
%17 = tail call i32 @BGBMID_SincPolate(i16* %2, i32 %3, i32 %16, i32 64)
%18 = icmp slt i32 %17, 32767
%19 = select i1 %18, i32 %17, i32 32767
%20 = icmp sgt i32 %19, -32768
%21 = select i1 %20, i32 %19, i32 -32768
%22 = trunc i32 %21 to i16
%23 = getelementptr inbounds i16, i16* %0, i64 %12
store i16 %22, i16* %23
%24 = add nuw nsw i64 %12, 1
%25 = icmp eq i64 %24, %10
br i1 %25, label %26, label %11
26: ; preds = %11, %4
ret i32 0
}