// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdx, -0x18(%rbp)
movq %rcx, -0x10(%rbp)
movq %rdi, -0x20(%rbp)
movq %rsi, -0x28(%rbp)
movq -0x20(%rbp), %rax
cmpl $0x2, 0x18(%rax)
jne 0xda9e
movb $0x0, -0x1(%rbp)
jmp 0xe041
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x10(%rbp), %rdx
callq 0xeba0
cmpq $0x0, %rax
jne 0xdb70
movq -0x20(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0xdb0b
movq $0x0, -0x38(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x30(%rbp), %rcx
callq 0xfec0
testb $0x1, %al
jne 0xdaef
jmp 0xdb0b
movq -0x28(%rbp), %rdi
callq 0xe6c0
movq %rax, -0x40(%rbp)
movq -0x20(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0xff00
jmp 0xdb6e
movq -0x10(%rbp), %rax
movq -0x20(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0xdb63
cmpq $0x0, -0x18(%rbp)
jne 0xdb63
movq -0x20(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rsi
addq $0x1, %rsi
callq 0xff00
testb $0x1, %al
jne 0xdb42
movb $0x0, -0x1(%rbp)
jmp 0xe041
movl $0x1, %esi
movq %rsi, %rdi
callq 0xd360
movq %rax, %rdx
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0xdb6c
movb $0x0, -0x1(%rbp)
jmp 0xe041
jmp 0xdb6e
jmp 0xdb70
movq -0x28(%rbp), %rdi
callq 0xe6c0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0xdb8d
movb $0x1, -0x1(%rbp)
jmp 0xe041
cmpq $0x1, -0x48(%rbp)
jne 0xdc75
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x50(%rbp)
movq -0x28(%rbp), %rdi
callq 0xd200
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rdi
callq 0xd200
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rax
addq -0x60(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x50(%rbp), %rdi
movq -0x68(%rbp), %rsi
addq $0x1, %rsi
callq 0xd500
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0xdbf6
movb $0x0, -0x1(%rbp)
jmp 0xe041
movq -0x50(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0xeca0
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
addq -0x58(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x70(%rbp), %rdi
callq 0xd200
movq %rax, -0x80(%rbp)
movq -0x78(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x80(%rbp), %rdx
callq 0xd560
movq -0x70(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x58(%rbp), %rdx
callq 0xd400
movq -0x50(%rbp), %rax
movq -0x68(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x50(%rbp), %rdx
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x20(%rbp), %rax
movl $0x1, 0x18(%rax)
movb $0x1, -0x1(%rbp)
jmp 0xe041
jmp 0xdc77
movq -0x48(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x88(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x20(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rsi
addq -0x88(%rbp), %rsi
callq 0xff00
testb $0x1, %al
jne 0xdcba
movb $0x0, -0x1(%rbp)
jmp 0xe041
movq -0x10(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x98(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x98(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
movq -0x88(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0xa8(%rbp)
movq -0x90(%rbp), %rax
subq -0x98(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xa8(%rbp), %rdi
movq -0xa0(%rbp), %rsi
movq -0xb0(%rbp), %rdx
shlq $0x3, %rdx
callq 0xd560
movq $0x0, -0xb8(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
addq -0x18(%rbp), %rdi
callq 0xd200
movq %rax, -0xc0(%rbp)
cmpq $0x0, -0xc0(%rbp)
je 0xdd87
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
addq -0x18(%rbp), %rdi
callq 0xd620
movq %rax, -0xb8(%rbp)
movq -0x28(%rbp), %rdi
movl $0xa, %esi
callq 0xd220
movq %rax, -0xc8(%rbp)
cmpq $0x0, -0xc8(%rbp)
je 0xdda8
jmp 0xddc7
leaq 0x34858(%rip), %rdi     # 0x42607
leaq 0x3485e(%rip), %rsi     # 0x42614
movl $0x310, %edx            # imm = 0x310
leaq 0x34857(%rip), %rcx     # 0x42619
callq 0xd270
movq -0xc8(%rbp), %rax
movq -0x28(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0xd0(%rbp)
movq -0x18(%rbp), %rax
addq -0xd0(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0xd8(%rbp), %rsi
addq $0x1, %rsi
callq 0xd500
movq %rax, %rdx
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
je 0xde4e
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
addq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0xd0(%rbp), %rdx
callq 0xd400
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0xd8(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0xc8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
movl $0xa, %esi
callq 0xd220
movq %rax, -0xc8(%rbp)
movq -0x10(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xe0(%rbp)
cmpq $0x0, -0xc8(%rbp)
je 0xdf62
movq -0xc8(%rbp), %rax
movq -0x28(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rsi
addq $0x1, %rsi
movl $0x1, %edi
callq 0xd360
movq %rax, %rdx
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0xe0(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0xe0(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x28(%rbp), %rsi
movq -0xd8(%rbp), %rdx
callq 0xd400
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0xe0(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0xd8(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0xc8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
movl $0xa, %esi
callq 0xd220
movq %rax, -0xc8(%rbp)
movq -0xe0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xe0(%rbp)
jmp 0xde9b
movq -0x28(%rbp), %rdi
callq 0xd200
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
addq -0xc0(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rsi
addq $0x1, %rsi
movl $0x1, %edi
callq 0xd360
movq %rax, %rdx
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0xe0(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0xe0(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x28(%rbp), %rsi
movq -0xd8(%rbp), %rdx
callq 0xd400
cmpq $0x0, -0xb8(%rbp)
je 0xe015
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0xe0(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
addq -0xd8(%rbp), %rdi
movq -0xb8(%rbp), %rsi
movq -0xc0(%rbp), %rdx
callq 0xd400
movq -0xb8(%rbp), %rdi
callq 0xd090
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0xe0(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0xe8(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x20(%rbp), %rax
movl $0x1, 0x18(%rax)
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xf0, %rsp
popq %rbp
retq
nop