// File: /workspace/github_c_makefile_star2_O3/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, 0x6428(%rip)      # 0x91c3
jne 0x2dea
movq 0x6434(%rip), %r15      # 0x91d8
cmpb $-0x80, %bl
leaq 0x2d63(%rip), %rax      # 0x5b11
leaq 0x3192(%rip), %rcx      # 0x5f47
cmoveq %rax, %rcx
cmpb $-0x70, %bl
leaq 0x2d49(%rip), %r12      # 0x5b0c
cmovneq %rcx, %r12
movq %r14, %rdi
callq 0x1da6
leaq 0x3162(%rip), %rsi      # 0x5f38
leaq 0x83a3(%rip), %rcx      # 0xb180
movq %r15, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x10b0
movb $0x1, %al
cmpl $0x64, 0x83f1(%rip)     # 0xb1e4
jl 0x2e04
callq 0x279b
cmpl $0x64, 0x83e3(%rip)     # 0xb1e4
setl %al
movzbl %al, %edi
leaq 0x313e(%rip), %rsi      # 0x5f4c
callq 0x1af6
movl 0x6393(%rip), %ebp      # 0x91ac
addl 0x6389(%rip), %ebp      # 0x91a8
movl %ebp, %edx
subl (%r14), %edx
jbe 0x2e4d
cmpb $0x1, 0x6396(%rip)      # 0x91c3
jne 0x2e44
movq 0x63a2(%rip), %rdi      # 0x91d8
leaq 0x3120(%rip), %rsi      # 0x5f5d
xorl %eax, %eax
callq 0x10b0
movl %ebp, (%r14)
incl 0x62fb(%rip)            # 0x9148
movl 0x8391(%rip), %eax      # 0xb1e4
testl %eax, %eax
je 0x2f2e
movslq 0x838a(%rip), %rdi      # 0xb1ec
movl (%r14), %esi
leaq (%rdi,%rdi,8), %rcx
leaq (%rcx,%rcx,2), %r8
addq %rdi, %r8
leaq 0x8389(%rip), %rcx      # 0xb200
movl %edi, %edx
cmpl %esi, 0x4(%rcx,%r8)
jbe 0x2ebc
movl 0x8361(%rip), %r8d      # 0xb1e8
movl $0x63, %r9d
movl %edi, %r10d
cmpl %r8d, %r10d
je 0x2f38
leal -0x1(%r10), %edx
testl %r10d, %r10d
cmovlel %r9d, %edx
movslq %edx, %r10
leaq (%r10,%r10,8), %r11
leaq (%r11,%r11,2), %r11
addq %r10, %r11
movl %edx, %r10d
cmpl %esi, 0x4(%rcx,%r11)
ja 0x2e90
leal 0x1(%rdi), %r8d
xorl %esi, %esi
cmpl $0x63, %edi
cmovgel %esi, %r8d
movl %r8d, 0x831c(%rip)      # 0xb1ec
leal -0x1(%r8), %r9d
testl %r8d, %r8d
movl $0x63, %edi
cmovlel %edi, %r9d
cmpl %edx, %r9d
je 0x2f23
movslq %r8d, %r8
leaq (%r8,%r8,8), %r10
leaq (%r10,%r10,2), %r10
addq %r8, %r10
movslq %r9d, %r11
leaq (%r11,%r11,8), %r8
leaq (%r8,%r8,2), %r8
addq %r11, %r8
movups (%rcx,%r8), %xmm0
movups 0xc(%rcx,%r8), %xmm1
movups %xmm1, 0xc(%rcx,%r10)
movups %xmm0, (%rcx,%r10)
movl %r9d, %r8d
leal -0x1(%r11), %r9d
testl %r11d, %r11d
jmp 0x2edc
leal 0x1(%rdx), %edi
cmpl $0x63, %edx
cmovgel %esi, %edi
jmp 0x2f4d
xorl %edi, %edi
movl %edi, 0x82b6(%rip)      # 0xb1ec
jmp 0x2f47
leal -0x1(%r8), %ecx
testl %r8d, %r8d
movl $0x63, %edi
cmovgl %ecx, %edi
movl %edi, 0x829b(%rip)      # 0xb1e8
incl %eax
movl %eax, 0x828f(%rip)      # 0xb1e4
movslq %edi, %rax
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rax, %rcx
leaq 0x8296(%rip), %rax      # 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 0x2f9f
cmpb $0x0, 0x6240(%rip)      # 0x91d0
je 0x2f9f
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x2cbe
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq