// File: /workspace/github_c_makefile_star2_O3/justy989[P]ce/ce.c
// Function: ce_buffer_insert_string
bool ce_buffer_insert_string(CeBuffer_t* buffer, const char* string, CePoint_t point){
     if(buffer->status == CE_BUFFER_STATUS_READONLY) return false;

     if(!ce_buffer_point_is_valid(buffer, point)){
          if(buffer->line_count == 0 && ce_points_equal(point, (CePoint_t){0, 0})){
               int64_t line_count = ce_util_count_string_lines(string);
               buffer_realloc_lines(buffer, line_count);
          }else if(point.y == buffer->line_count && point.x == 0){
               // allow inserting a string after a buffer by resizing
               if(!buffer_realloc_lines(buffer, buffer->line_count + 1)) return false;
               buffer->lines[point.y] = calloc(1, 1); // allocate an empty string
          }else{
               return false;
          }
     }

     int64_t string_lines = ce_util_count_string_lines(string);
     if(string_lines == 0){
          return true; // sure, yeah, we inserted that empty string
     }else if(string_lines == 1){
          char* line = buffer->lines[point.y];
          size_t insert_len = strlen(string);
          size_t existing_len = strlen(line);
          size_t total_len = insert_len + existing_len;

          // re-alloc the new size
          line = realloc(line, total_len + 1);
          if(!line) return false;

          // figure out where to move from and to
          char* src = ce_utf8_iterate_to(line, point.x);
          char* dst = src + insert_len;
          size_t src_len = strlen(src);
          memmove(dst, src, src_len);

          // insert the string
          memcpy(src, string, insert_len);

          // tidy up
          line[total_len] = 0;
          buffer->lines[point.y] = line;
          buffer->status = CE_BUFFER_STATUS_MODIFIED;
          return true;
     }

     int64_t shift_lines = string_lines - 1;
     int64_t old_line_count = buffer->line_count;

     // allocate space to fit the buffer plus the new multiline string
     if(!buffer_realloc_lines(buffer, buffer->line_count + shift_lines)){
          return false;
     }

     // shift down all the line pointers
     int64_t first_new_line = point.y + 1;
     char** src_line = buffer->lines + first_new_line;
     char** dst_line = src_line + shift_lines;
     size_t move_count = old_line_count - first_new_line;
     memmove(dst_line, src_line, move_count * sizeof(src_line));

     // save the last part of the first line to stick on the end of the multiline string
     char* end_string = NULL;
     int64_t end_string_len = strlen(buffer->lines[point.y] + point.x);
     if(end_string_len) end_string = strdup(buffer->lines[point.y] + point.x);

     // insert the first line of the string at the point specified
     const char* next_newline = strchr(string, CE_NEWLINE);
     assert(next_newline);
     size_t first_line_len = next_newline - string;
     size_t new_line_len = point.x + first_line_len;
     buffer->lines[point.y] = realloc(buffer->lines[point.y], new_line_len + 1);
     if(*string != CE_NEWLINE){ // if the first character is a newline, there is no first line of the string
          memcpy(buffer->lines[point.y] + point.x, string, first_line_len);
     }
     buffer->lines[point.y][new_line_len] = 0;

     // copy in each of the new lines
     string = next_newline + 1;
     next_newline = strchr(string, CE_NEWLINE);
     int64_t next_line = point.y + 1;
     while(next_newline){
          new_line_len = next_newline - string;
          buffer->lines[next_line] = calloc(1, new_line_len + 1);
          memcpy(buffer->lines[next_line], string, new_line_len);
          buffer->lines[next_line][new_line_len] = 0;
          string = next_newline + 1;
          next_newline = strchr(string, CE_NEWLINE);
          next_line++;
     }

     // copy in the last line
     new_line_len = strlen(string);
     int64_t last_line_len = new_line_len + end_string_len;
     buffer->lines[next_line] = calloc(1, last_line_len + 1);
     memcpy(buffer->lines[next_line], string, new_line_len);

     // attach the end part of the line we inserted into at the end of the last line
     if(end_string){
          memcpy(buffer->lines[next_line] + new_line_len, end_string, end_string_len);
          free(end_string);
     }

     buffer->lines[next_line][last_line_len] = 0;

     buffer->status = CE_BUFFER_STATUS_MODIFIED;
     return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
cmpl $0x2, 0x18(%rdi)
je 0xdc4e
movq %rcx, %r14
movq %rdx, %r12
movq %rsi, %rbp
movq %rdi, %rbx
testq %rcx, %rcx
js 0xdab9
cmpq %r14, 0x8(%rbx)
setle %al
testq %r12, %r12
sets %cl
orb %al, %cl
jne 0xdab9
movq (%rbx), %rax
movq (%rax,%r14,8), %rdi
callq 0xe445
cmpq %r12, %rax
jge 0xdb15
movq 0x8(%rbx), %rax
testq %rax, %rax
jne 0xdadf
movq %r12, %rcx
orq %r14, %rcx
jne 0xdadf
movq %rbp, %rdi
callq 0xe279
movq %rbx, %rdi
movq %rax, %rsi
callq 0xf04a
jmp 0xdb15
xorq %r14, %rax
orq %r12, %rax
jne 0xdc4e
leaq 0x1(%r14), %rsi
movq %rbx, %rdi
callq 0xf04a
testb %al, %al
je 0xdc4e
movl $0x1, %edi
movl $0x1, %esi
callq 0xd380
movq (%rbx), %rcx
movq %rax, (%rcx,%r14,8)
movq %rbp, %rdi
callq 0xe279
movb $0x1, %r15b
testq %rax, %rax
je 0xdc51
movq %rax, %r13
cmpq $0x1, %rax
jne 0xdbdd
movq (%rbx), %rax
movq (%rax,%r14,8), %rax
movq %rax, (%rsp)
movq %rbp, %rdi
callq 0xd210
movq %rax, %r13
movq (%rsp), %rdi
callq 0xd210
movq %r13, 0x8(%rsp)
movq %rax, 0x10(%rsp)
leaq (%rax,%r13), %rsi
incq %rsi
movq (%rsp), %rdi
callq 0xd520
testq %rax, %rax
je 0xdc4e
movq %rax, %r13
movq 0x8(%rsp), %rax
addq %rax, 0x10(%rsp)
movq %r13, %rdi
movq %r12, %rsi
callq 0xe577
movq %rax, %r12
movq %rbp, (%rsp)
movq 0x8(%rsp), %rbp
addq %rax, %rbp
movq %rax, %rdi
callq 0xd210
movq %rbp, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0xd580
movq %r12, %rdi
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
callq 0xd420
movq 0x10(%rsp), %rax
movb $0x0, (%r13,%rax)
movq (%rbx), %rax
movq %r13, (%rax,%r14,8)
jmp 0xddc7
movq 0x8(%rbx), %r15
leaq (%r15,%r13), %rsi
decq %rsi
movq %rbx, %rdi
callq 0xf04a
testb %al, %al
je 0xdc4e
movq %rbp, (%rsp)
decq %r13
leaq 0x8(,%r14,8), %rsi
addq (%rbx), %rsi
leaq 0x1(%r14), %rbp
leaq (%rsi,%r13,8), %rdi
subq %rbp, %r15
shlq $0x3, %r15
movq %r15, %rdx
callq 0xd580
movq (%rbx), %rax
movq (%rax,%r14,8), %rax
movq %rax, 0x20(%rsp)
leaq (%rax,%r12), %r13
movq %r13, %rdi
callq 0xd210
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0xdc63
movq %r13, %rdi
callq 0xd640
movq %rax, 0x10(%rsp)
jmp 0xdc6c
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $0x0, 0x10(%rsp)
movq (%rsp), %rdi
movl $0xa, %esi
callq 0xd230
testq %rax, %rax
je 0xddd3
movq %rax, %r13
movq %rax, %r15
subq (%rsp), %r15
leaq (%r15,%r12), %rax
movq %rax, 0x18(%rsp)
leaq (%r15,%r12), %rsi
incq %rsi
movq 0x20(%rsp), %rdi
callq 0xd520
movq (%rbx), %rcx
movq %rax, (%rcx,%r14,8)
movq (%rsp), %rax
cmpb $0xa, (%rax)
je 0xdccd
movq (%rbx), %rax
addq (%rax,%r14,8), %r12
movq %r12, %rdi
movq (%rsp), %rsi
movq %r15, %rdx
callq 0xd420
movq (%rbx), %rax
movq (%rax,%r14,8), %rax
movq 0x18(%rsp), %rcx
movb $0x0, (%rax,%rcx)
incq %r13
movq %r13, %rdi
movl $0xa, %esi
callq 0xd230
testq %rax, %rax
je 0xdd53
movq %rax, %r14
movq 0x8(%rsp), %r12
movq %r14, %r15
subq %r13, %r15
leaq 0x1(%r15), %rsi
movl $0x1, %edi
callq 0xd380
movq (%rbx), %rcx
movq %rax, (%rcx,%rbp,8)
movq (%rbx), %rax
movq (%rax,%rbp,8), %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0xd420
movq (%rbx), %rax
movq (%rax,%rbp,8), %rax
movb $0x0, (%rax,%r15)
movq %r14, %r13
incq %r13
movq %r13, %rdi
movl $0xa, %esi
callq 0xd230
movq %rax, %r14
incq %rbp
testq %rax, %rax
jne 0xdcfa
jmp 0xdd58
movq 0x8(%rsp), %r12
movq %r13, %rdi
callq 0xd210
movq %rax, %r14
leaq (%rax,%r12), %r15
leaq (%rax,%r12), %rsi
incq %rsi
movl $0x1, %edi
callq 0xd380
movq (%rbx), %rcx
movq %rax, (%rcx,%rbp,8)
movq (%rbx), %rax
movq (%rax,%rbp,8), %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0xd420
movq 0x10(%rsp), %r13
testq %r13, %r13
je 0xddb8
movq (%rbx), %rax
addq (%rax,%rbp,8), %r14
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0xd420
movq %r13, %rdi
callq 0xd090
movq (%rbx), %rax
movq (%rax,%rbp,8), %rax
movb $0x0, (%rax,%r15)
movb $0x1, %r15b
movl $0x1, 0x18(%rbx)
jmp 0xdc51
leaq 0x1f891(%rip), %rdi     # 0x2d66b
leaq 0x1f897(%rip), %rsi     # 0x2d678
leaq 0x1f895(%rip), %rcx     # 0x2d67d
movl $0x310, %edx            # imm = 0x310
callq 0xd280