// File: /workspace/github_c_makefile_star2_O1/justy989[P]ce/ce_app.c
// Function: apply_completion_to_buffer
bool apply_completion_to_buffer(CeComplete_t* complete,
                                CeBuffer_t* buffer,
                                int64_t start_x,
                                CePoint_t* cursor){
     if(complete->current < 0){
          return false;
     }

     // Do we even need completion ?
     int64_t completion_len = strlen(complete->elements[complete->current].string);
     int64_t input_len = strlen(buffer->lines[cursor->y] + start_x);
     int64_t input_offset = 0;
     if(input_len > completion_len) input_offset = input_len - completion_len;
     if(strcmp(complete->elements[complete->current].string, buffer->lines[cursor->y] + start_x + input_offset) == 0){
          return false;
     }

     // If the input text matches a later section of the current match, delete it.
     char* insertion = strdup(complete->elements[complete->current].string);
     int64_t insertion_len = strlen(insertion);
     CePoint_t delete_point = *cursor;
     int64_t delete_len = 0;
     if(complete->current_match){
          delete_len = strlen(complete->current_match);
          delete_point = ce_buffer_advance_point(buffer, *cursor, -delete_len);
          if(delete_len > 0){
               ce_buffer_remove_string_change(buffer, delete_point, delete_len, cursor, *cursor,
                                              false);
          }
     }

     // Insert the match from the start of the matching completion.
     CePoint_t cursor_end = {delete_point.x + insertion_len, delete_point.y};
     if(insertion_len > 0){
          // This chain is based on whether nor not the remove string change above was called.
          bool chain_undo = (complete->current_match && delete_len > 0);
          ce_buffer_insert_string_change(buffer, insertion, delete_point, cursor, cursor_end,
                                         chain_undo);
     }

     // Delete any previous match from after the cursor
     int64_t extra_len = strlen(buffer->lines[cursor->y] + cursor->x);
     char* extra = strdup(buffer->lines[cursor->y] + cursor->x);
     bool removed = false;
     for(int64_t i = extra_len; i > 0; i--){
          extra[i] = 0;
          for(int64_t c = 0; c < complete->count; c++){
               if(_str_endswith(complete->elements[c].string, extra)){
                    ce_buffer_remove_string_change(buffer, *cursor, i, cursor, cursor_end, true);
                    removed = true;
                    break;
               }
          }
          if(removed){
               break;
          }
     }
     free(extra);

     // We explicitly do not free insertion as its ownership was given to
     // ce_buffer_insert_string_change().

     return true;
}

// Assembly:
movq 0x18(%rdi), %rax
testq %rax, %rax
js 0x12904
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, %rbx
movq %rdx, %rbp
movq %rsi, %r14
movq %rdi, %r15
movq (%rdi), %rcx
leaq (%rax,%rax,2), %rax
movq (%rcx,%rax,8), %r13
movq %r13, %rdi
callq 0xd210
movq %rax, %r12
movq %r14, 0x20(%rsp)
movq (%r14), %rax
movq 0x8(%rbx), %rcx
movq %rcx, 0x18(%rsp)
addq (%rax,%rcx,8), %rbp
movq %rbp, %rdi
callq 0xd210
xorl %esi, %esi
subq %r12, %rax
cmovgq %rax, %rsi
addq %rbp, %rsi
movq %r13, %rdi
callq 0xd390
testl %eax, %eax
setne %al
je 0x129ff
movq %r13, %rdi
callq 0xd640
movq %rax, %r13
movq %rax, %rdi
callq 0xd210
movq (%rbx), %rbp
movq 0x10(%r15), %rdi
testq %rdi, %rdi
je 0x12907
movq %rax, 0x28(%rsp)
movq %r13, 0x30(%rsp)
callq 0xd210
movq %rax, %r13
movq %rax, %rcx
negq %rcx
movq 0x20(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
movq 0x18(%rsp), %rdx
callq 0xeffa
movq %rax, %rbp
movq %rdx, %r12
testq %r13, %r13
jle 0x12915
movups (%rbx), %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r13, %rcx
movq %rbx, %r8
xorl %r9d, %r9d
callq 0xfc21
movb $0x1, %dl
jmp 0x12917
xorl %eax, %eax
retq
xorl %edx, %edx
movq 0x20(%rsp), %r14
movq 0x18(%rsp), %r12
jmp 0x12921
xorl %edx, %edx
movq 0x30(%rsp), %r13
movq 0x28(%rsp), %rax
leaq (%rax,%rbp), %rcx
movq %rcx, 0x38(%rsp)
movq %r12, 0x40(%rsp)
testq %rax, %rax
jle 0x1295f
cmpq $0x0, 0x10(%r15)
setne %cl
andb %cl, %dl
movups 0x38(%rsp), %xmm0
movups %xmm0, (%rsp)
movzbl %dl, %r9d
movq %r14, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %r12, %rcx
movq %rbx, %r8
callq 0xfa43
movq (%r14), %rax
movq 0x8(%rbx), %rcx
movq (%rax,%rcx,8), %r13
movq %rbx, 0x18(%rsp)
addq (%rbx), %r13
movq %r13, %rdi
callq 0xd210
movq %rax, %r12
movq %r13, %rdi
callq 0xd640
movq %rax, %r13
testq %r12, %r12
jle 0x129f5
xorl %ebp, %ebp
movb $0x0, (%r13,%r12)
movq 0x8(%r15), %r14
testq %r14, %r14
jle 0x129e6
movq (%r15), %rbx
movq (%rbx), %rdi
movq %r13, %rsi
callq 0x127d5
testb %al, %al
jne 0x129bb
addq $0x18, %rbx
decq %r14
jne 0x129a1
jmp 0x129e6
movq 0x18(%rsp), %r8
movq (%r8), %rsi
movq 0x8(%r8), %rdx
movups 0x38(%rsp), %xmm0
movups %xmm0, (%rsp)
movq 0x20(%rsp), %rdi
movq %r12, %rcx
movl $0x1, %r9d
callq 0xfc21
movb $0x1, %bpl
cmpq $0x2, %r12
jl 0x129f5
decq %r12
testb $0x1, %bpl
je 0x1298f
movq %r13, %rdi
callq 0xd090
movb $0x1, %al
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq