// File: /workspace/github_c_makefile_star2_O2/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 0xa4cd(%rip), %xmm0     # 0xe498
movsd %xmm0, (%rsp)
movl 0xa45a(%rip), %edi      # 0xe430
movl 0x8(%r15), %esi
movl 0xc(%r15), %ecx
pushq $0x1
popq %rdx
callq 0x6670
movq %rax, %r12
movsd (%rsp), %xmm0
divsd 0x52ca(%rip), %xmm0     # 0x92c0
movsd %xmm0, (%rsp)
movsd (%r14), %xmm1
movq %rbx, 0x28(%rsp)
leal -0x1(%rbx), %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x30(%rsp)
cltq
movq %rax, 0x20(%rsp)
movsd 0x523f(%rip), %xmm3     # 0x9260
xorpd %xmm2, %xmm2
pushq $0x2
popq %rbp
xorl %r13d, %r13d
movl %ebp, %ebx
movl 0xc(%r15), %esi
cvtsi2sd %esi, %xmm0
ucomisd %xmm2, %xmm0
jbe 0x4150
movsd %xmm3, 0x8(%rsp)
movsd %xmm1, 0x10(%rsp)
movslq 0xc(%r12), %rax
cmpq %rax, %r13
movsd %xmm2, 0x18(%rsp)
jl 0x406d
movq %r12, %rdi
callq 0x73a1
movsd 0x18(%rsp), %xmm2
movl 0xc(%r15), %esi
movq 0x18(%r15), %rax
movq (%rax), %rdi
movapd %xmm2, %xmm0
callq *0x19c72(%rip)          # 0x1dcf0
movsd 0x18(%rsp), %xmm2
movq 0x18(%r12), %rcx
movq (%rcx), %rcx
movl %eax, (%rcx,%r13,4)
ucomisd 0x30(%rsp), %xmm2
jbe 0x40b1
movq 0x20(%rsp), %rax
addsd (%r14,%rax,8), %xmm2
movsd 0x10(%rsp), %xmm1
movsd 0x8(%rsp), %xmm3
jmp 0x4107
cvttsd2si %xmm2, %eax
cltq
addsd (%r14,%rax,8), %xmm2
cvttsd2si %xmm2, %eax
cmpl 0x28(%rsp), %eax
movsd 0x10(%rsp), %xmm1
movsd 0x8(%rsp), %xmm3
jge 0x4107
cltq
movsd (%r14,%rax,8), %xmm0
ucomisd %xmm1, %xmm0
jne 0x40e3
jnp 0x4107
cmpl $0x2, %ebx
jne 0x4103
movsd (%rsp), %xmm1
mulsd 0x51cb(%rip), %xmm1     # 0x92c0
movsd %xmm1, (%rsp)
incq %r13
movapd %xmm0, %xmm1
jmp 0x4113
movapd %xmm0, %xmm1
incq %r13
cmpl $0x1, %ebx
je 0x4124
testl %ebx, %ebx
jne 0x412f
mulsd 0x51b5(%rip), %xmm3     # 0x92d0
xorl %ebx, %ebx
ucomisd (%rsp), %xmm3
jb 0x412f
mulsd 0x519c(%rip), %xmm3     # 0x92c8
pushq $0x1
popq %rbx
movsd 0x5129(%rip), %xmm0     # 0x9260
ucomisd %xmm3, %xmm0
cmoval %ebp, %ebx
cmpl $0x2, %ebx
je 0x4147
movapd %xmm3, %xmm0
movapd %xmm0, %xmm3
jmp 0x402d
movq %r12, %rdi
movl %r13d, %esi
callq 0x73ab
movq %r12, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq