// File: /workspace/github_c_makefile_star2_O3/jonof[P]jftekwar/src/teksnd.c
// Function: playsound
int
playsound(int sn, int sndx, int sndy, int loop, short type)
{
     int       i,nr=0;
     int      dist=0L,pan=0L,dx,dy;

     if( (toggles[TOGGLE_SOUND] == 0) || !fxstarted || (sn < 0) || (sn >= TOTALSOUNDS) )
          return(-1);

     if( type&(ST_UNIQUE|ST_AMBUPDATE|ST_TOGGLE) ) {
          for( i=0; i<MAXSOUNDS; i++ ) {
               if( dsound[i].handle == NULL_HANDLE ) {
                    continue;
               }
               else if( dsound[i].sndnum == sn ) {
                    if( (type&ST_TOGGLE) != 0 ) {
                         stopsound(i);
                    }
                    return(-1);
               }
          }
     }

     for( i=0; i<MAXSOUNDS; i++ ) {
          if( dsound[i].handle == NULL_HANDLE )
               break;
     }
     if( i == MAXSOUNDS ) {
          // add plevel and multiple occurrence replacement
          return(-1);
     }

     dsound[i].type=type;
     dsound[i].x=sndx; dsound[i].y=sndy;

     sbuf[sn].cache_lock=200;

     if( sbuf[sn].cache_ptr == 0L ) {   // no longer in cache
          allocache(&sbuf[sn].cache_ptr, sbuf[sn].cache_length, &sbuf[sn].cache_lock);
          if( sbuf[sn].cache_ptr == 0L ) {
               sbuf[sn].cache_lock=0x00;
               return(-1);
          }
          klseek(fhsounds, sbuf[sn].offset, SEEK_SET);
          nr=kread(fhsounds, sbuf[sn].cache_ptr, sbuf[sn].cache_length);
          if( nr != sbuf[sn].cache_length ) {
               sbuf[sn].cache_ptr=0L;
               sbuf[sn].cache_lock=0x00;
               return(-1);
          }
     }

     if( (type&ST_IMMEDIATE) ) {
          dist=0;
          pan=0;
     }
     else {
          /*
          dist=klabs(posx[screenpeek]-sndx)+klabs(posy[screenpeek]-sndy);
          if( (type&ST_AMBUPDATE) || (type&ST_VEHUPDATE) ) {
               if( dist < AMBUPDATEDIST ) {
                   vol = (AMBUPDATEDIST<<3)-(dist<<3);
               }
               else {
                   vol=0;
               }
          }
          else {
               if(dist < 1500L)
                    vol = 0x7fff;
               else if(dist > 8500L) {
                    if(sn >= S_MALE_COMEONYOU)
                         vol = 0x0000;
                    else
                         vol = 0x1f00;
               }
               else
                    vol = 39000L-(dist<<2);
          }
          pan=((getangle(posx[screenpeek]-dsoundptr[i]->x,posy[screenpeek]-dsoundptr[i]->y)+(2047-ang[screenpeek]))&2047) >> 6;
          if( (pan < 0) || (pan > 35) )
              pan=13;
          */
          dx = klabs(posx[screenpeek]-sndx);
          dy = klabs(posy[screenpeek]-sndy);
          dist = ksqrt(dx*dx+dy*dy) >> 6;

          pan = 2048 + ang[screenpeek] - getangle(posx[screenpeek]-dsound[i].x,posy[screenpeek]-dsound[i].y);
          pan = (pan & 2047) >> 6;
     }

     dsound[i].handle = FX_PlayRaw3D(sbuf[sn].cache_ptr, sbuf[sn].cache_length, 11025, 0, pan, dist, 1, i );
     if (dsound[i].handle < 0) {
          dsound[i].handle=NULL_HANDLE;
          dsound[i].plevel=0;
          dsound[i].sndnum=-1;
          if( sbuf[sn].users == 0 ) {
               sbuf[sn].cache_lock=0;
          }
          return(-1);
     }
     else {
          sbuf[sn].users++;
         #ifdef SNDDEBUG
          showmessage("SND %03d ADDR %08ld USRS %02d", sn, sbuf[sn].cache_ptr, sbuf[sn].users);
         #endif
          dsound[i].sndnum=sn;
     }

     return(i);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
cmpl $0xcf, %edi
ja 0x1c5a7
leaq 0xb56c9(%rip), %rax     # 0xd1a60
cmpl $0x0, 0xc(%rax)
je 0x1c5a7
testb $0x1, 0x20cec8(%rip)    # 0x229270
je 0x1c5a7
movl %esi, %r12d
movl %edi, %ebp
movzwl %r8w, %r14d
testl $0x168, %r14d           # imm = 0x168
je 0x1c3e4
leaq 0x20cebd(%rip), %rax    # 0x229284
xorl %edi, %edi
cmpl $-0x1, -0x4(%rax)
je 0x1c3d7
cmpl %ebp, (%rax)
je 0x1c503
incq %rdi
addq $0x18, %rax
cmpq $0x10, %rdi
jne 0x1c3c9
leaq 0x20ce95(%rip), %r13    # 0x229280
xorl %ebx, %ebx
cmpl $-0x1, (%r13)
je 0x1c406
incq %rbx
addq $0x18, %r13
cmpq $0x10, %rbx
jne 0x1c3ed
jmp 0x1c5a7
movw %r8w, 0x14(%r13)
movl %r12d, 0xc(%r13)
movl %edx, 0x10(%r13)
movl %ebp, %eax
leaq (%rax,%rax,2), %rax
leaq 0x20cfe0(%rip), %rcx    # 0x229400
leaq (%rcx,%rax,8), %r15
leaq (%rcx,%rax,8), %r8
addq $0x14, %r8
movb $-0x38, (%r8)
cmpq $0x0, -0xc(%r8)
movq %r8, (%rsp)
je 0x1c518
movq %r15, 0x8(%rsp)
xorl %r9d, %r9d
movl $0x0, %r15d
testb $0x1, %r14b
jne 0x1c4c5
leaq 0xc8004(%rip), %r14     # 0xe445e
movswq (%r14), %rax
leaq 0xc806b(%rip), %r15     # 0xe44d0
movl (%r15,%rax,4), %ecx
subl %r12d, %ecx
leaq 0xc809d(%rip), %rsi     # 0xe4510
movl (%rsi,%rax,4), %edi
subl %edx, %edi
imull %ecx, %ecx
imull %edi, %edi
addl %ecx, %edi
callq 0x48a4d
movl %eax, %r12d
sarl $0x6, %r12d
movswq (%r14), %rax
leaq 0xc8019(%rip), %rcx     # 0xe44b0
movl (%r15,%rax,4), %edi
subl 0xc(%r13), %edi
movzwl (%rcx,%rax,2), %r15d
leaq 0xc8065(%rip), %rcx     # 0xe4510
movl (%rcx,%rax,4), %esi
subl 0x10(%r13), %esi
callq 0x48943
movl %r12d, %r9d
subl %eax, %r15d
shrl $0x6, %r15d
andl $0x1f, %r15d
movq 0x8(%rsp), %r14
movq 0x8(%r14), %rdi
movl 0x10(%r14), %esi
movl $0x2b11, %edx           # imm = 0x2B11
xorl %ecx, %ecx
movl %r15d, %r8d
pushq %rbx
pushq $0x1
callq 0x86e4a
addq $0x10, %rsp
movl %eax, (%r13)
testl %eax, %eax
js 0x1c584
incl (%r14)
movl %ebp, 0x4(%r13)
movl %ebx, %r15d
jmp 0x1c5a7
btl $0x8, %r14d
jae 0x1c5a7
callq 0x1c5b9
jmp 0x1c5a7
movl %edx, 0x8(%rsp)
leaq (%rcx,%rax,8), %rdi
addq $0x8, %rdi
movslq 0x10(%r15), %rsi
movq %rdi, 0x10(%rsp)
movq %r8, %rdx
callq 0x33388
cmpq $0x0, 0x8(%r15)
je 0x1c59a
movl 0xb628e(%rip), %edi     # 0xd27d0
movl 0x4(%r15), %esi
xorl %edx, %edx
callq 0x345aa
movl 0xb627d(%rip), %edi     # 0xd27d0
movq 0x8(%r15), %rsi
movl 0x10(%r15), %edx
callq 0x343bc
cmpl 0x10(%r15), %eax
movl 0x8(%rsp), %edx
movq (%rsp), %r8
je 0x1c43f
movq 0x10(%rsp), %rax
movq $0x0, (%rax)
movb $0x0, (%r8)
jmp 0x1c5a1
movq $-0x1, (%r13)
movl $0x0, 0x8(%r13)
cmpl $0x0, (%r14)
jne 0x1c5a1
movq (%rsp), %rax
movb $0x0, (%rax)
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl %r15d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq