// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x88(%rbp)
movq %rdi, %rax
movq %rax, -0x80(%rbp)
movq %rdx, -0x10(%rbp)
movq %rcx, -0x8(%rbp)
movq %rsi, -0x18(%rbp)
movq %r8, -0x20(%rbp)
movq 0x32d18(%rip), %rax     # 0x42088
movq %rax, (%rdi)
movq 0x32d16(%rip), %rax     # 0x42090
movq %rax, 0x8(%rdi)
movq 0x32d13(%rip), %rax     # 0x42098
movq %rax, 0x10(%rdi)
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x8(%rbp), %rdx
callq 0xeba0
cmpq $0x0, %rax
jne 0xf3a5
jmp 0xf4ff
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x28(%rbp)
movq $-0x1, -0x40(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x38(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpb $0x0, (%rax)
je 0xf4bc
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0xd620
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
callq 0xd200
movq %rax, -0x58(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x58(%rbp), %rax
jge 0xf4b3
movq -0x20(%rbp), %rsi
movq -0x50(%rbp), %rdx
addq -0x30(%rbp), %rdx
leaq -0x70(%rbp), %rdi
callq 0x2aa10
cmpq $0x0, -0x70(%rbp)
je 0xf45c
movq -0x70(%rbp), %rsi
leaq 0x331a4(%rip), %rdi     # 0x425eb
movb $0x0, %al
callq 0xd8f0
movq -0x70(%rbp), %rdi
callq 0xd090
jmp 0xf4ff
cmpq $-0x1, -0x68(%rbp)
je 0xf4a0
movq -0x30(%rbp), %rax
addq -0x68(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x10(%rbp), %rax
jl 0xf485
movq -0x28(%rbp), %rax
cmpq -0x8(%rbp), %rax
jne 0xf485
jmp 0xf4b3
movq -0x78(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0xf49e
jmp 0xf4b3
jmp 0xf4a2
jmp 0xf4b3
movq -0x40(%rbp), %rax
addq -0x48(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0xf412
movq -0x50(%rbp), %rdi
callq 0xd090
cmpq $0x0, -0x40(%rbp)
jl 0xf4e3
movq -0x88(%rbp), %rax
movq -0x40(%rbp), %rcx
movq %rcx, (%rax)
movq -0x38(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x48(%rbp), %rcx
movq %rcx, 0x10(%rax)
jmp 0xf4fd
movq -0x28(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jge 0xf4f8
jmp 0xf4fd
jmp 0xf3b5
jmp 0xf4ff
movq -0x80(%rbp), %rax
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)