// File: /workspace/github_c_makefile_star2_O1/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, %ebx       # imm = 0xFFFFFFFF
cmpl $0xcf, %edi
ja 0x1c446
leaq 0xb2849(%rip), %rax     # 0xcea60
cmpl $0x0, 0xc(%rax)
je 0x1c446
testb $0x1, 0x20a028(%rip)    # 0x226250
je 0x1c446
movl %esi, %r15d
movl %edi, %ebp
movzwl %r8w, %r14d
testl $0x168, %r14d           # imm = 0x168
je 0x1c264
leaq 0x20a01d(%rip), %rax    # 0x226264
xorl %edi, %edi
cmpl $-0x1, -0x4(%rax)
je 0x1c257
cmpl %ebp, (%rax)
je 0x1c3a2
incq %rdi
addq $0x18, %rax
cmpq $0x10, %rdi
jne 0x1c249
leaq 0x209ff5(%rip), %rax    # 0x226260
xorl %r13d, %r13d
cmpl $-0x1, (%rax)
je 0x1c286
incq %r13
addq $0x18, %rax
cmpq $0x10, %r13
jne 0x1c26e
movl $0x10, %r13d
cmpl $0x10, %r13d
je 0x1c446
movl %r13d, %eax
leaq (%rax,%rax,2), %r12
leaq 0x209fc2(%rip), %rsi    # 0x226260
movw %r8w, 0x14(%rsi,%r12,8)
movl %r15d, 0xc(%rsi,%r12,8)
movl %edx, 0x10(%rsi,%r12,8)
movl %ebp, %eax
leaq (%rax,%rax,2), %rax
leaq 0x20a125(%rip), %rcx    # 0x2263e0
leaq (%rcx,%rax,8), %rbx
leaq (%rcx,%rax,8), %r8
addq $0x14, %r8
movb $-0x38, (%r8)
cmpq $0x0, -0xc(%r8)
movq %r8, 0x8(%rsp)
je 0x1c3b7
movq %rbx, 0x10(%rsp)
leaq (%rsi,%r12,8), %rax
movq %rax, (%rsp)
xorl %r9d, %r9d
movl $0x0, %r12d
testb $0x1, %r14b
jne 0x1c364
leaq 0xc5160(%rip), %r14     # 0xe145e
movswq (%r14), %rax
leaq 0xc51c7(%rip), %r12     # 0xe14d0
movl (%r12,%rax,4), %ecx
subl %r15d, %ecx
leaq 0xc51f9(%rip), %r15     # 0xe1510
movl (%r15,%rax,4), %edi
subl %edx, %edi
imull %ecx, %ecx
imull %edi, %edi
addl %ecx, %edi
callq 0x46ae1
movl %eax, %ebx
sarl $0x6, %ebx
movswq (%r14), %rax
leaq 0xc5176(%rip), %rcx     # 0xe14b0
movl (%r12,%rax,4), %edi
movq (%rsp), %rdx
subl 0xc(%rdx), %edi
movzwl (%rcx,%rax,2), %r12d
movl (%r15,%rax,4), %esi
subl 0x10(%rdx), %esi
callq 0x469df
movl %ebx, %r9d
subl %eax, %r12d
shrl $0x6, %r12d
andl $0x1f, %r12d
movq 0x10(%rsp), %rbx
movq 0x8(%rbx), %rdi
movl 0x10(%rbx), %esi
movl $0x2b11, %edx           # imm = 0x2B11
xorl %ecx, %ecx
movl %r12d, %r8d
pushq %r13
pushq $0x1
callq 0x842b5
addq $0x10, %rsp
movq (%rsp), %rcx
movl %eax, (%rcx)
testl %eax, %eax
js 0x1c426
incl (%rbx)
movl %ebp, 0x4(%rcx)
movl %r13d, %ebx
jmp 0x1c446
btl $0x8, %r14d
jae 0x1c446
callq 0x1c457
jmp 0x1c446
movl %edx, (%rsp)
leaq (%rcx,%rax,8), %rdi
addq $0x8, %rdi
movslq 0x10(%rbx), %rsi
movq %rdi, 0x10(%rsp)
movq %r8, %rdx
callq 0x31fbc
cmpq $0x0, 0x8(%rbx)
je 0x1c439
movl 0xb33f0(%rip), %edi     # 0xcf7d0
movl 0x4(%rbx), %esi
xorl %edx, %edx
callq 0x33214
movl 0xb33e0(%rip), %edi     # 0xcf7d0
movq 0x8(%rbx), %rsi
movl 0x10(%rbx), %edx
callq 0x3302e
cmpl 0x10(%rbx), %eax
movl (%rsp), %edx
movq 0x8(%rsp), %r8
leaq 0x209e52(%rip), %rsi    # 0x226260
je 0x1c2db
movq 0x10(%rsp), %rax
movq $0x0, (%rax)
movb $0x0, (%r8)
jmp 0x1c441
movq $-0x1, (%rcx)
movl $0x0, 0x8(%rcx)
cmpl $0x0, (%rbx)
jne 0x1c441
movq 0x8(%rsp), %rax
movb $0x0, (%rax)
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq