// File: /workspace/github_c_makefile_star2_O2/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 $0x28, %rsp
pushq $-0x1
popq %r12
cmpl $0xcf, %edi
ja 0x1ad5e
leaq 0xacf11(%rip), %rax     # 0xc7a60
cmpl $0x0, 0xc(%rax)
je 0x1ad5e
testb $0x1, 0x204710(%rip)    # 0x21f270
je 0x1ad5e
movl %esi, %r15d
movl %edi, %ebp
movzwl %r8w, %esi
testw $0x168, %r8w            # imm = 0x168
je 0x1ab9d
leaq 0x204706(%rip), %rax    # 0x21f284
xorl %edi, %edi
cmpq $0x10, %rdi
je 0x1ab9d
cmpl $-0x1, -0x4(%rax)
je 0x1ab94
cmpl %ebp, (%rax)
je 0x1acc0
incq %rdi
addq $0x18, %rax
jmp 0x1ab80
leaq 0x2046dc(%rip), %r13    # 0x21f280
xorl %ebx, %ebx
cmpq $0x10, %rbx
je 0x1ad5e
cmpl $-0x1, (%r13)
je 0x1abc0
incq %rbx
addq $0x18, %r13
jmp 0x1aba6
movw %r8w, 0x14(%r13)
movl %r15d, 0xc(%r13)
movl %edx, 0x10(%r13)
movl %ebp, %eax
imulq $0x18, %rax, %rax
leaq 0x204826(%rip), %rcx    # 0x21f400
leaq (%rcx,%rax), %r14
leaq (%rax,%rcx), %r12
addq $0x14, %r12
movb $-0x38, (%r12)
cmpq $0x0, -0xc(%r12)
je 0x1acd4
movq %r14, 0x8(%rsp)
movq %r12, 0x20(%rsp)
xorl %r9d, %r9d
movl $0x0, %r12d
testb $0x1, %sil
jne 0x1ac84
leaq 0xbf847(%rip), %r12     # 0xda45e
movswq (%r12), %rax
leaq 0xbf8ad(%rip), %rcx     # 0xda4d0
movl (%rcx,%rax,4), %ecx
subl %r15d, %ecx
leaq 0xbf8e0(%rip), %r15     # 0xda510
movl (%r15,%rax,4), %edi
subl %edx, %edi
imull %ecx, %ecx
imull %edi, %edi
addl %ecx, %edi
callq 0x43f6c
movl %eax, %r14d
sarl $0x6, %r14d
movswq (%r12), %rax
leaq 0xbf85a(%rip), %rcx     # 0xda4b0
leaq 0xbf873(%rip), %rdx     # 0xda4d0
movl (%rdx,%rax,4), %edi
subl 0xc(%r13), %edi
movzwl (%rcx,%rax,2), %r12d
movl (%r15,%rax,4), %esi
subl 0x10(%r13), %esi
callq 0x43e64
movl %r14d, %r9d
subl %eax, %r12d
shrl $0x6, %r12d
andl $0x1f, %r12d
movq 0x8(%rsp), %r14
movq 0x8(%r14), %rdi
movl 0x10(%r14), %esi
movl $0x2b11, %edx           # imm = 0x2B11
xorl %ecx, %ecx
movl %r12d, %r8d
pushq %rbx
pushq $0x1
callq 0x79e56
popq %rcx
popq %rdx
movl %eax, (%r13)
testl %eax, %eax
js 0x1ad42
incl (%r14)
movl %ebp, 0x4(%r13)
movl %ebx, %r12d
jmp 0x1ad5e
btl $0x8, %esi
jae 0x1ad5e
callq 0x1ad70
jmp 0x1ad5e
movl %esi, 0x14(%rsp)
movl %edx, 0x8(%rsp)
leaq (%rax,%rcx), %rdi
addq $0x8, %rdi
movslq 0x10(%r14), %rsi
movq %rdi, 0x18(%rsp)
movq %r12, %rdx
callq 0x2f65d
cmpq $0x0, 0x8(%r14)
je 0x1ad3b
movl 0xadace(%rip), %edi     # 0xc87d0
movl 0x4(%r14), %esi
xorl %edx, %edx
callq 0x306cb
movl 0xadabd(%rip), %edi     # 0xc87d0
movq 0x8(%r14), %rsi
movl 0x10(%r14), %edx
callq 0x304e7
cmpl 0x10(%r14), %eax
movl 0x8(%rsp), %edx
movl 0x14(%rsp), %esi
je 0x1abf7
movq 0x18(%rsp), %rax
andq $0x0, (%rax)
movb $0x0, (%r12)
jmp 0x1ad5a
orq $-0x1, (%r13)
andl $0x0, 0x8(%r13)
cmpl $0x0, (%r14)
jne 0x1ad5a
movq 0x20(%rsp), %rax
movb $0x0, (%rax)
pushq $-0x1
popq %r12
movl %r12d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq