// File: 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 %rbp
movq %rsp, %rbp
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq -0x28(%rbp), %rax
movl $0xffffffff, 0x14(%rax) # imm = 0xFFFFFFFF
movq -0x28(%rbp), %rax
movl $0xffffffff, 0x10(%rax) # imm = 0xFFFFFFFF
movq -0x28(%rbp), %rax
movl 0x18(%rax), %ecx
andl $0xc0000000, %ecx       # imm = 0xC0000000
orl $0x3, %ecx
movl %ecx, 0x18(%rax)
movq -0x28(%rbp), %rax
movl $0xffffffff, 0xc(%rax)  # imm = 0xFFFFFFFF
movq -0x28(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
cmpq $0x0, -0x20(%rbp)
jne 0x638d6
movq 0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x63b05
movq 0x18(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x28(%rbp), %rax
movl 0x4(%rax), %eax
movq %rax, -0x58(%rbp)
movq -0x28(%rbp), %rax
movl 0x8(%rax), %eax
movq %rax, -0x60(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x40(%rbp)
movb %al, -0x61(%rbp)
jl 0x6391c
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpq -0x58(%rbp), %rax
setae %al
movb %al, -0x61(%rbp)
movb -0x61(%rbp), %al
testb $0x1, %al
jne 0x63925
jmp 0x63935
jmp 0x63927
movq -0x40(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x638f4
cmpq $0x0, -0x40(%rbp)
jge 0x63944
movq $0x0, -0x40(%rbp)
jmp 0x63946
movq -0x40(%rbp), %rcx
xorl %eax, %eax
cmpq -0x20(%rbp), %rcx
movb %al, -0x62(%rbp)
jge 0x63971
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpq -0x58(%rbp), %rax
setb %al
movb %al, -0x62(%rbp)
movb -0x62(%rbp), %al
testb $0x1, %al
jne 0x6397a
jmp 0x6398a
jmp 0x6397c
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x63946
cmpq $0x0, -0x40(%rbp)
jl 0x639bf
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpq -0x58(%rbp), %rax
ja 0x639b3
movq -0x40(%rbp), %rax
cmpq -0x20(%rbp), %rax
jl 0x639bf
movq -0x40(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x40(%rbp)
jl 0x639d8
movq -0x40(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x639e0
xorl %eax, %eax
movq %rax, -0x70(%rbp)
jmp 0x639e0
movq -0x70(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rcx
xorl %eax, %eax
cmpq -0x20(%rbp), %rcx
movb %al, -0x71(%rbp)
jge 0x63a13
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpq -0x60(%rbp), %rax
setb %al
movb %al, -0x71(%rbp)
movb -0x71(%rbp), %al
testb $0x1, %al
jne 0x63a1c
jmp 0x63a2c
jmp 0x63a1e
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x639e8
movq -0x40(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x40(%rbp), %rax
movq %rax, 0x18(%rbp)
cmpq $0x0, -0x48(%rbp)
jl 0x63a83
movq -0x18(%rbp), %rax
movq -0x48(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x18(%rbp), %rax
movq -0x48(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x48(%rbp), %rax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
jmp 0x63a98
movq -0x28(%rbp), %rax
movl $0x0, 0x4(%rax)
movq -0x28(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq -0x50(%rbp), %rax
cmpq -0x20(%rbp), %rax
jge 0x63ae3
movq -0x18(%rbp), %rax
movq -0x50(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq -0x50(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x50(%rbp), %rax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0xc(%rax)
jmp 0x63afd
movq -0x38(%rbp), %rax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x20(%rbp), %rax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0xc(%rax)
movq 0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
popq %rbp
retq
nopl (%rax,%rax)