// File: /workspace/github_c_makefile_star2_O2/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, 0xacc5(%rip)      # 0xf2e0
je 0x4841
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %r12
cmpl $0x46464952, (%rdi)     # imm = 0x46464952
jne 0x4825
cmpl $0x45564157, 0x8(%r12)  # imm = 0x45564157
jne 0x4825
cmpl $0x20746d66, 0xc(%r12)  # imm = 0x20746D66
jne 0x4825
cmpw $0x1, 0x14(%r12)
jne 0x4825
movl %edx, %r15d
movzwl 0x16(%r12), %edx
leal -0x3(%rdx), %eax
cmpw $-0x3, %ax
jbe 0x4825
movl %r9d, %ebx
movl %r8d, %ebp
movl %ecx, %r14d
movl 0x10(%r12), %esi
movl 0x18(%r12), %eax
movzwl 0x22(%r12), %r13d
movq 0x14(%r12,%rsi), %rcx
cmpl $0x8, %r13d
je 0x46ad
cmpl $0x10, %r13d
jne 0x4825
cmpl $0x61746164, %ecx       # imm = 0x61746164
jne 0x4825
movq %rsi, 0x18(%rsp)
movq %rdx, 0x10(%rsp)
movq %rcx, 0x20(%rsp)
movl %eax, 0xc(%rsp)
movl 0x70(%rsp), %edi
callq 0x38e9
testq %rax, %rax
je 0x484f
movq 0x20(%rsp), %rdx
shrq $0x20, %rdx
movl $0x3, 0x10(%rax)
movb %r13b, 0x14(%rax)
movq 0x10(%rsp), %rcx
movb %cl, 0x15(%rax)
leaq 0x282(%rip), %rcx       # 0x4983
movq %rax, %rdi
movq %rcx, 0x18(%rax)
xorl %r11d, %r11d
pushq $0x2
popq %rsi
movl %r14d, %r10d
movl %r15d, %ecx
movl %edx, %eax
cmpl $0x10, %r13d
jne 0x4733
leal (%r15,%r15), %ecx
leal (%r14,%r14), %r10d
movl %edx, %eax
movq %rdx, %r8
cltd
idivl %esi
movq %r8, %rdx
pushq $0x1
popq %r11
movl 0x78(%rsp), %r9d
movq 0x18(%rsp), %r8
addq %r12, %r8
addq $0x14, %r8
cmpw $0x2, 0x10(%rsp)
jne 0x4762
addl %ecx, %ecx
addl %r10d, %r10d
movq %rdx, %r12
cltd
idivl %esi
movq %r12, %rdx
leal 0x1(,%r11,2), %r11d
movl 0xc(%rsp), %esi
notl %r11d
andl %edx, %r11d
cmpl %r11d, %r10d
cmovael %r11d, %r10d
cmpl %r14d, %eax
cmovbl %eax, %r14d
movl 0x68(%rsp), %r13d
andl $0x0, 0x74(%rdi)
movl 0x60(%rsp), %r12d
andq $0x0, 0x80(%rdi)
andl $0x0, 0x40(%rdi)
movabsq $0x100000000, %rdx      # imm = 0x100000000
movq %rdx, 0x6c(%rdi)
andl $0x0, 0x60(%rdi)
movl %r14d, 0x48(%rdi)
leaq 0x8(%r8), %rdx
movq %rdx, 0x28(%rdi)
xorps %xmm0, %xmm0
movups %xmm0, (%rdi)
movl 0x70(%rsp), %edx
movl %edx, 0x7c(%rdi)
movl %r9d, 0xa0(%rdi)
movslq %ecx, %rdx
leaq 0x8(%r8,%rdx), %r9
movq %r9, 0x30(%rdi)
movslq %r10d, %r9
addq %r9, %r8
addq $0x8, %r8
movq %r8, 0x38(%rdi)
subl %r15d, %r14d
movl %r14d, 0x44(%rdi)
testl %edx, %edx
js 0x47f0
cmpl %r11d, %ecx
jl 0x47fd
movq %rdi, %rcx
addq $0x30, %rcx
movups %xmm0, (%rcx)
movl %eax, 0x48(%rdi)
movl %ebp, %edx
movq %rdi, %r14
callq 0x3a73
movq %r14, %rdi
movl %ebx, %esi
movl %r12d, %edx
movl %r13d, %ecx
callq 0x3c9d
movq %r14, %rdi
callq 0x362a
movl 0x78(%r14), %eax
jmp 0x4832
movl $0x8, 0xaab5(%rip)      # 0xf2e4
pushq $-0x1
popq %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x2, 0xaa99(%rip)      # 0xf2e4
pushq $-0x1
popq %rax
retq
movl $0x4, 0xaa8b(%rip)      # 0xf2e4
jmp 0x482f