// File: /workspace/github_c_makefile_star2_O2/LenShustek[P]miditones/miditones.c
// Function: queue_cmd
void queue_cmd(byte cmd, struct noteinfo *np) {
   if (loggen) fprintf(logfile, "  queue %s %s\n",
                          cmd == CMD_PLAYNOTE ? "PLAY" : cmd == CMD_STOPNOTE ? "STOP" : "????",
                          describe(np));
   if (queue_numitems >= QUEUE_SIZE) pull_queue();
   assert(queue_numitems < QUEUE_SIZE, "no room in queue");
   timestamp horizon = output_usec + output_deficit_usec;
   if (np->time_usec < horizon) { // don't allow revisionist history
      if (loggen) fprintf(logfile, "  event delayed by %lu usec because queue is too small\n",
                             horizon - np->time_usec);
      np->time_usec = horizon;
      ++events_delayed; }
   int ndx;
   if (queue_numitems == 0) { // queue is empty; restart it
      ndx = queue_oldest_ndx = queue_newest_ndx = queue_numitems = 0; }
   else { // find a place to insert the new entry in time order
      // this is a stable incremental insertion sort
      ndx = queue_newest_ndx; // start with newest, since we are most often newer
      while (queue[ndx].note.time_usec > np->time_usec) { // search backwards for something as new or older
         if (ndx == queue_oldest_ndx) { // none: we are oldest; add to the start
            if (--queue_oldest_ndx < 0) queue_oldest_ndx = QUEUE_SIZE - 1;
            ndx = queue_oldest_ndx;
            goto insert; }
         if (--ndx < 0) ndx = QUEUE_SIZE - 1; }
      // we are to insert the new item after "ndx", so shift all later entries down, if any
      int from_ndx, to_ndx;
      if (++queue_newest_ndx >= QUEUE_SIZE) queue_newest_ndx = 0;
      to_ndx = queue_newest_ndx;
      while (1) {
         if ((from_ndx = to_ndx - 1) < 0) from_ndx = QUEUE_SIZE - 1;
         if (from_ndx == ndx) break;
         queue[to_ndx] = queue[from_ndx]; // structure copy
         to_ndx = from_ndx; }
      if (++ndx >= QUEUE_SIZE) ndx = 0; }
insert: // store the item at ndx
   ++queue_numitems;
   queue[ndx].cmd = cmd;   // fill in the queue entry
   queue[ndx].delete = false;
   queue[ndx].note = *np;  // structure copy of the note
   if (noduplicates && cmd == CMD_STOPNOTE) remove_queue_duplicates(ndx); }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rsi, %r14
movl %edi, %ebx
cmpb $0x1, 0x6637(%rip)      # 0x91c3
jne 0x2bdb
movq 0x6643(%rip), %r15      # 0x91d8
cmpb $-0x80, %bl
leaq 0x2f72(%rip), %rax      # 0x5b11
leaq 0x33a1(%rip), %rcx      # 0x5f47
cmoveq %rax, %rcx
cmpb $-0x70, %bl
leaq 0x2f58(%rip), %r12      # 0x5b0c
cmovneq %rcx, %r12
movq %r14, %rdi
callq 0x1c93
leaq 0x3371(%rip), %rsi      # 0x5f38
leaq 0x85b2(%rip), %rcx      # 0xb180
movq %r15, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x10b0
movb $0x1, %al
cmpl $0x64, 0x8600(%rip)     # 0xb1e4
jl 0x2bf5
callq 0x2633
cmpl $0x64, 0x85f2(%rip)     # 0xb1e4
setl %al
movzbl %al, %edi
leaq 0x334d(%rip), %rsi      # 0x5f4c
callq 0x19f1
movl 0x65a2(%rip), %ebp      # 0x91ac
addl 0x6598(%rip), %ebp      # 0x91a8
movl %ebp, %edx
subl (%r14), %edx
jbe 0x2c3e
cmpb $0x1, 0x65a5(%rip)      # 0x91c3
jne 0x2c35
movq 0x65b1(%rip), %rdi      # 0x91d8
leaq 0x332f(%rip), %rsi      # 0x5f5d
xorl %eax, %eax
callq 0x10b0
movl %ebp, (%r14)
incl 0x650a(%rip)            # 0x9148
movl 0x85a0(%rip), %eax      # 0xb1e4
testl %eax, %eax
je 0x2c81
movl 0x859e(%rip), %edi      # 0xb1ec
movl (%r14), %r9d
movl 0x8590(%rip), %r8d      # 0xb1e8
leaq 0x85a1(%rip), %rdx      # 0xb200
pushq $0x63
popq %rcx
movl %edi, %esi
movslq %esi, %r10
imulq $0x1c, %r10, %r10
cmpl %r9d, 0x4(%r10,%rdx)
jbe 0x2c93
cmpl %r8d, %esi
je 0x2cec
testl %esi, %esi
leal -0x1(%rsi), %esi
cmovlel %ecx, %esi
jmp 0x2c64
andl $0x0, 0x8564(%rip)      # 0xb1ec
andl $0x0, 0x8559(%rip)      # 0xb1e8
xorl %edi, %edi
jmp 0x2cfc
leal 0x1(%rdi), %r9d
xorl %r8d, %r8d
cmpl $0x63, %edi
cmovll %r9d, %r8d
movl %r8d, 0x8544(%rip)      # 0xb1ec
leal -0x1(%r8), %edi
testl %r8d, %r8d
cmovlel %ecx, %edi
cmpl %esi, %edi
je 0x2cdf
movslq %r8d, %r8
imulq $0x1c, %r8, %r8
movslq %edi, %r9
imulq $0x1c, %r9, %r9
movups (%r9,%rdx), %xmm0
movups 0xc(%r9,%rdx), %xmm1
movups %xmm1, 0xc(%r8,%rdx)
movups %xmm0, (%r8,%rdx)
movl %edi, %r8d
jmp 0x2ca8
leal 0x1(%rsi), %ecx
xorl %edi, %edi
cmpl $0x63, %esi
cmovll %ecx, %edi
jmp 0x2cfc
leal -0x1(%r8), %edi
testl %r8d, %r8d
cmovlel %ecx, %edi
movl %edi, 0x84ec(%rip)      # 0xb1e8
incl %eax
movl %eax, 0x84e0(%rip)      # 0xb1e4
movslq %edi, %rax
imulq $0x1c, %rax, %rax
leaq 0x84ee(%rip), %rcx      # 0xb200
movb %bl, (%rax,%rcx)
movb $0x0, 0x1(%rax,%rcx)
movq 0x10(%r14), %rdx
movq %rdx, 0x14(%rax,%rcx)
movups (%r14), %xmm0
movups %xmm0, 0x4(%rax,%rcx)
cmpb $-0x80, %bl
jne 0x2d47
cmpb $0x0, 0x6498(%rip)      # 0x91d0
je 0x2d47
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x2acc
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq