// File: /workspace/github_c_makefile_star2_O2/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, 0x1190f(%rip)     # 0x18688
jne 0x6d80
callq 0x6c37
movl %ebp, 0x118fe(%rip)     # 0x18684
movq 0x118eb(%rip), %r15     # 0x18678
testq %r15, %r15
je 0x6dfb
cmpl $0x6468544d, (%rbx)     # imm = 0x6468544D
jne 0x6dff
leaq 0x4(%rbx), %rdi
pushq $0x4
popq %rsi
callq 0x7301
movl %eax, 0x4(%rsp)
leaq 0x8(%rbx), %r14
pushq $0x2
popq %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x7301
movl %eax, %r13d
leaq 0xa(%rbx), %rdi
movq %r12, %rsi
callq 0x7301
movl %eax, %ebp
movl %eax, 0x118c4(%rip)     # 0x18698
addq $0xc, %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x7301
testl %eax, %eax
pushq $0x60
popq %rcx
cmovnsl %eax, %ecx
movl %ecx, 0x118bb(%rip)     # 0x186ac
cmpl $0x1, %r13d
jle 0x6e11
pushq $0x3
jmp 0x6e01
pushq $0x1
jmp 0x6e01
pushq $0x2
popq %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %ebp, %ebp
je 0x6e79
imull $0x178, %ebp, %eax      # imm = 0x178
movslq %eax, %rdi
callq 0x22a0
movq %rax, 0x11866(%rip)     # 0x18690
testq %rax, %rax
je 0x6e7d
movq %rax, %rbx
movslq 0x4(%rsp), %rax
addq %rax, %r14
pushq $0x4
popq %r12
movq %rbx, %r13
subl $0x1, %ebp
jb 0x6e81
cmpl $0x6b72544d, (%r14)     # imm = 0x6B72544D
jne 0x72d5
leaq 0x4(%r14), %rdi
movq %r12, %rsi
callq 0x7301
leaq 0x8(%r14), %rcx
movq %rcx, (%r13)
cltq
addq %rax, %r14
addq $0x8, %r14
addq $0x178, %r13            # imm = 0x178
jmp 0x6e41
pushq $0x4
jmp 0x6e01
pushq $0x6
jmp 0x6e01
movq 0x50(%r15), %rax
testq %rax, %rax
je 0x6e99
callq *%rax
movl %eax, 0x841e(%rip)      # 0xf2b0
movq 0x117f7(%rip), %rbx     # 0x18690
callq 0x6cac
andl $0x0, 0x117f7(%rip)     # 0x1869c
andl $0x0, 0x117f4(%rip)     # 0x186a0
andl $0x0, 0x117f1(%rip)     # 0x186a4
andl $0x0, 0x117ee(%rip)     # 0x186a8
xorl %ebp, %ebp
pushq $0x1
popq %r15
pushq $0x4
popq %r12
cmpl 0x117ce(%rip), %ebp     # 0x18698
jge 0x7284
testq %rbx, %rbx
je 0x7284
andl $0x0, 0x117e0(%rip)     # 0x186c0
movl %r15d, 0x83cd(%rip)     # 0xf2b4
movl %r15d, 0x83ca(%rip)     # 0xf2b8
andl $0x0, 0x117c7(%rip)     # 0x186bc
movl %r12d, 0x11854(%rip)    # 0x18750
movl 0x117aa(%rip), %eax     # 0x186ac
andl $0x0, 0x117ab(%rip)     # 0x186b4
movl %eax, 0x11845(%rip)     # 0x18754
andl $0x0, 0x117a2(%rip)     # 0x186b8
movl %r12d, 0x1183b(%rip)    # 0x18758
orl $-0x1, 0x1175c(%rip)    # 0x18680
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
movl 0x10(%rbx), %eax
testl %eax, %eax
jle 0x6f61
callq 0x8032
movl 0x10(%rbx), %eax
decl %eax
movl %eax, 0x10(%rbx)
jmp 0x6f4e
xorl %r13d, %r13d
cmpw $0x0, 0x14(%rbx)
je 0x720b
movq 0x8(%rbx), %rcx
movzbl (%rcx), %eax
leaq 0x1(%rcx), %rsi
movq %rsi, 0x8(%rbx)
movl %eax, %edx
notl %edx
testb $-0x10, %dl
jne 0x6fd0
cmpl $0xf0, %eax
je 0x6f9c
cmpl $0xff, %eax
je 0x6fa6
cmpl $0xf7, %eax
jne 0x6fae
movq %rbx, %rdi
callq 0x7ba4
jmp 0x6fae
movq %rbx, %rdi
callq 0x7c3f
cmpw $0x0, 0x14(%rbx)
je 0x6f64
movq %rbx, %rdi
callq 0x7b70
movl %eax, 0x10(%rbx)
testl %eax, %eax
jle 0x6f64
callq 0x8032
movl 0x10(%rbx), %eax
decl %eax
jmp 0x6fbd
testb %al, %al
js 0x6fe4
movzwl 0x16(%rbx), %edx
movswl %dx, %eax
movq %rcx, 0x8(%rbx)
movq %rcx, %rsi
jmp 0x6fea
movw %ax, 0x16(%rbx)
movl %eax, %edx
sarl $0x4, %eax
movslq %eax, %rcx
leaq 0x2d99(%rip), %rax      # 0x9d90
movl (%rax,%rcx,4), %eax
cmpl $0xb, %ecx
jne 0x70b5
movzbl (%rsi), %edi
xorl %r8d, %r8d
cmpl $0x7e, %edi
sete %r8b
leaq 0x1(%rsi), %rcx
movq %rcx, 0x8(%rbx)
leaq 0x2(%rsi), %rcx
addl %r8d, %eax
addl $-0x2, %eax
addl $-0x6e, %edi
cmpl $0x9, %edi
ja 0x70b8
movzbl 0x1(%rsi), %esi
leaq 0x2cb7(%rip), %r8       # 0x9cf0
movslq (%r8,%rdi,4), %rdi
addq %r8, %rdi
jmpq *%rdi
cmpq $0x1, %rsi
movl $0x0, %edi
sbbl %edi, %edi
orl %esi, %edi
movw %di, 0x30(%rbx)
movq %rcx, 0x20(%rbx)
movq %rcx, 0x28(%rbx)
movw %dx, 0x32(%rbx)
movl 0x11657(%rip), %edx     # 0x186bc
movl %edx, 0x34(%rbx)
movl 0x11642(%rip), %edx     # 0x186b0
movl %edx, 0x38(%rbx)
movzwl 0x11648(%rip), %edx     # 0x186c0
movw %dx, 0x3c(%rbx)
movzwl 0x8231(%rip), %edx      # 0xf2b4
movw %dx, 0x3e(%rbx)
movzwl 0x822a(%rip), %edx      # 0xf2b8
movw %dx, 0x40(%rbx)
movzwl 0x116b7(%rip), %edx     # 0x18750
movw %dx, 0x42(%rbx)
movzwl 0x116b0(%rip), %edx     # 0x18754
movw %dx, 0x44(%rbx)
movzwl 0x116a9(%rip), %edx     # 0x18758
movw %dx, 0x46(%rbx)
jmp 0x70b8
movq %rsi, %rcx
cltq
addq %rcx, %rax
movq %rax, 0x8(%rbx)
movq %rbx, %rdi
callq 0x7b70
movl %eax, 0x10(%rbx)
testl %eax, %eax
jle 0x6f64
callq 0x8032
movl 0x10(%rbx), %eax
decl %eax
jmp 0x70c9
cmpl $0x7f, %esi
jne 0x70b8
andq $0x0, 0x28(%rbx)
andw $0x0, 0x30(%rbx)
jmp 0x70b8
cmpb $0x0, 0x171(%rbx)
jne 0x70b8
movb $0x1, 0x171(%rbx)
jmp 0x70b8
cmpl $0x7f, %esi
je 0x71fc
cmpl %esi, 0x1148e(%rip)     # 0x185a0
je 0x71fc
testl %r13d, %r13d
jne 0x7203
movb $0x0, 0x170(%rbx)
jmp 0x7203
cmpl $0x7f, %esi
je 0x713e
cmpl %esi, 0x11468(%rip)     # 0x185a0
jne 0x70b8
movb $0x0, 0x170(%rbx)
jmp 0x70b8
cmpb $0x0, 0x172(%rbx)
jne 0x70b8
movb $0x1, 0x172(%rbx)
jmp 0x70b8
movl %esi, %edi
decb %dil
cmpb $0x5, %dil
ja 0x70b8
imulq $0x30, %rsi, %rsi
movq %rcx, (%r14,%rsi)
movq 0x28(%rbx), %rdi
movq %rdi, 0x8(%r14,%rsi)
movzwl 0x30(%rbx), %edi
movw %di, 0x10(%r14,%rsi)
movw %dx, 0x12(%r14,%rsi)
movl 0x11523(%rip), %edx     # 0x186bc
movl %edx, 0x14(%r14,%rsi)
movl 0x1150c(%rip), %edx     # 0x186b0
movl %edx, 0x18(%r14,%rsi)
movzwl 0x11510(%rip), %edx     # 0x186c0
movw %dx, 0x1c(%r14,%rsi)
movzwl 0x80f7(%rip), %edx      # 0xf2b4
movw %dx, 0x1e(%r14,%rsi)
movzwl 0x80ee(%rip), %edx      # 0xf2b8
movw %dx, 0x20(%r14,%rsi)
movzwl 0x11579(%rip), %edx     # 0x18750
movw %dx, 0x22(%r14,%rsi)
movzwl 0x11570(%rip), %edx     # 0x18754
movw %dx, 0x24(%r14,%rsi)
movzwl 0x11567(%rip), %edx     # 0x18758
movw %dx, 0x26(%r14,%rsi)
jmp 0x70b8
movb $0x1, 0x170(%rbx)
movl %r15d, %r13d
jmp 0x70b8
movl 0x1148b(%rip), %eax     # 0x1869c
movl 0x114a5(%rip), %ecx     # 0x186bc
cmpl %ecx, %eax
cmoval %eax, %ecx
movl %ecx, 0x1147a(%rip)     # 0x1869c
movl 0x11498(%rip), %eax     # 0x186c0
movl 0x8086(%rip), %ecx      # 0xf2b4
movl %ecx, %esi
shll $0x9, %esi
addl %eax, %esi
movl 0x807d(%rip), %edx      # 0xf2b8
movl %edx, %edi
shll $0x10, %edi
addl %esi, %edi
movl 0x1145c(%rip), %esi     # 0x186a4
shll $0x9, %esi
addl 0x1144f(%rip), %esi     # 0x186a0
movl 0x11450(%rip), %r8d     # 0x186a8
shll $0x10, %r8d
addl %esi, %r8d
cmpl %r8d, %edi
jbe 0x7276
movl %eax, 0x11436(%rip)     # 0x186a0
movl %ecx, 0x11434(%rip)     # 0x186a4
movl %edx, 0x11432(%rip)     # 0x186a8
addq $0x178, %rbx            # imm = 0x178
incl %ebp
jmp 0x6ec4
callq 0x6cac
movq 0x113e8(%rip), %rax     # 0x18678
cmpq $0x0, 0x40(%rax)
je 0x729c
callq 0x7319
callq 0x6cac
cmpb $0x0, 0x113c8(%rip)     # 0x18670
jne 0x72af
callq 0x69d3
movb $0x0, 0x113ba(%rip)     # 0x18670
pushq $0x78
popq %rdi
callq 0x746f
leaq 0x1e0(%rip), %rdi       # 0x74a5
callq 0x2dae
testl %eax, %eax
je 0x72ec
pushq $0x7
jmp 0x6e01
movq %rbx, %rdi
callq 0x2280
andq $0x0, 0x113ab(%rip)     # 0x18690
pushq $0x5
jmp 0x6e01
movb $0x1, %al
movb %al, 0x11394(%rip)      # 0x18688
movb %al, 0x11392(%rip)      # 0x1868c
xorl %eax, %eax
jmp 0x6e02