// File: /workspace/github_c_makefile_star2_O2/jonof[P]jftekwar/src/tekprep.c
// Function: findrandomspot
void
findrandomspot(int *x, int *y, short *sectnum)
{
     short startwall, endwall, s;
     int dax, day, minx, maxx, miny, maxy, cnt, k;

     cnt = 256;
     while (cnt > 0)
     {
          do
          {
            k = mulscale(krand_intercept("PREP 521"),numsectors,16);
          } while ((sector[k].ceilingz >= sector[k].floorz) || (sector[k].lotag != 0) || ((sector[k].floorstat&2) != 0));

          startwall = sector[k].wallptr;
          endwall = startwall+sector[k].wallnum-1;
          if (endwall > startwall)
          {
               dax = 0L;
               day = 0L;
               minx = 0x7fffffff; maxx = 0x80000000;
               miny = 0x7fffffff; maxy = 0x80000000;

               for(s=startwall;s<=endwall;s++)
               {
                    dax += wall[s].x;
                    day += wall[s].y;
                    if (wall[s].x < minx) minx = wall[s].x;
                    if (wall[s].x > maxx) maxx = wall[s].x;
                    if (wall[s].y < miny) miny = wall[s].y;
                    if (wall[s].y > maxy) maxy = wall[s].y;
               }
               if ((maxx-minx > 256) && (maxy-miny > 256))
               {
                    dax /= (endwall-startwall+1);
                    day /= (endwall-startwall+1);
                    if (inside(dax,day,k) == 1)
                    {
                         *x = dax;
                         *y = day;
                         *sectnum = k;
                         return;
                    }
               }
          }
          cnt--;
     }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, 0x10(%rsp)
movl $0x100, 0x8(%rsp)       # imm = 0x100
leaq 0x6864e(%rip), %r12     # 0x81e56
leaq 0x55d5d1(%rip), %r14    # 0x576de0
leaq 0x3d10ca(%rip), %r15    # 0x3ea8e0
leaq 0x3460b3(%rip), %rbp    # 0x35f8d0
movl 0x8(%rsp), %eax
subl $0x1, %eax
jb 0x1994b
movl %eax, 0x8(%rsp)
movq %r12, %rdi
callq 0x2945b
movswq (%r14), %rcx
movslq %eax, %r13
imulq %rcx, %r13
movq %r13, %rax
sarq $0x10, %rax
imulq $0x28, %rax, %rax
movl 0x4(%r15,%rax), %ecx
cmpl 0x8(%r15,%rax), %ecx
jge 0x1982e
addq %r15, %rax
cmpw $0x0, 0x22(%rax)
jne 0x1982e
testb $0x2, 0xe(%rax)
jne 0x1982e
movzwl (%rax), %edi
movzwl 0x2(%rax), %r8d
addw %di, %r8w
decl %r8d
cmpw %di, %r8w
jle 0x1981d
shrq $0x10, %r13
movswl %di, %eax
movl %eax, 0xc(%rsp)
movswl %r8w, %eax
movl %eax, 0x4(%rsp)
movl $0x80000000, %r9d       # imm = 0x80000000
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
xorl %eax, %eax
xorl %ecx, %ecx
movl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
movl $0x80000000, %edx       # imm = 0x80000000
cmpw %r8w, %di
jg 0x198e3
movswq %di, %rsi
shlq $0x5, %rsi
movl (%rbp,%rsi), %ebx
movl 0x4(%rbp,%rsi), %esi
addl %ebx, %eax
addl %esi, %ecx
cmpl %r10d, %ebx
cmovll %ebx, %r10d
cmpl %r9d, %ebx
cmovgl %ebx, %r9d
cmpl %r11d, %esi
cmovll %esi, %r11d
cmpl %edx, %esi
cmovgl %esi, %edx
incl %edi
jmp 0x198ab
subl %r10d, %r9d
cmpl $0x101, %r9d            # imm = 0x101
jl 0x1981d
subl %r11d, %edx
cmpl $0x101, %edx            # imm = 0x101
jl 0x1981d
movl 0x4(%rsp), %esi
subl 0xc(%rsp), %esi
incl %esi
cltd
idivl %esi
movl %eax, %ebx
movl %ecx, %eax
cltd
idivl %esi
movswl %r13w, %edx
movl %ebx, %edi
movl %eax, 0x4(%rsp)
movl %eax, %esi
callq 0x43dc8
cmpl $0x1, %eax
jne 0x1981d
movq 0x10(%rsp), %rax
movl %ebx, (%rax)
movq 0x18(%rsp), %rax
movl 0x4(%rsp), %ecx
movl %ecx, (%rax)
movq 0x20(%rsp), %rax
movw %r13w, (%rax)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq