// File: /workspace/github_c_makefile_star2_O1/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:
movq %rdi, %rax
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, (%rdi)
movq $-0x1, 0x10(%rdi)
testq %rcx, %rcx
js 0xecb4
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %r13
cmpq %rcx, 0x8(%rsi)
movq %rax, 0x8(%rsp)
setle %al
testq %rdx, %rdx
sets %cl
orb %al, %cl
movq 0x8(%rsp), %rax
jne 0xeca6
movq %r8, %r14
movq (%r13), %rax
movq (%rax,%r15,8), %rdi
callq 0xe485
cmpq %r12, %rax
movq 0x8(%rsp), %rax
jl 0xeca6
movq (%rax), %rcx
movq %rcx, 0x20(%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x10(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x18(%rsp)
movq %r15, (%rsp)
movq %r15, 0x38(%rsp)
movq %r12, 0x30(%rsp)
movq %r13, 0x28(%rsp)
movq (%r13), %rax
movq (%rsp), %rcx
movq (%rax,%rcx,8), %rdi
cmpb $0x0, (%rdi)
je 0xec37
callq 0xd640
movq %rax, %r13
movq %rax, %rdi
callq 0xd210
movq %rax, %r12
movq $-0x1, %rbp
xorl %ebx, %ebx
xorl %r15d, %r15d
cmpq %r12, %r15
jge 0xec2b
leaq (%r15,%r13), %rdx
leaq 0x40(%rsp), %rdi
movq %r14, %rsi
callq 0x1f4a0
movq 0x40(%rsp), %rsi
testq %rsi, %rsi
je 0xebc4
leaq 0x1faa3(%rip), %rdi     # 0x2e64f
xorl %eax, %eax
callq 0xd954
movq 0x40(%rsp), %rdi
callq 0xd090
movl $0x1, %eax
jmp 0xec1e
movq 0x48(%rsp), %rcx
cmpq $-0x1, %rcx
je 0xebf0
addq %r15, %rcx
movq (%rsp), %rax
cmpq 0x38(%rsp), %rax
jne 0xebf7
cmpq 0x30(%rsp), %rcx
jl 0xebf7
xorl %edx, %edx
movl $0x5, %eax
movq %rbp, %rcx
jmp 0xec0d
movl $0x5, %eax
jmp 0xec1e
movq 0x50(%rsp), %rbx
xorl %eax, %eax
testq %rbx, %rbx
setne %dl
sete %al
leal (%rax,%rax,4), %eax
movq %rcx, %rbp
addq %rbx, %rcx
testb %dl, %dl
cmovneq %rcx, %r15
movl $0x0, %ecx
cmovnel %ecx, %eax
testl %eax, %eax
je 0xeb81
cmpl $0x5, %eax
jne 0xec42
movq %r13, %rdi
callq 0xd090
movb $0x1, %al
jmp 0xec44
movq $-0x1, %rbp
xorl %ebx, %ebx
jmp 0xec4d
xorl %eax, %eax
testb %al, %al
movq 0x28(%rsp), %r13
je 0xec69
testq %rbp, %rbp
js 0xec6d
xorl %eax, %eax
movq %rbx, 0x18(%rsp)
movq (%rsp), %rcx
movq %rcx, 0x10(%rsp)
movq %rbp, 0x20(%rsp)
jmp 0xec7f
xorl %eax, %eax
jmp 0xec7f
movq (%rsp), %rax
testq %rax, %rax
leaq -0x1(%rax), %rcx
setg %al
movq %rcx, (%rsp)
testb %al, %al
jne 0xeb4d
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rcx, 0x8(%rax)
movq 0x18(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq 0x20(%rsp), %rcx
movq %rcx, (%rax)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq