// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x38, %rsp
movb %dil, %al
movb %al, 0x37(%rsp)
movq %rsi, 0x28(%rsp)
testb $0x1, 0x7a1c(%rip)      # 0xb303
je 0x395e
movq 0x7a28(%rip), %rax      # 0xb318
movq %rax, 0x10(%rsp)
movzbl 0x37(%rsp), %eax
cmpl $0x90, %eax
jne 0x390f
leaq 0x43b8(%rip), %rax      # 0x7cc0
movq %rax, 0x8(%rsp)
jmp 0x3931
movzbl 0x37(%rsp), %edx
leaq 0x47e0(%rip), %rax      # 0x80fb
leaq 0x43a3(%rip), %rcx      # 0x7cc5
cmpl $0x80, %edx
cmoveq %rcx, %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rax
movq %rax, (%rsp)
movq 0x28(%rsp), %rdi
callq 0x2260
movq 0x10(%rsp), %rdi
movq (%rsp), %rdx
movq %rax, %rcx
leaq 0x4795(%rip), %rsi      # 0x80ec
movb $0x0, %al
callq 0x10a0
cmpl $0x64, 0x99bf(%rip)     # 0xd324
jl 0x396c
callq 0x3040
cmpl $0x64, 0x99b1(%rip)     # 0xd324
setl %al
movzbl %al, %edi
andl $0x1, %edi
leaq 0x477d(%rip), %rsi      # 0x8100
callq 0x1c70
movl 0x795a(%rip), %eax      # 0xb2e8
addl 0x7958(%rip), %eax      # 0xb2ec
movl %eax, 0x24(%rsp)
movq 0x28(%rsp), %rax
movl (%rax), %eax
cmpl 0x24(%rsp), %eax
jae 0x39e8
testb $0x1, 0x7957(%rip)      # 0xb303
je 0x39ce
movq 0x7963(%rip), %rdi      # 0xb318
movl 0x24(%rsp), %edx
movq 0x28(%rsp), %rax
subl (%rax), %edx
leaq 0x474a(%rip), %rsi      # 0x8111
movb $0x0, %al
callq 0x10a0
movl 0x24(%rsp), %ecx
movq 0x28(%rsp), %rax
movl %ecx, (%rax)
movl 0x78a9(%rip), %eax      # 0xb288
addl $0x1, %eax
movl %eax, 0x78a0(%rip)      # 0xb288
cmpl $0x0, 0x9935(%rip)      # 0xd324
jne 0x3a1c
movl $0x0, 0x9929(%rip)      # 0xd324
movl $0x0, 0x9927(%rip)      # 0xd32c
movl $0x0, 0x9919(%rip)      # 0xd328
movl $0x0, 0x20(%rsp)
jmp 0x3b4a
movl 0x990a(%rip), %eax      # 0xd32c
movl %eax, 0x20(%rsp)
movslq 0x20(%rsp), %rcx
leaq 0x990e(%rip), %rax      # 0xd340
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq 0x28(%rsp), %rcx
cmpl (%rcx), %eax
jbe 0x3a98
movl 0x20(%rsp), %eax
cmpl 0x98d9(%rip), %eax      # 0xd328
jne 0x3a7e
movl 0x98d1(%rip), %eax      # 0xd328
addl $-0x1, %eax
movl %eax, 0x98c8(%rip)      # 0xd328
cmpl $0x0, %eax
jge 0x3a6f
movl $0x63, 0x98b9(%rip)     # 0xd328
movl 0x98b3(%rip), %eax      # 0xd328
movl %eax, 0x20(%rsp)
jmp 0x3b4c
movl 0x20(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x20(%rsp)
cmpl $0x0, %eax
jge 0x3a96
movl $0x63, 0x20(%rsp)
jmp 0x3a26
movl 0x988e(%rip), %eax      # 0xd32c
addl $0x1, %eax
movl %eax, 0x9885(%rip)      # 0xd32c
cmpl $0x64, %eax
jl 0x3ab6
movl $0x0, 0x9876(%rip)      # 0xd32c
movl 0x9870(%rip), %eax      # 0xd32c
movl %eax, 0x18(%rsp)
movl 0x18(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x1c(%rsp)
cmpl $0x0, %eax
jge 0x3ad8
movl $0x63, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
cmpl 0x20(%rsp), %eax
jne 0x3ae4
jmp 0x3b30
movslq 0x18(%rsp), %rcx
leaq 0x9850(%rip), %rax      # 0xd340
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movslq 0x1c(%rsp), %rdx
leaq 0x983d(%rip), %rcx      # 0xd340
imulq $0x1c, %rdx, %rdx
addq %rdx, %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rax)
movq 0x10(%rcx), %rdx
movq %rdx, 0x10(%rax)
movl 0x18(%rcx), %ecx
movl %ecx, 0x18(%rax)
movl 0x1c(%rsp), %eax
movl %eax, 0x18(%rsp)
jmp 0x3ac0
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
cmpl $0x64, %eax
jl 0x3b48
movl $0x0, 0x20(%rsp)
jmp 0x3b4a
jmp 0x3b4c
movl 0x97d2(%rip), %eax      # 0xd324
addl $0x1, %eax
movl %eax, 0x97c9(%rip)      # 0xd324
movb 0x37(%rsp), %cl
movslq 0x20(%rsp), %rdx
leaq 0x97d5(%rip), %rax      # 0xd340
imulq $0x1c, %rdx, %rdx
addq %rdx, %rax
movb %cl, (%rax)
movslq 0x20(%rsp), %rcx
leaq 0x97c0(%rip), %rax      # 0xd340
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x1(%rax)
movslq 0x20(%rsp), %rcx
leaq 0x97a9(%rip), %rax      # 0xd340
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movq 0x28(%rsp), %rcx
movq (%rcx), %rdx
movq %rdx, 0x4(%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0xc(%rax)
movq 0x10(%rcx), %rcx
movq %rcx, 0x14(%rax)
testb $0x1, 0x774f(%rip)      # 0xb310
je 0x3bd8
movzbl 0x37(%rsp), %eax
cmpl $0x80, %eax
jne 0x3bd8
movl 0x20(%rsp), %edi
callq 0x37c0
addq $0x38, %rsp
retq
nopl (%rax)