// File: /workspace/github_c_makefile_star2_O3/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, %r14
xorl %ebx, %ebx
cmpl $0x2, (%r14)
jne 0x1e0d1
movq 0x10(%r14), %r13
movq %r14, %rdi
movq %r13, %rsi
callq 0x1e10f
testq %rax, %rax
je 0x1e0bf
movq %rax, %r15
movq %r13, %rdi
callq 0x1e1ac
testq %rax, %rax
je 0x1e0f0
movq %rax, %rbp
movl (%r15), %eax
cmpl $0x1, %eax
je 0x1df5b
cmpl $0x2, %eax
jne 0x1dfdb
movl $0x1, %edi
movl $0x60, %esi
callq 0xd380
movq %rax, %rbp
movl $0x1, (%rax)
movq $0x1, 0x10(%rax)
movl $0x1, %edi
movl $0x8, %esi
callq 0xd380
movq %rax, 0x8(%rbp)
movq %r13, (%rax)
movb %r12b, 0x18(%rbp)
movq %rbp, 0x8(%r15)
jmp 0x1dece
cmpb %r12b, 0x18(%r15)
je 0x1dff5
movl $0x1, %edi
movl $0x60, %esi
callq 0xd380
testq %rax, %rax
je 0x1e0bf
movq %rax, %rbp
movl $0x1, (%rax)
movl $0x8, %edi
callq 0xd4c0
movq %rax, 0x8(%rbp)
testq %rax, %rax
je 0x1e0bf
movq $0x1, 0x10(%rbp)
movb %r12b, 0x18(%rbp)
movq %r13, (%rax)
movq 0x10(%r15), %rax
testq %rax, %rax
jle 0x1dece
movq 0x8(%r15), %rcx
xorl %edx, %edx
cmpq %r13, (%rcx,%rdx,8)
je 0x1dfd2
incq %rdx
cmpq %rdx, %rax
jne 0x1dfbf
jmp 0x1dece
movq %rbp, (%rcx,%rdx,8)
jmp 0x1dece
cmpl $0x3, %eax
jne 0x1e0bf
movq %r13, %r14
testq %r13, %r13
jne 0x1dece
jmp 0x1e0bf
leaq 0x28(%rsp), %rdi
movq %r15, %rsi
callq 0x1e22d
movq %rbp, %rdi
callq 0x1dd8e
testq %rax, %rax
je 0x1e0bd
movq %rax, %rbx
movq 0x10(%r14), %rax
movups 0x28(%rax), %xmm0
movups %xmm0, 0x28(%rbx)
movups 0x38(%rax), %xmm0
movups %xmm0, 0x38(%rbx)
movq 0x10(%r15), %r12
cmpb $0x0, 0x24(%rsp)
je 0x1e03e
movq 0x8(%r15), %rdi
movq %r12, %r13
jmp 0x1e05b
movq 0x8(%r15), %rdi
testq %r12, %r12
jle 0x1e058
xorl %r13d, %r13d
cmpq %rax, (%rdi,%r13,8)
je 0x1e05b
incq %r13
cmpq %r13, %r12
jne 0x1e04a
xorl %r13d, %r13d
leaq 0x8(,%r12,8), %rsi
callq 0xd520
movq %rax, 0x8(%r15)
testq %rax, %rax
je 0x1e0bd
leaq 0x1(%r12), %rcx
movq %rcx, 0x10(%r15)
cmpq %r13, %r12
jle 0x1e098
movq 0x8(%r15), %rax
movq -0x8(%rax,%r12,8), %rcx
movq %rcx, (%rax,%r12,8)
decq %r12
cmpq %r13, %r12
jg 0x1e07f
movq 0x8(%r15), %rax
movq %rbx, (%rax,%r13,8)
movups 0x28(%rsp), %xmm0
movups 0x38(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r15, %rdi
callq 0x1e30e
movq %rbx, 0x10(%r14)
jmp 0x1e0bf
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x11c2e(%rip), %rdi     # 0x2fd06
leaq 0x11bea(%rip), %rsi     # 0x2fcc9
leaq 0x11c43(%rip), %rcx     # 0x2fd29
movl $0xb1, %edx
callq 0xd280
leaq 0x1146a(%rip), %rdi     # 0x2f561
leaq 0x11bcb(%rip), %rsi     # 0x2fcc9
leaq 0x11c24(%rip), %rcx     # 0x2fd29
movl $0xb5, %edx
callq 0xd280