// File: /workspace/github_c_makefile_star2_O2/justy989[P]ce/ce_layout.c
// Function: ce_layout_delete
bool ce_layout_delete(CeLayout_t* root, CeLayout_t* node){
     CeLayout_t* parent = ce_layout_find_parent(root, node);
     if(!parent) return false;

     switch(parent->type){
     default:
          return false;
     case CE_LAYOUT_TYPE_LIST:
     {
          CeRect_t parent_rect = ce_layout_rect(parent);

          // find index of matching node
          int64_t index = -1;
          for(int64_t i = 0; i < parent->list.layout_count; i++){
               if(parent->list.layouts[i] == node){
                    index = i;
                    break;
               }
          }

          if(index == -1) return false;

          ce_layout_free(&node);

          // remove element, keeping element order
          int64_t new_count = parent->list.layout_count - 1;
          for(int64_t i = index; i < new_count; i++){
               parent->list.layouts[i] = parent->list.layouts[i + 1];
          }
          parent->list.layout_count = new_count;
          ce_layout_distribute_rect(parent, parent_rect);
          assert(!(parent->list.layout_count == 0 && parent == root));

          if(new_count == 0){
              return ce_layout_delete(root, parent);
          }else if(new_count == 1){
               // reduce back to a single layout
               CeLayout_t* grandparent = ce_layout_find_parent(root, parent);
               if(!grandparent) return true;

               switch(grandparent->type){
               default:
                    return false;
               case CE_LAYOUT_TYPE_LIST:
                    // replace the parent at that element
                    for(int64_t i = 0; i < grandparent->list.layout_count; i++){
                         if(grandparent->list.layouts[i] == parent){
                              grandparent->list.layouts[i] = parent->list.layouts[0];
                              parent->list.layouts[0] = NULL;
                              break;
                         }
                    }
                    break;
               case CE_LAYOUT_TYPE_TAB:
                    grandparent->tab.root = parent->list.layouts[0];
                    parent->list.layouts[0] = NULL;
                    break;
               case CE_LAYOUT_TYPE_TAB_LIST:
                    return false;
               }

               ce_layout_free(&parent);
          }
     } break;
     case CE_LAYOUT_TYPE_TAB:
          break;
     case CE_LAYOUT_TYPE_TAB_LIST:
     {
          // find index of matching node
          int64_t index = -1;
          for(int64_t i = 0; i < parent->tab_list.tab_count; i++){
               if(parent->tab_list.tabs[i] == node){
                    index = i;
                    break;
               }
          }

          if(index == -1) return false;

          ce_layout_free(&node);

          // remove element, keeping element order
          int64_t new_count = parent->tab_list.tab_count - 1;
          for(int64_t i = index; i < new_count; i++){
               parent->tab_list.tabs[i] = parent->tab_list.tabs[i + 1];
          }
          parent->tab_list.tab_count = new_count;
     } break;
     }

     return true;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r15
movq %rdi, %r14
movq %rsi, 0x28(%rsp)
callq 0x1c2a3
movq %rax, 0x30(%rsp)
testq %rax, %rax
je 0x1d2e5
movq %rax, %rbx
movl (%rax), %eax
cmpl $0x1, %eax
je 0x1d1c0
cmpl $0x2, %eax
je 0x1d2a4
cmpl $0x3, %eax
jne 0x1d2e5
movq 0x18(%rbx), %rax
xorl %r14d, %r14d
testq %rax, %rax
cmovleq %r14, %rax
cmpq %r14, %rax
je 0x1d2e5
movq 0x8(%rbx), %rcx
cmpq %r15, (%rcx,%r14,8)
je 0x1d26e
incq %r14
jmp 0x1d1a4
leaq 0x38(%rsp), %rdi
movq %rbx, %rsi
callq 0x1c3d4
movq 0x10(%rbx), %rax
xorl %r12d, %r12d
testq %rax, %rax
cmovleq %r12, %rax
cmpq %r12, %rax
je 0x1d2e5
movq 0x8(%rbx), %rcx
cmpq %r15, (%rcx,%r12,8)
je 0x1d1f3
incq %r12
jmp 0x1d1db
leaq 0x28(%rsp), %rdi
callq 0x1bf8f
movq 0x10(%rbx), %r15
leaq -0x1(%r15), %rax
cmpq %rax, %r12
movq %rax, %rcx
cmovgq %r12, %rcx
cmpq %r12, %rcx
je 0x1d226
movq 0x8(%rbx), %rdx
movq 0x8(%rdx,%r12,8), %rsi
movq %rsi, (%rdx,%r12,8)
incq %r12
jmp 0x1d20f
movq %rax, 0x10(%rbx)
movups 0x38(%rsp), %xmm0
movups 0x48(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x1c4ba
cmpq %r14, %rbx
jne 0x1d255
cmpq $0x0, 0x10(%rbx)
je 0x1d31f
cmpq $0x2, %r15
je 0x1d2a8
cmpq $0x1, %r15
jne 0x1d2a4
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1d151
jmp 0x1d2e7
leaq 0x28(%rsp), %rdi
callq 0x1bf8f
movq 0x18(%rbx), %rax
decq %rax
cmpq %rax, %r14
movq %rax, %rcx
cmovgq %r14, %rcx
cmpq %r14, %rcx
je 0x1d2a0
movq 0x8(%rbx), %rdx
movq 0x8(%rdx,%r14,8), %rsi
movq %rsi, (%rdx,%r14,8)
incq %r14
jmp 0x1d289
movq %rax, 0x18(%rbx)
movb $0x1, %al
jmp 0x1d2e7
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1c2a3
testq %rax, %rax
je 0x1d2a4
movl (%rax), %ecx
cmpl $0x2, %ecx
je 0x1d2f3
cmpl $0x1, %ecx
jne 0x1d2e5
movq 0x10(%rax), %rdx
xorl %ecx, %ecx
testq %rdx, %rdx
cmovleq %rcx, %rdx
cmpq %rcx, %rdx
je 0x1d313
movq 0x8(%rax), %rsi
cmpq %rbx, (%rsi,%rcx,8)
je 0x1d300
incq %rcx
jmp 0x1d2d1
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq 0x8(%rbx), %rcx
movq (%rcx), %rcx
movq %rcx, 0x8(%rax)
jmp 0x1d30b
movq 0x8(%rbx), %rax
movq (%rax), %rax
movq %rax, (%rsi,%rcx,8)
movq 0x8(%rbx), %rax
andq $0x0, (%rax)
leaq 0x30(%rsp), %rdi
callq 0x1bf8f
jmp 0x1d2a4
leaq 0xfa29(%rip), %rdi      # 0x2cd4f
leaq 0xf934(%rip), %rsi      # 0x2cc61
leaq 0xfa4f(%rip), %rcx      # 0x2cd83
movl $0x356, %edx            # imm = 0x356
callq 0xd280