// File: /workspace/github_c_makefile_star2_O2/pipde[P]deos-songlib/lib/songlib.c
// Function: adraw
void
adraw(double length,int inst,int count,int* octaves,int *pitches,double *lengths)
    {
    int i,j;
    int index;
    int octave,pitch;
    RRA *note = 0,*resampled = 0;
    double beats;
    double factor,oldfactor,*factors;
    int baseNote,factorCount;

    dTrace("entering\n");;
    //printf("count is %d\n",count);

    index = 0;
    factor = 1.0;

    /* extract the arguments */

    for (i = 0; i < count; ++i)
        {
        double delta;
        int steps;

        octave = octaves[i];
        pitch = pitches[i];
        beats = lengths[i];

        //printf("[%d,%d,%f]\n",octave,pitch,beats);

        if (i == 0)
            {
            baseNote = octave * NOTES + pitch;
            if (baseNote < 0 || baseNote >= OCTAVES * NOTES)
                Fatal("adraw: bad octave/pitch pair: "
                    "%d,%d\n",octave,pitch);
            note = getNumberedNote(inst,baseNote);
            //printf("%d samples total\n",note->samples);
            factors = (double *) New(sizeof(double) * note->samples);
            factor = 1;
            oldfactor = 1;
            //printf("resampling factor starting at %f\n",factor);
            }
        else
            {
            oldfactor = factor;
            factor = pow(SEMITONE,(octave * NOTES + pitch) - baseNote);
            //printf("resampling factor changing from %f to %f\n",oldfactor,factor);
            }

        factorCount = (int) (beatsToSamples(beats) * factor);
        //printf("factorCount is %d\n",factorCount);
        if (factorCount == 0 && drawRamp <= 0.0)
            steps = 1;
        else
            steps = SampleRate * beatsToSeconds(drawRamp);
        if (steps < 1) steps = 1;
        delta = (factor - oldfactor) / steps;
        //printf("delta is %f, factorCount is %d, steps is %d\n",
        //    delta,factorCount,steps);

        for (j = 0; j < factorCount; ++j)
            {
            factors [index++] = oldfactor;
            if (j < steps) oldfactor += delta;
            }
        }

    /* fill out the rest of the array with the last value of factor */

    for ( ; index < note->samples; ++index)
        factors[index] = oldfactor;

    resampled = aresample(note,factors,index);

    assert(note->samples == index);
    
    dplay(length,resampled->data[0],resampled->samples,note->bitsPerSample);

    freeRRA(resampled,0);
    free(factors);
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r8, 0x28(%rsp)
movq %rcx, 0x20(%rsp)
movq %rdx, 0x18(%rsp)
movl %edi, 0x4(%rsp)
movsd %xmm0, 0x10(%rsp)
xorl %eax, %eax
testl %esi, %esi
cmovlel %eax, %esi
movq %rsi, 0x30(%rsp)
movsd 0x54bf(%rip), %xmm2     # 0x9260
xorl %r15d, %r15d
movq $0x0, 0x8(%rsp)
xorl %r14d, %r14d
movslq %r14d, %rbp
cmpq 0x30(%rsp), %r15
je 0x3f14
movq 0x18(%rsp), %rax
movl (%rax,%r15,4), %esi
movq 0x20(%rsp), %rax
movl (%rax,%r15,4), %edx
movq 0x28(%rsp), %rax
movsd (%rax,%r15,8), %xmm0
movsd %xmm0, 0x40(%rsp)
imull $0xc, %esi, %r12d
testq %r15, %r15
je 0x3e11
subl %ebx, %edx
addl %r12d, %edx
cvtsi2sd %edx, %xmm1
movsd 0x548d(%rip), %xmm0     # 0x9288
movsd %xmm2, 0x38(%rsp)
callq 0x20b0
movsd 0x38(%rsp), %xmm6
movl %ebx, %r12d
jmp 0x3e5a
addl %edx, %r12d
cmpl $0x84, %r12d
jb 0x3e2b
leaq 0x5621(%rip), %rdi      # 0x9445
xorl %eax, %eax
callq 0x6378
movl 0x4(%rsp), %edi
movl %r12d, %esi
callq 0x2c35
movq %rax, 0x8(%rsp)
movl 0xc(%rax), %edi
shll $0x3, %edi
callq 0x652d
movq %rax, %r13
movsd 0x540e(%rip), %xmm1     # 0x9260
movapd %xmm1, %xmm0
movapd %xmm1, %xmm6
movsd 0x5416(%rip), %xmm5     # 0x9278
movsd 0x40(%rsp), %xmm3
mulsd %xmm5, %xmm3
movsd 0xa63c(%rip), %xmm1     # 0xe4b0
divsd %xmm1, %xmm3
cvtsi2sdl 0xa5b0(%rip), %xmm2 # 0xe430
mulsd %xmm2, %xmm3
mulsd %xmm0, %xmm3
cvttsd2si %xmm3, %eax
movsd 0xa5fc(%rip), %xmm3     # 0xe490
movapd %xmm3, %xmm4
mulsd %xmm5, %xmm4
divsd %xmm1, %xmm4
mulsd %xmm2, %xmm4
cvttsd2si %xmm4, %edx
cmpl $0x2, %edx
pushq $0x1
popq %rsi
cmovll %esi, %edx
xorpd %xmm1, %xmm1
ucomisd %xmm3, %xmm1
movl %edx, %ecx
cmovael %esi, %ecx
testl %eax, %eax
cmovnel %edx, %ecx
movapd %xmm0, %xmm2
subsd %xmm6, %xmm2
cvtsi2sd %ecx, %xmm1
movl $0x0, %edx
cmovlel %edx, %eax
divsd %xmm1, %xmm2
leaq (,%rbp,8), %rdx
addq %r13, %rdx
xorl %esi, %esi
movapd %xmm6, %xmm1
cmpl %esi, %eax
je 0x3f02
movsd %xmm1, (%rdx,%rsi,8)
cmpl %ecx, %esi
jae 0x3efd
addsd %xmm2, %xmm1
incq %rsi
jmp 0x3eec
addl %esi, %r14d
incq %r15
movapd %xmm0, %xmm2
movl %r12d, %ebx
jmp 0x3db0
movq 0x8(%rsp), %r14
movslq 0xc(%r14), %rax
cmpq %rax, %rbp
jge 0x3f2e
movsd %xmm1, (%r13,%rbp,8)
incq %rbp
jmp 0x3f1d
movq %r14, %rdi
movq %r13, %rsi
movl %ebp, %edx
callq 0x3fad
cmpl %ebp, 0xc(%r14)
jne 0x3f7d
movq %rax, %rbx
movq 0x18(%rax), %rax
movq (%rax), %rdi
movl 0xc(%rbx), %esi
movl 0x8(%r14), %edx
movsd 0x10(%rsp), %xmm0
callq 0x2e63
movq %rbx, %rdi
xorl %esi, %esi
callq 0x67ac
movq %r13, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2040
leaq 0x54e6(%rip), %rdi      # 0x946a
leaq 0x54f6(%rip), %rsi      # 0x9481
leaq 0x54f9(%rip), %rcx      # 0x948b
movl $0x412, %edx            # imm = 0x412
callq 0x2120