// File: Ciaxur[P]yagb-emu/include/imgui/imstb_rectpack.h
// Function: stbrp__skyline_pack_rectangle(stbrp_context*, int, int)
static stbrp__findresult stbrp__skyline_pack_rectangle(stbrp_context *context, int width, int height)
{
   // find best position according to heuristic
   stbrp__findresult res = stbrp__skyline_find_best_pos(context, width, height);
   stbrp_node *node, *cur;

   // bail if:
   //    1. it failed
   //    2. the best node doesn't fit (we don't always check this)
   //    3. we're out of memory
   if (res.prev_link == NULL || res.y + height > context->height || context->free_head == NULL) {
      res.prev_link = NULL;
      return res;
   }

   // on success, create new node
   node = context->free_head;
   node->x = (stbrp_coord) res.x;
   node->y = (stbrp_coord) (res.y + height);

   context->free_head = node->next;

   // insert the new node into the right starting point, and
   // let 'cur' point to the remaining nodes needing to be
   // stiched back in

   cur = *res.prev_link;
   if (cur->x < res.x) {
      // preserve the existing one, so start testing with the next one
      stbrp_node *next = cur->next;
      cur->next = node;
      cur = next;
   } else {
      *res.prev_link = node;
   }

   // from here, traverse cur and free the nodes, until we get to one
   // that shouldn't be freed
   while (cur->next && cur->next->x <= res.x + width) {
      stbrp_node *next = cur->next;
      // move the current node to the free list
      cur->next = context->free_head;
      context->free_head = cur;
      cur = next;
   }

   // stitch the list back in
   node->next = cur;

   if (cur->x < res.x + width)
      cur->x = (stbrp_coord) (res.x + width);

#ifdef _DEBUG
   cur = context->active_head;
   while (cur->x < context->width) {
      STBRP_ASSERT(cur->x < cur->next->x);
      cur = cur->next;
   }
   STBRP_ASSERT(cur->next == NULL);

   {
      int count=0;
      cur = context->active_head;
      while (cur) {
         cur = cur->next;
         ++count;
      }
      cur = context->free_head;
      while (cur) {
         cur = cur->next;
         ++count;
      }
      STBRP_ASSERT(count == context->num_nodes+2);
   }
#endif

   return res;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x18(%rbp)
movl %esi, -0x1c(%rbp)
movl %edx, -0x20(%rbp)
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %esi
movl -0x20(%rbp), %edx
callq 0x121960
movq %rax, -0x10(%rbp)
movq %rdx, -0x8(%rbp)
cmpq $0x0, -0x8(%rbp)
je 0x1217ca
movl -0xc(%rbp), %eax
addl -0x20(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jg 0x1217ca
movq -0x18(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x1217d7
movq $0x0, -0x8(%rbp)
jmp 0x1218ea
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x28(%rbp)
movl -0x10(%rbp), %eax
movw %ax, %cx
movq -0x28(%rbp), %rax
movw %cx, (%rax)
movl -0xc(%rbp), %eax
addl -0x20(%rbp), %eax
movw %ax, %cx
movq -0x28(%rbp), %rax
movw %cx, 0x2(%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movzwl (%rax), %eax
cmpl -0x10(%rbp), %eax
jge 0x12184a
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x38(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x121855
movq -0x28(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, (%rax)
jmp 0x121857
movq -0x30(%rbp), %rcx
xorl %eax, %eax
cmpq $0x0, 0x8(%rcx)
movb %al, -0x41(%rbp)
je 0x121880
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movzwl (%rax), %eax
movl -0x10(%rbp), %ecx
addl -0x1c(%rbp), %ecx
cmpl %ecx, %eax
setle %al
movb %al, -0x41(%rbp)
movb -0x41(%rbp), %al
testb $0x1, %al
jne 0x121889
jmp 0x1218bb
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x40(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x121857
movq -0x30(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
movzwl (%rax), %eax
movl -0x10(%rbp), %ecx
addl -0x1c(%rbp), %ecx
cmpl %ecx, %eax
jge 0x1218e8
movl -0x10(%rbp), %eax
addl -0x1c(%rbp), %eax
movw %ax, %cx
movq -0x30(%rbp), %rax
movw %cx, (%rax)
jmp 0x1218ea
movq -0x10(%rbp), %rax
movq -0x8(%rbp), %rdx
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax,%rax)