// File: /workspace/github_c_makefile_star2_O1/jonof[P]jfaudiolib/src/multivoc.c
// Function: MV_PlayLoopedWAV
int MV_PlayLoopedWAV
   (
   char *ptr,
   unsigned int ptrlength,
   int   loopstart,
   int   loopend,
   int   pitchoffset,
   int   vol,
   int   left,
   int   right,
   int   priority,
   unsigned int callbackval
   )

   {
   riff_header   riff;
   format_header format;
   data_header   data;
   VoiceNode     *voice;
   char *dataptr = ptr;
   int length;
   int absloopend;
   int absloopstart;
   int sizemask;

   (void)ptrlength;

   if ( !MV_Installed )
      {
      MV_SetErrorCode( MV_NotInstalled );
      return( MV_Error );
      }

   memcpy(&riff, dataptr, sizeof(riff_header));
   riff.file_size   = LITTLE32(riff.file_size);
   riff.format_size = LITTLE32(riff.format_size);
   dataptr += sizeof(riff_header);

   if ( ( memcmp( riff.RIFF, "RIFF", 4 ) != 0 ) ||
      ( memcmp( riff.WAVE, "WAVE", 4 ) != 0 ) ||
      ( memcmp( riff.fmt, "fmt ", 4) != 0 ) )
      {
      MV_SetErrorCode( MV_InvalidWAVFile );
      return( MV_Error );
      }

   memcpy(&format, dataptr, sizeof(format_header));
   format.wFormatTag      = LITTLE16(format.wFormatTag);
   format.nChannels       = LITTLE16(format.nChannels);
   format.nSamplesPerSec  = LITTLE32(format.nSamplesPerSec);
   format.nAvgBytesPerSec = LITTLE32(format.nAvgBytesPerSec);
   format.nBlockAlign     = LITTLE16(format.nBlockAlign);
   format.nBitsPerSample  = LITTLE16(format.nBitsPerSample);
   dataptr += riff.format_size;

   memcpy(&data, dataptr, sizeof(data_header));
   data.size = LITTLE32(data.size);

   // Check if it's PCM data.
   if ( format.wFormatTag != 1 )
      {
      MV_SetErrorCode( MV_InvalidWAVFile );
      return( MV_Error );
      }

   if ( format.nChannels != 1 && format.nChannels != 2 )
      {
      MV_SetErrorCode( MV_InvalidWAVFile );
      return( MV_Error );
      }

   if ( ( format.nBitsPerSample != 8 ) &&
      ( format.nBitsPerSample != 16 ) )
      {
      MV_SetErrorCode( MV_InvalidWAVFile );
      return( MV_Error );
      }

   if ( memcmp( data.DATA, "data", 4 ) != 0 )
      {
      MV_SetErrorCode( MV_InvalidWAVFile );
      return( MV_Error );
      }

   // Request a voice from the voice pool
   voice = MV_AllocVoice( priority );
   if ( voice == NULL )
      {
      MV_SetErrorCode( MV_NoVoices );
      return( MV_Error );
      }

   voice->wavetype    = WAV;
   voice->bits        = format.nBitsPerSample;
   voice->channels    = format.nChannels;
   voice->GetSound    = MV_GetNextWAVBlock;

   length = data.size;
   absloopstart = loopstart;
   absloopend   = loopend;
   sizemask = 0;
   if ( voice->bits == 16 )
      {
      loopstart  *= 2;
      loopend    *= 2;
      length     /= 2;
      sizemask    = 1;
      }
   if ( voice->channels == 2 )
      {
      loopstart  *= 2;
      loopend    *= 2;
      length     /= 2;
      sizemask    = (sizemask<<1) | 1;
      }
   data.size  &= ~sizemask;

   loopend    = (int)min( (unsigned int)loopend, data.size );
   absloopend = (int)min( (unsigned int)absloopend, (unsigned int)length );

   voice->Playing     = TRUE;
   voice->Paused      = FALSE;
   voice->DemandFeed  = NULL;
   voice->LoopStart   = NULL;
   voice->LoopCount   = 0;
   voice->position    = 0;
   voice->length      = 0;
   voice->BlockLength = absloopend;
   voice->NextBlock   = dataptr + sizeof(data_header);
   voice->next        = NULL;
   voice->prev        = NULL;
   voice->priority    = priority;
   voice->callbackval = callbackval;
   voice->LoopStart   = voice->NextBlock + loopstart;
   voice->LoopEnd     = voice->NextBlock + loopend;
   voice->LoopSize    = absloopend - absloopstart;

   if ( ( loopstart >= (int)data.size ) || ( loopstart < 0 ) )
      {
      voice->LoopStart = NULL;
      voice->LoopEnd   = NULL;
      voice->BlockLength = length;
      }

   MV_SetVoicePitch( voice, format.nSamplesPerSec, pitchoffset );
   MV_SetVoiceVolume( voice, vol, left, right );
   MV_PlayVoice( voice );

   return( voice->handle );
   }

// Assembly:
cmpl $0x0, 0xba1d(%rip)      # 0x102d0
je 0x4aff
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %r13
cmpl $0x46464952, (%rdi)     # imm = 0x46464952
jne 0x4ae1
cmpl $0x45564157, 0x8(%r13)  # imm = 0x45564157
jne 0x4ae1
cmpl $0x20746d66, 0xc(%r13)  # imm = 0x20746D66
jne 0x4ae1
cmpw $0x1, 0x14(%r13)
jne 0x4ae1
movl %edx, %r15d
movzwl 0x16(%r13), %edx
leal -0x3(%rdx), %eax
cmpw $-0x3, %ax
jbe 0x4ae1
movl %r9d, %ebx
movl %r8d, %ebp
movl %ecx, %r14d
movl 0x10(%r13), %esi
movl 0x18(%r13), %eax
movzwl 0x22(%r13), %r12d
movq 0x14(%r13,%rsi), %rcx
cmpl $0x8, %r12d
je 0x493e
cmpl $0x10, %r12d
jne 0x4ae1
cmpl $0x61746164, %ecx       # imm = 0x61746164
jne 0x4ae1
movq %rsi, 0x10(%rsp)
movq %rdx, 0x18(%rsp)
movq %rcx, 0x20(%rsp)
movl %eax, 0xc(%rsp)
movl 0x70(%rsp), %edi
callq 0x3a02
testq %rax, %rax
je 0x4b0f
movq 0x20(%rsp), %r10
shrq $0x20, %r10
movl $0x3, 0x10(%rax)
movb %r12b, 0x14(%rax)
movq 0x18(%rsp), %r9
movb %r9b, 0x15(%rax)
leaq 0x2e6(%rip), %rcx       # 0x4c79
movq %rcx, 0x18(%rax)
xorl %edx, %edx
movl %r14d, %esi
movl %r15d, %r11d
movl %r10d, %ecx
cmpl $0x10, %r12d
jne 0x49c0
leal (%r15,%r15), %r11d
leal (%r14,%r14), %esi
movl %r10d, %ecx
shrl $0x1f, %ecx
addl %r10d, %ecx
sarl %ecx
movl $0x1, %edx
movl 0x78(%rsp), %r8d
movq 0x10(%rsp), %rdi
addq %r13, %rdi
addq $0x14, %rdi
cmpw $0x2, %r9w
jne 0x49f4
addl %r11d, %r11d
addl %esi, %esi
movl %ecx, %r9d
shrl $0x1f, %r9d
addl %ecx, %r9d
sarl %r9d
leal 0x1(,%rdx,2), %edx
movl %r9d, %ecx
notl %edx
andl %r10d, %edx
cmpl %edx, %esi
cmovael %edx, %esi
movl 0x68(%rsp), %r12d
cmpl %r14d, %ecx
cmovbl %ecx, %r14d
movl 0x60(%rsp), %r13d
xorl %r9d, %r9d
movl %r9d, 0x74(%rax)
movq $0x0, 0x80(%rax)
movl %r9d, 0x40(%rax)
movabsq $0x100000000, %r10      # imm = 0x100000000
movq %r10, 0x6c(%rax)
movl %r9d, 0x60(%rax)
movl %r14d, 0x48(%rax)
leaq 0x8(%rdi), %r9
movq %r9, 0x28(%rax)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movl 0x70(%rsp), %r9d
movl %r9d, 0x7c(%rax)
movl %r8d, 0xa0(%rax)
movslq %r11d, %r8
leaq 0x8(%rdi,%r8), %r9
movq %r9, 0x30(%rax)
movslq %esi, %rsi
addq %rdi, %rsi
addq $0x8, %rsi
movq %rsi, 0x38(%rax)
subl %r15d, %r14d
movl %r14d, 0x44(%rax)
testl %r8d, %r8d
js 0x4a84
cmpl %edx, %r11d
jl 0x4a91
movq %rax, %rdx
addq $0x30, %rdx
movups %xmm0, (%rdx)
movl %ecx, 0x48(%rax)
movl 0xc(%rsp), %r15d
movl %r15d, 0x64(%rax)
movl %ebp, %edi
movq %rax, %r14
callq 0x649c
movl %eax, 0x4c(%r14)
imull %r15d, %eax
xorl %edx, %edx
divl 0x14a4c(%rip)           # 0x19500
movl %eax, 0x68(%r14)
movl %eax, %ecx
shll $0x8, %ecx
subl %eax, %ecx
movl %ecx, 0x50(%r14)
movq %r14, %rdi
movl %ebx, %esi
movl %r13d, %edx
movl %r12d, %ecx
callq 0x3e48
movq %r14, %rdi
callq 0x3619
movl 0x78(%r14), %eax
jmp 0x4af0
movl $0x8, 0xb7e9(%rip)      # 0x102d4
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x2, 0xb7cb(%rip)      # 0x102d4
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
retq
movl $0x4, 0xb7bb(%rip)      # 0x102d4
jmp 0x4aeb