// File: /workspace/github_c_makefile_star2_O2/justy989[P]ce/ce.c
// Function: ce_buffer_regex_search_backward
CeRegexSearchResult_t ce_buffer_regex_search_backward(CeBuffer_t* buffer, CePoint_t start, CeRegex_t regex){
     CeRegexSearchResult_t result = {(CePoint_t){-1, -1}, -1};

     if(!ce_buffer_point_is_valid(buffer, start)) return result;

     CePoint_t location = start;

     // loop over each line, backwards
     while(true){
          CePoint_t last_valid_match = {-1, location.y};
          int64_t last_valid_match_len = 0;

          location.x = 0;

          if(buffer->lines[location.y][0]){
               // dupe the line up to the current index
               char* search_str = strdup(buffer->lines[location.y]);
               int64_t search_str_len = strlen(search_str);

               // start at the beginning of the line, find all matches up to the cursor and take that one
               while(location.x < search_str_len){
                    CeRegexResult_t regex_result = ce_regex_match(regex,
                                                                  search_str + location.x);
                    if(regex_result.error_message != NULL){
                         ce_log("ce_regex_match() failed: %s", regex_result.error_message);
                         free(regex_result.error_message);
                         return result;
                    }

                    if(regex_result.match_start != CE_REGEX_NO_MATCH){
                         int64_t match_x = location.x + regex_result.match_start;

                         // if the match is after the start, then stop looking in this line
                         if(match_x >= start.x && location.y == start.y) break;

                         // save the match if we find one
                         last_valid_match.x = match_x;
                         last_valid_match_len = regex_result.match_length;
                         if(last_valid_match_len == 0) break;
                    }else{
                         break;
                    }

                    // update the next location to start after the match
                    location.x = last_valid_match.x + last_valid_match_len;
               }

               free(search_str);
          }

          if(last_valid_match.x >= 0){
               result.point = last_valid_match;
               result.length = last_valid_match_len;
               break;
          }

          location.y--;

          if(location.y < 0) break;
     }

     return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r8, 0x28(%rsp)
movq %rsi, %rbx
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, (%rdi)
movq %rdi, 0x8(%rsp)
orq $-0x1, 0x10(%rdi)
movq %rsi, %rdi
movq %rdx, 0x20(%rsp)
movq %rdx, %rsi
movq %rcx, 0x10(%rsp)
movq %rcx, %rdx
callq 0xe425
testq %rax, %rax
je 0xe964
movq 0x10(%rsp), %r15
movq %rbx, 0x18(%rsp)
movq (%rbx), %rax
movq (%rax,%r15,8), %rdi
cmpb $0x0, (%rdi)
je 0xe93a
callq 0xd640
movq %rax, %rbx
movq %rax, %rdi
callq 0xd210
movq %rax, %r13
xorl %r14d, %r14d
pushq $-0x1
popq %r12
xorl %ebp, %ebp
cmpq %r13, %rbp
jge 0xe928
leaq (%rbx,%rbp), %rdx
leaq 0x30(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x1d69d
movq 0x30(%rsp), %rsi
testq %rsi, %rsi
jne 0xe94c
movq 0x38(%rsp), %rax
cmpq $-0x1, %rax
je 0xe928
cmpq 0x10(%rsp), %r15
sete %cl
addq %rbp, %rax
cmpq 0x20(%rsp), %rax
setge %dl
testb %cl, %dl
jne 0xe928
movq 0x40(%rsp), %r14
leaq (%r14,%rax), %rbp
movq %rax, %r12
testq %r14, %r14
jne 0xe8cd
xorl %r14d, %r14d
movq %rax, %r12
movq %rbx, %rdi
callq 0xd090
testq %r12, %r12
movq 0x18(%rsp), %rbx
jns 0xe978
leaq -0x1(%r15), %rax
testq %r15, %r15
movq %rax, %r15
jg 0xe8a1
jmp 0xe964
leaq 0x1bcfc(%rip), %rdi     # 0x2a64f
xorl %eax, %eax
callq 0xd8e5
movq 0x30(%rsp), %rdi
callq 0xd090
movq 0x8(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rax
movq %r12, (%rax)
movq %r15, 0x8(%rax)
movq %r14, 0x10(%rax)
jmp 0xe964