// File: /workspace/github_c_makefile_star2_O3/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, 0x18(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, 0x8(%rsp)
movl $0x100, %ebx            # imm = 0x100
leaq 0x74f9c(%rip), %r12     # 0x8fec2
leaq 0x565eb3(%rip), %r14    # 0x580de0
leaq 0x3d99ac(%rip), %r15    # 0x3f48e0
leaq 0x34e995(%rip), %rbp    # 0x3698d0
movq %r12, %rdi
callq 0x2d112
movswq (%r14), %rcx
movslq %eax, %r13
imulq %rcx, %r13
movq %r13, %rax
sarq $0x10, %rax
leaq (%rax,%rax,4), %rax
movl 0x4(%r15,%rax,8), %ecx
cmpl 0x8(%r15,%rax,8), %ecx
jge 0x1af3b
leaq (%r15,%rax,8), %rax
cmpw $0x0, 0x22(%rax)
jne 0x1af3b
testb $0x2, 0xe(%rax)
jne 0x1af3b
movzwl (%rax), %edi
movzwl 0x2(%rax), %r8d
addw %di, %r8w
decl %r8d
cmpw %di, %r8w
jle 0x1b03e
movq %rbx, 0x20(%rsp)
shrq $0x10, %r13
movswl %di, %eax
movl %eax, (%rsp)
movswl %r8w, %eax
movl %eax, 0x4(%rsp)
xorl %ecx, %ecx
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movl $0x80000000, %r9d       # imm = 0x80000000
movl $0x80000000, %r11d      # imm = 0x80000000
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
xorl %eax, %eax
movswq %di, %rsi
shlq $0x5, %rsi
movl (%rbp,%rsi), %ebx
movl 0x4(%rbp,%rsi), %esi
addl %ebx, %eax
addl %esi, %ecx
cmpl %edx, %ebx
cmovll %ebx, %edx
cmpl %r11d, %ebx
cmovgl %ebx, %r11d
cmpl %r10d, %esi
cmovll %esi, %r10d
cmpl %r9d, %esi
cmovgl %esi, %r9d
incl %edi
cmpw %r8w, %di
jle 0x1afc1
subl %edx, %r11d
cmpl $0x101, %r11d           # imm = 0x101
movq 0x20(%rsp), %rbx
jl 0x1b03e
subl %r10d, %r9d
cmpl $0x101, %r9d            # imm = 0x101
jl 0x1b03e
movl 0x4(%rsp), %esi
subl (%rsp), %esi
incl %esi
cltd
idivl %esi
movl %eax, %edi
movl %ecx, %eax
cltd
idivl %esi
movswl %r13w, %edx
movl %edi, 0x4(%rsp)
movl %eax, (%rsp)
movl %eax, %esi
callq 0x488a7
cmpl $0x1, %eax
je 0x1b04e
leal -0x1(%rbx), %eax
cmpl $0x1, %ebx
movl %eax, %ebx
ja 0x1af3b
jmp 0x1b06c
movq 0x8(%rsp), %rax
movl 0x4(%rsp), %ecx
movl %ecx, (%rax)
movq 0x10(%rsp), %rax
movl (%rsp), %ecx
movl %ecx, (%rax)
movq 0x18(%rsp), %rax
movw %r13w, (%rax)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq