// File: /workspace/github_c_makefile_star2_O2/LenShustek[P]miditones/miditones.c
// Function: find_next_note
void find_next_note (int tracknum) {
   unsigned long int delta_ticks;
   int event, chan;
   int note, velocity, controller, pressure, pitchbend, instrument;
   int meta_cmd, meta_length;
   unsigned long int sysex_length;
   char *tag;

   struct track_status *t = &track[tracknum];   // our track status structure
   while (t->trkptr < t->trkend) {
      delta_ticks = get_varlen (&t->trkptr);
      t->time += delta_ticks;
      if (logparse) {
         fprintf(logfile, "# trk %d ", tracknum);
         if (loggen) fprintf(logfile, "at ticks+%lu=%lu: ", delta_ticks, t->time);
         else {
            if (delta_ticks > 0) fprintf(logfile, "ticks+%-5lu%7lu  ", delta_ticks, t->time);
            else fprintf(logfile, "                    "); } }

      if (*t->trkptr < 0x80) event = t->last_event;  // using "running status": same event as before
      else event = *t->trkptr++; // otherwise get new "status" (event type) */

      if (event == 0xff) { // meta-event
         meta_cmd = *t->trkptr++;
         meta_length = get_varlen (&t->trkptr);
         switch (meta_cmd) {
         case 0x00:
            if (logparse) fprintf (logfile, "sequence number %d\n", rev_short (*(unsigned short *) t->trkptr));
            break;
         case 0x01:
            tag = "description"; goto show_text;
         case 0x02:
            tag = "copyright"; goto show_text;
         case 0x03:
            tag = "track name";
            if (tracknum == 0 && !parseonly && !binaryoutput && !asm_output) {
               /* Incredibly, MIDI has no standard for recording the name of the piece!
                  Track 0's "trackname" is often used for that so we output it to the C file as documentation. */
               fprintf (outfile, "// ");
               for (int i = 0; i < meta_length; ++i) {
                  int ch = t->trkptr[i];
                  fprintf (outfile, "%c", isprint (ch) ? ch : '?'); }
               fprintf (outfile, "\n"); }
            goto show_text;
         case 0x04:
            tag = "instrument name"; goto show_text;
         case 0x05:
            tag = "lyric"; goto show_text;
         case 0x06:
            tag = "marked point"; goto show_text;
         case 0x07:
            tag = "cue point"; goto show_text;
         case 0x08:
            tag = "program name"; goto show_text;
         case 0x09:
            tag = "device (port) name";
show_text:
            if (logparse) {
               fprintf (logfile, "meta cmd %02X, length %d, %s: \"", meta_cmd, meta_length, tag);
               for (int i = 0; i < meta_length; ++i) {
                  int ch = t->trkptr[i];
                  fprintf (logfile, "%c", isprint (ch) ? ch : '?'); }
               fprintf (logfile, "\"\n"); }
            break;
         case 0x20:
            if (logparse) fprintf (logfile, "channel prefix %d\n", *t->trkptr);
            break;
         case 0x21:
            if (logparse) fprintf(logfile, "MIDI port %d\n", *t->trkptr);
            break;
         case 0x2f:
            if (logparse) fprintf (logfile, "end of track\n");
            break;
         case 0x51:    // tempo: 3 byte big-endian integer, not a varlen integer!
            t->cmd = CMD_TEMPO;
            t->tempo = rev_long (*(uint32_t *) (t->trkptr - 1)) & 0xffffffL;
            if (logparse) fprintf (logfile, "set tempo %ld usec/qnote\n", t->tempo);
            t->trkptr += meta_length;
            return;
         case 0x54:
            if (logparse) fprintf (logfile, "SMPTE offset %08" PRIx32 "\n",
                                      rev_long (*(uint32_t *) t->trkptr));
            break;
         case 0x58:
            if (logparse) fprintf (logfile, "time signature %08" PRIx32 "\n",
                                      rev_long (*(uint32_t *) t->trkptr));
            break;
         case 0x59:
            if (logparse) fprintf (logfile, "key signature %04X\n", rev_short (*(unsigned short *) t->trkptr));
            break;
         case 0x7f:
            tag = "sequencer data"; goto show_hex;
         default:              /* unknown meta command */
            tag = "???";
show_hex:
            if (logparse) {
               fprintf (logfile, "meta cmd %02X, length %d, %s: ", meta_cmd, meta_length, tag);
               for (int i = 0; i < meta_length; ++i)
                  fprintf (logfile, "%02X ", t->trkptr[i]);
               fprintf (logfile, "\n"); }
            break; }
         t->trkptr += meta_length; }

      else if (event < 0x80) midi_error ("Unknown MIDI event type", t->trkptr);

      else {
         if (event < 0xf0)
            t->last_event = event;      // remember "running status" if not meta or sysex event
         t->chan = chan = event & 0xf;
         switch (event >> 4) {
         case 0x8: // note off
            t->note = *t->trkptr++;
            t->volume = *t->trkptr++;
note_off:   if (logparse) fprintf(logfile, "note %d (0x%02X) off, channel %d, volume %d\n", t->note, t->note, chan, t->volume);
            if ((1 << chan) & channel_mask  // we're processing this channel
                  && (!percussion_ignore || chan != PERCUSSION_TRACK)) {  // and not ignoring percussion
               if (!instrumentoutput) t->chan = 0; // if no instruments, force all notes to channel 0
               t->cmd = CMD_STOPNOTE;    /* stop processing and return */
               return; }
            break;
         case 0x9: // note on
            t->note = *t->trkptr++;
            t->volume = *t->trkptr++;
            if (t->volume == 0)  // some scores use note-on with zero velocity for off!
               goto note_off;
            if (logparse) fprintf(logfile, "note %d (0x%02X) on,  channel %d, volume %d\n", t->note, t->note, chan, t->volume);
            if ((1 << chan) & channel_mask // we're processing this channel
                  && (!percussion_ignore || chan != PERCUSSION_TRACK)) {  // and not ignoring percussion
               if (!instrumentoutput) t->chan = 0; // if no instruments, force all notes to channel 0
               t->cmd = CMD_PLAYNOTE;    /* stop processing and return */
               return; }
            break;
         case 0xa: // key pressure
            note = *t->trkptr++;
            velocity = *t->trkptr++;
            if (logparse) fprintf (logfile, "channel %d: note %d (0x%02X) has key pressure %d\n", chan, note, note, velocity);
            break;
         case 0xb: // control value change
            controller = *t->trkptr++;
            velocity = *t->trkptr++;
            if (logparse) fprintf (logfile, "channel %d: change control value of controller %d to %d\n", chan, controller, velocity);
            break;
         case 0xc: // program patch, ie which instrument
            instrument = *t->trkptr++;
            channel[chan].instrument = instrument;    // record new instrument for this channel
            if (logparse) fprintf (logfile, "channel %d: program patch to instrument %d\n", chan, instrument);
            break;
         case 0xd: // channel pressure
            pressure = *t->trkptr++;
            if (logparse) fprintf (logfile, "channel %d: after-touch pressure is %d\n", chan, pressure);
            break;
         case 0xe: // pitch wheel change
            pitchbend = *t->trkptr++ | (*t->trkptr++ << 7);
            if (logparse) fprintf (logfile, "pitch wheel change to %d\n", pitchbend);
            break;
         case 0xf: // sysex event
            sysex_length = get_varlen (&t->trkptr);
            if (logparse) fprintf (logfile, "SysEx event %d with %ld bytes\n", event, sysex_length);
            t->trkptr += sysex_length;
            break;
         default:
            midi_error ("Unknown MIDI command", t->trkptr); } } }
   t->cmd = CMD_TRACKDONE;   //no more events to process on this track
   ++tracks_done; }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edi, %ebp
movslq %edi, %rax
imulq $0x30, %rax, %rax
leaq 0x63e0(%rip), %rcx      # 0x93c0
leaq (%rcx,%rax), %rbx
leaq (%rax,%rcx), %r13
addq $0x28, %r13
movl %edi, 0x4(%rsp)
movq %r13, 0x8(%rsp)
movq (%rbx), %rax
cmpq 0x8(%rbx), %rax
jae 0x3709
movq %rbx, %rdi
callq 0x2f9b
addq %rax, 0x10(%rbx)
cmpb $0x1, 0x61b0(%rip)      # 0x91c5
jne 0x3076
movq %rax, %r15
movq 0x61b7(%rip), %rdi      # 0x91d8
leaq 0x3027(%rip), %rsi      # 0x604f
movl %ebp, %edx
xorl %eax, %eax
callq 0x10b0
cmpb $0x1, 0x618b(%rip)      # 0x91c3
jne 0x304e
movq 0x6197(%rip), %rdi      # 0x91d8
movq 0x10(%rbx), %rcx
leaq 0x300d(%rip), %rsi      # 0x6059
jmp 0x306c
movq 0x6183(%rip), %rax      # 0x91d8
testq %r15, %r15
je 0x31ff
movq 0x10(%rbx), %rcx
movq %rax, %rdi
leaq 0x3000(%rip), %rsi      # 0x606c
movq %r15, %rdx
xorl %eax, %eax
callq 0x10b0
movq (%rbx), %rax
cmpb $0x0, (%rax)
js 0x3086
movq %rax, %rsi
movq %r13, %rax
jmp 0x308d
leaq 0x1(%rax), %rsi
movq %rsi, (%rbx)
movzbl (%rax), %r12d
cmpl $0xff, %r12d
je 0x30f8
testb %r12b, %r12b
jns 0x3789
cmpb $-0x11, %r12b
ja 0x30ad
movb %r12b, (%r13)
movl %r12d, %r15d
andl $0xf, %r15d
movb %r15b, 0x25(%rbx)
movl %r12d, %eax
shrl $0x4, %eax
addl $-0x8, %eax
leaq 0x1f3c(%rip), %rcx      # 0x5004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x1(%rsi), %rax
movq %rax, (%rbx)
movb (%rsi), %al
movb %al, 0x26(%rbx)
leaq 0x2(%rsi), %rcx
movq %rcx, (%rbx)
movzbl 0x1(%rsi), %r9d
movb %r9b, 0x27(%rbx)
movb 0x60d2(%rip), %cl       # 0x91c5
jmp 0x3433
leaq 0x1(%rsi), %rax
movq %rax, (%rbx)
movzbl (%rsi), %r15d
movq %rbx, %rdi
callq 0x2f9b
movq %rax, %r12
cmpq $0x9, %r15
ja 0x3158
leaq 0x1f09(%rip), %rcx      # 0x5024
movslq (%rcx,%r15,4), %rax
addq %rcx, %rax
leaq 0x2f7e(%rip), %r8       # 0x60a7
jmpq *%rax
cmpb $0x1, 0x6093(%rip)      # 0x91c5
jne 0x36da
movq 0x6099(%rip), %rdi      # 0x91d8
movq (%rbx), %rax
movzwl (%rax), %eax
rolw $0x8, %ax
movzwl %ax, %edx
leaq 0x2f40(%rip), %rsi      # 0x6093
jmp 0x36d3
leal -0x51(%r15), %eax
cmpl $0x8, %eax
jbe 0x33f9
cmpl $0x20, %r15d
je 0x3673
cmpl $0x21, %r15d
je 0x36b6
cmpl $0x2f, %r15d
je 0x3692
leaq 0x3037(%rip), %r8       # 0x61c1
cmpl $0x7f, %r15d
je 0x3197
leaq 0x2db1(%rip), %r8       # 0x5f48
cmpb $0x1, 0x6027(%rip)      # 0x91c5
jne 0x36da
movq 0x602d(%rip), %rdi      # 0x91d8
leaq 0x301e(%rip), %rsi      # 0x61d0
movl %r15d, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x10b0
testl %r12d, %r12d
movl %r12d, %r14d
movl $0x0, %eax
cmovlel %eax, %r14d
xorl %r15d, %r15d
movq 0x6000(%rip), %rsi      # 0x91d8
cmpq %r15, %r14
je 0x3669
movq (%rbx), %rax
movzbl (%rax,%r15), %edx
movq %rsi, %rdi
leaq 0x284d(%rip), %rsi      # 0x5a40
xorl %eax, %eax
callq 0x10b0
incq %r15
jmp 0x31d1
leaq 0x2e78(%rip), %rdi      # 0x607e
pushq $0x14
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0x1150
jmp 0x3076
leaq 0x1(%rsi), %rax
movq %rax, (%rbx)
movzbl (%rsi), %ecx
imulq $0x194, %r15, %rax      # imm = 0x194
leaq 0x660f(%rip), %rdx      # 0x9840
movl %ecx, (%rax,%rdx)
cmpb $0x1, 0x5f8a(%rip)      # 0x91c5
jne 0x2ff5
movq 0x5f90(%rip), %rdi      # 0x91d8
leaq 0x307d(%rip), %rsi      # 0x62cc
jmp 0x33aa
leaq 0x1(%rsi), %rax
movq %rax, (%rbx)
movzbl (%rsi), %ecx
addq $0x2, %rsi
movq %rsi, (%rbx)
cmpb $0x1, 0x5f59(%rip)      # 0x91c5
jne 0x2ff5
movzbl (%rax), %r9d
movq 0x5f5b(%rip), %rdi      # 0x91d8
leaq 0x2fdd(%rip), %rsi      # 0x6261
movl %r15d, %edx
movl %ecx, %r8d
xorl %eax, %eax
callq 0x10b0
jmp 0x2ff5
leaq 0x1(%rsi), %rax
movq %rax, (%rbx)
movzbl (%rsi), %ecx
addq $0x2, %rsi
movq %rsi, (%rbx)
cmpb $0x1, 0x5f17(%rip)      # 0x91c5
jne 0x2ff5
movzbl (%rax), %r8d
movq 0x5f19(%rip), %rdi      # 0x91d8
leaq 0x2fcd(%rip), %rsi      # 0x6293
movl %r15d, %edx
xorl %eax, %eax
callq 0x10b0
jmp 0x2ff5
movq %rbx, %rdi
callq 0x2f9b
movq %rax, %r15
cmpb $0x1, 0x5ede(%rip)      # 0x91c5
jne 0x3304
movq 0x5ee8(%rip), %rdi      # 0x91d8
leaq 0x3043(%rip), %rsi      # 0x633a
movl %r12d, %edx
movq %r15, %rcx
xorl %eax, %eax
callq 0x10b0
addq %r15, (%rbx)
jmp 0x2ff5
leaq 0x1(%rsi), %rax
movq %rax, (%rbx)
movb (%rsi), %al
movb %al, 0x26(%rbx)
leaq 0x2(%rsi), %rcx
movq %rcx, (%rbx)
movzbl 0x1(%rsi), %r9d
movb %r9b, 0x27(%rbx)
movb 0x5e97(%rip), %cl       # 0x91c5
testl %r9d, %r9d
je 0x3430
testb $0x1, %cl
je 0x3359
movzbl %al, %ecx
movq 0x5e92(%rip), %rdi      # 0x91d8
leaq 0x2ee7(%rip), %rsi      # 0x6234
movl %ecx, %edx
movl %r15d, %r8d
xorl %eax, %eax
callq 0x10b0
movl 0x5d91(%rip), %eax      # 0x90f0
btl %r15d, %eax
jae 0x2ff5
cmpl $0x9, %r15d
jne 0x3735
cmpb $0x0, 0x5e4d(%rip)      # 0x91c7
jne 0x2ff5
jmp 0x3735
leaq 0x1(%rsi), %rax
movq %rax, (%rbx)
cmpb $0x1, 0x5e32(%rip)      # 0x91c5
jne 0x2ff5
movzbl (%rsi), %ecx
movq 0x5e35(%rip), %rdi      # 0x91d8
leaq 0x2f4e(%rip), %rsi      # 0x62f8
movl %r15d, %edx
xorl %eax, %eax
callq 0x10b0
jmp 0x2ff5
leaq 0x1(%rsi), %rcx
movq %rcx, (%rbx)
movzbl (%rsi), %eax
addq $0x2, %rsi
movq %rsi, (%rbx)
cmpb $0x1, 0x5df4(%rip)      # 0x91c5
jne 0x2ff5
movzbl (%rcx), %edx
shll $0x7, %edx
orl %eax, %edx
movq 0x5df2(%rip), %rdi      # 0x91d8
leaq 0x2f33(%rip), %rsi      # 0x6320
xorl %eax, %eax
callq 0x10b0
jmp 0x2ff5
leaq 0x1c4c(%rip), %rcx      # 0x504c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpb $0x1, 0x5db5(%rip)      # 0x91c5
jne 0x36da
movq 0x5dbb(%rip), %rdi      # 0x91d8
movq (%rbx), %rax
movl (%rax), %edx
bswapl %edx
leaq 0x2d5a(%rip), %rsi      # 0x6185
jmp 0x36d3
xorl %r9d, %r9d
testb $0x1, %cl
je 0x3455
movq 0x5d99(%rip), %rdi      # 0x91d8
movzbl %al, %ecx
leaq 0x2dbe(%rip), %rsi      # 0x6207
movl %ecx, %edx
movl %r15d, %r8d
xorl %eax, %eax
callq 0x10b0
movl 0x5c95(%rip), %eax      # 0x90f0
btl %r15d, %eax
jae 0x2ff5
cmpl $0x9, %r15d
jne 0x3722
cmpb $0x0, 0x5d51(%rip)      # 0x91c7
jne 0x2ff5
jmp 0x3722
cmpb $0x1, 0x5d3d(%rip)      # 0x91c5
jne 0x36da
movq 0x5d43(%rip), %rdi      # 0x91d8
movq (%rbx), %rax
movl (%rax), %edx
bswapl %edx
leaq 0x2cf5(%rip), %rsi      # 0x6198
jmp 0x36d3
cmpb $0x1, 0x5d16(%rip)      # 0x91c5
jne 0x36da
movq 0x5d1c(%rip), %rdi      # 0x91d8
movq (%rbx), %rax
movzwl (%rax), %eax
rolw $0x8, %ax
movzwl %ax, %edx
leaq 0x2cdd(%rip), %rsi      # 0x61ad
jmp 0x36d3
leaq 0x2bf0(%rip), %r8       # 0x60cc
jmp 0x35d3
leaq 0x2c1e(%rip), %r8       # 0x6106
jmp 0x35d3
leaq 0x2bbf(%rip), %r8       # 0x60b3
jmp 0x35d3
leaq 0x2bbd(%rip), %r8       # 0x60bd
testl %ebp, %ebp
jne 0x35d3
cmpb $0x0, 0x5cb7(%rip)      # 0x91c6
leaq 0x2ba7(%rip), %r8       # 0x60bd
jne 0x35d3
cmpb $0x0, 0x5c9d(%rip)      # 0x91c0
leaq 0x2b93(%rip), %r8       # 0x60bd
jne 0x35d3
cmpb $0x0, 0x5c93(%rip)      # 0x91ca
leaq 0x2b7f(%rip), %r8       # 0x60bd
jne 0x35d3
movq 0x5ca5(%rip), %rcx      # 0x91f0
leaq 0x2b76(%rip), %rdi      # 0x60c8
pushq $0x3
popq %rsi
pushq $0x1
popq %rdx
callq 0x1150
testl %r12d, %r12d
movl $0x0, %ebp
cmovgl %r12d, %ebp
xorl %r13d, %r13d
cmpq %r13, %rbp
je 0x36e5
movq (%rbx), %rax
movzbl (%rax,%r13), %r14d
movq 0x5c6c(%rip), %rax      # 0x91f0
movq %rax, 0x10(%rsp)
callq 0x1170
movq (%rax), %rax
movzwl (%rax,%r14,2), %eax
btl $0xe, %eax
pushq $0x3f
popq %rdi
jae 0x35a2
movl %r14d, %edi
movq 0x10(%rsp), %rsi
callq 0x1090
incq %r13
jmp 0x356c
leaq 0x2b37(%rip), %r8       # 0x60ef
jmp 0x35d3
leaq 0x2b1b(%rip), %r8       # 0x60dc
jmp 0x35d3
leaq 0x2b18(%rip), %r8       # 0x60e2
jmp 0x35d3
leaq 0x2b26(%rip), %r8       # 0x60f9
cmpb $0x1, 0x5beb(%rip)      # 0x91c5
jne 0x36da
movq 0x5bf1(%rip), %rdi      # 0x91d8
leaq 0x2b2b(%rip), %rsi      # 0x6119
movl %r15d, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x10b0
testl %r12d, %r12d
movl %r12d, %r15d
movl $0x0, %eax
cmovlel %eax, %r15d
xorl %r13d, %r13d
cmpq %r13, %r15
je 0x3645
movq (%rbx), %rax
movzbl (%rax,%r13), %ebp
movq 0x5bb7(%rip), %r14      # 0x91d8
callq 0x1170
movq (%rax), %rax
movzwl (%rax,%rbp,2), %eax
btl $0xe, %eax
pushq $0x3f
popq %rdi
jae 0x3638
movl %ebp, %edi
movq %r14, %rsi
callq 0x1090
incq %r13
jmp 0x360d
movq 0x5b8c(%rip), %rcx      # 0x91d8
leaq 0x2ae6(%rip), %rdi      # 0x6139
pushq $0x2
popq %rsi
pushq $0x1
popq %rdx
callq 0x1150
movl 0x4(%rsp), %ebp
movq 0x8(%rsp), %r13
jmp 0x36da
pushq $0xa
popq %rdi
callq 0x1090
jmp 0x36da
cmpb $0x1, 0x5b4b(%rip)      # 0x91c5
jne 0x36da
movq 0x5b55(%rip), %rdi      # 0x91d8
movq (%rbx), %rax
movzbl (%rax), %edx
leaq 0x2aac(%rip), %rsi      # 0x613c
jmp 0x36d3
cmpb $0x1, 0x5b2c(%rip)      # 0x91c5
jne 0x36da
movq 0x5b36(%rip), %rcx      # 0x91d8
leaq 0x2ab4(%rip), %rdi      # 0x615d
pushq $0xd
popq %rsi
pushq $0x1
popq %rdx
callq 0x1150
jmp 0x36da
cmpb $0x1, 0x5b08(%rip)      # 0x91c5
jne 0x36da
movq 0x5b12(%rip), %rdi      # 0x91d8
movq (%rbx), %rax
movzbl (%rax), %edx
leaq 0x2a7c(%rip), %rsi      # 0x614f
xorl %eax, %eax
callq 0x10b0
movslq %r12d, %rax
addq %rax, (%rbx)
jmp 0x2ff5
movq 0x5b04(%rip), %rsi      # 0x91f0
pushq $0xa
popq %rdi
callq 0x1090
leaq 0x29c2(%rip), %r8       # 0x60bd
movl 0x4(%rsp), %ebp
movq 0x8(%rsp), %r13
jmp 0x35d3
movb $-0x1, 0x24(%rbx)
incl 0x5a1d(%rip)            # 0x9130
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x0, 0x5a9b(%rip)      # 0x91c4
jne 0x372f
movb $0x0, 0x25(%rbx)
movb $-0x80, 0x24(%rbx)
jmp 0x3713
cmpb $0x0, 0x5a88(%rip)      # 0x91c4
jne 0x3742
movb $0x0, 0x25(%rbx)
movb $-0x70, 0x24(%rbx)
jmp 0x3713
movb $-0x2, 0x24(%rbx)
movq (%rbx), %rax
movl $0xffffff00, %edx       # imm = 0xFFFFFF00
andl -0x1(%rax), %edx
bswapl %edx
movq %rdx, 0x18(%rbx)
cmpb $0x1, 0x5a61(%rip)      # 0x91c5
jne 0x377e
movq 0x5a6b(%rip), %rdi      # 0x91d8
leaq 0x29f7(%rip), %rsi      # 0x616b
xorl %eax, %eax
callq 0x10b0
movq (%rbx), %rax
movslq %r12d, %rcx
addq %rax, %rcx
movq %rcx, (%rbx)
jmp 0x3713
leaq 0x2a5f(%rip), %rdi      # 0x61ef
callq 0x1a3a