// File: /workspace/github_c_makefile_star2_O1/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
cmpl $0x2, (%rdi)
jne 0x1dda3
movl %edx, %r15d
movl %esi, %r12d
movq %rdi, %rbx
movq 0x10(%rdi), %r13
movq %r13, %rsi
callq 0x1dde1
testq %rax, %rax
je 0x1dca0
movq %rax, %r14
movq %r13, %rdi
callq 0x1deda
testq %rax, %rax
je 0x1ddc2
movq %rax, %rbp
movl (%r14), %ecx
movb $0x1, %al
cmpl $0x3, %ecx
je 0x1dc7e
movl %r15d, 0x24(%rsp)
cmpl $0x2, %ecx
je 0x1dc32
cmpl $0x1, %ecx
jne 0x1dc9c
cmpb %r12b, 0x18(%r14)
jne 0x1dcb5
leaq 0x28(%rsp), %rdi
movq %r14, %rsi
callq 0x1dfb8
movq %rbp, %rdi
callq 0x1da25
testq %rax, %rax
je 0x1dd6f
movq %rax, %r15
movq 0x10(%rbx), %rax
movups 0x28(%rax), %xmm0
movups %xmm0, 0x28(%r15)
movups 0x38(%rax), %xmm0
movups %xmm0, 0x38(%r15)
movq 0x10(%r14), %r12
cmpb $0x0, 0x24(%rsp)
jne 0x1dd32
testq %r12, %r12
jle 0x1dc2a
movq 0x8(%r14), %rcx
xorl %edx, %edx
cmpq %rax, (%rcx,%rdx,8)
je 0x1dd2f
incq %rdx
cmpq %rdx, %r12
jne 0x1dc18
xorl %r12d, %r12d
jmp 0x1dd32
movl $0x1, %edi
movl $0x60, %esi
callq 0xd380
movq %rax, %r15
movl $0x1, (%rax)
movq $0x1, 0x10(%rax)
movl $0x1, %edi
movl $0x8, %esi
callq 0xd380
movq %rax, 0x8(%r15)
movq %r13, (%rax)
movb %r12b, 0x18(%r15)
movq %r15, 0x8(%r14)
movzbl %r12b, %esi
movzbl 0x24(%rsp), %edx
movq %rbx, %rdi
jmp 0x1dc92
testq %r13, %r13
je 0x1dd1a
movzbl %r12b, %esi
movzbl %r15b, %edx
movq %r13, %rdi
callq 0x1db50
movq %rax, %r15
xorl %eax, %eax
testb %al, %al
je 0x1dca3
xorl %r15d, %r15d
movq %r15, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %edi
movl $0x60, %esi
callq 0xd380
testq %rax, %rax
je 0x1dd1c
movq %rax, %r15
movl $0x1, (%rax)
movl $0x8, %edi
callq 0xd4c0
movq %rax, 0x8(%r15)
testq %rax, %rax
je 0x1dd1c
movq $0x1, 0x10(%r15)
movb %r12b, 0x18(%r15)
movq %r13, (%rax)
movq 0x10(%r14), %rax
testq %rax, %rax
jle 0x1dc70
movq 0x8(%r14), %rcx
xorl %edx, %edx
cmpq %r13, (%rcx,%rdx,8)
je 0x1dd26
incq %rdx
cmpq %rdx, %rax
jne 0x1dd07
jmp 0x1dc70
jmp 0x1dc9c
xorl %eax, %eax
xorl %r15d, %r15d
jmp 0x1dc9c
movq %r15, (%rcx,%rdx,8)
jmp 0x1dc70
movq %rdx, %r12
movq 0x8(%r14), %rdi
movq 0x10(%r14), %r13
leaq 0x8(,%r13,8), %rsi
callq 0xd520
movq %rax, 0x8(%r14)
testq %rax, %rax
je 0x1dd6f
leaq 0x1(%r13), %rax
movq %rax, 0x10(%r14)
cmpq %r12, %r13
jle 0x1dd77
movq 0x8(%r14), %rax
movq -0x8(%rax,%r13,8), %rcx
movq %rcx, (%rax,%r13,8)
decq %r13
jmp 0x1dd58
xorl %r15d, %r15d
jmp 0x1dc9a
movq 0x8(%r14), %rax
movq %r15, (%rax,%r12,8)
movups 0x28(%rsp), %xmm0
movups 0x38(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x1e0a5
movq %r15, 0x10(%rbx)
jmp 0x1dc9a
leaq 0x12ea8(%rip), %rdi     # 0x30c52
leaq 0x12e64(%rip), %rsi     # 0x30c15
leaq 0x12ebd(%rip), %rcx     # 0x30c75
movl $0xb1, %edx
callq 0xd280
leaq 0x1279b(%rip), %rdi     # 0x30564
leaq 0x12e45(%rip), %rsi     # 0x30c15
leaq 0x12e9e(%rip), %rcx     # 0x30c75
movl $0xb5, %edx
callq 0xd280