// File: /workspace/github_c_makefile_star2_O1/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, 0x6350(%rip)      # 0x91c3
jne 0x2ec2
movq 0x635c(%rip), %r15      # 0x91d8
cmpb $-0x80, %bl
leaq 0x2c8b(%rip), %rax      # 0x5b11
leaq 0x30ba(%rip), %rcx      # 0x5f47
cmoveq %rax, %rcx
cmpb $-0x70, %bl
leaq 0x2c71(%rip), %r12      # 0x5b0c
cmovneq %rcx, %r12
movq %r14, %rdi
callq 0x1e3b
leaq 0x308a(%rip), %rsi      # 0x5f38
leaq 0x82cb(%rip), %rcx      # 0xb180
movq %r15, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x10b0
cmpl $0x64, 0x831b(%rip)     # 0xb1e4
jl 0x2ed0
callq 0x286a
xorl %edi, %edi
cmpl $0x64, 0x830b(%rip)     # 0xb1e4
setl %dil
leaq 0x3068(%rip), %rsi      # 0x5f4c
callq 0x1b7b
movl 0x62bd(%rip), %ebp      # 0x91ac
addl 0x62b3(%rip), %ebp      # 0x91a8
movl %ebp, %edx
subl (%r14), %edx
jbe 0x2f23
cmpb $0x1, 0x62c0(%rip)      # 0x91c3
jne 0x2f1a
movq 0x62cc(%rip), %rdi      # 0x91d8
leaq 0x304a(%rip), %rsi      # 0x5f5d
xorl %eax, %eax
callq 0x10b0
movl %ebp, (%r14)
incl 0x6225(%rip)            # 0x9148
cmpl $0x0, 0x82ba(%rip)      # 0xb1e4
je 0x2ffb
movslq 0x82b5(%rip), %rsi      # 0xb1ec
movl (%r14), %edx
leaq (%rsi,%rsi,8), %rax
leaq (%rax,%rax,2), %rdi
addq %rsi, %rdi
leaq 0x82b4(%rip), %rax      # 0xb200
movl %esi, %ecx
cmpl %edx, 0x4(%rax,%rdi)
jbe 0x2f8f
movl 0x828e(%rip), %edi      # 0xb1e8
movl $0x63, %r8d
movl %esi, %r9d
cmpl %edi, %r9d
je 0x300b
leal -0x1(%r9), %ecx
testl %r9d, %r9d
cmovlel %r8d, %ecx
movslq %ecx, %r9
leaq (%r9,%r9,8), %r10
leaq (%r10,%r10,2), %r10
addq %r9, %r10
movl %ecx, %r9d
cmpl %edx, 0x4(%rax,%r10)
ja 0x2f63
leal 0x1(%rsi), %edi
xorl %edx, %edx
cmpl $0x63, %esi
cmovgel %edx, %edi
movl %edi, 0x824c(%rip)      # 0xb1ec
leal -0x1(%rdi), %r8d
testl %edi, %edi
movl $0x63, %esi
cmovlel %esi, %r8d
cmpl %ecx, %r8d
je 0x2ff0
movslq %edi, %rdi
leaq (%rdi,%rdi,8), %r9
leaq (%r9,%r9,2), %r9
addq %rdi, %r9
movslq %r8d, %r10
leaq (%r10,%r10,8), %rdi
leaq (%rdi,%rdi,2), %rdi
addq %r10, %rdi
movups (%rax,%rdi), %xmm0
movups 0xc(%rax,%rdi), %xmm1
movups %xmm1, 0xc(%rax,%r9)
movups %xmm0, (%rax,%r9)
movl %r8d, %edi
leal -0x1(%r10), %r8d
testl %r10d, %r10d
jmp 0x2fab
leal 0x1(%rcx), %edi
cmpl $0x63, %ecx
cmovgel %edx, %edi
jmp 0x301e
xorl %edi, %edi
movl %edi, 0x81e1(%rip)      # 0xb1e4
movl %edi, 0x81e3(%rip)      # 0xb1ec
jmp 0x3018
leal -0x1(%rdi), %eax
testl %edi, %edi
movl $0x63, %edi
cmovgl %eax, %edi
movl %edi, 0x81ca(%rip)      # 0xb1e8
incl 0x81c0(%rip)            # 0xb1e4
movslq %edi, %rax
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rax, %rcx
leaq 0x81c7(%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 0x306e
cmpb $0x0, 0x6171(%rip)      # 0x91d0
je 0x306e
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x2d96
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq