// File: /workspace/github_c_makefile_star2_O0/jonof[P]jfaudiolib/src/midi.c
// Function: MIDI_InitEMIDI
static void _MIDI_InitEMIDI
   (
   void
   )

   {
   int    event;
   int    command;
   int    channel;
   int    length;
   int    IncludeFound;
   track *Track;
   int    tracknum;
   int    c1;
   int    c2;

   (void)channel;

   _MIDI_ResetTracks();

   _MIDI_TotalTime     = 0;
   _MIDI_TotalTicks    = 0;
   _MIDI_TotalBeats    = 0;
   _MIDI_TotalMeasures = 0;

   Track = _MIDI_TrackPtr;
   tracknum = 0;
   while( ( tracknum < _MIDI_NumTracks ) && ( Track != NULL ) )
      {
      _MIDI_Tick = 0;
      _MIDI_Beat = 1;
      _MIDI_Measure = 1;
      _MIDI_Time = 0;
      _MIDI_BeatsPerMeasure = 4;
      _MIDI_TicksPerBeat = _MIDI_Division;
      _MIDI_TimeBase = 4;

      _MIDI_PositionInTicks = 0;
      _MIDI_ActiveTracks    = 0;
      _MIDI_Context         = -1;

      Track->RunningStatus = 0;
      Track->active        = TRUE;

      Track->EMIDI_ProgramChange = FALSE;
      Track->EMIDI_VolumeChange  = FALSE;
      Track->EMIDI_IncludeTrack  = TRUE;

      memset( Track->context, 0, sizeof( Track->context ) );

      while( Track->delay > 0 )
         {
         _MIDI_AdvanceTick();
         Track->delay--;
         }

      IncludeFound = FALSE;
      while ( Track->active )
         {
         GET_NEXT_EVENT( Track, event );

         if ( GET_MIDI_COMMAND( event ) == MIDI_SPECIAL )
            {
            switch( event )
               {
               case MIDI_SYSEX :
               case MIDI_SYSEX_CONTINUE :
                  _MIDI_SysEx( Track );
                  break;

               case MIDI_META_EVENT :
                  _MIDI_MetaEvent( Track );
                  break;
               }

            if ( Track->active )
               {
               Track->delay = _MIDI_ReadDelta( Track );
               while( Track->delay > 0 )
                  {
                  _MIDI_AdvanceTick();
                  Track->delay--;
                  }
               }

            continue;
            }

         if ( event & MIDI_RUNNING_STATUS )
            {
            Track->RunningStatus = event;
            }
         else
            {
            event = Track->RunningStatus;
            Track->pos--;
            }

         channel = GET_MIDI_CHANNEL( event );
         command = GET_MIDI_COMMAND( event );
         length = _MIDI_CommandLengths[ command ];

         if ( command == MIDI_CONTROL_CHANGE )
            {
            if ( *Track->pos == MIDI_MONO_MODE_ON )
               {
               length++;
               }
            GET_NEXT_EVENT( Track, c1 );
            GET_NEXT_EVENT( Track, c2 );
            length -= 2;

            switch( c1 )
               {
               case EMIDI_LOOP_START :
               case EMIDI_SONG_LOOP_START :
                  if ( c2 == 0 )
                     {
                     Track->context[ 0 ].loopcount = EMIDI_INFINITE;
                     }
                  else
                     {
                     Track->context[ 0 ].loopcount = c2;
                     }

                  Track->context[ 0 ].pos              = Track->pos;
                  Track->context[ 0 ].loopstart        = Track->pos;
                  Track->context[ 0 ].RunningStatus    = Track->RunningStatus;
                  Track->context[ 0 ].time             = _MIDI_Time;
                  Track->context[ 0 ].FPSecondsPerTick = _MIDI_FPSecondsPerTick;
                  Track->context[ 0 ].tick             = _MIDI_Tick;
                  Track->context[ 0 ].beat             = _MIDI_Beat;
                  Track->context[ 0 ].measure          = _MIDI_Measure;
                  Track->context[ 0 ].BeatsPerMeasure  = _MIDI_BeatsPerMeasure;
                  Track->context[ 0 ].TicksPerBeat     = _MIDI_TicksPerBeat;
                  Track->context[ 0 ].TimeBase         = _MIDI_TimeBase;
                  break;

               case EMIDI_LOOP_END :
               case EMIDI_SONG_LOOP_END :
                  if ( c2 == EMIDI_END_LOOP_VALUE )
                     {
                     Track->context[ 0 ].loopstart = NULL;
                     Track->context[ 0 ].loopcount = 0;
                     }
                  break;

               case EMIDI_INCLUDE_TRACK :
                  if ( EMIDI_AffectsCurrentCard( c2, MUSIC_EMIDIPersonality ) )
                     {
                     //printf( "Include track %d on card %d\n", tracknum, c2 );
                     IncludeFound = TRUE;
                     Track->EMIDI_IncludeTrack = TRUE;
                     }
                  else if ( !IncludeFound )
                     {
                     //printf( "Track excluded %d on card %d\n", tracknum, c2 );
                     IncludeFound = TRUE;
                     Track->EMIDI_IncludeTrack = FALSE;
                     }
                  break;

               case EMIDI_EXCLUDE_TRACK :
                  if ( EMIDI_AffectsCurrentCard( c2, MUSIC_EMIDIPersonality ) )
                     {
                     //printf( "Exclude track %d on card %d\n", tracknum, c2 );
                     Track->EMIDI_IncludeTrack = FALSE;
                     }
                  break;

               case EMIDI_PROGRAM_CHANGE :
                  if ( !Track->EMIDI_ProgramChange )
                     //printf( "Program change on track %d\n", tracknum );
                  Track->EMIDI_ProgramChange = TRUE;
                  break;

               case EMIDI_VOLUME_CHANGE :
                  if ( !Track->EMIDI_VolumeChange )
                     //printf( "Volume change on track %d\n", tracknum );
                  Track->EMIDI_VolumeChange = TRUE;
                  break;

               case EMIDI_CONTEXT_START :
                  if ( ( c2 > 0 ) && ( c2 < EMIDI_NUM_CONTEXTS ) )
                     {
                     Track->context[ c2 ].pos              = Track->pos;
                     Track->context[ c2 ].loopstart        = Track->context[ 0 ].loopstart;
                     Track->context[ c2 ].loopcount        = Track->context[ 0 ].loopcount;
                     Track->context[ c2 ].RunningStatus    = Track->RunningStatus;
                     Track->context[ c2 ].time             = _MIDI_Time;
                     Track->context[ c2 ].FPSecondsPerTick = _MIDI_FPSecondsPerTick;
                     Track->context[ c2 ].tick             = _MIDI_Tick;
                     Track->context[ c2 ].beat             = _MIDI_Beat;
                     Track->context[ c2 ].measure          = _MIDI_Measure;
                     Track->context[ c2 ].BeatsPerMeasure  = _MIDI_BeatsPerMeasure;
                     Track->context[ c2 ].TicksPerBeat     = _MIDI_TicksPerBeat;
                     Track->context[ c2 ].TimeBase         = _MIDI_TimeBase;
                     }
                  break;

               case EMIDI_CONTEXT_END :
                  break;
               }
            }

         Track->pos += length;
         Track->delay = _MIDI_ReadDelta( Track );

         while( Track->delay > 0 )
            {
            _MIDI_AdvanceTick();
            Track->delay--;
            }
         }

      _MIDI_TotalTime = max( _MIDI_TotalTime, _MIDI_Time );
      if ( RELATIVE_BEAT( _MIDI_Measure, _MIDI_Beat, _MIDI_Tick ) >
         RELATIVE_BEAT( _MIDI_TotalMeasures, _MIDI_TotalBeats,
         _MIDI_TotalTicks ) )
         {
         _MIDI_TotalTicks    = _MIDI_Tick;
         _MIDI_TotalBeats    = _MIDI_Beat;
         _MIDI_TotalMeasures = _MIDI_Measure;
         }

      Track++;
      tracknum++;
      }

   _MIDI_ResetTracks();
   }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
callq 0xb260
movl $0x0, 0x13a29(%rip)     # 0x1f0d0
movl $0x0, 0x13a23(%rip)     # 0x1f0d4
movl $0x0, 0x13a1d(%rip)     # 0x1f0d8
movl $0x0, 0x13a17(%rip)     # 0x1f0dc
movq 0x139f4(%rip), %rax     # 0x1f0c0
movq %rax, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %ecx
xorl %eax, %eax
cmpl 0x139e6(%rip), %ecx     # 0x1f0c8
movb %al, -0x2d(%rbp)
jge 0xb6f2
cmpq $0x0, -0x20(%rbp)
setne %al
movb %al, -0x2d(%rbp)
movb -0x2d(%rbp), %al
testb $0x1, %al
jne 0xb6fe
jmp 0xbd84
movl $0x0, 0x139ec(%rip)     # 0x1f0f4
movl $0x1, 0xa552(%rip)      # 0x15c64
movl $0x1, 0xa54c(%rip)      # 0x15c68
movl $0x0, 0x139ca(%rip)     # 0x1f0f0
movl $0x4, 0x13a50(%rip)     # 0x1f180
movl 0x139aa(%rip), %eax     # 0x1f0e0
movl %eax, 0x13a48(%rip)     # 0x1f184
movl $0x4, 0x13a42(%rip)     # 0x1f188
movl $0x0, 0x13998(%rip)     # 0x1f0e8
movl $0x0, 0x13992(%rip)     # 0x1f0ec
movl $0xffffffff, 0x1394c(%rip) # imm = 0xFFFFFFFF
movq -0x20(%rbp), %rax
movw $0x0, 0x16(%rax)
movq -0x20(%rbp), %rax
movw $0x1, 0x14(%rax)
movq -0x20(%rbp), %rax
movb $0x0, 0x171(%rax)
movq -0x20(%rbp), %rax
movb $0x0, 0x172(%rax)
movq -0x20(%rbp), %rax
movb $0x1, 0x170(%rax)
movq -0x20(%rbp), %rdi
addq $0x20, %rdi
xorl %esi, %esi
movl $0x150, %edx            # imm = 0x150
callq 0x2100
movq -0x20(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jle 0xb7cb
callq 0xd370
movq -0x20(%rbp), %rax
movl 0x10(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x10(%rax)
jmp 0xb7ad
movl $0x0, -0x14(%rbp)
movq -0x20(%rbp), %rax
cmpw $0x0, 0x14(%rax)
je 0xbcdd
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movzbl (%rax), %eax
movl %eax, -0x4(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x8(%rax)
movl -0x4(%rbp), %eax
sarl $0x4, %eax
cmpl $0xf, %eax
jne 0xb887
movl -0x4(%rbp), %eax
movl %eax, -0x34(%rbp)
subl $0xf0, %eax
je 0xb831
jmp 0xb819
movl -0x34(%rbp), %eax
subl $0xf7, %eax
je 0xb831
jmp 0xb825
movl -0x34(%rbp), %eax
subl $0xff, %eax
je 0xb83c
jmp 0xb845
movq -0x20(%rbp), %rdi
callq 0xcb70
jmp 0xb845
movq -0x20(%rbp), %rdi
callq 0xcc30
movq -0x20(%rbp), %rax
cmpw $0x0, 0x14(%rax)
je 0xb882
movq -0x20(%rbp), %rdi
callq 0xcaf0
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x20(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jle 0xb880
callq 0xd370
movq -0x20(%rbp), %rax
movl 0x10(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x10(%rax)
jmp 0xb862
jmp 0xb882
jmp 0xb7d2
movl -0x4(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0xb8a4
movl -0x4(%rbp), %eax
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x16(%rax)
jmp 0xb8bf
movq -0x20(%rbp), %rax
movswl 0x16(%rax), %eax
movl %eax, -0x4(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x8(%rax)
movl -0x4(%rbp), %eax
andl $0xf, %eax
movl %eax, -0xc(%rbp)
movl -0x4(%rbp), %eax
sarl $0x4, %eax
movl %eax, -0x8(%rbp)
movslq -0x8(%rbp), %rcx
leaq 0x4164(%rip), %rax      # 0xfa40
movl (%rax,%rcx,4), %eax
movl %eax, -0x10(%rbp)
cmpl $0xb, -0x8(%rbp)
jne 0xbc93
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movzbl (%rax), %eax
cmpl $0x7e, %eax
jne 0xb905
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movzbl (%rax), %eax
movl %eax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
incq %rcx
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movzbl (%rax), %eax
movl %eax, -0x2c(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
incq %rcx
movq %rcx, 0x8(%rax)
movl -0x10(%rbp), %eax
addl $-0x2, %eax
movl %eax, -0x10(%rbp)
movl -0x28(%rbp), %eax
addl $-0x6e, %eax
movl %eax, %ecx
movq %rcx, -0x40(%rbp)
subl $0x9, %eax
ja 0xbc91
movq -0x40(%rbp), %rax
leaq 0x3e98(%rip), %rcx      # 0xf800
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0x0, -0x2c(%rbp)
jne 0xb983
movq -0x20(%rbp), %rax
movw $0xffff, 0x30(%rax)     # imm = 0xFFFF
jmp 0xb991
movl -0x2c(%rbp), %eax
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x30(%rax)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x20(%rbp), %rax
movw 0x16(%rax), %cx
movq -0x20(%rbp), %rax
movw %cx, 0x32(%rax)
movl 0x13729(%rip), %ecx     # 0x1f0f0
movq -0x20(%rbp), %rax
movl %ecx, 0x34(%rax)
movl 0x13710(%rip), %ecx     # 0x1f0e4
movq -0x20(%rbp), %rax
movl %ecx, 0x38(%rax)
movl 0x13713(%rip), %eax     # 0x1f0f4
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x3c(%rax)
movl 0xa272(%rip), %eax      # 0x15c64
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x3e(%rax)
movl 0xa265(%rip), %eax      # 0x15c68
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x40(%rax)
movl 0x1376c(%rip), %eax     # 0x1f180
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x42(%rax)
movl 0x1375f(%rip), %eax     # 0x1f184
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x44(%rax)
movl 0x13752(%rip), %eax     # 0x1f188
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x46(%rax)
jmp 0xbc91
cmpl $0x7f, -0x2c(%rbp)
jne 0xba62
movq -0x20(%rbp), %rax
movq $0x0, 0x28(%rax)
movq -0x20(%rbp), %rax
movw $0x0, 0x30(%rax)
jmp 0xbc91
cmpl $0x7f, -0x2c(%rbp)
je 0xba78
movl -0x2c(%rbp), %eax
cmpl 0x1355a(%rip), %eax     # 0x1efd0
jne 0xba8c
movl $0x1, -0x14(%rbp)
movq -0x20(%rbp), %rax
movb $0x1, 0x170(%rax)
jmp 0xbaa6
cmpl $0x0, -0x14(%rbp)
jne 0xbaa4
movl $0x1, -0x14(%rbp)
movq -0x20(%rbp), %rax
movb $0x0, 0x170(%rax)
jmp 0xbaa6
jmp 0xbc91
cmpl $0x7f, -0x2c(%rbp)
je 0xbabc
movl -0x2c(%rbp), %eax
cmpl 0x13516(%rip), %eax     # 0x1efd0
jne 0xbac7
movq -0x20(%rbp), %rax
movb $0x0, 0x170(%rax)
jmp 0xbc91
movq -0x20(%rbp), %rax
cmpb $0x0, 0x171(%rax)
jne 0xbae4
movq -0x20(%rbp), %rax
movb $0x1, 0x171(%rax)
jmp 0xbc91
movq -0x20(%rbp), %rax
cmpb $0x0, 0x172(%rax)
jne 0xbb01
movq -0x20(%rbp), %rax
movb $0x1, 0x172(%rax)
jmp 0xbc91
cmpl $0x0, -0x2c(%rbp)
jle 0xbc8d
cmpl $0x7, -0x2c(%rbp)
jge 0xbc8d
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x20(%rbp), %rax
addq $0x20, %rax
movslq -0x2c(%rbp), %rdx
imulq $0x30, %rdx, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rcx
movq -0x20(%rbp), %rax
addq $0x20, %rax
movslq -0x2c(%rbp), %rdx
imulq $0x30, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movw 0x30(%rax), %cx
movq -0x20(%rbp), %rax
addq $0x20, %rax
movslq -0x2c(%rbp), %rdx
imulq $0x30, %rdx, %rdx
addq %rdx, %rax
movw %cx, 0x10(%rax)
movq -0x20(%rbp), %rax
movw 0x16(%rax), %cx
movq -0x20(%rbp), %rax
addq $0x20, %rax
movslq -0x2c(%rbp), %rdx
imulq $0x30, %rdx, %rdx
addq %rdx, %rax
movw %cx, 0x12(%rax)
movl 0x13555(%rip), %ecx     # 0x1f0f0
movq -0x20(%rbp), %rax
addq $0x20, %rax
movslq -0x2c(%rbp), %rdx
imulq $0x30, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x14(%rax)
movl 0x1352d(%rip), %ecx     # 0x1f0e4
movq -0x20(%rbp), %rax
addq $0x20, %rax
movslq -0x2c(%rbp), %rdx
imulq $0x30, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x18(%rax)
movl 0x13521(%rip), %eax     # 0x1f0f4
movw %ax, %cx
movq -0x20(%rbp), %rax
addq $0x20, %rax
movslq -0x2c(%rbp), %rdx
imulq $0x30, %rdx, %rdx
addq %rdx, %rax
movw %cx, 0x1c(%rax)
movl 0xa071(%rip), %eax      # 0x15c64
movw %ax, %cx
movq -0x20(%rbp), %rax
addq $0x20, %rax
movslq -0x2c(%rbp), %rdx
imulq $0x30, %rdx, %rdx
addq %rdx, %rax
movw %cx, 0x1e(%rax)
movl 0xa055(%rip), %eax      # 0x15c68
movw %ax, %cx
movq -0x20(%rbp), %rax
addq $0x20, %rax
movslq -0x2c(%rbp), %rdx
imulq $0x30, %rdx, %rdx
addq %rdx, %rax
movw %cx, 0x20(%rax)
movl 0x1354d(%rip), %eax     # 0x1f180
movw %ax, %cx
movq -0x20(%rbp), %rax
addq $0x20, %rax
movslq -0x2c(%rbp), %rdx
imulq $0x30, %rdx, %rdx
addq %rdx, %rax
movw %cx, 0x22(%rax)
movl 0x13531(%rip), %eax     # 0x1f184
movw %ax, %cx
movq -0x20(%rbp), %rax
addq $0x20, %rax
movslq -0x2c(%rbp), %rdx
imulq $0x30, %rdx, %rdx
addq %rdx, %rax
movw %cx, 0x24(%rax)
movl 0x13515(%rip), %eax     # 0x1f188
movw %ax, %cx
movq -0x20(%rbp), %rax
addq $0x20, %rax
movslq -0x2c(%rbp), %rdx
imulq $0x30, %rdx, %rdx
addq %rdx, %rax
movw %cx, 0x26(%rax)
jmp 0xbc91
jmp 0xbc91
jmp 0xbc93
movl -0x10(%rbp), %edx
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
movslq %edx, %rdx
addq %rdx, %rcx
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rdi
callq 0xcaf0
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x20(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jle 0xbcd8
callq 0xd370
movq -0x20(%rbp), %rax
movl 0x10(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x10(%rax)
jmp 0xbcba
jmp 0xb7d2
movl 0x133ed(%rip), %eax     # 0x1f0d0
cmpl 0x13407(%rip), %eax     # 0x1f0f0
jbe 0xbcf6
movl 0x133df(%rip), %eax     # 0x1f0d0
movl %eax, -0x44(%rbp)
jmp 0xbcff
movl 0x133f4(%rip), %eax     # 0x1f0f0
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
movl %eax, 0x133c8(%rip)     # 0x1f0d0
movl 0x133e6(%rip), %eax     # 0x1f0f4
movl 0x9f50(%rip), %ecx      # 0x15c64
shll $0x9, %ecx
addl %ecx, %eax
movl 0x9f49(%rip), %ecx      # 0x15c68
shll $0x10, %ecx
addl %ecx, %eax
movl 0x133aa(%rip), %ecx     # 0x1f0d4
movl 0x133a8(%rip), %edx     # 0x1f0d8
shll $0x9, %edx
addl %edx, %ecx
movl 0x133a1(%rip), %edx     # 0x1f0dc
shll $0x10, %edx
addl %edx, %ecx
cmpl %ecx, %eax
jbe 0xbd68
movl 0x133aa(%rip), %eax     # 0x1f0f4
movl %eax, 0x13384(%rip)     # 0x1f0d4
movl 0x9f0e(%rip), %eax      # 0x15c64
movl %eax, 0x1337c(%rip)     # 0x1f0d8
movl 0x9f06(%rip), %eax      # 0x15c68
movl %eax, 0x13374(%rip)     # 0x1f0dc
movq -0x20(%rbp), %rax
addq $0x178, %rax            # imm = 0x178
movq %rax, -0x20(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0xb6d7
callq 0xb260
addq $0x50, %rsp
popq %rbp
retq
nop