// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
cmpl $0x0, 0x13d12(%rip)     # 0x1f0b8
je 0xb3ad
callq 0xb1b0
movl -0x14(%rbp), %eax
movl %eax, 0x13cfe(%rip)     # 0x1f0b4
cmpq $0x0, 0x13cea(%rip)     # 0x1f0a8
jne 0xb3cc
movl $0x1, -0x4(%rbp)
jmp 0xb617
movq -0x10(%rbp), %rax
cmpl $0x6468544d, (%rax)     # imm = 0x6468544D
je 0xb3e4
movl $0x2, -0x4(%rbp)
jmp 0xb617
movq -0x10(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rdi
movl $0x4, %esi
callq 0xb620
movl %eax, -0x20(%rbp)
movq -0x10(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rdi
movl $0x2, %esi
callq 0xb620
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rdi
addq $0x2, %rdi
movl $0x2, %esi
callq 0xb620
movl %eax, 0x13c92(%rip)     # 0x1f0c8
movq -0x10(%rbp), %rdi
addq $0x4, %rdi
movl $0x2, %esi
callq 0xb620
movl %eax, 0x13c92(%rip)     # 0x1f0e0
cmpl $0x0, 0x13c8b(%rip)     # 0x1f0e0
jge 0xb461
movl $0x60, 0x13c7f(%rip)    # 0x1f0e0
cmpl $0x1, -0x1c(%rbp)
jle 0xb473
movl $0x3, -0x4(%rbp)
jmp 0xb617
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
cmpl $0x0, 0x13c3f(%rip)     # 0x1f0c8
jne 0xb497
movl $0x4, -0x4(%rbp)
jmp 0xb617
movslq 0x13c2a(%rip), %rax     # 0x1f0c8
imulq $0x178, %rax, %rax      # imm = 0x178
movl %eax, 0x13c21(%rip)     # 0x1f0cc
movslq 0x13c1a(%rip), %rdi     # 0x1f0cc
callq 0x2290
movq %rax, 0x13c02(%rip)     # 0x1f0c0
cmpq $0x0, 0x13bfa(%rip)     # 0x1f0c0
jne 0xb4d4
movl $0x6, -0x4(%rbp)
jmp 0xb617
movq 0x13be5(%rip), %rax     # 0x1f0c0
movq %rax, -0x30(%rbp)
movl 0x13be3(%rip), %eax     # 0x1f0c8
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, -0x18(%rbp)
cmpl $0x0, %eax
je 0xb585
movq -0x38(%rbp), %rax
cmpl $0x6b72544d, (%rax)     # imm = 0x6B72544D
je 0xb535
movq 0x13bb1(%rip), %rdi     # 0x1f0c0
callq 0x2270
movq $0x0, 0x13ba1(%rip) # 0x1f0c0
movl $0x0, 0x13ba3(%rip)     # 0x1f0cc
movl $0x5, -0x4(%rbp)
jmp 0xb617
movq -0x38(%rbp), %rdi
addq $0x4, %rdi
movl $0x4, %esi
callq 0xb620
movl %eax, -0x24(%rbp)
movq -0x38(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movl -0x24(%rbp), %ecx
movq -0x38(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
addq $0x178, %rax            # imm = 0x178
movq %rax, -0x30(%rbp)
jmp 0xb4e8
movq 0x13b1c(%rip), %rax     # 0x1f0a8
cmpq $0x0, 0x50(%rax)
je 0xb5a3
movq 0x13b0e(%rip), %rax     # 0x1f0a8
callq *0x50(%rax)
movl %eax, 0xa6bd(%rip)      # 0x15c60
callq 0xb690
movq 0x13af9(%rip), %rax     # 0x1f0a8
cmpq $0x0, 0x40(%rax)
je 0xb5bb
callq 0xbd90
callq 0xb260
cmpl $0x0, 0x13ad9(%rip)     # 0x1f0a0
jne 0xb5ce
callq 0xada0
movl $0x0, 0x13ac8(%rip)     # 0x1f0a0
movl $0x78, %edi
callq 0xc020
leaq 0xa87(%rip), %rdi       # 0xc070
callq 0x33f0
cmpl $0x0, %eax
je 0xb5fc
movl $0x7, -0x4(%rbp)
jmp 0xb617
movl $0x1, 0x13ab2(%rip)     # 0x1f0b8
movl $0x1, 0x13aac(%rip)     # 0x1f0bc
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq