// File: /workspace/github_c_makefile_star2_O2/justy989[P]ce/ce_draw_term.c
// Function: draw_view
static void _draw_view(CeView_t* view, int64_t tab_width, CeLineNumber_t line_number, CeVisualLineDisplayType_t visual_line_display_type,
                       CeDrawColorList_t* draw_color_list, CeColorDefs_t* color_defs, CeSyntaxDef_t* syntax_defs,
                       int64_t terminal_right, CeRune_t show_line_extends_passed_view_as){
     int64_t view_width = ce_view_width(view);
     int64_t view_height = ce_view_height(view);
     int64_t row_min = view->scroll.y;
     int64_t col_min = view->scroll.x;
     int64_t col_max = col_min + view_width;

     char tab_str[tab_width + 1];
     memset(tab_str, ' ', tab_width);
     tab_str[tab_width] = 0;

     CeDrawColorNode_t* draw_color_node = draw_color_list->head;

     // figure out how wide the line number margin needs to be
     int line_number_size = 0;
     if(!view->buffer->no_line_numbers){
          line_number_size = ce_line_number_column_width(line_number, view->buffer->line_count, view->rect.top, view->rect.bottom);
          if(line_number_size){
               col_max -= line_number_size;
               line_number_size--;
          }
     }

     if(view->buffer->line_count >= 0){
          for(int64_t y = 0; y < view_height; y++){
               int64_t index = 0;
               int64_t x = 0;
               int64_t rune_len = 0;
               int64_t line_index = y + row_min;
               CeRune_t rune = 1;
               int64_t real_y = y + view->scroll.y;

               move(view->rect.top + y, view->rect.left);

               if(!view->buffer->no_line_numbers && line_number){
                    int fg = CE_COLOR_DEFAULT;
                    int bg = CE_COLOR_DEFAULT;
                    if(draw_color_node){
                         fg = draw_color_node->fg;
                         bg = draw_color_node->bg;
                    }
                    fg = ce_syntax_def_get_fg(syntax_defs, CE_SYNTAX_COLOR_LINE_NUMBER, fg);
                    bg = ce_syntax_def_get_bg(syntax_defs, CE_SYNTAX_COLOR_LINE_NUMBER, bg);
                    int change_color_pair = ce_color_def_get(color_defs, fg, bg);
                    attron(COLOR_PAIR(change_color_pair));
                    int value = real_y + 1;
                    if(line_number == CE_LINE_NUMBER_RELATIVE || (line_number == CE_LINE_NUMBER_ABSOLUTE_AND_RELATIVE && view->cursor.y != real_y)){
                         value = abs((int)(view->cursor.y - real_y));
                    }
                    printw("%*d ", line_number_size, value);
               }

               standend();
               if(!view->buffer->no_highlight_current_line && real_y == view->cursor.y){
                    int bg = ce_syntax_def_get_bg(syntax_defs, CE_SYNTAX_COLOR_CURRENT_LINE, CE_COLOR_DEFAULT);
                    int change_color_pair = ce_color_def_get(color_defs, CE_COLOR_DEFAULT, bg);
                    attron(COLOR_PAIR(change_color_pair));
               }else if(draw_color_node && ce_point_after((CePoint_t){index, y + view->scroll.y}, draw_color_node->point)){
                    int change_color_pair = ce_color_def_get(color_defs, draw_color_node->fg, draw_color_node->bg);
                    attron(COLOR_PAIR(change_color_pair));
               }

               if(line_index < view->buffer->line_count){
                    const char* line = view->buffer->lines[y + row_min];

                    while(rune > 0){
                         rune = ce_utf8_decode(line, &rune_len);

                         // check if we need to move to the next color
                         while(draw_color_node && !ce_point_after(draw_color_node->point, (CePoint_t){index, real_y})){
                              int bg = draw_color_node->bg;
                              if(!view->buffer->no_highlight_current_line && bg == CE_COLOR_DEFAULT && real_y == view->cursor.y){
                                   bg = ce_syntax_def_get_bg(syntax_defs, CE_SYNTAX_COLOR_CURRENT_LINE, bg);
                              }

                              int change_color_pair = ce_color_def_get(color_defs, draw_color_node->fg, bg);
                              attron(COLOR_PAIR(change_color_pair));
                              draw_color_node = draw_color_node->next;
                         }

                         const char* look_ahead = line + rune_len;
                         int64_t next_rune_len = 0;
                         CeRune_t next_rune = 1;

                         next_rune = ce_utf8_decode(look_ahead, &next_rune_len);

                         if(x > col_max){
                              x++;
                         }else if(show_line_extends_passed_view_as &&
                                  ((view->rect.right >= terminal_right && x == col_max) ||
                                   (view->rect.right < terminal_right && x == (col_max - 1))) &&
                                  next_rune != 0){
                              int new_bg = ce_syntax_def_get_bg(syntax_defs, CE_SYNTAX_COLOR_LINE_EXTENDS_PASSED_VIEW, CE_COLOR_DEFAULT);
                              int new_fg = ce_syntax_def_get_fg(syntax_defs, CE_SYNTAX_COLOR_LINE_EXTENDS_PASSED_VIEW, CE_COLOR_DEFAULT);
                              int change_color_pair = ce_color_def_get(color_defs, new_fg, new_bg);
                              attron(COLOR_PAIR(change_color_pair));
                              addch(show_line_extends_passed_view_as);
                              standend();
                              x++;
                         }else if(x >= col_min && rune > 0){
                              if(rune == CE_TAB){
                                   x += tab_width;
                                   addstr(tab_str);
                              }else if(rune >= 0x80){
                                   char utf8_string[CE_UTF8_SIZE + 1];
                                   int64_t bytes_written = 0;
                                   ce_utf8_encode(rune, utf8_string, CE_UTF8_SIZE, &bytes_written);
                                   utf8_string[bytes_written] = 0;
                                   addstr(utf8_string);
                                   x++;
                              }else{
                                   addch(rune);
                                   x++;
                              }
                         }else if(rune == CE_TAB){
                              x += tab_width;
                         }else{
                              x++;
                         }

                         line += rune_len;
                         index++;
                    }

                    x--;
               }

               if(x < col_min) x = col_min;
               switch(visual_line_display_type){
               default:
                    break;
               case CE_VISUAL_LINE_DISPLAY_TYPE_FULL_LINE:
                    for(; x <= col_max; x++) addch(' ');
                    break;
               case CE_VISUAL_LINE_DISPLAY_TYPE_INCLUDE_NEWLINE:
                    addch(' ');
                    x++;
               // intentional fall through
               case CE_VISUAL_LINE_DISPLAY_TYPE_EXCLUDE_NEWLINE:
                    standend();
                    if(!view->buffer->no_highlight_current_line && real_y == view->cursor.y){
                         int bg = ce_syntax_def_get_bg(syntax_defs, CE_SYNTAX_COLOR_CURRENT_LINE, CE_COLOR_DEFAULT);
                         int change_color_pair = ce_color_def_get(color_defs, CE_COLOR_DEFAULT, bg);
                         attron(COLOR_PAIR(change_color_pair));
                    }
                    for(; x <= col_max; x++) addch(' ');
                    break;
               }
          }
     }else{
          standend();
     }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %r9, -0x50(%rbp)
movq %r8, -0x38(%rbp)
movl %ecx, -0x58(%rbp)
movl %edx, %ebx
movq %rsi, %r15
movq %rdi, %r13
movl 0x20(%rbp), %eax
movl %eax, -0x60(%rbp)
movq 0x18(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq 0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
callq 0xfbf0
movq %rax, %r12
movq %r13, %rdi
callq 0xfbf8
movq %rax, -0x78(%rbp)
movq 0x20(%r13), %rax
movq 0x28(%r13), %rcx
movq %rcx, -0xb8(%rbp)
movq %rax, -0xa0(%rbp)
addq %rax, %r12
movq %rsp, %r14
leaq 0x10(%r15), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
pushq $0x20
popq %rsi
movq %r14, %rdi
movq %r15, %rdx
callq 0xd2a0
movq %r15, -0x98(%rbp)
movq %r14, -0xc8(%rbp)
movb $0x0, (%r15,%r14)
movq -0x38(%rbp), %rax
movq (%rax), %r14
movq 0x40(%r13), %rax
movl $0x0, -0x54(%rbp)
cmpb $0x0, 0x50(%rax)
movl %ebx, -0x5c(%rbp)
jne 0x1a1c8
movq 0x8(%rax), %rsi
movq 0x10(%r13), %rdx
movq 0x18(%r13), %rcx
movl %ebx, %edi
callq 0x10283
testl %eax, %eax
je 0x1a1c8
movslq %eax, %rcx
subq %rcx, %r12
decl %eax
movl %eax, -0x54(%rbp)
movq 0x40(%r13), %rax
cmpq $0x0, 0x8(%rax)
js 0x1a69c
leaq -0x1(%r12), %rax
movq %rax, -0xc0(%rbp)
xorl %eax, %eax
movq -0x78(%rbp), %rcx
testq %rcx, %rcx
cmovleq %rax, %rcx
movq %rcx, -0x78(%rbp)
movq 0x1ed95(%rip), %rax     # 0x38f90
xorl %ecx, %ecx
movq %r12, -0x68(%rbp)
cmpq -0x78(%rbp), %rcx
je 0x1a68d
andq $0x0, -0x88(%rbp)
movq 0x28(%r13), %r12
addq %rcx, %r12
movq %rax, %rbx
movq (%rax), %rdi
movl 0x10(%r13), %esi
movq %rcx, -0x80(%rbp)
addl %ecx, %esi
movl (%r13), %edx
callq 0xd070
cmpl $0x0, -0x5c(%rbp)
je 0x1a2d9
movq 0x40(%r13), %rax
cmpb $0x0, 0x50(%rax)
jne 0x1a2d9
pushq $-0x1
popq %rdx
movl %edx, %eax
testq %r14, %r14
je 0x1a25c
movl (%r14), %edx
movl 0x4(%r14), %eax
movl %eax, -0x40(%rbp)
movq -0x48(%rbp), %r15
movq %r15, %rdi
pushq $0x14
popq %rsi
callq 0x1dac4
movl %eax, -0x38(%rbp)
movq %r15, %rdi
pushq $0x14
popq %rsi
movl -0x40(%rbp), %edx
callq 0x1dad0
movq -0x50(%rbp), %rdi
movl -0x38(%rbp), %esi
movl %eax, %edx
callq 0x1dd23
movq (%rbx), %rdi
shll $0x8, %eax
movzwl %ax, %esi
xorl %edx, %edx
callq 0xd0f0
leal 0x1(%r12), %edx
movl -0x5c(%rbp), %eax
cmpl $0x3, %eax
je 0x1a2b5
cmpl $0x2, %eax
jne 0x1a2c8
movq 0x38(%r13), %rax
jmp 0x1a2be
movq 0x38(%r13), %rax
cmpq %r12, %rax
je 0x1a2c8
subl %r12d, %eax
movl %eax, %edx
negl %edx
cmovsl %eax, %edx
leaq 0x12392(%rip), %rdi     # 0x2c661
movl -0x54(%rbp), %esi
xorl %eax, %eax
callq 0xd4d0
movq (%rbx), %rdi
xorl %esi, %esi
callq 0xd730
movq 0x40(%r13), %rax
cmpb $0x0, 0x51(%rax)
jne 0x1a311
cmpq 0x38(%r13), %r12
jne 0x1a311
movq -0x48(%rbp), %rdi
pushq $0xd
popq %rsi
pushq $-0x1
popq %r15
movl %r15d, %edx
callq 0x1dad0
movq -0x50(%rbp), %rdi
movl %r15d, %esi
movl %eax, %edx
jmp 0x1a33c
testq %r14, %r14
je 0x1a351
movq 0x28(%r13), %rsi
addq -0x80(%rbp), %rsi
movq 0x8(%r14), %rdx
movq 0x10(%r14), %rcx
xorl %edi, %edi
callq 0xea69
testb %al, %al
je 0x1a351
movl (%r14), %esi
movl 0x4(%r14), %edx
movq -0x50(%rbp), %rdi
callq 0x1dd23
movq (%rbx), %rdi
shll $0x8, %eax
movzwl %ax, %esi
xorl %edx, %edx
callq 0xd0f0
movq -0xb8(%rbp), %rax
movq -0x80(%rbp), %rcx
addq %rcx, %rax
movq 0x40(%r13), %rcx
cmpq 0x8(%rcx), %rax
jge 0x1a59c
movq (%rcx), %rcx
movq (%rcx,%rax,8), %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
xorl %r15d, %r15d
testl %esi, %esi
jle 0x1a5a0
movq %rdx, -0x40(%rbp)
movq %rdi, -0x38(%rbp)
leaq -0x88(%rbp), %rsi
callq 0xe53f
movl %eax, -0x2c(%rbp)
testq %r14, %r14
je 0x1a403
movq 0x8(%r14), %rdi
movq 0x10(%r14), %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0xea69
testb %al, %al
jne 0x1a403
movl 0x4(%r14), %edx
movq 0x40(%r13), %rax
cmpb $0x0, 0x51(%rax)
jne 0x1a3e1
cmpl $-0x1, %edx
jne 0x1a3e1
pushq $-0x1
popq %rdx
cmpq 0x38(%r13), %r12
jne 0x1a3e1
movq -0x48(%rbp), %rdi
pushq $0xd
popq %rsi
callq 0x1dad0
movl %eax, %edx
movl (%r14), %esi
movq -0x50(%rbp), %rdi
callq 0x1dd23
movq (%rbx), %rdi
shll $0x8, %eax
movzwl %ax, %esi
xorl %edx, %edx
callq 0xd0f0
movq 0x18(%r14), %r14
jmp 0x1a39b
movq -0x88(%rbp), %rdi
addq -0x38(%rbp), %rdi
andq $0x0, -0xd0(%rbp)
leaq -0xd0(%rbp), %rsi
callq 0xe53f
movq -0x40(%rbp), %rdx
cmpq -0x68(%rbp), %rdx
jg 0x1a4d3
cmpl $0x0, -0x60(%rbp)
je 0x1a4e2
movq 0x8(%r13), %rcx
cmpq -0xa8(%rbp), %rcx
jl 0x1a44d
cmpq -0x68(%rbp), %rdx
je 0x1a463
cmpq -0xa8(%rbp), %rcx
jge 0x1a4e2
cmpq -0xc0(%rbp), %rdx
jne 0x1a4e2
testl %eax, %eax
je 0x1a4e2
movq -0x48(%rbp), %rdi
pushq $0x17
popq %rsi
pushq $-0x1
popq %rdx
callq 0x1dad0
movl %eax, -0x8c(%rbp)
movq -0x48(%rbp), %rdi
pushq $0x17
popq %rsi
pushq $-0x1
popq %rdx
callq 0x1dac4
movq -0x50(%rbp), %rdi
movl %eax, %esi
movl -0x8c(%rbp), %edx
callq 0x1dd23
movq (%rbx), %rdi
shll $0x8, %eax
movzwl %ax, %esi
xorl %edx, %edx
callq 0xd0f0
movq 0x1eadd(%rip), %rax     # 0x38f90
movq (%rax), %rdi
movl -0x60(%rbp), %esi
callq 0xd620
movq 0x1eacb(%rip), %rax     # 0x38f90
movq (%rax), %rdi
xorl %esi, %esi
callq 0xd730
movq -0x40(%rbp), %rdx
incq %rdx
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %esi
jmp 0x1a58d
cmpq -0xa0(%rbp), %rdx
movl -0x2c(%rbp), %esi
jl 0x1a520
testl %esi, %esi
jle 0x1a520
cmpl $0x9, %esi
movq %rbx, %rax
jne 0x1a52e
addq -0x98(%rbp), %rdx
movq %rdx, -0x40(%rbp)
movq (%rax), %rdi
movq -0xc8(%rbp), %rsi
pushq $-0x1
popq %rdx
callq 0xd1a0
movl -0x2c(%rbp), %esi
movq -0x40(%rbp), %rdx
jmp 0x1a589
cmpl $0x9, %esi
jne 0x1a586
addq -0x98(%rbp), %rdx
jmp 0x1a589
cmpl $0x80, %esi
jb 0x1a577
andq $0x0, -0xb0(%rbp)
movl %esi, %edi
leaq -0x6d(%rbp), %rsi
pushq $0x4
popq %rdx
leaq -0xb0(%rbp), %rcx
callq 0xee6f
movq -0xb0(%rbp), %rax
movb $0x0, -0x6d(%rbp,%rax)
movq 0x1ea2a(%rip), %rax     # 0x38f90
movq (%rax), %rdi
leaq -0x6d(%rbp), %rsi
pushq $-0x1
popq %rdx
callq 0xd1a0
jmp 0x1a57f
movq (%rax), %rdi
callq 0xd620
movl -0x2c(%rbp), %esi
movq -0x40(%rbp), %rdx
incq %rdx
movq -0x38(%rbp), %rdi
addq -0x88(%rbp), %rdi
incq %r15
jmp 0x1a37c
xorl %edx, %edx
jmp 0x1a5a3
decq %rdx
movl -0x58(%rbp), %ecx
movq -0xa0(%rbp), %rax
cmpq %rax, %rdx
cmovleq %rax, %rdx
cmpl $0x2, %ecx
je 0x1a609
cmpl $0x1, %ecx
movq %rbx, %rax
je 0x1a5ef
testl %ecx, %ecx
movq -0x68(%rbp), %r12
jne 0x1a681
cmpq %r12, %rdx
jg 0x1a681
movq (%rax), %rdi
pushq $0x20
popq %rsi
movq %rdx, %r15
callq 0xd620
movq %r15, %rdx
movq %rbx, %rax
incq %rdx
jmp 0x1a5cd
movq (%rax), %rdi
pushq $0x20
popq %rsi
movq %rdx, %r15
callq 0xd620
movq %rbx, %rax
incq %r15
movq %r15, -0x40(%rbp)
jmp 0x1a610
movq %rdx, -0x40(%rbp)
movq %rbx, %rax
movq (%rax), %rdi
xorl %esi, %esi
callq 0xd730
movq 0x40(%r13), %rax
cmpb $0x0, 0x51(%rax)
jne 0x1a65b
cmpq 0x38(%r13), %r12
jne 0x1a65b
movq -0x48(%rbp), %rdi
pushq $0xd
popq %rsi
pushq $-0x1
popq %r15
movl %r15d, %edx
callq 0x1dad0
movq -0x50(%rbp), %rdi
movl %r15d, %esi
movl %eax, %edx
callq 0x1dd23
movq (%rbx), %rdi
shll $0x8, %eax
movzwl %ax, %esi
xorl %edx, %edx
callq 0xd0f0
movq -0x68(%rbp), %r12
movq -0x40(%rbp), %rcx
cmpq %r12, %rcx
movq %rbx, %rax
jg 0x1a681
movq (%rax), %rdi
pushq $0x20
popq %rsi
movq %rcx, %r15
callq 0xd620
movq %r15, %rcx
incq %rcx
jmp 0x1a663
movq -0x80(%rbp), %rcx
incq %rcx
jmp 0x1a201
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1e8ed(%rip), %rax     # 0x38f90
movq (%rax), %rdi
xorl %esi, %esi
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xd730
nop