// File: /workspace/github_c_makefile_star2_O1/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 $0x38, %rsp
cmpl $0x2, 0x18(%rdi)
je 0xdbd2
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 0xe485
cmpq %r12, %rax
jge 0xdb17
movq 0x8(%rbx), %rsi
testq %rsi, %rsi
jne 0xdadf
movq %r12, %rax
orq %r14, %rax
jne 0xdadf
movq %rbp, %rdi
callq 0xe2bc
movq %rbx, %rdi
movq %rax, %rsi
callq 0xf1e3
jmp 0xdb17
movq %rsi, %rax
xorq %r14, %rax
orq %r12, %rax
jne 0xdbd2
incq %rsi
movq %rbx, %rdi
callq 0xf1e3
testb %al, %al
je 0xdbd2
movl $0x1, %edi
movl $0x1, %esi
callq 0xd380
movq (%rbx), %rcx
movq %rax, (%rcx,%r14,8)
movq %rbp, %rdi
callq 0xe2bc
testq %rax, %rax
je 0xdbe3
movq %rax, %r13
cmpq $0x1, %rax
jne 0xdbe7
movq (%rbx), %rax
movq (%rax,%r14,8), %r15
movq %rbp, %rdi
callq 0xd210
movq %rax, %r13
movq %r15, %rdi
callq 0xd210
movq %r13, 0x8(%rsp)
movq %rax, 0x10(%rsp)
leaq (%rax,%r13), %rsi
incq %rsi
movq %r15, %rdi
callq 0xd520
movq %rax, %r13
testq %rax, %rax
setne %al
je 0xdbd4
movq 0x8(%rsp), %rax
addq %rax, 0x10(%rsp)
movq %r13, %rdi
movq %r12, %rsi
callq 0xe5be
movq %rax, %r15
movq 0x8(%rsp), %r12
addq %rax, %r12
movq %rax, %rdi
callq 0xd210
movq %r12, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0xd580
movq %r15, %rdi
movq %rbp, %rsi
movq 0x8(%rsp), %rdx
callq 0xd420
movq 0x10(%rsp), %rax
movb $0x0, (%r13,%rax)
movq (%rbx), %rax
movq %r13, (%rax,%r14,8)
movb $0x1, %al
jmp 0xddc6
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, %al
jmp 0xdbd4
movq 0x8(%rbx), %r15
leaq (%r15,%r13), %rsi
decq %rsi
movq %rbx, %rdi
callq 0xf1e3
testb %al, %al
je 0xdbd4
movq %rbp, 0x8(%rsp)
movl %eax, 0x10(%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, 0x30(%rsp)
leaq (%rax,%r12), %r15
movq %r15, %rdi
callq 0xd210
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0xdc5d
movq %r15, %rdi
callq 0xd640
movq %rax, 0x18(%rsp)
jmp 0xdc66
movq $0x0, 0x18(%rsp)
movq 0x8(%rsp), %rdi
movl $0xa, %esi
callq 0xd230
testq %rax, %rax
je 0xddd2
movq %rax, %r13
movq %rax, %r15
subq 0x8(%rsp), %r15
leaq (%r15,%r12), %rax
movq %rax, 0x28(%rsp)
leaq (%r15,%r12), %rsi
incq %rsi
movq 0x30(%rsp), %rdi
callq 0xd520
movq (%rbx), %rcx
movq %rax, (%rcx,%r14,8)
movq 0x8(%rsp), %rax
cmpb $0xa, (%rax)
je 0xdccb
movq (%rbx), %rax
addq (%rax,%r14,8), %r12
movq %r12, %rdi
movq 0x8(%rsp), %rsi
movq %r15, %rdx
callq 0xd420
movq (%rbx), %rax
movq (%rax,%r14,8), %rax
movq 0x28(%rsp), %rcx
movb $0x0, (%rax,%rcx)
incq %r13
movq %r13, %rdi
movl $0xa, %esi
callq 0xd230
testq %rax, %rax
je 0xdd51
movq %rax, %r14
movq 0x20(%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 0xdcf8
jmp 0xdd56
movq 0x20(%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 0x18(%rsp), %r13
testq %r13, %r13
je 0xddb6
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)
movl 0x10(%rsp), %eax
movl $0x1, 0x18(%rbx)
jmp 0xdbd4
leaq 0x20892(%rip), %rdi     # 0x2e66b
leaq 0x20898(%rip), %rsi     # 0x2e678
leaq 0x20896(%rip), %rcx     # 0x2e67d
movl $0x310, %edx            # imm = 0x310
callq 0xd280