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

     cnt = 256;
     while (cnt > 0)
     {
          do
          {
            k = mulscale(krand_intercept("PREP 585"),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)
                    {
                         j=jsinsertsprite(k,0);
                         if( j != -1 ) {
                              fillsprite(j,dax,day,sector[k].floorz,0,-8,0,
                                         12,16,16,0,0,0,0,
                                         0,0,0,0,k,0,0,0,-1);
                              sprite[j].z-=((tilesizy[sprite[j].picnum]*sprite[j].yrepeat)<<1);
                              switch( picnum ) {
                              }
                              sprite[j].picnum=picnum;
                         }
                         return;
                    }
               }
          }
          cnt--;
     }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edi, 0xc(%rsp)
movl $0x100, 0x8(%rsp)       # imm = 0x100
leaq 0x68879(%rip), %r14     # 0x81e68
leaq 0x55d7ea(%rip), %r13    # 0x576de0
leaq 0x3d12e3(%rip), %rbx    # 0x3ea8e0
movl 0x8(%rsp), %eax
subl $0x1, %eax
jb 0x197cd
movl %eax, 0x8(%rsp)
movq %r14, %rdi
callq 0x2945b
movswq (%r13), %rcx
movslq %eax, %r12
imulq %rcx, %r12
movq %r12, %rax
sarq $0x10, %rax
imulq $0x28, %rax, %rbp
movl 0x4(%rbx,%rbp), %eax
cmpl 0x8(%rbx,%rbp), %eax
jge 0x1960e
addq %rbx, %rbp
cmpw $0x0, 0x22(%rbp)
jne 0x1960e
testb $0x2, 0xe(%rbp)
jne 0x1960e
movzwl (%rbp), %edi
movzwl 0x2(%rbp), %ecx
addw %di, %cx
decl %ecx
cmpw %di, %cx
jle 0x195fd
shrq $0x10, %r12
movswl %di, %eax
movl %eax, 0x10(%rsp)
movswl %cx, %eax
movl %eax, 0x14(%rsp)
movl $0x0, 0x4(%rsp)
movl $0x80000000, %r11d      # imm = 0x80000000
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
movl $0x80000000, %r10d      # imm = 0x80000000
xorl %eax, %eax
cmpw %cx, %di
jg 0x196d0
movswq %di, %rsi
shlq $0x5, %rsi
leaq 0x346230(%rip), %r8     # 0x35f8d0
movl (%r8,%rsi), %r15d
movl 0x4(%r8,%rsi), %esi
addl %r15d, %eax
addl %esi, 0x4(%rsp)
cmpl %edx, %r15d
cmovll %r15d, %edx
cmpl %r11d, %r15d
cmovgl %r15d, %r11d
cmpl %r9d, %esi
cmovll %esi, %r9d
cmpl %r10d, %esi
cmovgl %esi, %r10d
incl %edi
jmp 0x1968c
subl %edx, %r11d
cmpl $0x101, %r11d           # imm = 0x101
jl 0x195fd
subl %r9d, %r10d
cmpl $0x101, %r10d           # imm = 0x101
jl 0x195fd
movl 0x14(%rsp), %esi
subl 0x10(%rsp), %esi
incl %esi
cltd
idivl %esi
movl %eax, %edi
movl 0x4(%rsp), %eax
cltd
idivl %esi
movl %eax, %r15d
movswl %r12w, %edx
movl %edi, 0x4(%rsp)
movl %eax, %esi
callq 0x43dc8
cmpl $0x1, %eax
jne 0x195fd
movl 0x4(%rsp), %r13d
movswl %r12w, %edi
xorl %esi, %esi
callq 0x1cf0c
cmpw $-0x1, %ax
je 0x197cd
movswq %ax, %rax
imulq $0x2c, %rax, %rbx
leaq 0x40a3e6(%rip), %r14    # 0x423b30
movl %r13d, (%r14,%rbx)
movl %r15d, 0x4(%r14,%rbx)
movl 0x8(%rbp), %ecx
movl %ecx, 0x8(%r14,%rbx)
andl $0x0, 0xc(%r14,%rbx)
movw $0xf8, 0x10(%r14,%rbx)
movb $0xc, 0x12(%r14,%rbx)
movl $0x1010, 0x14(%r14,%rbx) # imm = 0x1010
andq $0x0, 0x1c(%r14,%rbx)
andq $0x0, 0x22(%r14,%rbx)
orw $-0x1, 0x2a(%r14,%rbx)
leaq (%r14,%rbx), %rdi
imulq $0xc, %rax, %rsi
addq 0xad81e(%rip), %rsi     # 0xc6fb8
pushq $0x3
popq %rdx
callq 0x56a36
movswq 0xe(%r14,%rbx), %rax
leaq 0x58eeb1(%rip), %rcx    # 0x5a8660
movswl (%rcx,%rax,2), %eax
movzbl 0x15(%r14,%rbx), %ecx
imull %eax, %ecx
addl %ecx, %ecx
subl %ecx, 0x8(%r14,%rbx)
movl 0xc(%rsp), %eax
movw %ax, 0xe(%r14,%rbx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq