// File: /workspace/github_c_makefile_star2_O1/jonof[P]jfaudiolib/src/midi.c
// Function: MIDI_PlaySong
int MIDI_PlaySong
   (
   unsigned char *song,
   int loopflag
   )

   {
   int    numtracks;
   int    format;
   int    headersize;
   int    tracklength;
   track *CurrentTrack;
   unsigned char *ptr;

   if ( _MIDI_SongLoaded )
      {
      MIDI_StopSong();
      }

   _MIDI_Loop = loopflag;

   if ( _MIDI_Funcs == NULL )
      {
      return( MIDI_NullMidiModule );
      }

   if ( *( unsigned int * )song != LITTLE32(MIDI_HEADER_SIGNATURE) )
      {
      return( MIDI_InvalidMidiFile );
      }

   song += 4;

   headersize      = _MIDI_ReadNumber( song, 4 );
   song += 4;
   format          = _MIDI_ReadNumber( song, 2 );
   _MIDI_NumTracks = _MIDI_ReadNumber( song + 2, 2 );
   _MIDI_Division  = _MIDI_ReadNumber( song + 4, 2 );
   if ( _MIDI_Division < 0 )
      {
      // If a SMPTE time division is given, just set to 96 so no errors occur
      _MIDI_Division = 96;
      }

   if ( format > MAX_FORMAT )
      {
      return( MIDI_UnknownMidiFormat );
      }

   ptr = song + headersize;

   if ( _MIDI_NumTracks == 0 )
      {
      return( MIDI_NoTracks );
      }

   _MIDI_TrackMemSize = _MIDI_NumTracks  * sizeof( track );
   _MIDI_TrackPtr = (track *) malloc(_MIDI_TrackMemSize);
   if ( !_MIDI_TrackPtr )
      {
      return( MIDI_NoMemory );
      }

   CurrentTrack = _MIDI_TrackPtr;
   numtracks    = _MIDI_NumTracks;
   while( numtracks-- )
      {
      if ( *( unsigned int * )ptr != LITTLE32(MIDI_TRACK_SIGNATURE) )
         {
         free( _MIDI_TrackPtr );

         _MIDI_TrackPtr = NULL;
         _MIDI_TrackMemSize = 0;

         return( MIDI_InvalidTrack );
         }

      tracklength = _MIDI_ReadNumber( ptr + 4, 4 );
      ptr += 8;
      CurrentTrack->start = ptr;
      ptr += tracklength;
      CurrentTrack++;
      }

   if ( _MIDI_Funcs->GetVolume != NULL )
      {
      _MIDI_TotalVolume = _MIDI_Funcs->GetVolume();
      }

   _MIDI_InitEMIDI();

   if ( _MIDI_Funcs->LoadPatch )
      {
      MIDI_LoadTimbres();
      }

   _MIDI_ResetTracks();

   if ( !Reset )
      {
      MIDI_Reset();
      }

   Reset = FALSE;

   MIDI_SetTempo( 120 );

    if (SoundDriver_MIDI_StartPlayback(_MIDI_ServiceRoutine) != MIDI_Ok) {
        return MIDI_DriverError;
    }

   _MIDI_SongLoaded = TRUE;
   _MIDI_SongActive = TRUE;

   return( MIDI_Ok );
   }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %esi, %ebp
movq %rdi, %rbx
cmpb $0x1, 0x1236f(%rip)     # 0x19678
jne 0x7310
callq 0x71a0
movl %ebp, 0x1235e(%rip)     # 0x19674
movq 0x1234b(%rip), %r14     # 0x19668
testq %r14, %r14
je 0x797d
movl $0x2, %eax
cmpl $0x6468544d, (%rbx)     # imm = 0x6468544D
jne 0x7982
xorl %eax, %eax
xorl %r12d, %r12d
shll $0x8, %r12d
movzbl 0x4(%rbx,%rax), %ecx
movslq %r12d, %r12
orq %rcx, %r12
incq %rax
cmpq $0x4, %rax
jne 0x733c
leaq 0x8(%rbx), %r15
xorl %eax, %eax
xorl %ecx, %ecx
movl %ecx, %edx
shll $0x8, %edx
movzbl (%r15,%rax), %ecx
orl %edx, %ecx
incq %rax
cmpq $0x2, %rax
jne 0x735c
xorl %eax, %eax
xorl %ebp, %ebp
movl %ebp, %edx
shll $0x8, %edx
movzbl 0xa(%rbx,%rax), %ebp
orl %edx, %ebp
incq %rax
cmpq $0x2, %rax
jne 0x7375
movl %ebp, 0x122f8(%rip)     # 0x19688
xorl %edx, %edx
xorl %eax, %eax
movl %eax, %esi
shll $0x8, %esi
movzbl 0xc(%rbx,%rdx), %eax
orl %esi, %eax
incq %rdx
cmpq $0x2, %rdx
jne 0x7394
testl %esi, %esi
movl $0x60, %edx
cmovsl %edx, %eax
movl %eax, 0x122e3(%rip)     # 0x1969c
movl $0x3, %eax
cmpl $0x1, %ecx
jg 0x7982
testl %ebp, %ebp
je 0x7991
imull $0x178, %ebp, %eax      # imm = 0x178
movslq %eax, %rdi
callq 0x2280
movq %rax, 0x1229c(%rip)     # 0x19680
testq %rax, %rax
je 0x7998
addq %r12, %r15
movq %rax, %rcx
cmpl $0x6b72544d, (%r15)     # imm = 0x6B72544D
jne 0x799f
decl %ebp
xorl %esi, %esi
xorl %edx, %edx
shll $0x8, %edx
movzbl 0x4(%r15,%rsi), %edi
movslq %edx, %rdx
orq %rdi, %rdx
incq %rsi
cmpq $0x4, %rsi
jne 0x7406
leaq 0x8(%r15), %rsi
movq %rsi, (%rcx)
addq %rdx, %r15
addq $0x8, %r15
addq $0x178, %rcx            # imm = 0x178
testl %ebp, %ebp
jne 0x73f3
movq 0x50(%r14), %rax
testq %rax, %rax
je 0x7448
callq *%rax
movl %eax, 0x8e58(%rip)      # 0x102a0
callq 0x7210
xorl %ebp, %ebp
movl %ebp, 0x12237(%rip)     # 0x1968c
movl %ebp, 0x12235(%rip)     # 0x19690
movl %ebp, 0x12233(%rip)     # 0x19694
movl %ebp, 0x12231(%rip)     # 0x19698
movq 0x12212(%rip), %rbx     # 0x19680
cmpl %ebp, 0x12214(%rip)     # 0x19688
setg %al
testq %rbx, %rbx
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x78f1
movl $0x1, %r15d
movl $0x4, %r12d
xorl %r13d, %r13d
movl %ebp, 0x12213(%rip)     # 0x196b0
movl %r15d, 0x8e00(%rip)     # 0x102a4
movl %r15d, 0x8dfd(%rip)     # 0x102a8
movl %ebp, 0x121fb(%rip)     # 0x196ac
movl %r12d, 0x12288(%rip)    # 0x19740
movl 0x121de(%rip), %eax     # 0x1969c
movl %eax, 0x12280(%rip)     # 0x19744
movl %r12d, 0x1227d(%rip)    # 0x19748
movl %ebp, 0x121d3(%rip)     # 0x196a4
movl %ebp, 0x121d1(%rip)     # 0x196a8
movl $0xffffffff, 0x1218f(%rip) # imm = 0xFFFFFFFF
movl %r15d, 0x14(%rbx)
movb $0x0, 0x172(%rbx)
movw $0x1, 0x170(%rbx)
leaq 0x20(%rbx), %r14
movl $0x150, %edx            # imm = 0x150
movq %r14, %rdi
xorl %esi, %esi
callq 0x20f0
cmpl %ebp, 0x10(%rbx)
jle 0x7520
callq 0x89a0
movl 0x10(%rbx), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x10(%rbx)
cmpl $0x1, %eax
jg 0x750d
cmpw $0x0, 0x14(%rbx)
je 0x786f
movl $0x0, 0x4(%rsp)
movq 0x8(%rbx), %rcx
movzbl (%rcx), %eax
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
movl %eax, %edx
notl %edx
testb $-0x10, %dl
jne 0x75da
cmpl $0xf0, %eax
je 0x7564
cmpl $0xff, %eax
je 0x756e
cmpl $0xf7, %eax
jne 0x7576
movq %rbx, %rdi
callq 0x843a
jmp 0x7576
movq %rbx, %rdi
callq 0x851b
cmpw $0x0, 0x14(%rbx)
je 0x75ca
movq 0x8(%rbx), %rcx
movzbl (%rcx), %eax
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
testb %al, %al
jns 0x75b0
andl $0x7f, %eax
addq $0x2, %rcx
movzbl -0x1(%rcx), %edx
movq %rcx, 0x8(%rbx)
shll $0x7, %eax
movl %edx, %esi
andl $0x7f, %esi
orl %esi, %eax
incq %rcx
testb %dl, %dl
js 0x7597
movl %eax, 0x10(%rbx)
testl %eax, %eax
jle 0x75ca
callq 0x89a0
movl 0x10(%rbx), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x10(%rbx)
cmpl $0x1, %eax
jg 0x75b7
cmpw $0x0, 0x14(%rbx)
jne 0x7533
jmp 0x786f
testb %al, %al
js 0x75e8
movswl 0x16(%rbx), %eax
movq %rcx, 0x8(%rbx)
jmp 0x75ec
movw %ax, 0x16(%rbx)
sarl $0x4, %eax
movslq %eax, %rcx
leaq 0x37c7(%rip), %rax      # 0xadc0
movl (%rax,%rcx,4), %eax
cmpl $0xb, %ecx
jne 0x7807
movq 0x8(%rbx), %rcx
movzbl (%rcx), %esi
xorl %edi, %edi
cmpl $0x7e, %esi
sete %dil
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
movzbl 0x1(%rcx), %edx
addq $0x2, %rcx
movq %rcx, 0x8(%rbx)
addl %edi, %eax
addl $-0x2, %eax
addl $-0x6e, %esi
cmpl $0x9, %esi
ja 0x7807
leaq 0x36df(%rip), %rdi      # 0xad20
movslq (%rdi,%rsi,4), %rsi
addq %rdi, %rsi
jmpq *%rsi
cmpq $0x1, %rdx
movl $0x0, %esi
sbbl %esi, %esi
orl %edx, %esi
movw %si, 0x30(%rbx)
movq %rcx, 0x20(%rbx)
movq %rcx, 0x28(%rbx)
movzwl 0x16(%rbx), %ecx
movw %cx, 0x32(%rbx)
movl 0x1203b(%rip), %ecx     # 0x196ac
movl %ecx, 0x34(%rbx)
movl 0x12026(%rip), %ecx     # 0x196a0
movl %ecx, 0x38(%rbx)
movzwl 0x1202c(%rip), %ecx     # 0x196b0
movw %cx, 0x3c(%rbx)
movzwl 0x8c15(%rip), %ecx      # 0x102a4
movw %cx, 0x3e(%rbx)
movzwl 0x8c0e(%rip), %ecx      # 0x102a8
movw %cx, 0x40(%rbx)
movzwl 0x1209b(%rip), %ecx     # 0x19740
movw %cx, 0x42(%rbx)
movzwl 0x12094(%rip), %ecx     # 0x19744
movw %cx, 0x44(%rbx)
movzwl 0x1208d(%rip), %ecx     # 0x19748
movw %cx, 0x46(%rbx)
jmp 0x7807
cmpl $0x7f, %edx
jne 0x7807
movq $0x0, 0x28(%rbx)
movw $0x0, 0x30(%rbx)
jmp 0x7807
cmpb $0x0, 0x171(%rbx)
jne 0x7807
movb $0x1, 0x171(%rbx)
jmp 0x7807
cmpl $0x7f, %edx
je 0x77f8
cmpl %edx, 0x11e88(%rip)     # 0x19590
je 0x77f8
cmpl $0x0, 0x4(%rsp)
jne 0x77ff
movb $0x0, 0x170(%rbx)
jmp 0x77ff
cmpl $0x7f, %edx
je 0x7736
cmpl %edx, 0x11e60(%rip)     # 0x19590
jne 0x7807
movb $0x0, 0x170(%rbx)
jmp 0x7807
cmpb $0x0, 0x172(%rbx)
jne 0x7807
movb $0x1, 0x172(%rbx)
jmp 0x7807
movl %edx, %esi
decb %sil
cmpb $0x5, %sil
ja 0x7807
leaq (%rdx,%rdx,2), %rdx
shll $0x4, %edx
movq %rcx, (%r14,%rdx)
movq 0x28(%rbx), %rcx
movq %rcx, 0x8(%r14,%rdx)
movzwl 0x30(%rbx), %ecx
movw %cx, 0x10(%r14,%rdx)
movzwl 0x16(%rbx), %ecx
movw %cx, 0x12(%r14,%rdx)
movl 0x11f14(%rip), %ecx     # 0x196ac
movl %ecx, 0x14(%r14,%rdx)
movl 0x11efd(%rip), %ecx     # 0x196a0
movl %ecx, 0x18(%r14,%rdx)
movzwl 0x11f01(%rip), %ecx     # 0x196b0
movw %cx, 0x1c(%r14,%rdx)
movzwl 0x8ae8(%rip), %ecx      # 0x102a4
movw %cx, 0x1e(%r14,%rdx)
movzwl 0x8adf(%rip), %ecx      # 0x102a8
movw %cx, 0x20(%r14,%rdx)
movzwl 0x11f6a(%rip), %ecx     # 0x19740
movw %cx, 0x22(%r14,%rdx)
movzwl 0x11f61(%rip), %ecx     # 0x19744
movw %cx, 0x24(%r14,%rdx)
movzwl 0x11f58(%rip), %ecx     # 0x19748
movw %cx, 0x26(%r14,%rdx)
jmp 0x7807
movb $0x1, 0x170(%rbx)
movl $0x1, 0x4(%rsp)
movq 0x8(%rbx), %rcx
movslq %eax, %rdx
leaq (%rcx,%rdx), %rax
movq %rax, 0x8(%rbx)
leaq (%rcx,%rdx), %rsi
incq %rsi
movzbl -0x1(%rsi), %eax
movq %rsi, 0x8(%rbx)
testb %al, %al
jns 0x784c
andl $0x7f, %eax
addq %rdx, %rcx
addq $0x2, %rcx
movzbl -0x1(%rcx), %edx
movq %rcx, 0x8(%rbx)
shll $0x7, %eax
movl %edx, %esi
andl $0x7f, %esi
orl %esi, %eax
incq %rcx
testb %dl, %dl
js 0x7833
movl %eax, 0x10(%rbx)
testl %eax, %eax
jle 0x75ca
callq 0x89a0
movl 0x10(%rbx), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x10(%rbx)
cmpl $0x1, %eax
jg 0x7857
jmp 0x75ca
movl 0x11e17(%rip), %eax     # 0x1968c
movl 0x11e31(%rip), %ecx     # 0x196ac
cmpl %ecx, %eax
cmoval %eax, %ecx
movl %ecx, 0x11e06(%rip)     # 0x1968c
movl 0x11e24(%rip), %eax     # 0x196b0
movl 0x8a12(%rip), %ecx      # 0x102a4
movl %ecx, %esi
shll $0x9, %esi
addl %eax, %esi
movl 0x8a09(%rip), %edx      # 0x102a8
movl %edx, %edi
shll $0x10, %edi
addl %esi, %edi
movl 0x11de8(%rip), %esi     # 0x19694
shll $0x9, %esi
addl 0x11ddb(%rip), %esi     # 0x19690
movl 0x11ddc(%rip), %r8d     # 0x19698
shll $0x10, %r8d
addl %esi, %r8d
cmpl %r8d, %edi
jbe 0x78da
movl %eax, 0x11dc2(%rip)     # 0x19690
movl %ecx, 0x11dc0(%rip)     # 0x19694
movl %edx, 0x11dbe(%rip)     # 0x19698
addq $0x178, %rbx            # imm = 0x178
incl %r13d
cmpl 0x11d9d(%rip), %r13d    # 0x19688
jl 0x7497
callq 0x7210
movq 0x11d6b(%rip), %rax     # 0x19668
cmpq $0x0, 0x40(%rax)
je 0x7909
callq 0x79b9
callq 0x7210
cmpb $0x0, 0x11d4b(%rip)     # 0x19660
jne 0x791c
callq 0x6f03
movb $0x0, 0x11d3d(%rip)     # 0x19660
movl $0x78, 0x8923(%rip)     # 0x10250
movl 0x11d69(%rip), %esi     # 0x1969c
movl $0x78, %edi
callq 0x2e5c
movl 0x11d59(%rip), %ecx     # 0x1969c
addl %ecx, %ecx
movl $0x10000, %eax          # imm = 0x10000
xorl %edx, %edx
idivl %ecx
movl %eax, 0x11d4c(%rip)     # 0x196a0
leaq 0x2ab(%rip), %rdi       # 0x7c06
callq 0x2e24
movl %eax, %ecx
movl $0x7, %eax
testl %ecx, %ecx
jne 0x7982
movb $0x1, %al
movb %al, 0x11d05(%rip)      # 0x19678
movb %al, 0x11d03(%rip)      # 0x1967c
xorl %eax, %eax
jmp 0x7982
movl $0x1, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x4, %eax
jmp 0x7982
movl $0x6, %eax
jmp 0x7982
movq %rax, %rdi
callq 0x2260
movq $0x0, 0x11cce(%rip) # 0x19680
movl $0x5, %eax
jmp 0x7982