// File: /workspace/github_c_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq 0x18(%rdi), %rax
testq %rax, %rax
js 0x11f71
movq %rcx, %r12
movq %rdx, %r13
movq %rsi, %r14
movq %rdi, %r15
movq (%rdi), %rcx
imulq $0x18, %rax, %rax
movq (%rcx,%rax), %rbp
movq %rbp, %rdi
callq 0xd210
movq %rax, %rbx
movq %r14, 0x20(%rsp)
movq (%r14), %rax
movq 0x8(%r12), %rcx
movq %rcx, 0x18(%rsp)
addq (%rax,%rcx,8), %r13
movq %r13, %rdi
callq 0xd210
xorl %r14d, %r14d
subq %rbx, %rax
cmovleq %r14, %rax
addq %rax, %r13
movq %rbp, %rdi
movq %r13, %rsi
callq 0xd390
testl %eax, %eax
je 0x12072
movq %rbp, %rdi
callq 0xd640
movq %rax, %r14
movq %rax, %rdi
callq 0xd210
movq %rax, %r13
movq (%r12), %rbp
movq 0x10(%r15), %rdi
testq %rdi, %rdi
movq %r12, %rbx
je 0x11f79
movq %r14, 0x10(%rsp)
callq 0xd210
movq %rax, %r14
movq %rax, %rcx
negq %rcx
movq 0x20(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
movq 0x18(%rsp), %rdx
callq 0xec3a
movq %rax, %rbp
movq %rdx, %r8
testq %r14, %r14
jle 0x11f87
movups (%rbx), %xmm0
movups %xmm0, (%rsp)
movq %r12, %rdi
movq %rbp, %rsi
movq %r8, %rdx
movq %r14, %rcx
movq %r8, %r14
movq %rbx, %r8
xorl %r9d, %r9d
callq 0xf77c
movq %r14, %r8
movb $0x1, %al
jmp 0x11f89
xorl %r14d, %r14d
jmp 0x12072
xorl %eax, %eax
movq 0x20(%rsp), %r12
movq 0x18(%rsp), %r8
jmp 0x11f8e
xorl %eax, %eax
movq 0x10(%rsp), %r14
movq %rbp, %rcx
addq %r13, %rcx
movq %rcx, 0x28(%rsp)
movq %r8, 0x30(%rsp)
testq %r13, %r13
jle 0x11fce
cmpq $0x0, 0x10(%r15)
setne %cl
andb %cl, %al
movups 0x28(%rsp), %xmm0
movups %xmm0, (%rsp)
movzbl %al, %r9d
movq %r12, %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %r8, %rcx
movq %rbx, %r8
callq 0xf5c8
movq (%r12), %rax
movq (%rbx), %r13
movq %rbx, 0x18(%rsp)
movq 0x8(%rbx), %rcx
movq %rcx, 0x10(%rsp)
movq (%rax,%rcx,8), %rbx
addq %r13, %rbx
movq %rbx, %rdi
callq 0xd210
movq %rax, %r12
movq %rbx, %rdi
callq 0xd640
movq %rax, %rbp
xorl %eax, %eax
testq %r12, %r12
jle 0x12067
movb $0x0, (%rbp,%r12)
movq 0x8(%r15), %r14
testq %r14, %r14
cmovleq %rax, %r14
incq %r14
xorl %ebx, %ebx
decq %r14
je 0x1203b
movq (%r15), %rax
movq (%rax,%rbx), %rdi
addq $0x18, %rbx
movq %rbp, %rsi
callq 0x11e38
testb %al, %al
je 0x1201d
jmp 0x12040
decq %r12
jmp 0x12000
movups 0x28(%rsp), %xmm0
movups %xmm0, (%rsp)
pushq $0x1
popq %r9
movq 0x20(%rsp), %rdi
movq %r13, %rsi
movq 0x10(%rsp), %rdx
movq %r12, %rcx
movq 0x18(%rsp), %r8
callq 0xf77c
movq %rbp, %rdi
callq 0xd090
movb $0x1, %r14b
movl %r14d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq