// File: /workspace/github_c_makefile_star2_O2/justy989[P]ce/ce_layout.c
// Function: ce_layout_split
CeLayout_t* ce_layout_split(CeLayout_t* layout, bool vertical, bool always_add_last){
     assert(layout->type == CE_LAYOUT_TYPE_TAB);
     CeLayout_t* parent_of_current = ce_layout_find_parent(layout, layout->tab.current);
     if(parent_of_current){
          CeBuffer_t* buffer = ce_layout_find_buffer(layout->tab.current);
          assert(buffer);
          switch(parent_of_current->type){
          default:
               break;
          case CE_LAYOUT_TYPE_LIST:
               if(parent_of_current->list.vertical == vertical){
                    CeRect_t parent_layout_rect = ce_layout_rect(parent_of_current);
                    CeLayout_t* new_layout = ce_layout_view_init(buffer);
                    if(!new_layout) return NULL;
                    new_layout->view.scroll = layout->tab.current->view.scroll;
                    new_layout->view.cursor = layout->tab.current->view.cursor;

                    int64_t split_layout_index = 0;
                    if(always_add_last){
                        split_layout_index = parent_of_current->list.layout_count;
                    }else{
                        for(int64_t i = 0; i < parent_of_current->list.layout_count; i++){
                             if(parent_of_current->list.layouts[i] == layout->tab.current){
                                  split_layout_index = i;
                                  break;
                             }
                        }
                    }

                    int64_t new_layout_count = parent_of_current->list.layout_count + 1;
                    parent_of_current->list.layouts = realloc(parent_of_current->list.layouts,
                                                              new_layout_count * sizeof(*parent_of_current->list.layouts));
                    if(parent_of_current->list.layouts){
                         parent_of_current->list.layout_count = new_layout_count;
                    }else{
                         return NULL;
                    }

                    // shift down the rest of the layouts first
                    for(int64_t i = parent_of_current->list.layout_count - 1; i > split_layout_index; i--){
                         parent_of_current->list.layouts[i] = parent_of_current->list.layouts[i - 1];
                    }
                    parent_of_current->list.layouts[split_layout_index] = new_layout;
                    ce_layout_distribute_rect(parent_of_current, parent_layout_rect);
                    layout->tab.current = new_layout;
                    return new_layout;
               }else{
                    CeLayout_t* new_list_layout = calloc(1, sizeof(*new_list_layout));
                    if(!new_list_layout) return NULL;
                    new_list_layout->type = CE_LAYOUT_TYPE_LIST;
                    new_list_layout->list.layouts = malloc(sizeof(*new_list_layout->list.layouts));
                    if(!new_list_layout->list.layouts) return NULL;
                    new_list_layout->list.layout_count = 1;
                    new_list_layout->list.vertical = vertical;
                    new_list_layout->list.layouts[0] = layout->tab.current;

                    // point parent at new list
                    for(int64_t i = 0; i < parent_of_current->list.layout_count; i++){
                         if(parent_of_current->list.layouts[i] == layout->tab.current){
                              parent_of_current->list.layouts[i] = new_list_layout;
                              break;
                         }
                    }

                    return ce_layout_split(layout, vertical, always_add_last);
               }
               break;
          case CE_LAYOUT_TYPE_TAB:
          {
               CeLayout_t* list_layout = calloc(1, sizeof(*list_layout));
               list_layout->type = CE_LAYOUT_TYPE_LIST;
               list_layout->list.layout_count = 1;
               list_layout->list.layouts = calloc(list_layout->list.layout_count, sizeof(*list_layout->list.layouts));
               list_layout->list.layouts[0] = layout->tab.current;
               list_layout->list.vertical = vertical;

               parent_of_current->tab.root = list_layout;

               return ce_layout_split(layout, vertical, always_add_last);
          } break;
          case CE_LAYOUT_TYPE_TAB_LIST:
               if(layout->tab_list.current) return ce_layout_split(layout->tab_list.current,
                                                                   vertical, always_add_last);
               break;
          }
     }

     return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %edx, 0x24(%rsp)
movl %esi, %r12d
movq %rdi, %rbx
pushq $0x1
popq %r13
cmpl $0x2, (%rbx)
jne 0x1c265
movq 0x10(%rbx), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1c2a3
testq %rax, %rax
je 0x1c22b
movq %rax, %r15
movq %r14, %rdi
callq 0x1c349
testq %rax, %rax
je 0x1c284
movq %rax, %rbp
movl (%r15), %eax
cmpl $0x1, %eax
je 0x1c0ef
cmpl $0x2, %eax
jne 0x1c16d
movq %r13, %rdi
pushq $0x60
popq %rsi
callq 0xd380
movq %rax, %rbp
movl $0x1, (%rax)
movq $0x1, 0x10(%rax)
movq %r13, %rdi
pushq $0x8
popq %rsi
callq 0xd380
movq %rax, 0x8(%rbp)
movq %r14, (%rax)
movb %r12b, 0x18(%rbp)
movq %rbp, 0x8(%r15)
jmp 0x1c06b
cmpb %r12b, 0x18(%r15)
je 0x1c187
movq %r13, %rdi
pushq $0x60
popq %rsi
callq 0xd380
testq %rax, %rax
je 0x1c22b
movq %rax, %rbp
movl $0x1, (%rax)
pushq $0x8
popq %rdi
callq 0xd4c0
movq %rax, 0x8(%rbp)
testq %rax, %rax
je 0x1c22b
movq $0x1, 0x10(%rbp)
movb %r12b, 0x18(%rbp)
movq %r14, (%rax)
movq 0x10(%r15), %rax
testq %rax, %rax
movl $0x0, %ecx
cmovleq %rcx, %rax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x1c06b
movq 0x8(%r15), %rdx
cmpq %r14, (%rdx,%rcx,8)
je 0x1c164
incq %rcx
jmp 0x1c14c
movq %rbp, (%rdx,%rcx,8)
jmp 0x1c06b
cmpl $0x3, %eax
jne 0x1c22b
movq %r14, %rbx
testq %r14, %r14
jne 0x1c06b
jmp 0x1c22b
leaq 0x28(%rsp), %rdi
movq %r15, %rsi
callq 0x1c3d4
movq %rbp, %rdi
callq 0x1bf55
testq %rax, %rax
je 0x1c22b
movq %rax, %r13
movq 0x10(%rbx), %rax
movups 0x28(%rax), %xmm0
movups %xmm0, 0x28(%r13)
movups 0x38(%rax), %xmm0
movups %xmm0, 0x38(%r13)
movq 0x10(%r15), %r14
movq %r14, %r12
cmpb $0x0, 0x24(%rsp)
jne 0x1c1f2
testq %r14, %r14
movl $0x0, %ecx
cmovgq %r14, %rcx
xorl %r12d, %r12d
cmpq %r12, %rcx
je 0x1c1ef
movq 0x8(%r15), %rdx
cmpq %rax, (%rdx,%r12,8)
je 0x1c1f2
incq %r12
jmp 0x1c1db
xorl %r12d, %r12d
movq 0x8(%r15), %rdi
leaq 0x8(,%r14,8), %rsi
callq 0xd520
movq %rax, 0x8(%r15)
testq %rax, %rax
je 0x1c22b
leaq 0x1(%r14), %rax
movq %rax, 0x10(%r15)
movq 0x8(%r15), %rax
cmpq %r12, %r14
jle 0x1c240
movq -0x8(%rax,%r14,8), %rcx
movq %rcx, (%rax,%r14,8)
decq %r14
jmp 0x1c214
xorl %r13d, %r13d
movq %r13, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, (%rax,%r12,8)
movups 0x28(%rsp), %xmm0
movups 0x38(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r15, %rdi
callq 0x1c4ba
movq %r13, 0x10(%rbx)
jmp 0x1c22e
leaq 0x10a32(%rip), %rdi     # 0x2cc9e
leaq 0x109ee(%rip), %rsi     # 0x2cc61
leaq 0x10a47(%rip), %rcx     # 0x2ccc1
movl $0xb1, %edx
callq 0xd280
leaq 0x102d6(%rip), %rdi     # 0x2c561
leaq 0x109cf(%rip), %rsi     # 0x2cc61
leaq 0x10a28(%rip), %rcx     # 0x2ccc1
movl $0xb5, %edx
callq 0xd280