// File: /workspace/github_c_makefile_star2_O0/stetre[P]moonnuklear/src/./nuklear/nuklear.h
// Function: stbrp__skyline_find_best_pos
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:
subq $0x68, %rsp
movq %rdi, 0x50(%rsp)
movl %esi, 0x4c(%rsp)
movl %edx, 0x48(%rsp)
movl $0x40000000, 0x44(%rsp) # imm = 0x40000000
movl $0x40000000, 0x3c(%rsp) # imm = 0x40000000
movq $0x0, 0x18(%rsp)
movl 0x4c(%rsp), %eax
movq 0x50(%rsp), %rcx
addl 0x8(%rcx), %eax
subl $0x1, %eax
movl %eax, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
movq 0x50(%rsp), %rcx
cltd
idivl 0x8(%rcx)
movl 0x4c(%rsp), %eax
subl %edx, %eax
movl %eax, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
movq 0x50(%rsp), %rcx
cltd
idivl 0x8(%rcx)
cmpl $0x0, %edx
jne 0x7deb8
jmp 0x7ded7
leaq 0x338b5(%rip), %rdi     # 0xb1774
leaq 0x334f8(%rip), %rsi     # 0xb13be
movl $0x2bb1, %edx           # imm = 0x2BB1
leaq 0x338b8(%rip), %rcx     # 0xb178a
callq 0x13b40
movl 0x4c(%rsp), %eax
movq 0x50(%rsp), %rcx
cmpl (%rcx), %eax
jg 0x7def2
movl 0x48(%rsp), %eax
movq 0x50(%rsp), %rcx
cmpl 0x4(%rcx), %eax
jle 0x7df10
movq $0x0, 0x60(%rsp)
movl $0x0, 0x5c(%rsp)
movl $0x0, 0x58(%rsp)
jmp 0x7e215
movq 0x50(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x28(%rsp)
movq 0x50(%rsp), %rax
addq $0x18, %rax
movq %rax, 0x30(%rsp)
movq 0x28(%rsp), %rax
movl (%rax), %eax
addl 0x4c(%rsp), %eax
movq 0x50(%rsp), %rcx
cmpl (%rcx), %eax
jg 0x7dfff
movq 0x50(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq 0x28(%rsp), %rax
movl (%rax), %edx
movl 0x4c(%rsp), %ecx
leaq 0x10(%rsp), %r8
callq 0x7e230
movl %eax, 0x14(%rsp)
movq 0x50(%rsp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x7df90
movl 0x14(%rsp), %eax
cmpl 0x3c(%rsp), %eax
jge 0x7df8e
movl 0x14(%rsp), %eax
movl %eax, 0x3c(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x18(%rsp)
jmp 0x7dfde
movl 0x14(%rsp), %eax
addl 0x48(%rsp), %eax
movq 0x50(%rsp), %rcx
cmpl 0x4(%rcx), %eax
jg 0x7dfdc
movl 0x14(%rsp), %eax
cmpl 0x3c(%rsp), %eax
jl 0x7dfc0
movl 0x14(%rsp), %eax
cmpl 0x3c(%rsp), %eax
jne 0x7dfda
movl 0x10(%rsp), %eax
cmpl 0x44(%rsp), %eax
jge 0x7dfda
movl 0x14(%rsp), %eax
movl %eax, 0x3c(%rsp)
movl 0x10(%rsp), %eax
movl %eax, 0x44(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x18(%rsp)
jmp 0x7dfdc
jmp 0x7dfde
movq 0x28(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x30(%rsp)
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x28(%rsp)
jmp 0x7df2c
cmpq $0x0, 0x18(%rsp)
jne 0x7e00e
xorl %eax, %eax
movl %eax, (%rsp)
jmp 0x7e01b
movq 0x18(%rsp), %rax
movq (%rax), %rax
movl (%rax), %eax
movl %eax, (%rsp)
movl (%rsp), %eax
movl %eax, 0x40(%rsp)
movq 0x50(%rsp), %rax
cmpl $0x1, 0x10(%rax)
jne 0x7e1fb
movq 0x50(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x50(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x28(%rsp)
movq 0x50(%rsp), %rax
addq $0x18, %rax
movq %rax, 0x30(%rsp)
movq 0x20(%rsp), %rax
movl (%rax), %eax
cmpl 0x4c(%rsp), %eax
jge 0x7e078
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x20(%rsp)
jmp 0x7e05b
jmp 0x7e07a
cmpq $0x0, 0x20(%rsp)
je 0x7e1f9
movq 0x20(%rsp), %rax
movl (%rax), %eax
subl 0x4c(%rsp), %eax
movl %eax, 0xc(%rsp)
cmpl $0x0, 0xc(%rsp)
jl 0x7e09e
jmp 0x7e0bd
leaq 0x3372f(%rip), %rdi     # 0xb17d4
leaq 0x33312(%rip), %rsi     # 0xb13be
movl $0x2bf1, %edx           # imm = 0x2BF1
leaq 0x336d2(%rip), %rcx     # 0xb178a
callq 0x13b40
jmp 0x7e0bf
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rax
movl (%rax), %eax
cmpl 0xc(%rsp), %eax
jg 0x7e0ee
movq 0x28(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x30(%rsp)
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x28(%rsp)
jmp 0x7e0bf
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rax
movl (%rax), %eax
cmpl 0xc(%rsp), %eax
jle 0x7e10e
movq 0x28(%rsp), %rax
movl (%rax), %eax
cmpl 0xc(%rsp), %eax
jg 0x7e10e
jmp 0x7e12d
leaq 0x336c9(%rip), %rdi     # 0xb17de
leaq 0x332a2(%rip), %rsi     # 0xb13be
movl $0x2bf7, %edx           # imm = 0x2BF7
leaq 0x33662(%rip), %rcx     # 0xb178a
callq 0x13b40
movq 0x50(%rsp), %rdi
movq 0x28(%rsp), %rsi
movl 0xc(%rsp), %edx
movl 0x4c(%rsp), %ecx
leaq 0x4(%rsp), %r8
callq 0x7e230
movl %eax, 0x8(%rsp)
movl 0x8(%rsp), %eax
addl 0x48(%rsp), %eax
movq 0x50(%rsp), %rcx
cmpl 0x4(%rcx), %eax
jg 0x7e1e6
movl 0x8(%rsp), %eax
cmpl 0x3c(%rsp), %eax
jg 0x7e1e4
movl 0x8(%rsp), %eax
cmpl 0x3c(%rsp), %eax
jl 0x7e195
movl 0x4(%rsp), %eax
cmpl 0x44(%rsp), %eax
jl 0x7e195
movl 0x4(%rsp), %eax
cmpl 0x44(%rsp), %eax
jne 0x7e1e2
movl 0xc(%rsp), %eax
cmpl 0x40(%rsp), %eax
jge 0x7e1e2
movl 0xc(%rsp), %eax
movl %eax, 0x40(%rsp)
movl 0x8(%rsp), %eax
cmpl 0x3c(%rsp), %eax
jg 0x7e1a9
jmp 0x7e1c8
leaq 0x33656(%rip), %rdi     # 0xb1806
leaq 0x33207(%rip), %rsi     # 0xb13be
movl $0x2bfd, %edx           # imm = 0x2BFD
leaq 0x335c7(%rip), %rcx     # 0xb178a
callq 0x13b40
movl 0x8(%rsp), %eax
movl %eax, 0x3c(%rsp)
movl 0x4(%rsp), %eax
movl %eax, 0x44(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x18(%rsp)
jmp 0x7e1e4
jmp 0x7e1e6
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x20(%rsp)
jmp 0x7e07a
jmp 0x7e1fb
movq 0x18(%rsp), %rax
movq %rax, 0x60(%rsp)
movl 0x40(%rsp), %eax
movl %eax, 0x58(%rsp)
movl 0x3c(%rsp), %eax
movl %eax, 0x5c(%rsp)
movq 0x58(%rsp), %rax
movq 0x60(%rsp), %rdx
addq $0x68, %rsp
retq
nopw %cs:(%rax,%rax)