// File: /workspace/github_c_makefile_star2_O3/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 0x1e10f
movq %rax, 0x30(%rsp)
testq %rax, %rax
je 0x1ef8b
movq %rax, %rbx
movl (%rax), %eax
cmpl $0x1, %eax
je 0x1ef60
cmpl $0x2, %eax
je 0x1f03e
cmpl $0x3, %eax
jne 0x1ef8b
movq 0x18(%rbx), %rax
testq %rax, %rax
jle 0x1ef8b
movq 0x8(%rbx), %rcx
xorl %r14d, %r14d
cmpq %r15, (%rcx,%r14,8)
je 0x1f00b
incq %r14
cmpq %r14, %rax
jne 0x1ef4c
jmp 0x1ef8b
leaq 0x38(%rsp), %rdi
movq %rbx, %rsi
callq 0x1e22d
movq 0x10(%rbx), %rax
testq %rax, %rax
jle 0x1ef8b
movq 0x8(%rbx), %rcx
xorl %r12d, %r12d
cmpq %r15, (%rcx,%r12,8)
je 0x1ef92
incq %r12
cmpq %r12, %rax
jne 0x1ef7d
xorl %eax, %eax
jmp 0x1f040
leaq 0x28(%rsp), %rdi
callq 0x1ddce
movq 0x10(%rbx), %r15
leaq -0x1(%r15), %rax
cmpq %rax, %r12
jge 0x1efc3
movq 0x8(%rbx), %rcx
movq 0x8(%rcx,%r12,8), %rdx
movq %rdx, (%rcx,%r12,8)
leaq 0x1(%r12), %rcx
movq %rcx, %r12
cmpq %rax, %rcx
jl 0x1efa9
movq %rax, 0x10(%rbx)
movups 0x38(%rsp), %xmm0
movups 0x48(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x1e30e
cmpq %r14, %rbx
jne 0x1eff2
cmpq $0x0, 0x10(%rbx)
je 0x1f0ba
cmpq $0x2, %r15
je 0x1f04c
cmpq $0x1, %r15
jne 0x1f03e
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1eeff
jmp 0x1f040
leaq 0x28(%rsp), %rdi
callq 0x1ddce
movq 0x18(%rbx), %rax
decq %rax
cmpq %rax, %r14
jge 0x1f03a
movq 0x8(%rbx), %rcx
movq 0x8(%rcx,%r14,8), %rdx
movq %rdx, (%rcx,%r14,8)
leaq 0x1(%r14), %rcx
movq %rcx, %r14
cmpq %rax, %rcx
jl 0x1f021
movq %rax, 0x18(%rbx)
movb $0x1, %al
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1e10f
testq %rax, %rax
je 0x1f03e
movl (%rax), %ecx
cmpl $0x2, %ecx
je 0x1f08b
cmpl $0x1, %ecx
jne 0x1ef8b
movq 0x10(%rax), %rcx
testq %rcx, %rcx
jle 0x1f0ae
movq 0x8(%rax), %rax
xorl %edx, %edx
cmpq %rbx, (%rax,%rdx,8)
je 0x1f098
incq %rdx
cmpq %rdx, %rcx
jne 0x1f07b
jmp 0x1f0ae
movq 0x8(%rbx), %rcx
movq (%rcx), %rcx
movq %rcx, 0x8(%rax)
jmp 0x1f0a3
movq 0x8(%rbx), %rcx
movq (%rcx), %rcx
movq %rcx, (%rax,%rdx,8)
movq 0x8(%rbx), %rax
movq $0x0, (%rax)
leaq 0x30(%rsp), %rdi
callq 0x1ddce
jmp 0x1f03e
leaq 0x10cf6(%rip), %rdi     # 0x2fdb7
leaq 0x10c01(%rip), %rsi     # 0x2fcc9
leaq 0x10d1c(%rip), %rcx     # 0x2fdeb
movl $0x356, %edx            # imm = 0x356
callq 0xd280