// File: /workspace/github_c_makefile_star2_O3/LenShustek[P]miditones/miditones.c
// Function: remove_queue_entry
void remove_queue_entry(int ndx) { // remove the oldest queue entry
   struct queue_entry *q = &queue[ndx];
   if (q->delete) return; // if marked for deletion, just ignore it
   if (q->cmd == CMD_STOPNOTE) {
      if (loggen) fprintf(logfile, "      dequeue stopnote for %s\n", describe(&q->note));
      // find the tone generator playing this note, and record a pending stop
      int tgnum;
      for (tgnum = 0; tgnum < num_tonegens; ++tgnum) {
         struct tonegen_status *tg = &tonegen[tgnum];
         if (tg->playing
               && tg->note.note == q->note.note
               && tg->note.channel == q->note.channel) {  // found the note
            tg->stopnote_pending = true; // "stop note needed unless another start note follows"
            tg->playing = false; // free the tg to be reallocated, but note the stop time in case
            tg->note.time_usec = q->note.time_usec;  // the tg doesn't get used and we generate it
            if (loggen) fprintf(logfile, "      pending stop tgen %d %s\n", tgnum, describe(&q->note));
            break; } }
      if (tgnum >= num_tonegens) {
         // If we exited the loop without finding the generator playing this note, presumably it never started
         // because there weren't any free tone generators. Is there some assertion we can use to verify that?
         ++stopnotes_without_playnotes;
         if (loggen) fprintf(logfile, "      *** stopnote without playnote, %s\n", describe(&q->note)); } }

   else { // CMD_PLAYNOTE
      assert(q->cmd == CMD_PLAYNOTE, "bad cmd in remove_queue_entry");
      if (loggen) fprintf(logfile, "      dequeue playnote for %s\n", describe(&q->note));
      int tgnum = find_idle_tgen(&q->note);
      struct tonegen_status *tg = &tonegen[tgnum];
      if (tgnum >= 0) { // we found a tone generator we can use
         if (tgnum + 1 > num_tonegens_used) num_tonegens_used = tgnum + 1;
         if (tg->note.instrument != q->note.instrument) { // it's a new instrument for this generator
            tg->note.instrument = q->note.instrument;
            ++instrument_changes;
            if (loggen) fprintf(logfile, "      tgen %d changed to instrument %d\n", tgnum, tg->note.instrument);
            if (instrumentoutput) { // output a "change instrument" command
               if (binaryoutput) {
                  putc(CMD_INSTRUMENT | tgnum, outfile);
                  putc(tg->note.instrument, outfile);
                  outfile_bytecount += 2; }
               else if (asm_output) {
                  fprintf(outfile, "%s%02X", hex, CMD_INSTRUMENT | tgnum);
                  outfile_items(1);
                  fprintf(outfile, "%3d", tg->note.instrument);
                  outfile_items(1); }
               else {
                  fprintf(outfile, "0x%02X,%d", CMD_INSTRUMENT | tgnum, tg->note.instrument);
                  outfile_items(2); } } }
         if (loggen) fprintf(logfile, "      play tgen %d %s\n", tgnum, describe(&q->note));
         tg->playing = true;
         tg->stopnote_pending = false; // don't bother to issue "stop note"
         tg->note = q->note;  // structure copy of note info
         track[tg->note.track].preferred_tonegen = tgnum;
         ++note_on_commands;
         last_output_was_delay = false;
         if (binaryoutput) {
            putc(CMD_PLAYNOTE | tgnum, outfile);
            putc(tg->note.note, outfile);
            outfile_bytecount += 2;
            if (volume_output) {
               putc(tg->note.volume, outfile);
               outfile_bytecount +=1; } }
         else if (asm_output) {
            fprintf(outfile, "%s%02X", hex, CMD_PLAYNOTE | tgnum);
            outfile_items(1);
            fprintf(outfile, "%3d", tg->note.note);
            outfile_items(1);
            if (volume_output) {
               fprintf(outfile, "%3d", tg->note.volume);
               outfile_items(1); } }
         else {
            if (volume_output == 0) {
               fprintf(outfile, "0x%02X,%d", CMD_PLAYNOTE | tgnum, tg->note.note);
               outfile_items(2); }
            else {
               fprintf(outfile, "0x%02X,%d,%d", CMD_PLAYNOTE | tgnum, tg->note.note, tg->note.volume);
               outfile_items(3); } } }
      else {
         if (loggen) fprintf(logfile, "  *** at %lu.%03lu msec no free generator; skipping %s\n",
                                output_usec / 1000, output_usec % 1000, describe(&q->note));
         if (showskipped) printf("  *** no free generator %s\n",
                                    describe(&q->note)); ++notes_skipped; } } }

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movslq %edi, %rcx
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rax
addq %rcx, %rax
leaq 0x907d(%rip), %r15      # 0xb200
cmpb $0x0, 0x1(%r15,%rax)
je 0x2195
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
addq %rax, %r15
movb (%r15), %al
cmpb $-0x80, %al
jne 0x2213
cmpb $0x1, 0x701d(%rip)      # 0x91c3
jne 0x21d0
movq 0x7029(%rip), %rbx      # 0x91d8
leaq 0x4(%r15), %rdi
callq 0x1da6
leaq 0x3a27(%rip), %rsi      # 0x5be6
leaq 0x8fba(%rip), %rdx      # 0xb180
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10b0
movl 0x6f16(%rip), %eax      # 0x90ec
testl %eax, %eax
jle 0x231c
leaq 0x701b(%rip), %rcx      # 0x9200
xorl %ebx, %ebx
cmpb $0x1, (%rcx)
jne 0x2202
movl 0x10(%rcx), %edx
cmpl 0x10(%r15), %edx
jne 0x2202
movl 0xc(%rcx), %edx
cmpl 0xc(%r15), %edx
je 0x23f0
incq %rbx
addq $0x1c, %rcx
cmpq %rbx, %rax
jne 0x21e7
jmp 0x2326
xorl %edi, %edi
cmpb $-0x70, %al
sete %dil
leaq 0x3a2b(%rip), %rsi      # 0x5c4d
callq 0x1af6
cmpb $0x1, 0x6f95(%rip)      # 0x91c3
jne 0x2258
movq 0x6fa1(%rip), %rbx      # 0x91d8
leaq 0x4(%r15), %rdi
callq 0x1da6
leaq 0x3a24(%rip), %rsi      # 0x5c6b
leaq 0x8f32(%rip), %rdx      # 0xb180
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10b0
leaq 0x4(%r15), %r14
movq %r14, %rdi
callq 0x2034
testl %eax, %eax
js 0x236d
movl %eax, %ebx
cltq
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rcx
leaq 0x6f81(%rip), %r12      # 0x9200
addq %rax, %r12
addq %rcx, %r12
cmpl 0x6ead(%rip), %ebx      # 0x9138
jl 0x2296
leal 0x1(%rbx), %eax
movl %eax, 0x6ea2(%rip)      # 0x9138
movl 0x14(%r15), %ecx
cmpl %ecx, 0x14(%r12)
je 0x24bb
incl 0x6e91(%rip)            # 0x913c
movl %ecx, 0x14(%r12)
cmpb $0x1, 0x6f0c(%rip)      # 0x91c3
jne 0x22d0
movq 0x6f18(%rip), %rdi      # 0x91d8
leaq 0x39c3(%rip), %rsi      # 0x5c8a
movl %ebx, %edx
xorl %eax, %eax
callq 0x10b0
cmpb $0x1, 0x6eed(%rip)      # 0x91c4
jne 0x24bb
cmpb $0x1, 0x6edc(%rip)      # 0x91c0
jne 0x243e
movl %ebx, %edi
orl $0xc0, %edi
movq 0x6ef7(%rip), %rsi      # 0x91f0
callq 0x10a0
movl 0x14(%r12), %edi
movq 0x6ee6(%rip), %rsi      # 0x91f0
callq 0x10a0
addq $0x2, 0x6e71(%rip)      # 0x9188
jmp 0x24bb
xorl %ebx, %ebx
cmpl %eax, %ebx
jl 0x218b
incl 0x6e20(%rip)            # 0x914c
cmpb $0x1, 0x6e90(%rip)      # 0x91c3
jne 0x218b
movq 0x6e98(%rip), %rbx      # 0x91d8
addq $0x4, %r15
movq %r15, %rdi
callq 0x1da6
leaq 0x38d1(%rip), %rsi      # 0x5c24
leaq 0x8e26(%rip), %rdx      # 0xb180
movq %rbx, %rdi
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x10b0
cmpb $0x1, 0x6e4f(%rip)      # 0x91c3
jne 0x23bf
movq 0x6e5b(%rip), %rbx      # 0x91d8
movl 0x6e24(%rip), %r15d     # 0x91a8
imulq $0x10624dd3, %r15, %r12 # imm = 0x10624DD3
shrq $0x26, %r12
imull $0x3e8, %r12d, %eax     # imm = 0x3E8
subl %eax, %r15d
movq %r14, %rdi
callq 0x1da6
leaq 0x3943(%rip), %rsi      # 0x5ceb
leaq 0x8dd1(%rip), %r8       # 0xb180
movq %rbx, %rdi
movl %r12d, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x10b0
cmpb $0x1, 0x6e09(%rip)      # 0x91cf
jne 0x23e5
movq %r14, %rdi
callq 0x1da6
leaq 0x394c(%rip), %rdi      # 0x5d23
leaq 0x8da2(%rip), %rsi      # 0xb180
xorl %eax, %eax
callq 0x1080
incl 0x6d59(%rip)            # 0x9144
jmp 0x218b
movw $0x100, (%rcx)          # imm = 0x100
movl 0x4(%r15), %edx
movl %edx, 0x4(%rcx)
cmpb $0x0, 0x6dc0(%rip)      # 0x91c3
je 0x231e
leaq 0x4(%r15), %rdi
movq 0x6dc4(%rip), %r14      # 0x91d8
callq 0x1da6
leaq 0x37e5(%rip), %rsi      # 0x5c05
leaq 0x8d59(%rip), %rcx      # 0xb180
movq %r14, %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0x10b0
movl 0x6cb3(%rip), %eax      # 0x90ec
jmp 0x231e
movq 0x6dab(%rip), %rdi      # 0x91f0
cmpb $0x1, 0x6d7e(%rip)      # 0x91ca
jne 0x2496
movq 0x6c83(%rip), %rdx      # 0x90d8
movl %ebx, %ecx
orl $0xc0, %ecx
leaq 0x384e(%rip), %rsi      # 0x5cb2
xorl %eax, %eax
callq 0x10b0
movl $0x1, %edi
callq 0x1d12
movq 0x6d74(%rip), %rdi      # 0x91f0
movl 0x14(%r12), %edx
leaq 0x3831(%rip), %rsi      # 0x5cb9
xorl %eax, %eax
callq 0x10b0
movl $0x1, %edi
jmp 0x24b6
movl %ebx, %edx
orl $0xc0, %edx
movl 0x14(%r12), %ecx
leaq 0x3813(%rip), %rsi      # 0x5cbd
xorl %eax, %eax
callq 0x10b0
movl $0x2, %edi
callq 0x1d12
leaq 0x4(%r12), %r13
cmpb $0x1, 0x6cfc(%rip)      # 0x91c3
jne 0x24f2
movq 0x6d08(%rip), %r15      # 0x91d8
movq %r14, %rdi
callq 0x1da6
leaq 0x37e8(%rip), %rsi      # 0x5cc7
leaq 0x8c9a(%rip), %rcx      # 0xb180
movq %r15, %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0x10b0
movw $0x1, (%r12)
movq 0x10(%r14), %rax
movq %rax, 0x10(%r13)
movups (%r14), %xmm0
movups %xmm0, (%r13)
movslq 0x8(%r12), %rax
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
leaq 0x6ea2(%rip), %rcx      # 0x93c0
incl 0x6c1c(%rip)            # 0x9140
movl %ebx, 0x20(%rax,%rcx)
movb $0x0, 0x6c31(%rip)      # 0x9160
cmpb $0x1, 0x6c8a(%rip)      # 0x91c0
jne 0x258f
orl $0x90, %ebx
movq 0x6cab(%rip), %rsi      # 0x91f0
movl %ebx, %edi
callq 0x10a0
movl 0x10(%r12), %edi
movq 0x6c98(%rip), %rsi      # 0x91f0
callq 0x10a0
addq $0x2, 0x6c23(%rip)      # 0x9188
cmpb $0x1, 0x6c5f(%rip)      # 0x91cb
jne 0x218b
movl 0x18(%r12), %edi
movq 0x6c72(%rip), %rsi      # 0x91f0
callq 0x10a0
incq 0x6bfe(%rip)            # 0x9188
jmp 0x218b
cmpb $0x1, 0x6c34(%rip)      # 0x91ca
jne 0x261c
movq 0x6c4d(%rip), %rdi      # 0x91f0
movq 0x6b2e(%rip), %rdx      # 0x90d8
orl $0x90, %ebx
leaq 0x36fb(%rip), %rsi      # 0x5cb2
movl %ebx, %ecx
xorl %eax, %eax
callq 0x10b0
movl $0x1, %edi
callq 0x1d12
movq 0x6c1f(%rip), %rdi      # 0x91f0
movl 0x10(%r12), %edx
leaq 0x36dc(%rip), %rsi      # 0x5cb9
xorl %eax, %eax
callq 0x10b0
movl $0x1, %edi
callq 0x1d12
cmpb $0x1, 0x6bd6(%rip)      # 0x91cb
jne 0x218b
movq 0x6bee(%rip), %rdi      # 0x91f0
movl 0x18(%r12), %edx
leaq 0x36ab(%rip), %rsi      # 0x5cb9
xorl %eax, %eax
callq 0x10b0
movl $0x1, %edi
jmp 0x2668
movq 0x6bcd(%rip), %rdi      # 0x91f0
orl $0x90, %ebx
movl 0x10(%r12), %ecx
cmpb $0x0, 0x6b96(%rip)      # 0x91cb
je 0x2653
movl 0x18(%r12), %r8d
leaq 0x369b(%rip), %rsi      # 0x5cde
movl %ebx, %edx
xorl %eax, %eax
callq 0x10b0
movl $0x3, %edi
jmp 0x2668
leaq 0x3663(%rip), %rsi      # 0x5cbd
movl %ebx, %edx
xorl %eax, %eax
callq 0x10b0
movl $0x2, %edi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x1d12