// File: /workspace/github_cpp_makefile_star2_O3/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 %r14
pushq %rbx
movq 0x20(%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 0x67918
movl 0x4(%rcx), %r8d
testq %rax, %rax
js 0x67806
movq %rax, %rdi
shlq $0x4, %rdi
addq %rsi, %rdi
addq $0x8, %rdi
cmpl %r8d, (%rdi)
jb 0x67806
leaq -0x1(%rax), %r10
addq $-0x10, %rdi
testq %rax, %rax
movq %r10, %rax
jg 0x677ea
movq $-0x1, %rax
xorl %edi, %edi
testq %rax, %rax
cmovgq %rax, %rdi
movb $0x1, %al
cmpq %rdx, %rdi
jge 0x67859
movq %rdi, %rax
shlq $0x4, %rax
cmpl %r8d, 0x8(%rsi,%rax)
jae 0x6784c
incq %rdi
leaq (%rax,%rsi), %r10
addq $0x18, %r10
movq %rdi, %rax
cmpq %rdi, %rdx
je 0x67850
leaq 0x1(%rax), %rdi
leaq 0x10(%r10), %r11
cmpl %r8d, (%r10)
movq %r11, %r10
jb 0x6782f
decq %rdi
jmp 0x67853
xorl %eax, %eax
jmp 0x67859
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, %eax
xorl %r8d, %r8d
subq %rax, %rdi
movl $0x0, %eax
cmovnsq %rdi, %rax
cmpq %rdx, %rax
jge 0x678c0
movl 0x8(%rcx), %r11d
movq %rax, %rbx
shlq $0x4, %rbx
movb $0x1, %r10b
cmpl %r11d, 0x8(%rsi,%rbx)
jae 0x678cf
incq %rax
addq %rsi, %rbx
addq $0x18, %rbx
movq %rax, %r10
cmpq %rax, %rdx
je 0x678c5
leaq 0x1(%r10), %rax
leaq 0x10(%rbx), %r14
cmpl %r11d, (%rbx)
movq %r14, %rbx
jb 0x678a3
decq %rax
jmp 0x678c8
xorl %r10d, %r10d
jmp 0x678cf
movq %rdx, %rax
cmpq %rdx, %r10
setl %r10b
testq %rdi, %rdi
js 0x678eb
movq %rdi, %r8
shlq $0x4, %r8
movl 0x4(%rsi,%r8), %r11d
movl 0x8(%rsi,%r8), %r8d
movl %r11d, 0x10(%rcx)
jmp 0x678f0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl %r8d, 0x4(%rcx)
movl %edi, (%rcx)
testb %r10b, %r10b
je 0x67911
movq %rax, %rdx
shlq $0x4, %rdx
movl 0x4(%rsi,%rdx), %edi
movl 0x8(%rsi,%rdx), %r9d
movl %edi, 0x14(%rcx)
movq %rax, %rdx
movl %r9d, 0x8(%rcx)
movl %edx, 0xc(%rcx)
popq %rbx
popq %r14
retq