// File: /workspace/github_c_makefile_star2_O1/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
testq %rdx, %rdx
js 0xf7db
movq %rcx, %r13
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
cmpq %rdx, 0x8(%rdi)
setle %al
testq %rsi, %rsi
sets %cl
orb %al, %cl
jne 0xf7db
movq (%r14), %rax
movq (%rax,%rbx,8), %rdi
callq 0xe485
cmpq %r15, %rax
jge 0xf7f0
xorl %r13d, %r13d
movq %r13, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r14), %rax
movq (%rax,%rbx,8), %rdi
movq %r15, %rsi
callq 0xe5be
movq %rax, %r12
movq %rax, %rdi
callq 0xe485
movq %rax, %rbp
movq %r12, %rdi
callq 0xd210
cmpq %r13, %rbp
jge 0xf84c
movq %rax, %r15
incq %rbp
cmpq %r13, %rbp
jne 0xf873
movq %r15, %rdi
addq $0x2, %rdi
callq 0xd4c0
movq %rax, %r13
movq %rax, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0xd0b0
movw $0xa, (%r13,%r15)
jmp 0xf7de
movq %r12, %rdi
movq %r13, %rsi
callq 0xe5be
subq %r12, %rax
movq %r12, %rdi
movq %rax, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xd360
incq %rbx
cmpq 0x8(%r14), %rbx
jge 0xf9c1
movq %r12, 0x8(%rsp)
incq %r15
movq %rbx, %r12
movq (%r14), %rax
movq (%rax,%r12,8), %rdi
callq 0xe485
addq %rax, %rbp
incq %rbp
movq (%r14), %rcx
movq (%rcx,%r12,8), %rdi
cmpq %r13, %rbp
jle 0xf8c8
incq %rax
addq %r13, %rax
subq %rbp, %rax
movq %rax, %rsi
callq 0xe5be
movq (%r14), %rcx
subq (%rcx,%r12,8), %rax
movl $0x3, %ecx
jmp 0xf8e6
callq 0xd210
incq %rax
movl $0x3, %ecx
cmpq %r13, %rbp
je 0xf8e6
incq %r12
xorl %ecx, %ecx
cmpq 0x8(%r14), %r12
setge %cl
addq %rax, %r15
testl %ecx, %ecx
je 0xf88b
cmpl $0x3, %ecx
jne 0xf7db
leaq 0x1(%r15), %rdi
callq 0xd4c0
movq %rax, %r13
movq 0x8(%rsp), %r12
movq %r12, %rdi
callq 0xd210
movq %rax, %rbp
movq %r13, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0xd420
cmpq %r15, %rbp
jge 0xf9b6
movb $0xa, (%r13,%rbp)
leaq 0x1(,%rbp), %rax
addq %r13, %rax
movq %rax, 0x8(%rsp)
incq %rbp
cmpq %r15, %rbp
jge 0xf9b6
movq (%r14), %rax
movq (%rax,%rbx,8), %rdi
movq %rdi, 0x10(%rsp)
callq 0xd210
movq %rax, %r12
addq %rax, %rbp
cmpq %r15, %rbp
jle 0xf980
addq %r15, %r12
subq %rbp, %r12
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %r12, %rdx
callq 0xd420
movq %rbp, %rax
jmp 0xf9ae
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %r12, %rdx
callq 0xd420
movq 0x8(%rsp), %rax
movb $0xa, (%rax,%r12)
addq %r12, %rax
incq %rax
movq %rax, 0x8(%rsp)
leaq 0x1(%rbp), %rax
incq %rbx
cmpq %r15, %rbp
movq %rax, %rbp
jle 0xf942
movb $0x0, (%r13,%r15)
jmp 0xf7de
leaq 0x2052e(%rip), %rdi     # 0x2fef6
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xd640