// File: donfreiday[P]gb/./imgui/stb_rect_pack.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);

   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 0x469bd
jmp 0x469dc
leaq 0x1e171(%rip), %rdi     # 0x64b35
leaq 0x1e01a(%rip), %rsi     # 0x649e5
movl $0x138, %edx            # imm = 0x138
leaq 0x1e174(%rip), %rcx     # 0x64b4b
callq 0x7160
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 0x46aa6
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x40(%rbp), %rax
movzwl (%rax), %edx
movl -0x1c(%rbp), %ecx
leaq -0x58(%rbp), %r8
callq 0x46ca0
movl %eax, -0x54(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x46a4a
movl -0x54(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x46a48
movl -0x54(%rbp), %eax
movl %eax, -0x2c(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0x46a89
movl -0x54(%rbp), %eax
addl -0x20(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jg 0x46a87
movl -0x54(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jl 0x46a71
movl -0x54(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jne 0x46a85
movl -0x58(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x46a85
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 0x46a87
jmp 0x46a89
movq -0x40(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
jmp 0x469f4
cmpq $0x0, -0x50(%rbp)
jne 0x46ab4
xorl %eax, %eax
movl %eax, -0x68(%rbp)
jmp 0x46ac1
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 0x46c7a
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 0x46b13
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x48(%rbp)
jmp 0x46af9
jmp 0x46b15
cmpq $0x0, -0x48(%rbp)
je 0x46c78
movq -0x48(%rbp), %rax
movzwl (%rax), %eax
subl -0x1c(%rbp), %eax
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
jl 0x46b35
jmp 0x46b54
leaq 0x1e059(%rip), %rdi     # 0x64b95
leaq 0x1dea2(%rip), %rsi     # 0x649e5
movl $0x171, %edx            # imm = 0x171
leaq 0x1dffc(%rip), %rcx     # 0x64b4b
callq 0x7160
jmp 0x46b56
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movzwl (%rax), %eax
cmpl -0x5c(%rbp), %eax
jg 0x46b80
movq -0x40(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
jmp 0x46b56
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movzwl (%rax), %ecx
xorl %eax, %eax
cmpl -0x5c(%rbp), %ecx
movb %al, -0x69(%rbp)
jle 0x46ba5
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 0x46bae
jmp 0x46bb0
jmp 0x46bcf
leaq 0x1dfe8(%rip), %rdi     # 0x64b9f
leaq 0x1de27(%rip), %rsi     # 0x649e5
movl $0x177, %edx            # imm = 0x177
leaq 0x1df81(%rip), %rcx     # 0x64b4b
callq 0x7160
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rsi
movl -0x5c(%rbp), %edx
movl -0x1c(%rbp), %ecx
leaq -0x64(%rbp), %r8
callq 0x46ca0
movl %eax, -0x60(%rbp)
movl -0x60(%rbp), %eax
addl -0x20(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jge 0x46c67
movl -0x60(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jg 0x46c65
movl -0x60(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jl 0x46c20
movl -0x64(%rbp), %eax
cmpl -0x24(%rbp), %eax
jl 0x46c20
movl -0x64(%rbp), %eax
cmpl -0x24(%rbp), %eax
jne 0x46c63
movl -0x5c(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x46c63
movl -0x5c(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x60(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jg 0x46c30
jmp 0x46c4f
leaq 0x1df90(%rip), %rdi     # 0x64bc7
leaq 0x1dda7(%rip), %rsi     # 0x649e5
movl $0x17d, %edx            # imm = 0x17D
leaq 0x1df01(%rip), %rcx     # 0x64b4b
callq 0x7160
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 0x46c65
jmp 0x46c67
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x48(%rbp)
jmp 0x46b15
jmp 0x46c7a
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)