// File: /workspace/github_c_makefile_star2_O3/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 $0x28, %rsp
testq %rdx, %rdx
js 0xf64c
movq %rcx, %r13
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r12
cmpq %rdx, 0x8(%rdi)
setle %al
testq %rsi, %rsi
sets %cl
orb %al, %cl
jne 0xf64c
movq (%r12), %rax
movq (%rax,%rbx,8), %rdi
callq 0xe445
cmpq %r14, %rax
jge 0xf661
xorl %r15d, %r15d
movq %r15, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r12), %rax
movq (%rax,%rbx,8), %rdi
movq %r14, %rsi
callq 0xe577
movq %rax, %rbp
movq %rax, %rdi
callq 0xe445
movq %rax, %r15
movq %rbp, %rdi
callq 0xd210
cmpq %r13, %r15
jge 0xf6bd
incq %r15
cmpq %r13, %r15
jne 0xf6e4
movq %rax, %rdi
addq $0x2, %rdi
movq %rax, %rbx
callq 0xd4c0
movq %rax, %r15
movq %rax, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0xd0b0
movw $0xa, (%r15,%rbx)
jmp 0xf64f
movq %rbp, %rdi
movq %r13, %rsi
callq 0xe577
subq %rbp, %rax
movq %rbp, %rdi
movq %rax, %rsi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xd360
leaq 0x1(%rbx), %r14
cmpq 0x8(%r12), %r14
jge 0xf71b
movq %rbp, 0x20(%rsp)
incq %rax
movq %rax, 0x18(%rsp)
movq (%r12), %rax
movq 0x8(%rax,%rbx,8), %rdi
callq 0xe445
leaq (%rax,%r15), %rbp
incq %rbp
cmpq %r13, %rbp
jle 0xf735
incq %rax
jmp 0xf789
leaq 0x1f7d0(%rip), %rdi     # 0x2eef2
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xd640
leaq 0x2(%rbx), %r14
xorl %r15d, %r15d
movq (%r12), %rax
movq %rax, 0x10(%rsp)
movq -0x8(%rax,%r14,8), %rdi
callq 0xd210
incq %rax
cmpq %r13, %rbp
je 0xf7ac
cmpq 0x8(%r12), %r14
jge 0xf64f
addq %rax, 0x18(%rsp)
movq 0x10(%rsp), %rax
movq (%rax,%r14,8), %rdi
callq 0xe445
addq %rax, %rbp
incq %rbp
incq %r14
cmpq %r13, %rbp
jle 0xf73c
incq %rax
decq %r14
movq (%r12), %rcx
movq (%rcx,%r14,8), %rdi
addq %r13, %rax
subq %rbp, %rax
movq %rax, %rsi
callq 0xe577
movq (%r12), %rcx
movq %rcx, 0x10(%rsp)
subq (%rcx,%r14,8), %rax
movq 0x18(%rsp), %rcx
leaq (%rax,%rcx), %r13
leaq (%rax,%rcx), %rdi
incq %rdi
callq 0xd4c0
movq %rax, %r15
movq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0xd210
movq %rax, %r12
movq %r15, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0xd420
movq %r13, %rax
cmpq %r13, %r12
jge 0xf87c
movb $0xa, (%r15,%r12)
leaq 0x1(%r12), %rbp
cmpq %rax, %rbp
jge 0xf87c
movq %r15, %r14
addq %r12, %r14
movq 0x10(%rsp), %rcx
leaq (%rcx,%rbx,8), %r13
addq $0x8, %r13
movq %rax, 0x8(%rsp)
incq %r14
movq (%r13), %r12
movq %r12, %rdi
callq 0xd210
movq %rax, %rbx
movq 0x8(%rsp), %rax
addq %rbx, %rbp
cmpq %rax, %rbp
jg 0xf863
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0xd420
movq %rbx, %rax
addq %r14, %rax
movb $0xa, (%r14,%rbx)
incq %rbp
addq $0x8, %r13
movq %rax, %r14
movq 0x8(%rsp), %rax
cmpq %rax, %rbp
jl 0xf815
jmp 0xf87c
addq %rax, %rbx
subq %rbp, %rbx
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0xd420
movq 0x8(%rsp), %rax
movb $0x0, (%r15,%rax)
jmp 0xf64f