// File: /workspace/github_c_makefile_star2_O3/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, %ebx
movq %rdi, %r14
cmpb $0x1, 0x122df(%rip)     # 0x19678
jne 0x73a0
callq 0x7230
movl %ebx, 0x122ce(%rip)     # 0x19674
movq 0x122bb(%rip), %r15     # 0x19668
testq %r15, %r15
je 0x74b3
movl $0x2, %eax
cmpl $0x6468544d, (%r14)     # imm = 0x6468544D
jne 0x74b8
xorl %eax, %eax
xorl %r12d, %r12d
shll $0x8, %r12d
movzbl 0x4(%r14,%rax), %ecx
movslq %r12d, %r12
orq %rcx, %r12
incq %rax
cmpq $0x4, %rax
jne 0x73cd
movzwl 0xa(%r14), %eax
rolw $0x8, %ax
movzwl 0x8(%r14), %ecx
movzwl %ax, %ebp
movl %ebp, 0x1228b(%rip)     # 0x19688
movzwl 0xc(%r14), %eax
rolw $0x8, %ax
rolw $0x8, %cx
movzwl %ax, %eax
movl %eax, 0x12289(%rip)     # 0x1969c
movl $0x3, %eax
cmpw $0x1, %cx
ja 0x74b8
testl %ebp, %ebp
je 0x74c7
imull $0x178, %ebp, %edi      # imm = 0x178
callq 0x2290
movq %rax, 0x12244(%rip)     # 0x19680
testq %rax, %rax
je 0x74ce
movq %rax, %rbx
addq $0x8, %r14
addq %r12, %r14
cmpl $0x6b72544d, (%r14)     # imm = 0x6B72544D
jne 0x74d5
decl %ebp
xorl %edx, %edx
xorl %ecx, %ecx
shll $0x8, %ecx
movzbl 0x4(%r14,%rdx), %esi
movslq %ecx, %rcx
orq %rsi, %rcx
incq %rdx
cmpq $0x4, %rdx
jne 0x745e
leaq 0x8(%r14), %rdx
movq %rdx, (%rax)
addq %rcx, %r14
addq $0x8, %r14
addq $0x178, %rax            # imm = 0x178
testl %ebp, %ebp
jne 0x744f
movq 0x50(%r15), %rax
testq %rax, %rax
je 0x74ef
callq *%rax
movl %eax, 0x8e01(%rip)      # 0x102a0
movq 0x121da(%rip), %rbx     # 0x19680
cmpl $0x0, 0x121db(%rip)     # 0x19688
setg %bpl
jmp 0x74f2
movl $0x1, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x4, %eax
jmp 0x74b8
movl $0x6, %eax
jmp 0x74b8
movq %rbx, %rdi
callq 0x2270
movq $0x0, 0x12198(%rip) # 0x19680
movl $0x5, %eax
jmp 0x74b8
movb $0x1, %bpl
callq 0x72a0
xorl %eax, %eax
movl %eax, 0x1218d(%rip)     # 0x1968c
movl %eax, 0x1218b(%rip)     # 0x19690
movl %eax, 0x12189(%rip)     # 0x19694
movl %eax, 0x12187(%rip)     # 0x19698
testb %bpl, %bpl
je 0x79a2
testq %rbx, %rbx
je 0x79a2
xorl %ebp, %ebp
movl $0x1, %r15d
xorl %r13d, %r13d
movl %ebp, 0x1217c(%rip)     # 0x196b0
movl %r15d, 0x8d69(%rip)     # 0x102a4
movl %r15d, 0x8d66(%rip)     # 0x102a8
movl %ebp, 0x12164(%rip)     # 0x196ac
movl $0x4, %ecx
movl %ecx, 0x121ed(%rip)     # 0x19740
movl 0x12143(%rip), %eax     # 0x1969c
movl %eax, 0x121e5(%rip)     # 0x19744
movl %ecx, 0x121e3(%rip)     # 0x19748
movl %ebp, 0x12139(%rip)     # 0x196a4
movl %ebp, 0x12137(%rip)     # 0x196a8
movl $0xffffffff, 0x120f5(%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 0x7652
callq 0x8a0e
movl 0x10(%rbx), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x10(%rbx)
cmpl $0x1, %eax
jg 0x75ab
movzwl 0x14(%rbx), %eax
testw %ax, %ax
jne 0x7656
movl 0x120bb(%rip), %eax     # 0x1968c
movl 0x120d5(%rip), %ecx     # 0x196ac
cmpl %ecx, %eax
cmoval %eax, %ecx
movl %ecx, 0x120aa(%rip)     # 0x1968c
movl 0x120c8(%rip), %eax     # 0x196b0
movl 0x8cb6(%rip), %ecx      # 0x102a4
movl %ecx, %esi
shll $0x9, %esi
addl %eax, %esi
movl 0x8cad(%rip), %edx      # 0x102a8
movl %edx, %edi
shll $0x10, %edi
addl %esi, %edi
movl 0x1208c(%rip), %esi     # 0x19694
shll $0x9, %esi
addl 0x1207f(%rip), %esi     # 0x19690
movl 0x12080(%rip), %r8d     # 0x19698
shll $0x10, %r8d
addl %esi, %r8d
cmpl %r8d, %edi
jbe 0x7636
movl %eax, 0x12066(%rip)     # 0x19690
movl %ecx, 0x12064(%rip)     # 0x19694
movl %edx, 0x12062(%rip)     # 0x19698
addq $0x178, %rbx            # imm = 0x178
incl %r13d
cmpl 0x12041(%rip), %r13d    # 0x19688
jl 0x752e
jmp 0x79a2
movw $0x1, %ax
xorl %r12d, %r12d
movq 0x8(%rbx), %rsi
movb (%rsi), %dl
movzbl %dl, %ecx
leaq 0x1(%rsi), %rdi
movq %rdi, 0x8(%rbx)
movl %ecx, %edi
notl %edi
testb $-0x10, %dil
jne 0x770d
cmpb $-0x10, %dl
je 0x768e
movzbl %dl, %eax
cmpl $0xff, %eax
je 0x7698
cmpl $0xf7, %eax
jne 0x76a0
movq %rbx, %rdi
callq 0x84a8
jmp 0x76a0
movq %rbx, %rdi
callq 0x8591
movzwl 0x14(%rbx), %eax
testw %ax, %ax
je 0x75cb
movq 0x8(%rbx), %rdx
movzbl (%rdx), %ecx
leaq 0x1(%rdx), %rsi
movq %rsi, 0x8(%rbx)
testb %cl, %cl
jns 0x76e1
andl $0x7f, %ecx
addq $0x2, %rdx
movzbl -0x1(%rdx), %esi
movq %rdx, 0x8(%rbx)
shll $0x7, %ecx
movl %esi, %edi
andl $0x7f, %edi
orl %edi, %ecx
incq %rdx
testb %sil, %sil
js 0x76c7
movl %ecx, 0x10(%rbx)
testl %ecx, %ecx
jle 0x76ff
callq 0x8a0e
movl 0x10(%rbx), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x10(%rbx)
cmpl $0x1, %eax
jg 0x76e8
movzwl 0x14(%rbx), %eax
testw %ax, %ax
jne 0x7659
jmp 0x75cb
testb %dl, %dl
js 0x771e
movzwl 0x16(%rbx), %edi
movswl %di, %ecx
movq %rsi, 0x8(%rbx)
jmp 0x7728
incq %rsi
movzbl %dl, %edi
movw %di, 0x16(%rbx)
sarl $0x4, %ecx
movslq %ecx, %rdx
leaq 0x36fb(%rip), %rcx      # 0xae30
movl (%rcx,%rdx,4), %ecx
cmpl $0xb, %edx
jne 0x77fc
movzbl (%rsi), %r8d
xorl %r9d, %r9d
cmpl $0x7e, %r8d
sete %r9b
leaq 0x1(%rsi), %rdx
movq %rdx, 0x8(%rbx)
leaq 0x2(%rsi), %rdx
addl %r9d, %ecx
addl $-0x2, %ecx
addl $-0x6e, %r8d
cmpl $0x9, %r8d
ja 0x77ff
movzbl 0x1(%rsi), %esi
leaq 0x3615(%rip), %r9       # 0xad90
movslq (%r9,%r8,4), %r8
addq %r9, %r8
jmpq *%r8
cmpq $0x1, %rsi
movl $0x0, %r8d
sbbl %r8d, %r8d
orl %esi, %r8d
movw %r8w, 0x30(%rbx)
movq %rdx, 0x20(%rbx)
movq %rdx, 0x28(%rbx)
movw %di, 0x32(%rbx)
movl 0x11f00(%rip), %esi     # 0x196ac
movl %esi, 0x34(%rbx)
movl 0x11eeb(%rip), %esi     # 0x196a0
movl %esi, 0x38(%rbx)
movzwl 0x11ef1(%rip), %esi     # 0x196b0
movw %si, 0x3c(%rbx)
movzwl 0x8ada(%rip), %esi      # 0x102a4
movw %si, 0x3e(%rbx)
movzwl 0x8ad3(%rip), %esi      # 0x102a8
movw %si, 0x40(%rbx)
movzwl 0x11f60(%rip), %esi     # 0x19740
movw %si, 0x42(%rbx)
movzwl 0x11f59(%rip), %esi     # 0x19744
movw %si, 0x44(%rbx)
movzwl 0x11f52(%rip), %esi     # 0x19748
movw %si, 0x46(%rbx)
jmp 0x77ff
movq %rsi, %rdx
movslq %ecx, %rsi
leaq (%rdx,%rsi), %rcx
movq %rcx, 0x8(%rbx)
leaq (%rdx,%rsi), %rdi
incq %rdi
movzbl -0x1(%rdi), %ecx
movq %rdi, 0x8(%rbx)
testb %cl, %cl
jns 0x7841
andl $0x7f, %ecx
addq %rsi, %rdx
addq $0x2, %rdx
movzbl -0x1(%rdx), %esi
movq %rdx, 0x8(%rbx)
shll $0x7, %ecx
movl %esi, %edi
andl $0x7f, %edi
orl %edi, %ecx
incq %rdx
testb %sil, %sil
js 0x7827
movl %ecx, 0x10(%rbx)
testl %ecx, %ecx
jle 0x76ff
callq 0x8a0e
movl 0x10(%rbx), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x10(%rbx)
cmpl $0x1, %eax
jg 0x784c
jmp 0x76fb
cmpl $0x7f, %esi
jne 0x77ff
movq $0x0, 0x28(%rbx)
movw $0x0, 0x30(%rbx)
jmp 0x77ff
cmpb $0x0, 0x171(%rbx)
jne 0x77ff
movb $0x1, 0x171(%rbx)
jmp 0x77ff
cmpl $0x7f, %esi
je 0x7990
cmpl %esi, 0x11cef(%rip)     # 0x19590
je 0x7990
testl %r12d, %r12d
jne 0x7997
movb $0x0, 0x170(%rbx)
jmp 0x7997
cmpl $0x7f, %esi
je 0x78cd
cmpl %esi, 0x11cc9(%rip)     # 0x19590
jne 0x77ff
movb $0x0, 0x170(%rbx)
jmp 0x77ff
cmpb $0x0, 0x172(%rbx)
jne 0x77ff
movb $0x1, 0x172(%rbx)
jmp 0x77ff
movl %esi, %r8d
decb %r8b
cmpb $0x5, %r8b
ja 0x77ff
leaq (%rsi,%rsi,2), %rsi
shll $0x4, %esi
movq %rdx, (%r14,%rsi)
movq 0x28(%rbx), %r8
movq %r8, 0x8(%r14,%rsi)
movzwl 0x30(%rbx), %r8d
movw %r8w, 0x10(%r14,%rsi)
movw %di, 0x12(%r14,%rsi)
movl 0x11d7f(%rip), %edi     # 0x196ac
movl %edi, 0x14(%r14,%rsi)
movl 0x11d68(%rip), %edi     # 0x196a0
movl %edi, 0x18(%r14,%rsi)
movzwl 0x11d6c(%rip), %edi     # 0x196b0
movw %di, 0x1c(%r14,%rsi)
movzwl 0x8953(%rip), %edi      # 0x102a4
movw %di, 0x1e(%r14,%rsi)
movzwl 0x894a(%rip), %edi      # 0x102a8
movw %di, 0x20(%r14,%rsi)
movzwl 0x11dd5(%rip), %edi     # 0x19740
movw %di, 0x22(%r14,%rsi)
movzwl 0x11dcc(%rip), %edi     # 0x19744
movw %di, 0x24(%r14,%rsi)
movzwl 0x11dc3(%rip), %edi     # 0x19748
movw %di, 0x26(%r14,%rsi)
jmp 0x77ff
movb $0x1, 0x170(%rbx)
movl $0x1, %r12d
jmp 0x77ff
callq 0x72a0
movq 0x11cba(%rip), %rax     # 0x19668
cmpq $0x0, 0x40(%rax)
je 0x79ba
callq 0x7a35
callq 0x72a0
cmpb $0x0, 0x11c9a(%rip)     # 0x19660
jne 0x79cd
callq 0x6f93
movb $0x0, 0x11c8c(%rip)     # 0x19660
movl $0x78, 0x8872(%rip)     # 0x10250
movl 0x11cb8(%rip), %esi     # 0x1969c
movl $0x78, %edi
callq 0x2e64
movl 0x11ca8(%rip), %ecx     # 0x1969c
addl %ecx, %ecx
movl $0x10000, %eax          # imm = 0x10000
xorl %edx, %edx
idivl %ecx
movl %eax, 0x11c9b(%rip)     # 0x196a0
leaq 0x252(%rip), %rdi       # 0x7c5e
callq 0x2e2c
movl %eax, %ecx
movl $0x7, %eax
testl %ecx, %ecx
jne 0x74b8
movb $0x1, %al
movb %al, 0x11c50(%rip)      # 0x19678
movb %al, 0x11c4e(%rip)      # 0x1967c
xorl %eax, %eax
jmp 0x74b8