// File: /workspace/github_c_makefile_star2_O3/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 %rdx, 0x18(%rsp)
movq %rdi, %rbx
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, (%rdi)
movq $-0x1, 0x10(%rdi)
movq %rcx, 0x10(%rsp)
testq %rcx, %rcx
js 0xeb14
movq %rsi, %r13
movq 0x10(%rsp), %rax
cmpq %rax, 0x8(%rsi)
setle %al
cmpq $0x0, 0x18(%rsp)
sets %cl
orb %al, %cl
jne 0xeb14
movq (%r13), %rax
movq 0x10(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0xe445
cmpq 0x18(%rsp), %rax
jl 0xeb14
movq 0x10(%rsp), %r15
movq %rbx, 0x8(%rsp)
movq %r13, 0x20(%rsp)
movq (%r13), %rax
movq (%rax,%r15,8), %rdi
cmpb $0x0, (%rdi)
je 0xeae5
callq 0xd640
movq %rax, %rbp
movq %rax, %rdi
callq 0xd210
movq %rax, %r13
movq $-0x1, %r14
xorl %r12d, %r12d
xorl %ebx, %ebx
cmpq %r13, %rbx
jge 0xeace
leaq (%rbx,%rbp), %rdx
leaq 0x30(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x1f430
movq 0x30(%rsp), %rsi
testq %rsi, %rsi
jne 0xeaf7
movq 0x38(%rsp), %rax
cmpq $-0x1, %rax
je 0xeace
addq %rbx, %rax
cmpq 0x10(%rsp), %r15
jne 0xeab7
cmpq 0x18(%rsp), %rax
jge 0xeace
movq 0x40(%rsp), %r12
leaq (%r12,%rax), %rbx
movq %rax, %r14
testq %r12, %r12
jne 0xea79
xorl %r12d, %r12d
movq %rax, %r14
movq 0x8(%rsp), %rbx
movq %rbp, %rdi
callq 0xd090
testq %r14, %r14
movq 0x20(%rsp), %r13
jns 0xeb26
leaq -0x1(%r15), %rax
testq %r15, %r15
movq %rax, %r15
jg 0xea49
jmp 0xeb14
leaq 0x1eb51(%rip), %rdi     # 0x2d64f
xorl %eax, %eax
callq 0xd954
movq 0x30(%rsp), %rdi
callq 0xd090
movq 0x8(%rsp), %rbx
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, (%rbx)
movq %r15, 0x8(%rbx)
movq %r12, 0x10(%rbx)
jmp 0xeb14