// File: /workspace/github_c_makefile_star2_O3/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 $0x48, %rsp
movq 0x18(%rdi), %rax
testq %rax, %rax
js 0x126db
movq %rcx, %r14
movq %rdx, %r13
movq %rsi, %r15
movq (%rdi), %rcx
leaq (%rax,%rax,2), %rax
movq (%rcx,%rax,8), %rbp
movq %rdi, %r12
movq %rbp, %rdi
callq 0xd210
movq %rax, %rbx
movq %r15, 0x28(%rsp)
movq (%r15), %rax
movq 0x8(%r14), %rcx
movq %rcx, 0x20(%rsp)
addq (%rax,%rcx,8), %r13
movq %r13, %rdi
callq 0xd210
movq %r14, %r15
xorl %r14d, %r14d
subq %rbx, %rax
cmovleq %r14, %rax
addq %rax, %r13
movq %rbp, %rdi
movq %r13, %rsi
callq 0xd390
testl %eax, %eax
je 0x127f3
movq %rbp, %rdi
callq 0xd640
movq %rax, %r14
movq %rax, %rdi
callq 0xd210
movq %rax, %r13
movq (%r15), %rbp
movq %r12, 0x30(%rsp)
movq 0x10(%r12), %rdi
testq %rdi, %rdi
movq %r15, %rbx
je 0x126e3
movq %r14, 0x18(%rsp)
callq 0xd210
movq %rax, %r14
movq %rax, %rcx
negq %rcx
movq 0x28(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
movq 0x20(%rsp), %rdx
callq 0xee7a
movq %rax, %rbp
movq %rdx, %r12
testq %r14, %r14
jle 0x126f1
movups (%rbx), %xmm0
movups %xmm0, (%rsp)
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r14, %rcx
movq %rbx, %r8
xorl %r9d, %r9d
callq 0xfab0
movb $0x1, %al
jmp 0x126f3
xorl %r14d, %r14d
jmp 0x127f3
xorl %eax, %eax
movq 0x28(%rsp), %r15
movq 0x20(%rsp), %r12
jmp 0x126f8
xorl %eax, %eax
movq 0x18(%rsp), %r14
movq %rbp, %rcx
addq %r13, %rcx
movq %rcx, 0x38(%rsp)
movq %r12, 0x40(%rsp)
testq %r13, %r13
jle 0x1273d
movq 0x30(%rsp), %rcx
cmpq $0x0, 0x10(%rcx)
setne %cl
andb %cl, %al
movups 0x38(%rsp), %xmm0
movups %xmm0, (%rsp)
movzbl %al, %r9d
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %r12, %rcx
movq %rbx, %r8
callq 0xf8ee
movq (%r15), %rax
movq (%rbx), %rdx
movq %rbx, 0x28(%rsp)
movq 0x8(%rbx), %rcx
movq %rcx, 0x20(%rsp)
movq (%rax,%rcx,8), %rbx
movq %rdx, 0x18(%rsp)
addq %rdx, %rbx
movq %rbx, %rdi
callq 0xd210
movq %rax, %r12
movq %rbx, %rdi
callq 0xd640
movq %rax, %rbp
testq %r12, %r12
jle 0x127e8
movq 0x30(%rsp), %rax
movq 0x8(%rax), %r13
movb $0x0, (%rbp,%r12)
testq %r13, %r13
jle 0x127af
movq 0x30(%rsp), %rax
movq (%rax), %r14
movq %r13, %rbx
movq (%r14), %rdi
movq %rbp, %rsi
callq 0x125a1
testb %al, %al
jne 0x127bf
addq $0x18, %r14
decq %rbx
jne 0x12797
leaq -0x1(%r12), %rax
cmpq $0x1, %r12
movq %rax, %r12
jg 0x12781
jmp 0x127e8
movups 0x38(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r15, %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq %r12, %rcx
movq 0x28(%rsp), %r8
movl $0x1, %r9d
callq 0xfab0
movq %rbp, %rdi
callq 0xd090
movb $0x1, %r14b
movl %r14d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq