#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 fmod(double, size_t);
double arp(double t, double t2, size_t n_count, double n_arr[]) {
  const double u = fmod(t, n_count * t2);
  for (size_t i = 0; i < n_count - 1; i++) {
    if (u < (i + 1) * t2)
      return n_arr[i];
  }
  return n_arr[n_count - 1];
}


x86 assembly (clang, -O0):
<arp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
mov    %rdi,-0x20(%rbp)
mov    %rsi,-0x28(%rbp)
movsd  -0x10(%rbp),%xmm0
movaps 0xeca(%rip),%xmm2
movq   -0x20(%rbp),%xmm1
punpckldq %xmm2,%xmm1
movapd 0xec9(%rip),%xmm2
subpd  %xmm2,%xmm1
movaps %xmm1,%xmm2
unpckhpd %xmm1,%xmm1
addsd  %xmm2,%xmm1
movsd  -0x18(%rbp),%xmm2
mulsd  %xmm2,%xmm1
movaps %xmm1,%xmm2
cvttsd2si %xmm2,%rdi
mov    %rdi,%rcx
sar    $0x3f,%rcx
movsd  0xeaa(%rip),%xmm2
subsd  %xmm2,%xmm1
cvttsd2si %xmm1,%rax
and    %rcx,%rax
or     %rax,%rdi
call   1030 <fmod@plt>
movsd  %xmm0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jae    121c <arp+0x10c>
movsd  -0x30(%rbp),%xmm1
mov    -0x38(%rbp),%rax
add    $0x1,%rax
movq   %rax,%xmm0
movaps 0xe3b(%rip),%xmm2
punpckldq %xmm2,%xmm0
movapd 0xe3f(%rip),%xmm2
subpd  %xmm2,%xmm0
movaps %xmm0,%xmm2
unpckhpd %xmm0,%xmm0
addsd  %xmm2,%xmm0
mulsd  -0x18(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    1206 <arp+0xf6>
mov    -0x28(%rbp),%rax
mov    -0x38(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1232 <arp+0x122>
jmp    120b <arp+0xfb>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1197 <arp+0x87>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<arp>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
movq   %rdi,%xmm2
punpckldq 0xedc(%rip),%xmm2
movsd  %xmm1,(%rsp)
subpd  0xedf(%rip),%xmm2
movapd %xmm2,%xmm3
unpckhpd %xmm2,%xmm3
addsd  %xmm2,%xmm3
mulsd  %xmm1,%xmm3
cvttsd2si %xmm3,%rax
mov    %rsi,%r14
mov    %rax,%rcx
sar    $0x3f,%rcx
subsd  0xec8(%rip),%xmm3
cvttsd2si %xmm3,%rdi
and    %rcx,%rdi
or     %rax,%rdi
call   1030 <fmod@plt>
movsd  (%rsp),%xmm5
lea    -0x1(%rbx),%rax
mov    $0x1,%ecx
movdqa 0xe82(%rip),%xmm1
movapd 0xe8a(%rip),%xmm2
cs nopw 0x0(%rax,%rax,1)
cmp    %rcx,%rbx
je     11c3 <arp+0xb3>
movq   %rcx,%xmm3
punpckldq %xmm1,%xmm3
subpd  %xmm2,%xmm3
movapd %xmm3,%xmm4
unpckhpd %xmm3,%xmm4
addsd  %xmm3,%xmm4
mulsd  %xmm5,%xmm4
add    $0x1,%rcx
ucomisd %xmm0,%xmm4
jbe    1190 <arp+0x80>
add    $0xfffffffffffffffe,%rcx
mov    %rcx,%rax
movsd  (%r14,%rax,8),%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define double @arp(double %0, double %1, i64 %2, double* %3) {
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca i64
%9 = alloca double*
%10 = alloca double
%11 = alloca i64
store double %0, double* %6
store double %1, double* %7
store i64 %2, i64* %8
store double* %3, double** %9
%12 = load double, double* %6
%13 = load i64, i64* %8
%14 = uitofp i64 %13 to double
%15 = load double, double* %7
%16 = fmul double %14, %15
%17 = fptoui double %16 to i64
%18 = call double @fmod(double %12, i64 %17)
store double %18, double* %10
store i64 0, i64* %11
br label %19
19: ; preds = %38, %4
%20 = load i64, i64* %11
%21 = load i64, i64* %8
%22 = sub i64 %21, 1
%23 = icmp ult i64 %20, %22
br i1 %23, label %24, label %41
24: ; preds = %19
%25 = load double, double* %10
%26 = load i64, i64* %11
%27 = add i64 %26, 1
%28 = uitofp i64 %27 to double
%29 = load double, double* %7
%30 = fmul double %28, %29
%31 = fcmp olt double %25, %30
br i1 %31, label %32, label %37
32: ; preds = %24
%33 = load double*, double** %9
%34 = load i64, i64* %11
%35 = getelementptr inbounds double, double* %33, i64 %34
%36 = load double, double* %35
store double %36, double* %5
br label %47
37: ; preds = %24
br label %38
38: ; preds = %37
%39 = load i64, i64* %11
%40 = add i64 %39, 1
store i64 %40, i64* %11
br label %19
41: ; preds = %19
%42 = load double*, double** %9
%43 = load i64, i64* %8
%44 = sub i64 %43, 1
%45 = getelementptr inbounds double, double* %42, i64 %44
%46 = load double, double* %45
store double %46, double* %5
br label %47
47: ; preds = %41, %32
%48 = load double, double* %5
ret double %48
}

LLVM IR (-O2):
define double @arp(double %0, double %1, i64 %2, double* nocapture %3) {
%5 = uitofp i64 %2 to double
%6 = fmul double %5, %1
%7 = fptoui double %6 to i64
%8 = tail call double @fmod(double %0, i64 %7)
%9 = add i64 %2, -1
br label %10
10: ; preds = %13, %4
%11 = phi i64 [ 0, %4 ], [ %14, %13 ]
%12 = icmp eq i64 %11, %9
br i1 %12, label %18, label %13
13: ; preds = %10
%14 = add i64 %11, 1
%15 = uitofp i64 %14 to double
%16 = fmul double %15, %1
%17 = fcmp olt double %8, %16
br i1 %17, label %18, label %10
18: ; preds = %10, %13
%19 = phi i64 [ %11, %13 ], [ %9, %10 ]
%20 = getelementptr inbounds double, double* %3, i64 %19
%21 = load double, double* %20
ret double %21
}