// File: /workspace/github_c_makefile_star2_O1/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 0x8fae(%rip), %r15      # 0xb200
cmpb $0x0, 0x1(%r15,%rax)
je 0x2264
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
addq %rax, %r15
movb (%r15), %al
cmpb $-0x80, %al
jne 0x22e4
cmpb $0x1, 0x6f4e(%rip)      # 0x91c3
jne 0x229f
movq 0x6f5a(%rip), %rbx      # 0x91d8
leaq 0x4(%r15), %rdi
callq 0x1e3b
leaq 0x3958(%rip), %rsi      # 0x5be6
leaq 0x8eeb(%rip), %rdx      # 0xb180
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10b0
movl 0x6e47(%rip), %eax      # 0x90ec
testl %eax, %eax
jle 0x23ed
leaq 0x6f4c(%rip), %rcx      # 0x9200
xorl %ebx, %ebx
cmpb $0x1, (%rcx)
jne 0x22d1
movl 0x10(%rcx), %edx
cmpl 0x10(%r15), %edx
jne 0x22d1
movl 0xc(%rcx), %edx
cmpl 0xc(%r15), %edx
je 0x24c5
incq %rbx
addq $0x1c, %rcx
cmpq %rbx, %rax
jne 0x22b6
movl %eax, %ebx
jmp 0x23ef
xorl %edi, %edi
cmpb $-0x70, %al
sete %dil
leaq 0x395a(%rip), %rsi      # 0x5c4d
callq 0x1b7b
cmpb $0x1, 0x6ec4(%rip)      # 0x91c3
jne 0x2329
movq 0x6ed0(%rip), %rbx      # 0x91d8
leaq 0x4(%r15), %rdi
callq 0x1e3b
leaq 0x3953(%rip), %rsi      # 0x5c6b
leaq 0x8e61(%rip), %rdx      # 0xb180
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10b0
leaq 0x4(%r15), %r14
movq %r14, %rdi
callq 0x20d1
testl %eax, %eax
js 0x2442
movl %eax, %ebx
cltq
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rcx
leaq 0x6eb0(%rip), %r12      # 0x9200
addq %rax, %r12
addq %rcx, %r12
cmpl 0x6ddc(%rip), %ebx      # 0x9138
jl 0x2367
leal 0x1(%rbx), %eax
movl %eax, 0x6dd1(%rip)      # 0x9138
movl 0x14(%r15), %ecx
cmpl %ecx, 0x14(%r12)
je 0x258a
incl 0x6dc0(%rip)            # 0x913c
movl %ecx, 0x14(%r12)
cmpb $0x1, 0x6e3b(%rip)      # 0x91c3
jne 0x23a1
movq 0x6e47(%rip), %rdi      # 0x91d8
leaq 0x38f2(%rip), %rsi      # 0x5c8a
movl %ebx, %edx
xorl %eax, %eax
callq 0x10b0
cmpb $0x1, 0x6e1c(%rip)      # 0x91c4
jne 0x258a
cmpb $0x1, 0x6e0b(%rip)      # 0x91c0
jne 0x250d
movl %ebx, %edi
orl $0xc0, %edi
movq 0x6e26(%rip), %rsi      # 0x91f0
callq 0x10a0
movl 0x14(%r12), %edi
movq 0x6e15(%rip), %rsi      # 0x91f0
callq 0x10a0
addq $0x2, 0x6da0(%rip)      # 0x9188
jmp 0x258a
xorl %ebx, %ebx
cmpl 0x6cf7(%rip), %ebx      # 0x90ec
jl 0x225a
incl 0x6d4b(%rip)            # 0x914c
cmpb $0x1, 0x6dbb(%rip)      # 0x91c3
jne 0x225a
movq 0x6dc3(%rip), %rbx      # 0x91d8
addq $0x4, %r15
movq %r15, %rdi
callq 0x1e3b
leaq 0x37fc(%rip), %rsi      # 0x5c24
leaq 0x8d51(%rip), %rdx      # 0xb180
movq %rbx, %rdi
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x10b0
cmpb $0x1, 0x6d7a(%rip)      # 0x91c3
jne 0x2494
movq 0x6d86(%rip), %rbx      # 0x91d8
movl 0x6d4f(%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 0x1e3b
leaq 0x386e(%rip), %rsi      # 0x5ceb
leaq 0x8cfc(%rip), %r8       # 0xb180
movq %rbx, %rdi
movl %r12d, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x10b0
cmpb $0x1, 0x6d34(%rip)      # 0x91cf
jne 0x24ba
movq %r14, %rdi
callq 0x1e3b
leaq 0x3877(%rip), %rdi      # 0x5d23
leaq 0x8ccd(%rip), %rsi      # 0xb180
xorl %eax, %eax
callq 0x1080
incl 0x6c84(%rip)            # 0x9144
jmp 0x225a
movw $0x100, (%rcx)          # imm = 0x100
movl 0x4(%r15), %eax
movl %eax, 0x4(%rcx)
cmpb $0x0, 0x6ceb(%rip)      # 0x91c3
je 0x23ef
leaq 0x4(%r15), %rdi
movq 0x6cef(%rip), %r14      # 0x91d8
callq 0x1e3b
leaq 0x3710(%rip), %rsi      # 0x5c05
leaq 0x8c84(%rip), %rcx      # 0xb180
movq %r14, %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0x10b0
jmp 0x23ef
movq 0x6cdc(%rip), %rdi      # 0x91f0
cmpb $0x1, 0x6caf(%rip)      # 0x91ca
jne 0x2565
movq 0x6bb4(%rip), %rdx      # 0x90d8
movl %ebx, %ecx
orl $0xc0, %ecx
leaq 0x377f(%rip), %rsi      # 0x5cb2
xorl %eax, %eax
callq 0x10b0
movl $0x1, %edi
callq 0x1da7
movq 0x6ca5(%rip), %rdi      # 0x91f0
movl 0x14(%r12), %edx
leaq 0x3762(%rip), %rsi      # 0x5cb9
xorl %eax, %eax
callq 0x10b0
movl $0x1, %edi
jmp 0x2585
movl %ebx, %edx
orl $0xc0, %edx
movl 0x14(%r12), %ecx
leaq 0x3744(%rip), %rsi      # 0x5cbd
xorl %eax, %eax
callq 0x10b0
movl $0x2, %edi
callq 0x1da7
leaq 0x4(%r12), %r13
cmpb $0x1, 0x6c2d(%rip)      # 0x91c3
jne 0x25c1
movq 0x6c39(%rip), %r15      # 0x91d8
movq %r14, %rdi
callq 0x1e3b
leaq 0x3719(%rip), %rsi      # 0x5cc7
leaq 0x8bcb(%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 0x6dd3(%rip), %rcx      # 0x93c0
incl 0x6b4d(%rip)            # 0x9140
movl %ebx, 0x20(%rax,%rcx)
movb $0x0, 0x6b62(%rip)      # 0x9160
cmpb $0x1, 0x6bbb(%rip)      # 0x91c0
jne 0x265e
orl $0x90, %ebx
movq 0x6bdc(%rip), %rsi      # 0x91f0
movl %ebx, %edi
callq 0x10a0
movl 0x10(%r12), %edi
movq 0x6bc9(%rip), %rsi      # 0x91f0
callq 0x10a0
addq $0x2, 0x6b54(%rip)      # 0x9188
cmpb $0x1, 0x6b90(%rip)      # 0x91cb
jne 0x225a
movl 0x18(%r12), %edi
movq 0x6ba3(%rip), %rsi      # 0x91f0
callq 0x10a0
incq 0x6b2f(%rip)            # 0x9188
jmp 0x225a
cmpb $0x1, 0x6b65(%rip)      # 0x91ca
jne 0x26eb
movq 0x6b7e(%rip), %rdi      # 0x91f0
movq 0x6a5f(%rip), %rdx      # 0x90d8
orl $0x90, %ebx
leaq 0x362c(%rip), %rsi      # 0x5cb2
movl %ebx, %ecx
xorl %eax, %eax
callq 0x10b0
movl $0x1, %edi
callq 0x1da7
movq 0x6b50(%rip), %rdi      # 0x91f0
movl 0x10(%r12), %edx
leaq 0x360d(%rip), %rsi      # 0x5cb9
xorl %eax, %eax
callq 0x10b0
movl $0x1, %edi
callq 0x1da7
cmpb $0x1, 0x6b07(%rip)      # 0x91cb
jne 0x225a
movq 0x6b1f(%rip), %rdi      # 0x91f0
movl 0x18(%r12), %edx
leaq 0x35dc(%rip), %rsi      # 0x5cb9
xorl %eax, %eax
callq 0x10b0
movl $0x1, %edi
jmp 0x2737
movq 0x6afe(%rip), %rdi      # 0x91f0
orl $0x90, %ebx
movl 0x10(%r12), %ecx
cmpb $0x0, 0x6ac7(%rip)      # 0x91cb
je 0x2722
movl 0x18(%r12), %r8d
leaq 0x35cc(%rip), %rsi      # 0x5cde
movl %ebx, %edx
xorl %eax, %eax
callq 0x10b0
movl $0x3, %edi
jmp 0x2737
leaq 0x3594(%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 0x1da7