// File: /workspace/github_c_makefile_star2_O2/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 $0x98, %rsp
movq %r9, 0x30(%rsp)
movq %r8, %r15
movq %rcx, 0x28(%rsp)
movq %rdx, %r12
movq %rsi, 0x8(%rsp)
movq %rdi, %r13
movq 0xf8(%rsp), %rbp
movl (%r13), %eax
cmpl $0x2, %eax
jne 0x19b64
movq 0x8(%r13), %r13
jmp 0x19b55
testl %eax, %eax
je 0x19bf5
cmpl $0x1, %eax
jne 0x1a0da
xorl %ebx, %ebx
movzbl 0x100(%rsp), %r14d
cmpq 0x10(%r13), %rbx
jge 0x1a0da
movq 0x8(%r13), %rax
movq (%rax,%rbx,8), %rdi
movq 0x8(%rsp), %rsi
movq %r12, %rdx
movq 0x28(%rsp), %rcx
movq %r15, %r8
movq 0x30(%rsp), %r9
movl 0x118(%rsp), %eax
pushq %rax
movl 0x118(%rsp), %eax
pushq %rax
movl 0x118(%rsp), %eax
pushq %rax
pushq %r14
pushq %rbp
pushq 0x118(%rsp)
pushq 0x118(%rsp)
movl 0x118(%rsp), %eax
pushq %rax
movl 0x118(%rsp), %eax
pushq %rax
pushq 0x118(%rsp)
callq 0x19b24
addq $0x50, %rsp
incq %rbx
jmp 0x19b80
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
leaq 0x8(%r13), %rbx
movq 0x48(%r13), %rax
movq 0x58(%rax), %rax
cmpq $0x0, 0x600(%rax)
je 0x19fd8
movq %rax, 0x60(%rsp)
movq %rbx, 0x68(%rsp)
movaps %xmm0, 0x10(%rsp)
movq %r13, (%rsp)
cmpq 0xe8(%rsp), %r13
jne 0x19cde
movq 0x8(%rsp), %rax
movl (%rax), %eax
cmpl $0x4, %eax
je 0x19e8c
cmpl $0x3, %eax
je 0x19c86
cmpl $0x2, %eax
jne 0x19cde
movups (%r12), %xmm0
leaq 0x40(%rsp), %r14
movaps %xmm0, (%r14)
movq (%rsp), %rax
movups 0x38(%rax), %xmm0
movaps %xmm0, 0x10(%r14)
movq %r14, %rdi
callq 0x1021f
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq 0x10(%r14), %rcx
jmp 0x19cd0
movups (%r12), %xmm0
leaq 0x40(%rsp), %r14
movaps %xmm0, (%r14)
movq (%rsp), %rbx
movups 0x38(%rbx), %xmm0
movaps %xmm0, 0x10(%r14)
movq %r14, %rdi
callq 0x1021f
andq $0x0, (%r14)
movq 0x48(%rbx), %rax
movq (%rax), %rax
movq 0x18(%r14), %rcx
movq (%rax,%rcx,8), %rdi
callq 0xe784
leaq 0x1(%rax), %rcx
movq %rcx, 0x10(%r14)
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq 0x18(%r14), %r8
leaq 0x10(%rsp), %rdi
callq 0x1db8b
cmpb $0x0, 0x100(%rsp)
movq 0xf8(%rsp), %rbp
je 0x19f9c
movq 0x10(%r15), %r14
leaq 0x1295b(%rip), %rsi     # 0x2c65a
movq %r14, %rdi
callq 0xd390
testl %eax, %eax
je 0x19d44
leaq 0x12940(%rip), %rsi     # 0x2c652
movq %r14, %rdi
callq 0xd390
testl %eax, %eax
je 0x19d44
leaq 0x126ab(%rip), %rsi     # 0x2c3d0
movq %r14, %rdi
callq 0xd390
testl %eax, %eax
je 0x19d44
leaq 0x12914(%rip), %rsi     # 0x2c64c
movq %r14, %rdi
callq 0xd390
testl %eax, %eax
jne 0x19d58
cmpq $0x0, 0x8(%r15)
je 0x19d58
movq (%r15), %rax
movq (%rax), %r12
cmpb $0x0, (%r12)
jne 0x19d7a
pushq $0x2f
popq %rdi
callq 0x22581
imulq $0x18, %rax, %rax
movq 0x8(%rsp), %rcx
movq 0x1058(%rcx,%rax), %r12
testq %r12, %r12
je 0x19f9c
movq %r12, %rdi
callq 0xe38c
movq (%rsp), %rsi
movq 0x20(%rsi), %rcx
movq 0x30(%rsi), %rdx
subq 0x18(%rsi), %rcx
addq %rdx, %rcx
movq 0x48(%rsi), %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, 0x20(%rsp)
movq %rax, %r13
movq 0x8(%rsp), %rax
movl 0x19dc(%rax), %eax
leal -0x2(%rax), %ecx
cmpl $0x2, %ecx
jb 0x19e61
cmpl $0x1, %eax
ja 0x19f9c
leaq -0x1(%r13), %rbx
leaq 0x10(%rsp), %r14
cmpq 0x20(%rsp), %r15
jg 0x19f9c
movq (%rsp), %rax
movq 0x48(%rax), %rax
movq (%rax), %rax
movq (%rax,%r15,8), %rbp
movq %rbp, %rdi
movq %r12, %rsi
callq 0xd6f0
testq %rax, %rax
je 0x19e54
movq %rax, %rbp
movq (%rsp), %rax
movq 0x48(%rax), %rax
movq (%rax), %rax
movq (%rax,%r15,8), %rdi
movq %rbp, %rsi
callq 0xfc01
leaq (%rbx,%rax), %rcx
decq %rcx
leaq -0x1(%rax), %rsi
movq %r14, %rdi
movq %r15, %rdx
movq %r15, %r8
callq 0x1db8b
addq %r13, %rbp
jmp 0x19e0c
incq %r15
movq 0xf8(%rsp), %rbp
jmp 0x19df2
leaq 0x38(%rsp), %rdx
andq $0x0, (%rdx)
leaq 0x40(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x1d614
movq (%r14), %rdi
testq %rdi, %rdi
je 0x19ee4
callq 0xd090
jmp 0x19f9c
movq (%r12), %rax
movq 0x8(%r12), %rcx
movq (%rsp), %rdx
movq 0x38(%rdx), %r14
movq 0x40(%rdx), %rbx
cmpq %r14, %rax
movq %r14, %r12
cmovlq %rax, %r12
cmovgq %rax, %r14
cmpq %rbx, %rcx
movq %rbx, %r13
cmovlq %rcx, %r13
cmovgq %rcx, %rbx
leaq 0x10(%rsp), %rbp
cmpq %rbx, %r13
jg 0x19cde
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %r14, %rcx
movq %r13, %r8
callq 0x1db8b
incq %r13
jmp 0x19ec2
leaq 0x80(%rsp), %r14
cmpq 0x20(%rsp), %r15
movq (%rsp), %rax
jg 0x19f92
movq 0x48(%rax), %rax
movq (%rax), %rax
movq (%rax,%r15,8), %r12
xorl %r13d, %r13d
testq %r12, %r12
je 0x19f8a
movq 0x38(%rsp), %rsi
movq %r14, %rdi
movq %r12, %rdx
callq 0x1d69d
movq 0x90(%rsp), %rax
movq %rax, 0x50(%rsp)
movups 0x80(%rsp), %xmm0
movaps %xmm0, 0x40(%rsp)
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
jne 0x19f85
movq 0x50(%rsp), %rbx
testq %rbx, %rbx
jle 0x19f8a
addq 0x48(%rsp), %r13
leaq (%rbx,%r13), %rcx
decq %rcx
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
movq %r15, %rdx
movq %r15, %r8
callq 0x1db8b
movq 0x50(%rsp), %rsi
addq 0x48(%rsp), %rsi
movq %r12, %rdi
callq 0xe4a6
movq %rax, %r12
addq %rbx, %r13
jmp 0x19f09
callq 0xd090
incq %r15
jmp 0x19eec
movq 0x38(%rsp), %rdi
callq 0x1d726
movq (%rsp), %r13
movq 0x48(%r13), %rax
movq 0x60(%rax), %r8
leaq 0x10(%rsp), %r14
leaq 0x70(%rsp), %rdx
movq 0x68(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
movq 0xf0(%rsp), %rcx
movq 0x60(%rsp), %rax
callq *0x600(%rax)
movq %r14, %rdi
callq 0x1dcb4
subq $0x8, %rsp
leaq 0x78(%rsp), %r14
movq %rbx, %rdi
movq 0xd8(%rsp), %rsi
movl 0xe0(%rsp), %edx
movl 0xe8(%rsp), %ecx
movq %r14, %r8
movq 0x38(%rsp), %r15
movq %r15, %r9
movl 0x120(%rsp), %eax
pushq %rax
pushq %rbp
pushq 0x108(%rsp)
callq 0x1a0ec
addq $0x20, %rsp
movq %r14, %rdi
callq 0x1db60
xorl %eax, %eax
cmpq 0xe8(%rsp), %r13
movq 0x8(%rsp), %rsi
cmovneq %rax, %rsi
subq $0x8, %rsp
movq %rbx, %rdi
movq 0x30(%rsp), %rdx
movq %r15, %rcx
xorl %r8d, %r8d
movl 0x110(%rsp), %ebx
movl %ebx, %r9d
movl 0x118(%rsp), %r12d
pushq %r12
callq 0x190c4
popq %rax
popq %rcx
movq 0x18(%r13), %rax
movq %rax, (%rsp)
movq 0x20(%r13), %r14
movq %r15, %rdi
movl %ebx, %esi
movl %r12d, %edx
callq 0x1dd23
movq 0x1ef0a(%rip), %r15     # 0x38f90
movq (%r15), %rdi
shll $0x8, %eax
movzwl %ax, %esi
xorl %edx, %edx
callq 0xd0f0
decq %rbp
cmpq %rbp, 0x10(%r13)
jge 0x1a0da
subq (%rsp), %r14
xorl %ebx, %ebx
testq %r14, %r14
cmovleq %rbx, %r14
pushq $0x20
popq %rbp
cmpq %rbx, %r14
je 0x1a0da
movq (%r15), %rdi
movl 0x18(%r13), %esi
addl %ebx, %esi
movl 0x10(%r13), %edx
callq 0xd070
cmpl $-0x1, %eax
je 0x1a0d5
movq (%r15), %rdi
movl %ebp, %esi
callq 0xd620
incq %rbx
jmp 0x1a0af
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq