// File: /workspace/github_c_makefile_star2_O0/pipde[P]deos-songlib/lib/songlib.c
// Function: useClosestChord
static void
useClosestChord(int *notes,int length)
    {
    int i,j;
    int drop,score,bestScore;
    int proposed[CHORD_LENGTH];
    int bestChord[CHORD_LENGTH];

    /*
     * if this is the first chord played, then just store the chord
     * if the length of the current chord is different then the length
     * of the previous chord, start over
     */

    for (i = 0; i < length; ++i)
        bestChord[i] = notes[i];

    if (lastChordLength == length)
        {
        /* 
         * choose best inversion for each note
         * if the lowest note of the previous chord is lower than the 
         *    root of the current chord, notes can only go down an octave
         * if the lowest note is higher, though, notes can only go up
         */

        /* assume, at the start, the current cord is the best chord */

        //printf("last chord: ");
        //for (i = 0; i < length; ++i) printf("%d ",lastChord[i]);
        //printf("\n");
        //printf("new chord: ");
        //for (i = 0; i < length; ++i) printf("%d ",notes[i]);
        //printf("\n");

        bestScore = INT_MAX;

        drop = lastChord[0] - notes[0];
        //printf("drop is %d\n",drop);

        for (i = 0; drop != 0 && i < length; ++i)
            {
            score = scoreLeading(lastChord,notes,proposed,length,i,drop > 0);
            //printf("proposed chord (%d): ",score);
            //for (j = 0; j < length; ++j) printf("%d ",proposed[j]);
            //printf("\n");
            if (score < bestScore)
                {
                bestScore = score;
                for (j = 0; j < length; ++j)
                    bestChord[j] = proposed[j];
                }
            }
        //printf("better chord: ");
        //for (i = 0; i < length; ++i) printf("%d ",bestChord[i]);
        //printf("\n");
        //getchar();
        }


    /* store this chord */
    for (i = 0; i < length; ++i)
        lastChord[i] = notes[i] = bestChord[i];
    lastChordLength = length;
    sort(lastChord,length);
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1030, %rsp           # imm = 0x1030
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl $0x0, -0x10(%rbp)
movl -0x10(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x4252
movq -0x8(%rbp), %rax
movslq -0x10(%rbp), %rcx
movl (%rax,%rcx,4), %ecx
movslq -0x10(%rbp), %rax
movl %ecx, -0x1020(%rbp,%rax,4)
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x4229
movl 0xe2b8(%rip), %eax      # 0x12510
cmpl -0xc(%rbp), %eax
jne 0x4327
movl $0x7fffffff, -0x20(%rbp) # imm = 0x7FFFFFFF
movl 0x1de32(%rip), %eax     # 0x220a0
movq -0x8(%rbp), %rcx
subl (%rcx), %eax
movl %eax, -0x18(%rbp)
movl $0x0, -0x10(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x18(%rbp)
movb %al, -0x1021(%rbp)
je 0x429b
movl -0x10(%rbp), %eax
cmpl -0xc(%rbp), %eax
setl %al
movb %al, -0x1021(%rbp)
movb -0x1021(%rbp), %al
testb $0x1, %al
jne 0x42a7
jmp 0x4325
movq -0x8(%rbp), %rsi
leaq -0x820(%rbp), %rdx
movl -0xc(%rbp), %ecx
movl -0x10(%rbp), %r8d
cmpl $0x0, -0x18(%rbp)
setg %al
andb $0x1, %al
movzbl %al, %r9d
leaq 0x1ddd3(%rip), %rdi     # 0x220a0
callq 0x8020
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x4315
movl -0x1c(%rbp), %eax
movl %eax, -0x20(%rbp)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x4313
movslq -0x14(%rbp), %rax
movl -0x820(%rbp,%rax,4), %ecx
movslq -0x14(%rbp), %rax
movl %ecx, -0x1020(%rbp,%rax,4)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x42ea
jmp 0x4315
jmp 0x4317
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x427e
jmp 0x4327
movl $0x0, -0x10(%rbp)
movl -0x10(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x4365
movslq -0x10(%rbp), %rax
movl -0x1020(%rbp,%rax,4), %edx
movq -0x8(%rbp), %rax
movslq -0x10(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movslq -0x10(%rbp), %rcx
leaq 0x1dd49(%rip), %rax     # 0x220a0
movl %edx, (%rax,%rcx,4)
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x432e
movl -0xc(%rbp), %eax
movl %eax, 0xe1a2(%rip)      # 0x12510
movl -0xc(%rbp), %esi
leaq 0x1dd28(%rip), %rdi     # 0x220a0
callq 0x81e0
addq $0x1030, %rsp           # imm = 0x1030
popq %rbp
retq
nopw %cs:(%rax,%rax)