// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Correct.cpp
// Function: adjust_base_coordinates(overlap_region*, k_mer_hit*, long, ul_ov_t*, long, long, long, long)
int64_t adjust_base_coordinates(overlap_region *z, k_mer_hit *ch_a, int64_t ch_n, 
ul_ov_t *res, int64_t wl, int64_t ql, int64_t tl, int64_t ch_i)
{
    res->ts = res->te = (uint32_t)-1; 
    res->sec = 3; res->qn = res->tn = (uint32_t)-1; //semi-global
    if(ch_n == 0) return ch_i; 
    int64_t i = ch_i, ibeg, iend; uint64_t s = res->qs, e = res->qe; 
    for (; i >= 0 && ch_a[i].self_offset >= s; i--); if(i < 0) i = 0;
    for (; i < ch_n && ch_a[i].self_offset < s; i++); 
    if((i >= 0) && ((ch_a[i].self_offset > s) || (i >= ch_n))) i--; ibeg = i; ///if ch_a[i].self_offset == s, do nothing
    for (i = (i>=0?i:0); i < ch_n && ch_a[i].self_offset < e; i++); iend = i;
    ch_i = i;///ch_i must be here
    ///ibeg might be < 0, iend might be == ch_n
    ///1. [s, e) contain anchors
    ///2. anchors contain [s, e)
    // fprintf(stderr, "[M::%s::] ibeg::%ld, iend::%ld, ch_n::%ld\n", __func__, ibeg, iend, ch_n);
    // if(ibeg >= 0) {
    //     fprintf(stderr, "[M::%s::] ch_a[ibeg].self_offset::%u, ch_a[ibeg].offset::%u\n", 
    //                                     __func__, ch_a[ibeg].self_offset, ch_a[ibeg].offset);
    // }
    // if(ibeg+1 >= 0) {
    //     fprintf(stderr, "[M::%s::] ch_a[ibeg+1].self_offset::%u, ch_a[ibeg+1].offset::%u\n", 
    //                                     __func__, ch_a[ibeg+1].self_offset, ch_a[ibeg+1].offset);
    // }

    // if(iend < ch_n) {
    //     fprintf(stderr, "[M::%s::] ch_a[iend].self_offset::%u, ch_a[iend].offset::%u\n", 
    //                                     __func__, ch_a[iend].self_offset, ch_a[iend].offset);
    // } 
    // if(iend > 0) {
    //     fprintf(stderr, "[M::%s::] ch_a[iend-1].self_offset::%u, ch_a[iend-1].offset::%u\n", 
    //                                     __func__, ch_a[iend-1].self_offset, ch_a[iend-1].offset);
    // }

    // if(z->y_id == 109111) {
    //     int64_t di;
    //     fprintf(stderr, "[M::%s] ch_n::%ld, s::%lu, e::%lu, ibeg::%ld, iend::%ld\n", __func__, ch_n, s, e, ibeg, iend);
    //     for (di = 0; di < ch_n; di++) {
    //         fprintf(stderr, "[M::%s::di->%ld] qoff::%u, toff::%u\n", __func__, di, 
    //                                 ch_a[di].self_offset, ch_a[di].offset);
    //     }        
    // }

    if(ibeg >= 0) {
        res->qs = ch_a[ibeg].self_offset;
        res->ts = ch_a[ibeg].offset;
        res->qn = ibeg;
    } else {//extension to left
        res->qs = 0; res->qn = (uint32_t)-1;
    }

    if(iend < ch_n) {
        res->qe = ch_a[iend].self_offset;
        res->te = ch_a[iend].offset;
        res->tn = iend;
    } else {//extension to right
        res->qe = ql; res->tn = ch_n;
    }

    return ch_i; 
}

// Assembly:
pushq %rbx
movq 0x18(%rsp), %rax
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl %edi, 0x14(%rcx)
movl $0xc0000000, %r8d       # imm = 0xC0000000
andl 0x18(%rcx), %r8d
orl $0x3, %r8d
movl %r8d, 0x18(%rcx)
movq $-0x1, 0xc(%rcx)
movl %edi, (%rcx)
testq %rdx, %rdx
je 0x67c07
movl 0x4(%rcx), %r8d
testq %rax, %rax
js 0x67af8
movq %rax, %rdi
shlq $0x4, %rdi
addq %rsi, %rdi
addq $0x8, %rdi
cmpl %r8d, (%rdi)
jb 0x67af8
leaq -0x1(%rax), %r10
addq $-0x10, %rdi
testq %rax, %rax
movq %r10, %rax
jg 0x67adc
movq $-0x1, %rax
xorl %edi, %edi
testq %rax, %rax
cmovgq %rax, %rdi
cmpq %rdx, %rdi
setge %al
jge 0x67b47
movq %rdi, %r10
shlq $0x4, %r10
cmpl %r8d, 0x8(%rsi,%r10)
jae 0x67b47
incq %rdi
addq %rsi, %r10
addq $0x18, %r10
movq %rdi, %rax
cmpq %rdi, %rdx
je 0x67b3e
leaq 0x1(%rax), %rdi
leaq 0x10(%r10), %r11
cmpl %r8d, (%r10)
movq %r11, %r10
jb 0x67b21
decq %rdi
jmp 0x67b41
movq %rdx, %rdi
cmpq %rdx, %rax
setge %al
movq %rdi, %r10
shlq $0x4, %r10
cmpl %r8d, 0x8(%rsi,%r10)
seta %r8b
orb %al, %r8b
movzbl %r8b, %r8d
xorl %eax, %eax
subq %r8, %rdi
cmovnsq %rdi, %rax
cmpq %rdx, %rax
setl %r8b
jge 0x67bb3
movl 0x8(%rcx), %r10d
movq %rax, %r11
shlq $0x4, %r11
cmpl %r10d, 0x8(%rsi,%r11)
jae 0x67bb3
incq %rax
addq %rsi, %r11
addq $0x18, %r11
movq %rax, %r8
cmpq %rax, %rdx
je 0x67ba9
leaq 0x1(%r8), %rax
leaq 0x10(%r11), %rbx
cmpl %r10d, (%r11)
movq %rbx, %r11
jb 0x67b8c
decq %rax
jmp 0x67bac
movq %rdx, %rax
cmpq %rdx, %r8
setl %r8b
testq %rdi, %rdi
js 0x67bd3
movq %rdi, %r10
shlq $0x4, %r10
movl 0x8(%rsi,%r10), %r11d
movl %r11d, 0x4(%rcx)
movl 0x4(%rsi,%r10), %r10d
movl %r10d, 0x10(%rcx)
jmp 0x67bdf
movl $0x0, 0x4(%rcx)
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl %edi, (%rcx)
testb %r8b, %r8b
je 0x67c00
movq %rax, %rdx
shlq $0x4, %rdx
movl 0x8(%rsi,%rdx), %edi
movl %edi, 0x8(%rcx)
movl 0x4(%rsi,%rdx), %edx
movl %edx, 0x14(%rcx)
movq %rax, %rdx
jmp 0x67c04
movl %r9d, 0x8(%rcx)
movl %edx, 0xc(%rcx)
popq %rbx
retq