// File: /workspace/github_c_makefile_star2_O2/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movslq %edi, %rax
imulq $0x1c, %rax, %rax
leaq 0x91d1(%rip), %r15      # 0xb200
cmpb $0x0, 0x1(%rax,%r15)
je 0x2046
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq %rax, %r15
movb (%r15), %al
cmpb $-0x80, %al
jne 0x20c7
cmpb $0x1, 0x716c(%rip)      # 0x91c3
jne 0x2081
movq 0x7178(%rip), %rbx      # 0x91d8
leaq 0x4(%r15), %rdi
callq 0x1c93
leaq 0x3b76(%rip), %rsi      # 0x5be6
leaq 0x9109(%rip), %rdx      # 0xb180
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10b0
movl 0x7065(%rip), %eax      # 0x90ec
xorl %ebx, %ebx
testl %eax, %eax
movl $0x0, %edx
cmovgl %eax, %edx
leaq 0x7166(%rip), %rcx      # 0x9200
cmpq %rbx, %rdx
je 0x2290
cmpb $0x1, (%rcx)
jne 0x20be
movl 0x10(%rcx), %esi
cmpl 0x10(%r15), %esi
jne 0x20be
movl 0xc(%rcx), %esi
cmpl 0xc(%r15), %esi
je 0x2243
incq %rbx
addq $0x1c, %rcx
jmp 0x209a
xorl %edi, %edi
cmpb $-0x70, %al
sete %dil
leaq 0x3b77(%rip), %rsi      # 0x5c4d
callq 0x19f1
cmpb $0x1, 0x70e1(%rip)      # 0x91c3
jne 0x210c
movq 0x70ed(%rip), %rbx      # 0x91d8
leaq 0x4(%r15), %rdi
callq 0x1c93
leaq 0x3b70(%rip), %rsi      # 0x5c6b
leaq 0x907e(%rip), %rdx      # 0xb180
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10b0
leaq 0x4(%r15), %r14
movq %r14, %rdi
callq 0x1f01
testl %eax, %eax
js 0x21c9
movl %eax, %ebx
cltq
imulq $0x1c, %rax, %rax
leaq 0x70d1(%rip), %r12      # 0x9200
addq %rax, %r12
cmpl 0x7000(%rip), %ebx      # 0x9138
jl 0x2143
leal 0x1(%rbx), %eax
movl %eax, 0x6ff5(%rip)      # 0x9138
movl 0x14(%r15), %ecx
cmpl %ecx, 0x14(%r12)
je 0x2354
incl 0x6fe4(%rip)            # 0x913c
movl %ecx, 0x14(%r12)
cmpb $0x1, 0x705f(%rip)      # 0x91c3
jne 0x217d
movq 0x706b(%rip), %rdi      # 0x91d8
leaq 0x3b16(%rip), %rsi      # 0x5c8a
movl %ebx, %edx
xorl %eax, %eax
callq 0x10b0
cmpb $0x1, 0x7040(%rip)      # 0x91c4
jne 0x2354
cmpb $0x1, 0x702f(%rip)      # 0x91c0
jne 0x22dc
movl %ebx, %edi
orl $0xc0, %edi
movq 0x704a(%rip), %rsi      # 0x91f0
callq 0x10a0
movl 0x14(%r12), %edi
movq 0x7039(%rip), %rsi      # 0x91f0
callq 0x10a0
addq $0x2, 0x6fc4(%rip)      # 0x9188
jmp 0x2354
cmpb $0x1, 0x6ff3(%rip)      # 0x91c3
jne 0x2212
movq 0x6fff(%rip), %rbx      # 0x91d8
movl 0x6fc9(%rip), %eax      # 0x91a8
movl $0x3e8, %ecx            # imm = 0x3E8
xorl %edx, %edx
divl %ecx
movl %edx, %ebp
movl %eax, %r15d
movq %r14, %rdi
callq 0x1c93
leaq 0x3aef(%rip), %rsi      # 0x5ceb
leaq 0x8f7d(%rip), %r8       # 0xb180
movq %rbx, %rdi
movl %r15d, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x10b0
cmpb $0x1, 0x6fb6(%rip)      # 0x91cf
jne 0x2238
movq %r14, %rdi
callq 0x1c93
leaq 0x3af9(%rip), %rdi      # 0x5d23
leaq 0x8f4f(%rip), %rsi      # 0xb180
xorl %eax, %eax
callq 0x1080
incl 0x6f06(%rip)            # 0x9144
jmp 0x2037
movw $0x100, (%rcx)          # imm = 0x100
movl 0x4(%r15), %edx
movl %edx, 0x4(%rcx)
cmpb $0x1, 0x6f6d(%rip)      # 0x91c3
jne 0x2288
leaq 0x4(%r15), %rdi
movq 0x6f75(%rip), %r14      # 0x91d8
callq 0x1c93
leaq 0x3996(%rip), %rsi      # 0x5c05
leaq 0x8f0a(%rip), %rcx      # 0xb180
movq %r14, %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0x10b0
movl 0x6e64(%rip), %eax      # 0x90ec
cmpl %ebx, %eax
jg 0x2037
incl 0x6eb6(%rip)            # 0x914c
cmpb $0x1, 0x6f26(%rip)      # 0x91c3
jne 0x2037
movq 0x6f2e(%rip), %rbx      # 0x91d8
addq $0x4, %r15
movq %r15, %rdi
callq 0x1c93
leaq 0x3967(%rip), %rsi      # 0x5c24
leaq 0x8ebc(%rip), %rdx      # 0xb180
movq %rbx, %rdi
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10b0
movq 0x6f0d(%rip), %rdi      # 0x91f0
cmpb $0x1, 0x6ee0(%rip)      # 0x91ca
jne 0x2331
movq 0x6de5(%rip), %rdx      # 0x90d8
movl %ebx, %ecx
orl $0xc0, %ecx
leaq 0x39b0(%rip), %rsi      # 0x5cb2
xorl %eax, %eax
callq 0x10b0
pushq $0x1
popq %rbp
movl %ebp, %edi
callq 0x1c0a
movq 0x6ed6(%rip), %rdi      # 0x91f0
movl 0x14(%r12), %edx
leaq 0x3993(%rip), %rsi      # 0x5cb9
xorl %eax, %eax
callq 0x10b0
movl %ebp, %edi
jmp 0x234f
movl %ebx, %edx
orl $0xc0, %edx
movl 0x14(%r12), %ecx
leaq 0x3978(%rip), %rsi      # 0x5cbd
xorl %eax, %eax
callq 0x10b0
pushq $0x2
popq %rdi
callq 0x1c0a
leaq 0x4(%r12), %r13
cmpb $0x1, 0x6e63(%rip)      # 0x91c3
jne 0x238b
movq 0x6e6f(%rip), %r15      # 0x91d8
movq %r14, %rdi
callq 0x1c93
leaq 0x394f(%rip), %rsi      # 0x5cc7
leaq 0x8e01(%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
imulq $0x30, %rax, %rax
leaq 0x700d(%rip), %rcx      # 0x93c0
incl 0x6d87(%rip)            # 0x9140
movl %ebx, 0x20(%rax,%rcx)
movb $0x0, 0x6d9c(%rip)      # 0x9160
cmpb $0x1, 0x6df5(%rip)      # 0x91c0
jne 0x2424
orl $0x90, %ebx
movq 0x6e16(%rip), %rsi      # 0x91f0
movl %ebx, %edi
callq 0x10a0
movl 0x10(%r12), %edi
movq 0x6e03(%rip), %rsi      # 0x91f0
callq 0x10a0
addq $0x2, 0x6d8e(%rip)      # 0x9188
cmpb $0x1, 0x6dca(%rip)      # 0x91cb
jne 0x2037
movl 0x18(%r12), %edi
movq 0x6ddd(%rip), %rsi      # 0x91f0
callq 0x10a0
incq 0x6d69(%rip)            # 0x9188
jmp 0x2037
cmpb $0x1, 0x6d9f(%rip)      # 0x91ca
jne 0x24a7
movq 0x6dbc(%rip), %rdi      # 0x91f0
movq 0x6c9d(%rip), %rdx      # 0x90d8
orl $0x90, %ebx
leaq 0x386a(%rip), %rsi      # 0x5cb2
movl %ebx, %ecx
xorl %eax, %eax
callq 0x10b0
pushq $0x1
popq %rbx
movl %ebx, %edi
callq 0x1c0a
movq 0x6d8e(%rip), %rdi      # 0x91f0
movl 0x10(%r12), %edx
leaq 0x384b(%rip), %rsi      # 0x5cb9
xorl %eax, %eax
callq 0x10b0
movl %ebx, %edi
callq 0x1c0a
cmpb $0x1, 0x6d48(%rip)      # 0x91cb
jne 0x2037
movq 0x6d60(%rip), %rdi      # 0x91f0
movl 0x18(%r12), %edx
leaq 0x381d(%rip), %rsi      # 0x5cb9
xorl %eax, %eax
callq 0x10b0
pushq $0x1
jmp 0x24ed
movq 0x6d42(%rip), %rdi      # 0x91f0
orl $0x90, %ebx
movl 0x10(%r12), %ecx
cmpb $0x0, 0x6d0b(%rip)      # 0x91cb
je 0x24db
movl 0x18(%r12), %r8d
leaq 0x3810(%rip), %rsi      # 0x5cde
movl %ebx, %edx
xorl %eax, %eax
callq 0x10b0
pushq $0x3
jmp 0x24ed
leaq 0x37db(%rip), %rsi      # 0x5cbd
movl %ebx, %edx
xorl %eax, %eax
callq 0x10b0
pushq $0x2
popq %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1c0a