// File: /workspace/github_c_makefile_star2_O0/justy989[P]ce/ce.c
// Function: ce_buffer_dupe_string
char* ce_buffer_dupe_string(CeBuffer_t* buffer, CePoint_t point, int64_t length){
     if(!ce_buffer_point_is_valid(buffer, point)) return NULL;

     char* start = ce_utf8_iterate_to(buffer->lines[point.y], point.x);
     // NOTE: would a function that returns both utf8 len and byte len be helpful here?
     int64_t buffer_utf8_length = ce_utf8_strlen(start) + 1;
     int64_t real_length = strlen(start) + 1;

     // exit early if the whole string is just on this line
     if(buffer_utf8_length > length){
          char* end = ce_utf8_iterate_to(start, length);
          return ce_strndup(start, end - start);
     }else if(buffer_utf8_length == length){
          char* new_string = malloc(real_length + 1);
          strncpy(new_string, start, real_length - 1);
          new_string[real_length - 1] = CE_NEWLINE;
          new_string[real_length] = 0;
          return new_string;
     }

     // calculate how big of an array we need to allocate for the dupe
     int64_t current_line = point.y + 1;

     // this means we asked for a string passed the length of the buffer, starting at the end, just return an empty string
     if(current_line >= buffer->line_count) return strdup("");

     while(true){
          int64_t line_utf8_length = ce_utf8_strlen(buffer->lines[current_line]) + 1;
          buffer_utf8_length += line_utf8_length;
          if(buffer_utf8_length > length){
               int64_t diff = buffer_utf8_length - length;
               char* end_of_dupe = ce_utf8_iterate_to(buffer->lines[current_line], line_utf8_length - diff);
               real_length += end_of_dupe - buffer->lines[current_line];
               break;
          }

          real_length += strlen(buffer->lines[current_line]) + 1;
          if(buffer_utf8_length == length) break;
          current_line++;
          if(current_line >= buffer->line_count) return NULL; // not enough length in the buffer
     }

     // alloc
     char* dupe = malloc(real_length + 1);
     char* itr = dupe;

     // copy in the first line
     int64_t copy_length = strlen(start);
     memcpy(itr, start, copy_length);
     itr += copy_length;

     if(copy_length < real_length){
          // append newline
          *itr = CE_NEWLINE;
          itr++;
          copy_length++; // account for newline

          // loop over each line again from the beginning
          current_line = point.y + 1;
          while(copy_length < real_length){
               int64_t line_length = strlen(buffer->lines[current_line]);
               copy_length += line_length;

               // just copy in the rest of the characters
               if(copy_length > real_length){
                    int64_t diff = copy_length - real_length;
                    memcpy(itr, buffer->lines[current_line], line_length - diff);
                    break;
               }

               // copy in the whole line
               memcpy(itr, buffer->lines[current_line], line_length);
               itr += line_length;

               // append a newline
               *itr = CE_NEWLINE;
               itr++;
               copy_length++;
               current_line++;
          }
     }

     dupe[real_length] = 0;
     return dupe;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rsi, -0x18(%rbp)
movq %rdx, -0x10(%rbp)
movq %rdi, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x10(%rbp), %rdx
callq 0xeba0
cmpq $0x0, %rax
jne 0x1093f
movq $0x0, -0x8(%rbp)
jmp 0x10ca7
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x18(%rbp), %rsi
callq 0xeca0
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0xea70
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rdi
callq 0xd200
addq $0x1, %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x28(%rbp), %rax
jle 0x109b5
movq -0x30(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0xeca0
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x30(%rbp), %rax
subq %rax, %rsi
callq 0x10cc0
movq %rax, -0x8(%rbp)
jmp 0x10ca7
movq -0x38(%rbp), %rax
cmpq -0x28(%rbp), %rax
jne 0x10a0e
movq -0x40(%rbp), %rdi
addq $0x1, %rdi
callq 0xd4a0
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x40(%rbp), %rdx
subq $0x1, %rdx
callq 0xd0b0
movq -0x50(%rbp), %rax
movq -0x40(%rbp), %rcx
subq $0x1, %rcx
movb $0xa, (%rax,%rcx)
movq -0x50(%rbp), %rax
movq -0x40(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x50(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x10ca7
jmp 0x10a10
movq -0x10(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0x20(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jl 0x10a3f
leaq 0x33478(%rip), %rdi     # 0x43ea9
callq 0xd620
movq %rax, -0x8(%rbp)
jmp 0x10ca7
jmp 0x10a41
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0xea70
addq $0x1, %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x28(%rbp), %rax
jle 0x10abf
movq -0x38(%rbp), %rax
subq -0x28(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x60(%rbp), %rsi
subq -0x68(%rbp), %rsi
callq 0xeca0
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq -0x20(%rbp), %rcx
movq (%rcx), %rcx
movq -0x58(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
subq %rcx, %rax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x10b17
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0xd200
addq $0x1, %rax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x28(%rbp), %rax
jne 0x10aeb
jmp 0x10b17
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0x20(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jl 0x10b12
movq $0x0, -0x8(%rbp)
jmp 0x10ca7
jmp 0x10a41
movq -0x40(%rbp), %rdi
addq $0x1, %rdi
callq 0xd4a0
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x30(%rbp), %rdi
callq 0xd200
movq %rax, -0x88(%rbp)
movq -0x80(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x88(%rbp), %rdx
callq 0xd400
movq -0x88(%rbp), %rax
addq -0x80(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x88(%rbp), %rax
cmpq -0x40(%rbp), %rax
jge 0x10c93
movq -0x80(%rbp), %rax
movb $0xa, (%rax)
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
movq -0x10(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
movq -0x88(%rbp), %rax
cmpq -0x40(%rbp), %rax
jge 0x10c91
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0xd200
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
addq -0x88(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
cmpq -0x40(%rbp), %rax
jle 0x10c2d
movq -0x88(%rbp), %rax
subq -0x40(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x80(%rbp), %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movq -0x90(%rbp), %rdx
subq -0x98(%rbp), %rdx
callq 0xd400
jmp 0x10c91
movq -0x80(%rbp), %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movq -0x90(%rbp), %rdx
callq 0xd400
movq -0x90(%rbp), %rax
addq -0x80(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movb $0xa, (%rax)
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x10ba5
jmp 0x10c93
movq -0x78(%rbp), %rax
movq -0x40(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x78(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)