// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movw %r8w, %ax
movl %edi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
movw %ax, -0x16(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
leaq 0xf34d8(%rip), %rax     # 0x11a000
cmpl $0x0, 0xc(%rax)
je 0x26b46
cmpl $0x0, 0x24b6eb(%rip)    # 0x272220
je 0x26b46
cmpl $0x0, -0x8(%rbp)
jl 0x26b46
cmpl $0xd0, -0x8(%rbp)
jl 0x26b52
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x27008
movswl -0x16(%rbp), %eax
andl $0x168, %eax            # imm = 0x168
cmpl $0x0, %eax
je 0x26bd3
movl $0x0, -0x1c(%rbp)
cmpl $0x10, -0x1c(%rbp)
jge 0x26bd1
movslq -0x1c(%rbp), %rcx
leaq 0x24b6b8(%rip), %rax    # 0x272230
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
cmpl $-0x1, (%rax)
jne 0x26b86
jmp 0x26bc6
movslq -0x1c(%rbp), %rcx
leaq 0x24b69f(%rip), %rax    # 0x272230
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
cmpl -0x8(%rbp), %eax
jne 0x26bc2
movswl -0x16(%rbp), %eax
andl $0x100, %eax            # imm = 0x100
cmpl $0x0, %eax
je 0x26bb6
movl -0x1c(%rbp), %edi
callq 0x27020
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x27008
jmp 0x26bc4
jmp 0x26bc6
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x26b67
jmp 0x26bd3
movl $0x0, -0x1c(%rbp)
cmpl $0x10, -0x1c(%rbp)
jge 0x26c06
movslq -0x1c(%rbp), %rcx
leaq 0x24b645(%rip), %rax    # 0x272230
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
cmpl $-0x1, (%rax)
jne 0x26bf9
jmp 0x26c06
jmp 0x26bfb
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x26bda
cmpl $0x10, -0x1c(%rbp)
jne 0x26c18
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x27008
movw -0x16(%rbp), %cx
movslq -0x1c(%rbp), %rdx
leaq 0x24b609(%rip), %rax    # 0x272230
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movw %cx, 0x14(%rax)
movl -0xc(%rbp), %ecx
movslq -0x1c(%rbp), %rdx
leaq 0x24b5f0(%rip), %rax    # 0x272230
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0xc(%rax)
movl -0x10(%rbp), %ecx
movslq -0x1c(%rbp), %rdx
leaq 0x24b5d8(%rip), %rax    # 0x272230
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x10(%rax)
movslq -0x8(%rbp), %rcx
leaq 0x24b743(%rip), %rax    # 0x2723b0
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movb $-0x38, 0x14(%rax)
movslq -0x8(%rbp), %rcx
leaq 0x24b72d(%rip), %rax    # 0x2723b0
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
jne 0x26dca
movslq -0x8(%rbp), %rax
leaq 0x24b710(%rip), %rdi    # 0x2723b0
imulq $0x18, %rax, %rax
addq %rax, %rdi
addq $0x8, %rdi
movslq -0x8(%rbp), %rcx
leaq 0x24b6fa(%rip), %rax    # 0x2723b0
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movslq 0x10(%rax), %rsi
movslq -0x8(%rbp), %rax
leaq 0x24b6e4(%rip), %rdx    # 0x2723b0
imulq $0x18, %rax, %rax
addq %rax, %rdx
addq $0x14, %rdx
callq 0x4bc80
movslq -0x8(%rbp), %rcx
leaq 0x24b6c9(%rip), %rax    # 0x2723b0
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
jne 0x26d17
movslq -0x8(%rbp), %rcx
leaq 0x24b6b0(%rip), %rax    # 0x2723b0
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x14(%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x27008
movl 0xf4053(%rip), %edi     # 0x11ad70
movslq -0x8(%rbp), %rcx
leaq 0x24b688(%rip), %rax    # 0x2723b0
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %esi
xorl %edx, %edx
callq 0x4db20
movl 0xf4031(%rip), %edi     # 0x11ad70
movslq -0x8(%rbp), %rcx
leaq 0x24b666(%rip), %rax    # 0x2723b0
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rsi
movslq -0x8(%rbp), %rcx
leaq 0x24b650(%rip), %rax    # 0x2723b0
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %edx
callq 0x4d840
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
movslq -0x8(%rbp), %rdx
leaq 0x24b630(%rip), %rcx    # 0x2723b0
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x10(%rcx), %eax
je 0x26dc8
movslq -0x8(%rbp), %rcx
leaq 0x24b619(%rip), %rax    # 0x2723b0
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq $0x0, 0x8(%rax)
movslq -0x8(%rbp), %rcx
leaq 0x24b5ff(%rip), %rax    # 0x2723b0
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x14(%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x27008
jmp 0x26dca
movswl -0x16(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x26de9
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
jmp 0x26ed3
leaq 0x1069ee(%rip), %rax    # 0x12d7de
movswq (%rax), %rcx
leaq 0x106a55(%rip), %rax    # 0x12d850
movl (%rax,%rcx,4), %edi
subl -0xc(%rbp), %edi
callq 0x271a0
movl %eax, -0x2c(%rbp)
leaq 0x1069ce(%rip), %rax    # 0x12d7de
movswq (%rax), %rcx
leaq 0x106a75(%rip), %rax    # 0x12d890
movl (%rax,%rcx,4), %edi
subl -0x10(%rbp), %edi
callq 0x271a0
movl %eax, -0x30(%rbp)
movl -0x2c(%rbp), %edi
imull -0x2c(%rbp), %edi
movl -0x30(%rbp), %eax
imull -0x30(%rbp), %eax
addl %eax, %edi
callq 0x69ab0
sarl $0x6, %eax
movl %eax, -0x24(%rbp)
leaq 0x106993(%rip), %rax    # 0x12d7de
movswq (%rax), %rcx
leaq 0x1069da(%rip), %rax    # 0x12d830
movswl (%rax,%rcx,2), %eax
addl $0x800, %eax            # imm = 0x800
movl %eax, -0x34(%rbp)
leaq 0x106975(%rip), %rax    # 0x12d7de
movswq (%rax), %rcx
leaq 0x1069dc(%rip), %rax    # 0x12d850
movl (%rax,%rcx,4), %edi
movslq -0x1c(%rbp), %rcx
leaq 0x24b3ae(%rip), %rax    # 0x272230
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
subl 0xc(%rax), %edi
leaq 0x10694b(%rip), %rax    # 0x12d7de
movswq (%rax), %rcx
leaq 0x1069f2(%rip), %rax    # 0x12d890
movl (%rax,%rcx,4), %esi
movslq -0x1c(%rbp), %rcx
leaq 0x24b384(%rip), %rax    # 0x272230
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
subl 0x10(%rax), %esi
callq 0x69950
movl %eax, %ecx
movl -0x34(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
andl $0x7ff, %eax            # imm = 0x7FF
sarl $0x6, %eax
movl %eax, -0x28(%rbp)
movslq -0x8(%rbp), %rcx
leaq 0x24b4d2(%rip), %rax    # 0x2723b0
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rdi
movslq -0x8(%rbp), %rcx
leaq 0x24b4bc(%rip), %rax    # 0x2723b0
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %esi
movl -0x28(%rbp), %r8d
movl -0x24(%rbp), %r9d
movl -0x1c(%rbp), %eax
movl $0x2b11, %edx           # imm = 0x2B11
xorl %ecx, %ecx
movl $0x1, (%rsp)
movl %eax, 0x8(%rsp)
callq 0xcca40
movl %eax, %ecx
movslq -0x1c(%rbp), %rdx
leaq 0x24b303(%rip), %rax    # 0x272230
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movslq -0x1c(%rbp), %rcx
leaq 0x24b2ef(%rip), %rax    # 0x272230
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, (%rax)
jge 0x26fd1
movslq -0x1c(%rbp), %rcx
leaq 0x24b2d4(%rip), %rax    # 0x272230
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movslq -0x1c(%rbp), %rcx
leaq 0x24b2bc(%rip), %rax    # 0x272230
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x8(%rax)
movslq -0x1c(%rbp), %rcx
leaq 0x24b2a3(%rip), %rax    # 0x272230
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl $0xffffffff, 0x4(%rax)  # imm = 0xFFFFFFFF
movslq -0x8(%rbp), %rcx
leaq 0x24b40a(%rip), %rax    # 0x2723b0
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, (%rax)
jne 0x26fc8
movslq -0x8(%rbp), %rcx
leaq 0x24b3f3(%rip), %rax    # 0x2723b0
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x14(%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x27008
movslq -0x8(%rbp), %rcx
leaq 0x24b3d4(%rip), %rax    # 0x2723b0
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movl -0x8(%rbp), %ecx
movslq -0x1c(%rbp), %rdx
leaq 0x24b238(%rip), %rax    # 0x272230
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)