// File: /workspace/github_c_makefile_star2_O3/pipde[P]deos-songlib/lib/songlib.c
// Function: aresample
RRA *
aresample(RRA *note,double *factors,int factorLength)
    {
    int UP = 0,DOWN = 1,OFF = 2;
    int i;
    int amping = OFF;
    double maxAmp = drawAmpFactor;
    double t;
    double amp;
    double oldfactor;
    RRA *h;

    h = newRRA(SampleRate,note->bitsPerSample,1,note->samples);

    maxAmp /= 0.8;
    i = 0;
    t = 0;
    amp = 1;
    oldfactor = factors[0];
    while (t < note->samples)
        {
        if (i >= h->samples) growRRA(h);

        h->data[0][i] = interp(note->data[0],note->samples,t); // * amp;

        if (t > factorLength - 1)
            t += factors[factorLength - 1];
        else
            {
            t += factors[(int) t];
            if ((int) t < factorLength && factors[(int) t] != oldfactor)
                {
                //printf("factor change from %.16f to %.16f at %f\n",oldfactor,
                    //factors[(int) t],t);
                if (amping == OFF)
                    {
                    maxAmp = maxAmp * 0.8;
                    amping = UP;
                    //printf("changing to UP at %f\n",t);
                    }
                //printf("   amping is %d, amp is %f\n",amping,amp);
                oldfactor = factors[(int) t];
                }
            }
        ++i;

        if (amping == UP)
            {
            amp *= 1.01;
            //printf("amping up %f at %f\n",amp,t);
            }

        if (amping == UP && amp >= maxAmp)
            {
            amping = DOWN;
            //printf("changing to DOWN at %f\n",t);
            }

        if (amping == DOWN)
            {
            amp *= 0.9999;
            //printf("amping down %f at %f\n",amp,t);
            }

        if (amp < 1)
            {
            amping = OFF;
            //printf("changing to OFF at %f\n",t);
            }

        if (amping == OFF) amp = 1;
        }

    //printf("factor length is %d\n",factorLength);

    /* remove the excess capacity */

    shrinkRRA(h,i);

    dDebug("leaving\n");

    return h;
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %edx, %ebx
movq %rsi, %r14
movq %rdi, %r15
movsd 0xafb0(%rip), %xmm0     # 0xf498
movsd %xmm0, 0x10(%rsp)
movl 0xaf3c(%rip), %edi      # 0xf430
movl 0x8(%r15), %esi
movl 0xc(%r15), %ecx
movl $0x1, %edx
callq 0x6f2c
movq %rax, %r12
movl 0xc(%r15), %esi
testl %esi, %esi
jle 0x467b
movsd (%r14), %xmm4
movsd 0x10(%rsp), %xmm2
divsd 0x5da0(%rip), %xmm2     # 0xa2c8
movq %rbx, 0x28(%rsp)
leal -0x1(%rbx), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x30(%rsp)
movslq %eax, %r13
xorpd %xmm0, %xmm0
movsd 0x5d14(%rip), %xmm3     # 0xa260
xorl %ebp, %ebp
movl $0x2, %ebx
movsd %xmm3, 0x8(%rsp)
movsd %xmm4, 0x18(%rsp)
movsd %xmm2, 0x10(%rsp)
cmpl 0xc(%r12), %ebp
movsd %xmm0, 0x20(%rsp)
jl 0x4584
movq %r12, %rdi
callq 0x7daa
movsd 0x20(%rsp), %xmm0
movl 0xc(%r15), %esi
movq 0x18(%r15), %rax
movq (%rax), %rdi
callq *0x1a75f(%rip)          # 0x1ecf0
movsd 0x20(%rsp), %xmm0
movq 0x18(%r12), %rcx
movq (%rcx), %rcx
movl %ebp, %edx
movl %eax, (%rcx,%rdx,4)
ucomisd 0x30(%rsp), %xmm0
jbe 0x45c0
addsd (%r14,%r13,8), %xmm0
movsd 0x10(%rsp), %xmm2
movsd 0x18(%rsp), %xmm4
jmp 0x4619
cvttsd2si %xmm0, %eax
cltq
addsd (%r14,%rax,8), %xmm0
cvttsd2si %xmm0, %eax
cmpl 0x28(%rsp), %eax
movsd 0x18(%rsp), %xmm4
jge 0x460d
cltq
movsd (%r14,%rax,8), %xmm1
ucomisd %xmm4, %xmm1
movsd 0x10(%rsp), %xmm2
jne 0x45f2
jnp 0x4619
cmpl $0x2, %ebx
jne 0x4615
mulsd 0x5cc9(%rip), %xmm2     # 0xa2c8
incl %ebp
movapd %xmm1, %xmm4
movsd 0x8(%rsp), %xmm1
jmp 0x462a
movsd 0x10(%rsp), %xmm2
jmp 0x4619
movapd %xmm1, %xmm4
movsd 0x8(%rsp), %xmm1
incl %ebp
cmpl $0x1, %ebx
je 0x463a
testl %ebx, %ebx
jne 0x4647
mulsd 0x5ca6(%rip), %xmm1     # 0xa2d8
xorl %ebx, %ebx
ucomisd %xmm2, %xmm1
jb 0x4647
mulsd 0x5c8e(%rip), %xmm1     # 0xa2d0
movl $0x1, %ebx
movsd 0x5c11(%rip), %xmm3     # 0xa260
ucomisd %xmm1, %xmm3
movl $0x2, %eax
cmoval %eax, %ebx
cmpl $0x2, %ebx
je 0x4664
movapd %xmm1, %xmm3
movl 0xc(%r15), %esi
xorps %xmm1, %xmm1
cvtsi2sd %esi, %xmm1
ucomisd %xmm0, %xmm1
ja 0x4553
jmp 0x467d
xorl %ebp, %ebp
movq %r12, %rdi
movl %ebp, %esi
callq 0x7db4
movq %r12, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq