// File: Ciaxur[P]yagb-emu/include/imgui/imstb_rectpack.h
// Function: stbrp__skyline_find_best_pos(stbrp_context*, int, int)
static stbrp__findresult stbrp__skyline_find_best_pos(stbrp_context *c, int width, int height)
{
   int best_waste = (1<<30), best_x, best_y = (1 << 30);
   stbrp__findresult fr;
   stbrp_node **prev, *node, *tail, **best = NULL;

   // align to multiple of c->align
   width = (width + c->align - 1);
   width -= width % c->align;
   STBRP_ASSERT(width % c->align == 0);

   // if it can't possibly fit, bail immediately
   if (width > c->width || height > c->height) {
      fr.prev_link = NULL;
      fr.x = fr.y = 0;
      return fr;
   }

   node = c->active_head;
   prev = &c->active_head;
   while (node->x + width <= c->width) {
      int y,waste;
      y = stbrp__skyline_find_min_y(c, node, node->x, width, &waste);
      if (c->heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight) { // actually just want to test BL
         // bottom left
         if (y < best_y) {
            best_y = y;
            best = prev;
         }
      } else {
         // best-fit
         if (y + height <= c->height) {
            // can only use it if it first vertically
            if (y < best_y || (y == best_y && waste < best_waste)) {
               best_y = y;
               best_waste = waste;
               best = prev;
            }
         }
      }
      prev = &node->next;
      node = node->next;
   }

   best_x = (best == NULL) ? 0 : (*best)->x;

   // if doing best-fit (BF), we also have to try aligning right edge to each node position
   //
   // e.g, if fitting
   //
   //     ____________________
   //    |____________________|
   //
   //            into
   //
   //   |                         |
   //   |             ____________|
   //   |____________|
   //
   // then right-aligned reduces waste, but bottom-left BL is always chooses left-aligned
   //
   // This makes BF take about 2x the time

   if (c->heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight) {
      tail = c->active_head;
      node = c->active_head;
      prev = &c->active_head;
      // find first node that's admissible
      while (tail->x < width)
         tail = tail->next;
      while (tail) {
         int xpos = tail->x - width;
         int y,waste;
         STBRP_ASSERT(xpos >= 0);
         // find the left position that matches this
         while (node->next->x <= xpos) {
            prev = &node->next;
            node = node->next;
         }
         STBRP_ASSERT(node->next->x > xpos && node->x <= xpos);
         y = stbrp__skyline_find_min_y(c, node, xpos, width, &waste);
         if (y + height <= c->height) {
            if (y <= best_y) {
               if (y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) {
                  best_x = xpos;
                  STBRP_ASSERT(y <= best_y);
                  best_y = y;
                  best_waste = waste;
                  best = prev;
               }
            }
         }
         tail = tail->next;
      }         
   }

   fr.prev_link = best;
   fr.x = best_x;
   fr.y = best_y;
   return fr;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x18(%rbp)
movl %esi, -0x1c(%rbp)
movl %edx, -0x20(%rbp)
movl $0x40000000, -0x24(%rbp) # imm = 0x40000000
movl $0x40000000, -0x2c(%rbp) # imm = 0x40000000
movq $0x0, -0x50(%rbp)
movl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rcx
addl 0x8(%rcx), %eax
subl $0x1, %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rcx
cltd
idivl 0x8(%rcx)
movl -0x1c(%rbp), %eax
subl %edx, %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rcx
cltd
idivl 0x8(%rcx)
cmpl $0x0, %edx
jne 0x1219bd
jmp 0x1219dc
leaq 0x6997a(%rip), %rdi     # 0x18b33e
leaq 0x6990f(%rip), %rsi     # 0x18b2da
movl $0x168, %edx            # imm = 0x168
leaq 0x6997d(%rip), %rcx     # 0x18b354
callq 0xd180
jmp 0x1219de
movl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl (%rcx), %eax
jg 0x1219f5
movl -0x20(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jle 0x121a10
movq $0x0, -0x8(%rbp)
movl $0x0, -0xc(%rbp)
movl $0x0, -0x10(%rbp)
jmp 0x121cca
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %rax
movzwl (%rax), %eax
addl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl (%rcx), %eax
jg 0x121ada
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x40(%rbp), %rax
movzwl (%rax), %edx
movl -0x1c(%rbp), %ecx
leaq -0x58(%rbp), %r8
callq 0x121ce0
movl %eax, -0x54(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x121a7e
movl -0x54(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x121a7c
movl -0x54(%rbp), %eax
movl %eax, -0x2c(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0x121abd
movl -0x54(%rbp), %eax
addl -0x20(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jg 0x121abb
movl -0x54(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jl 0x121aa5
movl -0x54(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jne 0x121ab9
movl -0x58(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x121ab9
movl -0x54(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x58(%rbp), %eax
movl %eax, -0x24(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0x121abb
jmp 0x121abd
movq -0x40(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
jmp 0x121a28
cmpq $0x0, -0x50(%rbp)
jne 0x121ae8
xorl %eax, %eax
movl %eax, -0x68(%rbp)
jmp 0x121af5
movq -0x50(%rbp), %rax
movq (%rax), %rax
movzwl (%rax), %eax
movl %eax, -0x68(%rbp)
movl -0x68(%rbp), %eax
movl %eax, -0x28(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x1, 0x10(%rax)
jne 0x121cb6
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x38(%rbp)
movq -0x48(%rbp), %rax
movzwl (%rax), %eax
cmpl -0x1c(%rbp), %eax
jge 0x121b47
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x48(%rbp)
jmp 0x121b2d
jmp 0x121b49
cmpq $0x0, -0x48(%rbp)
je 0x121cb4
movq -0x48(%rbp), %rax
movzwl (%rax), %eax
subl -0x1c(%rbp), %eax
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
jl 0x121b69
jmp 0x121b88
leaq 0x6982e(%rip), %rdi     # 0x18b39e
leaq 0x69763(%rip), %rsi     # 0x18b2da
movl $0x1a8, %edx            # imm = 0x1A8
leaq 0x697d1(%rip), %rcx     # 0x18b354
callq 0xd180
jmp 0x121b8a
jmp 0x121b8c
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movzwl (%rax), %eax
cmpl -0x5c(%rbp), %eax
jg 0x121bb6
movq -0x40(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
jmp 0x121b8c
jmp 0x121bb8
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movzwl (%rax), %ecx
xorl %eax, %eax
cmpl -0x5c(%rbp), %ecx
movb %al, -0x69(%rbp)
jle 0x121bdd
movq -0x40(%rbp), %rax
movzwl (%rax), %eax
cmpl -0x5c(%rbp), %eax
setle %al
movb %al, -0x69(%rbp)
movb -0x69(%rbp), %al
testb $0x1, %al
jne 0x121be6
jmp 0x121be8
jmp 0x121c07
leaq 0x697b9(%rip), %rdi     # 0x18b3a8
leaq 0x696e4(%rip), %rsi     # 0x18b2da
movl $0x1ae, %edx            # imm = 0x1AE
leaq 0x69752(%rip), %rcx     # 0x18b354
callq 0xd180
jmp 0x121c09
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rsi
movl -0x5c(%rbp), %edx
movl -0x1c(%rbp), %ecx
leaq -0x64(%rbp), %r8
callq 0x121ce0
movl %eax, -0x60(%rbp)
movl -0x60(%rbp), %eax
addl -0x20(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jg 0x121ca3
movl -0x60(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jg 0x121ca1
movl -0x60(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jl 0x121c5a
movl -0x64(%rbp), %eax
cmpl -0x24(%rbp), %eax
jl 0x121c5a
movl -0x64(%rbp), %eax
cmpl -0x24(%rbp), %eax
jne 0x121c9f
movl -0x5c(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x121c9f
movl -0x5c(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x60(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jg 0x121c6a
jmp 0x121c89
leaq 0x6975f(%rip), %rdi     # 0x18b3d0
leaq 0x69662(%rip), %rsi     # 0x18b2da
movl $0x1b4, %edx            # imm = 0x1B4
leaq 0x696d0(%rip), %rcx     # 0x18b354
callq 0xd180
jmp 0x121c8b
movl -0x60(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x64(%rbp), %eax
movl %eax, -0x24(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0x121ca1
jmp 0x121ca3
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x48(%rbp)
jmp 0x121b49
jmp 0x121cb6
movq -0x50(%rbp), %rax
movq %rax, -0x8(%rbp)
movl -0x28(%rbp), %eax
movl %eax, -0x10(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0xc(%rbp)
movq -0x10(%rbp), %rax
movq -0x8(%rbp), %rdx
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax,%rax)