// File: /workspace/github_c_makefile_star2_O1/justy989[P]ce/ce_draw_term.c
// Function: draw_layout
static void _draw_layout(CeLayout_t* layout, CeVim_t* vim, CeVimVisualData_t* visual, CeMacros_t* macros,
                         CeBuffer_t* input_buffer, CeColorDefs_t* color_defs, int64_t tab_width, CeLineNumber_t line_number,
                         CeVisualLineDisplayType_t visual_line_display_type, CeLayout_t* current, CeSyntaxDef_t* syntax_defs,
                         int64_t terminal_width, bool highlight_search, int ui_fg_color, int ui_bg_color,
                         CeRune_t show_line_extends_passed_view_as){
     switch(layout->type){
     default:
          break;
     case CE_LAYOUT_TYPE_VIEW:
     {
          CeDrawColorList_t draw_color_list = {};
          CeAppBufferData_t* buffer_data = layout->view.buffer->app_data;

          if(buffer_data->syntax_function){
               CeRangeList_t range_list = {};
               // add to the highlight range list only if this is the current view
               if(layout == current){
                    switch(vim->mode){
                    default:
                         break;
                    case CE_VIM_MODE_VISUAL:
                    {
                         CeRange_t range = {visual->point, layout->view.cursor};
                         ce_range_sort(&range);
                         ce_range_list_insert(&range_list, range.start, range.end);
                    } break;
                    case CE_VIM_MODE_VISUAL_LINE:
                    {
                         CeRange_t range = {visual->point, layout->view.cursor};
                         ce_range_sort(&range);
                         range.start.x = 0;
                         range.end.x = ce_utf8_last_index(layout->view.buffer->lines[range.end.y]) + 1;
                         ce_range_list_insert(&range_list, range.start, range.end);
                    } break;
                    case CE_VIM_MODE_VISUAL_BLOCK:
                    {
                         CeRange_t range = {visual->point, layout->view.cursor};
                         if(range.start.x > range.end.x){
                              int64_t tmp = range.start.x;
                              range.start.x = range.end.x;
                              range.end.x = tmp;
                         }
                         if(range.start.y > range.end.y){
                              int64_t tmp = range.start.y;
                              range.start.y = range.end.y;
                              range.end.y = tmp;
                         }
                         for(int64_t i = range.start.y; i <= range.end.y; i++){
                              CePoint_t start = {range.start.x, i};
                              CePoint_t end = {range.end.x, i};
                              ce_range_list_insert(&range_list, start, end);
                         }
                    } break;
                    }
               }

               // TODO: doesn't work for multiline searches
               if(highlight_search){
                    const char* pattern = NULL;

                    if((strcmp(input_buffer->name, "Search") == 0 ||
                        strcmp(input_buffer->name, "Reverse Search") == 0 ||
                        strcmp(input_buffer->name, "Regex Search") == 0 ||
                        strcmp(input_buffer->name, "Regex Reverse Search") == 0) &&
                       input_buffer->line_count && strlen(input_buffer->lines[0])){
                         pattern = input_buffer->lines[0];
                    }else{
                         const CeVimYank_t* yank = vim->yanks + ce_vim_register_index('/');
                         if(yank->text) pattern = yank->text;
                    }

                    if(pattern){
                         int64_t pattern_len = ce_utf8_strlen(pattern);
                         int64_t min = layout->view.scroll.y;
                         int64_t max = min + (layout->view.rect.bottom - layout->view.rect.top);
                         int64_t clamp_max = (layout->view.buffer->line_count - 1);
                         CE_CLAMP(min, 0, clamp_max);
                         CE_CLAMP(max, 0, clamp_max);

                         if(vim->search_mode == CE_VIM_SEARCH_MODE_FORWARD ||
                            vim->search_mode == CE_VIM_SEARCH_MODE_BACKWARD){
                              for(int64_t i = min; i <= max; i++){
                                   char* match = NULL;
                                   char* itr = layout->view.buffer->lines[i];
                                   while((match = strstr(itr, pattern))){
                                        CePoint_t start = {ce_utf8_strlen_between(layout->view.buffer->lines[i], match) - 1, i};
                                        CePoint_t end = {start.x + (pattern_len - 1), i};
                                        ce_range_list_insert(&range_list, start, end);
                                        itr = match + pattern_len;
                                   }
                              }
                         }else if(vim->search_mode == CE_VIM_SEARCH_MODE_REGEX_FORWARD ||
                                  vim->search_mode == CE_VIM_SEARCH_MODE_REGEX_BACKWARD){
                              CeRegex_t regex = NULL;
                              CeRegexResult_t regex_result = ce_regex_init(pattern,
                                                                           &regex);
                              if(regex_result.error_message == NULL){
                                   for(int64_t i = min; i <= max; i++){
                                        char* itr = layout->view.buffer->lines[i];
                                        int64_t prev_end_x = 0;
                                        while(itr){
                                             regex_result = ce_regex_match(regex, itr);
                                             if(regex_result.error_message != NULL){
                                                  free(regex_result.error_message);
                                                  break;
                                             }else{
                                                  if(regex_result.match_length > 0){
                                                       CePoint_t start = {prev_end_x + regex_result.match_start, i};
                                                       CePoint_t end = {start.x + (regex_result.match_length - 1), i};
                                                       ce_range_list_insert(&range_list, start, end);
                                                       itr = ce_utf8_iterate_to(itr, regex_result.match_start + regex_result.match_length);
                                                       prev_end_x = end.x + 1;
                                                  }else{
                                                       break;
                                                  }
                                             }
                                        }
                                   }
                                   ce_regex_free(regex);
                              }else{
                                   free(regex_result.error_message);
                              }
                         }
                    }
               }

               buffer_data->syntax_function(&layout->view, &range_list, &draw_color_list, syntax_defs,
                                            layout->view.buffer->syntax_data);
               ce_range_list_free(&range_list);
          }

          _draw_view(&layout->view, tab_width, line_number, visual_line_display_type, &draw_color_list, color_defs, syntax_defs,
                    terminal_width, show_line_extends_passed_view_as);
          ce_draw_color_list_free(&draw_color_list);
          _draw_view_status(&layout->view, layout == current ? vim : NULL, macros, color_defs, 0,
                           ui_fg_color, ui_bg_color);
          int64_t rect_height = layout->view.rect.bottom - layout->view.rect.top;
          int color_pair = ce_color_def_get(color_defs, ui_fg_color, ui_bg_color);
          attron(COLOR_PAIR(color_pair));
          if(layout->view.rect.right < (terminal_width - 1)){
               for(int i = 0; i < rect_height; i++){
                    mvaddch(layout->view.rect.top + i, layout->view.rect.right, ' ');
               }
          }
     } break;
     case CE_LAYOUT_TYPE_LIST:
          for(int64_t i = 0; i < layout->list.layout_count; i++){
               _draw_layout(layout->list.layouts[i], vim, visual, macros, input_buffer, color_defs, tab_width,
                           line_number, visual_line_display_type, current, syntax_defs, terminal_width, highlight_search,
                           ui_fg_color, ui_bg_color, show_line_extends_passed_view_as);
          }
          break;
     case CE_LAYOUT_TYPE_TAB:
          _draw_layout(layout->tab.root, vim, visual, macros, input_buffer, color_defs, tab_width, line_number,
                      visual_line_display_type, current, syntax_defs, terminal_width, highlight_search, ui_fg_color, ui_bg_color,
                      show_line_extends_passed_view_as);
          break;
     }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rcx, %r12
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %rbx
movl 0x138(%rsp), %r15d
movl 0x130(%rsp), %r10d
movl 0x128(%rsp), %ebp
movq 0x118(%rsp), %rdi
movq 0x108(%rsp), %r11
movl 0x100(%rsp), %ecx
movl 0xf8(%rsp), %edx
movq 0xf0(%rsp), %rsi
movl (%rbx), %eax
cmpl $0x2, %eax
jne 0x1aeb0
movq 0x8(%rbx), %rbx
jmp 0x1aea3
testl %eax, %eax
je 0x1af8e
cmpl $0x1, %eax
jne 0x1b4cb
cmpq $0x0, 0x10(%rbx)
jle 0x1b4cb
movq %r14, %rax
xorl %r14d, %r14d
movzbl 0x120(%rsp), %ecx
movq %rcx, 0x20(%rsp)
movq %r9, 0x28(%rsp)
movq %r13, 0x10(%rsp)
movq %rbx, 0x98(%rsp)
movq %r8, 0x8(%rsp)
movq %rax, 0x18(%rsp)
movq 0x8(%rbx), %rax
movq (%rax,%r14,8), %rdi
movq %r13, %rsi
movq 0x18(%rsp), %rdx
movq %r12, %rcx
movq 0x8(%rsp), %r8
movq 0x28(%rsp), %r9
pushq %r15
pushq %r10
pushq %rbp
pushq 0x38(%rsp)
pushq 0x138(%rsp)
pushq 0x138(%rsp)
pushq %r11
movl 0x138(%rsp), %eax
pushq %rax
movl 0x138(%rsp), %eax
pushq %rax
pushq 0x138(%rsp)
movq %r12, %r13
movq %rbp, %r12
movq %r10, %rbp
movq %r15, %rbx
movq %r11, %r15
callq 0x1ae49
movq %r15, %r11
movq %rbx, %r15
movq %rbp, %r10
movq %r12, %rbp
movq %r13, %r12
movq 0x60(%rsp), %r13
movq 0xe8(%rsp), %rbx
addq $0x50, %rsp
incq %r14
cmpq 0x10(%rbx), %r14
jl 0x1aefb
jmp 0x1b4cb
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
leaq 0x8(%rbx), %r15
movq 0x48(%rbx), %rax
movq 0x58(%rax), %rax
cmpq $0x0, 0x600(%rax)
je 0x1b3e2
movq %r8, 0x8(%rsp)
movq %rax, 0x88(%rsp)
movq %r15, 0x90(%rsp)
movq %r13, 0x10(%rsp)
movq %r12, 0x20(%rsp)
movq %r9, 0x28(%rsp)
movaps %xmm0, 0x30(%rsp)
cmpq 0x108(%rsp), %rbx
jne 0x1b0d7
movq 0x10(%rsp), %rax
movl (%rax), %eax
cmpl $0x4, %eax
je 0x1b088
cmpl $0x3, %eax
je 0x1b030
cmpl $0x2, %eax
jne 0x1b0d7
movups (%r14), %xmm0
leaq 0x50(%rsp), %r14
movaps %xmm0, (%r14)
movups 0x38(%rbx), %xmm0
movaps %xmm0, 0x10(%r14)
movq %r14, %rdi
callq 0x1079c
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq 0x10(%r14), %rcx
jmp 0x1b078
movups (%r14), %xmm0
leaq 0x50(%rsp), %r14
movaps %xmm0, (%r14)
movups 0x38(%rbx), %xmm0
movaps %xmm0, 0x10(%r14)
movq %r14, %rdi
callq 0x1079c
movq $0x0, (%r14)
movq 0x48(%rbx), %rax
movq (%rax), %rax
movq 0x18(%r14), %rcx
movq (%rax,%rcx,8), %rdi
callq 0xe97d
leaq 0x1(%rax), %rcx
movq %rcx, 0x10(%r14)
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq 0x18(%r14), %r8
leaq 0x30(%rsp), %rdi
callq 0x1fa0e
jmp 0x1b0d7
movq (%r14), %rax
movq 0x8(%r14), %rcx
movq 0x38(%rbx), %r14
movq 0x40(%rbx), %rbp
cmpq %r14, %rax
movq %r14, %r12
cmovlq %rax, %r12
cmovgq %rax, %r14
cmpq %rbp, %rcx
movq %rbp, %r13
cmovlq %rcx, %r13
cmovgq %rcx, %rbp
incq %rbp
leaq 0x30(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %r14, %rcx
movq %r13, %r8
callq 0x1fa0e
incq %r13
cmpq %r13, %rbp
jne 0x1b0bb
cmpb $0x0, 0x120(%rsp)
je 0x1b370
movq 0x8(%rsp), %rax
movq 0x10(%rax), %r14
leaq 0x15583(%rip), %rsi     # 0x30678
movq %r14, %rdi
callq 0xd390
testl %eax, %eax
je 0x1b13a
leaq 0x15568(%rip), %rsi     # 0x30670
movq %r14, %rdi
callq 0xd390
testl %eax, %eax
je 0x1b13a
leaq 0x152b8(%rip), %rsi     # 0x303d3
movq %r14, %rdi
callq 0xd390
testl %eax, %eax
je 0x1b13a
leaq 0x1553c(%rip), %rsi     # 0x3066a
movq %r14, %rdi
callq 0xd390
testl %eax, %eax
jne 0x1b158
movq 0x8(%rsp), %rax
cmpq $0x0, 0x8(%rax)
je 0x1b158
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq (%rax), %r13
cmpb $0x0, (%r13)
jne 0x1b173
movl $0x2f, %edi
callq 0x2517f
leaq (%rax,%rax,2), %rax
movq 0x10(%rsp), %rcx
movq 0x1058(%rcx,%rax,8), %r13
testq %r13, %r13
je 0x1b370
movq %r13, %rdi
callq 0xe485
movq 0x20(%rbx), %rcx
movq 0x30(%rbx), %rdx
subq 0x18(%rbx), %rcx
addq %rdx, %rcx
movq 0x48(%rbx), %rsi
movq 0x8(%rsi), %rdi
decq %rdi
cmpq %rdi, %rdx
movq %rdi, %r15
cmovlq %rdx, %r15
xorl %esi, %esi
testq %rdx, %rdx
cmovsq %rsi, %r15
cmpq %rdi, %rcx
cmovlq %rcx, %rdi
testq %rcx, %rcx
cmovsq %rsi, %rdi
movq %rdi, 0x8(%rsp)
movq %rax, %rdx
movq 0x10(%rsp), %rax
movl 0x19dc(%rax), %eax
leal -0x2(%rax), %ecx
cmpl $0x2, %ecx
jb 0x1b277
movq %rdx, 0x18(%rsp)
cmpl $0x1, %eax
ja 0x1b370
cmpq 0x8(%rsp), %r15
jg 0x1b370
movq 0x18(%rsp), %rax
leaq -0x1(%rax), %r14
movq 0x18(%rsp), %r12
movq 0x48(%rbx), %rax
movq (%rax), %rax
movq (%rax,%r15,8), %rdi
movq %r13, %rsi
callq 0xd6f0
testq %rax, %rax
je 0x1b264
movq %rax, %rbp
movq 0x48(%rbx), %rax
movq (%rax), %rax
movq (%rax,%r15,8), %rdi
movq %rbp, %rsi
callq 0x10101
leaq (%r14,%rax), %rcx
decq %rcx
leaq -0x1(%rax), %rsi
leaq 0x30(%rsp), %rdi
movq %r15, %rdx
movq %r15, %r8
callq 0x1fa0e
addq %r12, %rbp
movq %rbp, %rdi
movq %r13, %rsi
callq 0xd6f0
movq %rax, %rbp
testq %rax, %rax
jne 0x1b220
leaq 0x1(%r15), %rax
cmpq 0x8(%rsp), %r15
movq %rax, %r15
jne 0x1b205
jmp 0x1b370
leaq 0x48(%rsp), %rdx
movq $0x0, (%rdx)
leaq 0x50(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x1f410
movq (%r14), %rdi
testq %rdi, %rdi
je 0x1b2a5
callq 0xd090
jmp 0x1b370
cmpq 0x8(%rsp), %r15
jg 0x1b366
leaq 0xa0(%rsp), %r12
movq 0x48(%rbx), %rax
movq (%rax), %rax
movq (%rax,%r15,8), %r14
testq %r14, %r14
je 0x1b354
xorl %r13d, %r13d
movq 0x48(%rsp), %rsi
movq %r12, %rdi
movq %r14, %rdx
callq 0x1f4a0
movq 0xb0(%rsp), %rax
movq %rax, 0x60(%rsp)
movups 0xa0(%rsp), %xmm0
movaps %xmm0, 0x50(%rsp)
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
jne 0x1b34f
movq 0x60(%rsp), %rbp
testq %rbp, %rbp
jle 0x1b354
addq 0x58(%rsp), %r13
leaq -0x1(,%r13), %rcx
addq %rbp, %rcx
leaq 0x30(%rsp), %rdi
movq %r13, %rsi
movq %r15, %rdx
movq %r15, %r8
callq 0x1fa0e
movq 0x60(%rsp), %rsi
addq 0x58(%rsp), %rsi
movq %r14, %rdi
callq 0xe5be
movq %rax, %r14
addq %rbp, %r13
testq %rax, %rax
jne 0x1b2cf
jmp 0x1b354
callq 0xd090
leaq 0x1(%r15), %rax
cmpq 0x8(%rsp), %r15
movq %rax, %r15
jne 0x1b2b8
movq 0x48(%rsp), %rdi
callq 0x1f52e
movq 0x48(%rbx), %rax
movq 0x60(%rax), %r8
leaq 0x30(%rsp), %r14
leaq 0x70(%rsp), %rdx
movq 0x90(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movq 0x110(%rsp), %rcx
movq 0x88(%rsp), %rax
callq *0x600(%rax)
movq %r14, %rdi
callq 0x1fb71
movq 0x118(%rsp), %rdi
movq 0x28(%rsp), %r9
movl 0x128(%rsp), %ebp
movq 0x20(%rsp), %r12
movq 0x10(%rsp), %r13
movl 0x100(%rsp), %ecx
movl 0xf8(%rsp), %edx
movq 0xf0(%rsp), %rsi
subq $0x8, %rsp
leaq 0x78(%rsp), %r8
movq %rdi, %r10
movq %r15, %rdi
movq %r15, %r14
movq %r9, %r15
movl 0x140(%rsp), %eax
pushq %rax
pushq %r10
pushq 0x128(%rsp)
callq 0x1b4dd
addq $0x20, %rsp
leaq 0x70(%rsp), %rdi
callq 0x1f9e0
xorl %eax, %eax
cmpq 0x108(%rsp), %rbx
cmovneq %rax, %r13
subq $0x8, %rsp
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %r15, %rcx
xorl %r8d, %r8d
movl %ebp, %r9d
movl 0x138(%rsp), %r12d
pushq %r12
callq 0x1a340
addq $0x10, %rsp
movq 0x20(%rbx), %r14
subq 0x18(%rbx), %r14
movq %r15, %rdi
movl %ebp, %esi
movl %r12d, %edx
callq 0x1fbe9
movq 0x1fb22(%rip), %r15     # 0x3af90
movq (%r15), %rdi
shll $0x8, %eax
movzwl %ax, %esi
xorl %edx, %edx
callq 0xd0f0
movq 0x118(%rsp), %rax
decq %rax
cmpq %rax, 0x10(%rbx)
setl %al
testq %r14, %r14
setg %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x1b4cb
xorl %r12d, %r12d
movq (%r15), %rdi
movl 0x18(%rbx), %esi
addl %r12d, %esi
movl 0x10(%rbx), %edx
callq 0xd070
cmpl $-0x1, %eax
je 0x1b4c3
movq (%r15), %rdi
movl $0x20, %esi
callq 0xd620
incq %r12
cmpq %r12, %r14
jne 0x1b4a0
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq