// File: /workspace/github_cpp_makefile_star2_O2/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
orl $-0x1, 0x14(%rcx)
movl $0xc0000000, %edi       # imm = 0xC0000000
andl 0x18(%rcx), %edi
orl $0x3, %edi
movl %edi, 0x18(%rcx)
orq $-0x1, 0xc(%rcx)
orl $-0x1, (%rcx)
testq %rdx, %rdx
je 0x366d2
movl 0x4(%rcx), %r10d
cmpq $-0x1, %rax
movabsq $-0x1, %r8
cmovlq %rax, %r8
movl 0x8(%rcx), %edi
movq %rax, %r11
shlq $0x4, %r11
addq %rsi, %r11
addq $0x8, %r11
testq %rax, %rax
js 0x365f3
cmpl %r10d, (%r11)
jb 0x365f0
decq %rax
addq $-0x10, %r11
jmp 0x365dd
movq %rax, %r8
xorl %eax, %eax
testq %r8, %r8
cmovgq %r8, %rax
cmpq %rdx, %rax
movq %rdx, %r8
cmovgq %rax, %r8
movq %rax, %r11
shlq $0x4, %r11
addq %rsi, %r11
addq $0x8, %r11
cmpq %rdx, %rax
jge 0x3662a
movl (%r11), %ebx
cmpl %r10d, %ebx
jae 0x36638
incq %rax
addq $0x10, %r11
jmp 0x36614
movq %r8, %r11
shlq $0x4, %r11
movl 0x8(%rsi,%r11), %ebx
jmp 0x3663b
movq %rax, %r8
cmpq %rdx, %rax
setge %al
cmpl %r10d, %ebx
seta %r10b
orb %al, %r10b
movzbl %r10b, %eax
xorl %r10d, %r10d
subq %rax, %r8
cmovnsq %r8, %r10
cmpq %rdx, %r10
movq %rdx, %rax
cmovgq %r10, %rax
movq %r10, %r11
shlq $0x4, %r11
addq %rsi, %r11
addq $0x8, %r11
cmpq %rdx, %r10
jge 0x36687
cmpl %edi, (%r11)
jae 0x36684
incq %r10
addq $0x10, %r11
jmp 0x36671
movq %r10, %rax
testq %r8, %r8
js 0x366a2
movq %r8, %rdi
shlq $0x4, %rdi
movl 0x4(%rsi,%rdi), %r11d
movl 0x8(%rsi,%rdi), %edi
movl %r11d, 0x10(%rcx)
jmp 0x366aa
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
xorl %edi, %edi
movl %edi, 0x4(%rcx)
movl %r8d, (%rcx)
cmpq %rdx, %r10
jge 0x366cb
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
retq