// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %rbp
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %r13
callq 0xe425
testq %rax, %rax
je 0xf467
movq (%r13), %rax
movq (%rax,%r15,8), %rdi
movq %r14, %rsi
callq 0xe4a6
movq %rax, %rbx
movq %rax, %rdi
callq 0xe38c
movq %rax, %r14
movq %rbx, %rdi
callq 0xd210
cmpq %rbp, %r14
jge 0xf3e8
movq %rax, %r12
incq %r14
cmpq %rbp, %r14
jne 0xf40f
movq %r12, %rdi
addq $0x2, %rdi
callq 0xd4c0
movq %rbx, %rsi
movq %rax, %rbx
movq %rax, %rdi
movq %r12, %rdx
callq 0xd0b0
movw $0xa, (%rbx,%r12)
jmp 0xf469
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xe4a6
subq %rbx, %rax
movq %rbx, %rdi
movq %rax, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xd360
leaq 0x1(%r15), %rax
cmpq 0x8(%r13), %rax
jge 0xf47b
movq %rbx, 0x10(%rsp)
incq %r12
movq (%r13), %rbx
movq %r15, 0x8(%rsp)
addq $0x2, %r15
movq -0x8(%rbx,%r15,8), %rdi
callq 0xe38c
addq %rax, %r14
incq %r14
movq (%r13), %rbx
movq -0x8(%rbx,%r15,8), %rdi
cmpq %rbp, %r14
jg 0xf495
callq 0xd210
incq %rax
cmpq %rbp, %r14
je 0xf4af
cmpq 0x8(%r13), %r15
jge 0xf467
addq %rax, %r12
incq %r15
jmp 0xf42e
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1ca70(%rip), %rdi     # 0x2bef2
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xd640
incq %rax
addq %rbp, %rax
subq %r14, %rax
movq %rax, %rsi
callq 0xe4a6
movq (%r13), %rbx
subq -0x8(%rbx,%r15,8), %rax
leaq (%rax,%r12), %r13
leaq (%rax,%r12), %rdi
incq %rdi
callq 0xd4c0
movq %rax, %r15
movq 0x10(%rsp), %r12
movq %r12, %rdi
callq 0xd210
movq %rax, %r14
movq %r15, (%rsp)
movq %r15, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0xd420
cmpq %r13, %r14
jge 0xf55d
movq (%rsp), %rax
movq %rax, %r15
addq %r14, %r15
movb $0xa, (%rax,%r14)
movq 0x8(%rsp), %rax
leaq (%rbx,%rax,8), %rbp
addq $0x8, %rbp
incq %r14
cmpq %r13, %r14
jge 0xf55d
incq %r15
movq (%rbp), %r12
movq %r12, %rdi
callq 0xd210
movq %rax, %rbx
addq %rax, %r14
cmpq %r13, %r14
jg 0xf549
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0xd420
movq %rbx, %rax
addq %r15, %rax
movb $0xa, (%r15,%rbx)
addq $0x8, %rbp
movq %rax, %r15
jmp 0xf505
addq %r13, %rbx
subq %r14, %rbx
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0xd420
movq (%rsp), %rbx
movb $0x0, (%rbx,%r13)
jmp 0xf469