// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/ecovlp.cpp
// Function: extract_max_exact(overlap_region*, asg16_v*, unsigned int*, unsigned int*, unsigned int*, unsigned int*)
uint64_t extract_max_exact(overlap_region *z, asg16_v *ec, /**UC_Read *qu, UC_Read *tu,**/ uint32_t *rxs, uint32_t *rxe, uint32_t *rys, uint32_t *rye)
{
    // if(z->x_id == 75 && z->y_id == 59) {
    //     fprintf(stderr, "[M::%s]\tz->y_id::%u\n", __func__, z->y_id);
    //     if(z->y_pos_strand) {
    //         recover_UC_Read_RC(tu, &R_INF, z->y_id);
    //     } else {
    //         recover_UC_Read(tu, &R_INF, z->y_id); ///b->z.length
    //     }
    // }

    *rxs = *rxe = *rys = *rye = (uint32_t)-1;
    uint64_t k, rx[2], ry[2], xk, yk, wx[2], wy[2], mx[2], my[2]; 
    uint32_t cl, ck; uint16_t c; asg16_v ct; int64_t exk, eyk, eck;
    exk = eyk = eck = 0; mx[0] = mx[1] = my[0] = my[1] = 0;
    for (k = 0; k < z->w_list.n; k++) {
        ct.a = z->w_list.c.a + z->w_list.a[k].cidx;
        ct.n = ct.m = z->w_list.a[k].clen; ck = 0; 
        xk = z->w_list.a[k].x_start; yk = z->w_list.a[k].y_start;
        while (ck < ct.n) {
            wx[0] = xk; wy[0] = yk;
            ck = pop_trace(&ct, ck, &c, &cl);
            if(c != 2) xk += cl;
            if(c != 3) yk += cl;
            wx[1] = xk; wy[1] = yk;
            // if(c == 0) {
            //     if(memcmp(qref + wx[0], tu->seq + wy[0], wx[1] - wx[0])) {
            //         fprintf(stderr, "-0-[M::%s]\teq::[%lu,\t%lu)\tet::[%lu,\t%lu)\t%c\n", __func__, wx[0], wx[1], wy[0], wy[1], "+-"[z->y_pos_strand]);
            //         // exit(1);
            //     }
            // }
            if(wx[1] <= wx[0]) continue;
            if((wx[1] - wx[0]) <= (mx[1] - mx[0])) continue;
            if((c == 0) && (extract_max_exact_sub(ec, wx[0], wx[1], wy[0], wy[1], &exk, &eyk, &eck, &(rx[0]), &(rx[1]), &(ry[0]), &(ry[1])))) {
                // if(z->x_id == 75 && z->y_id == 59) {
                //     if(memcmp(qu->seq + rx[0], tu->seq + ry[0], rx[1] - rx[0])) {
                //         fprintf(stderr, "-1-[M::%s]\tzq::[%lu,\t%lu)\tzt::[%lu,\t%lu)\teq::[%lu,\t%lu)\tet::[%lu,\t%lu)\n", 
                //         __func__, wx[0], wx[1], wy[0], wy[1], rx[0], rx[1], ry[0], ry[1]);
                //         exit(1);
                //     } 
                //     else {
                //         fprintf(stderr, "-2-[M::%s]\tzq::[%lu,\t%lu)\tzt::[%lu,\t%lu)\teq::[%lu,\t%lu)\tet::[%lu,\t%lu)\n", 
                //         __func__, wx[0], wx[1], wy[0], wy[1], rx[0], rx[1], ry[0], ry[1]);

                //         fprintf(stderr, "[M::%s] qstr::%.*s\n", __func__, ((int)(rx[1] - rx[0])), qu->seq + rx[0]);
                //         fprintf(stderr, "[M::%s] tstr::%.*s\n", __func__, ((int)(ry[1] - ry[0])), tu->seq + ry[0]);
                //     }
                // }
                if((rx[1] - rx[0]) > (mx[1] - mx[0])) {
                    mx[0] = rx[0]; mx[1] = rx[1];
                    my[0] = ry[0]; my[1] = ry[1];
                }
            }
        }
    }

    if(mx[1] > mx[0]) {
        *rxs = mx[0]; *rxe = mx[1]; 
        *rys = my[0]; *rye = my[1]; 
        return 1;
    }
    
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %r9, 0x58(%rsp)
orl $-0x1, (%r9)
movq %r8, 0x50(%rsp)
orl $-0x1, (%r8)
movq %rsi, 0x60(%rsp)
movq %rcx, 0x48(%rsp)
orl $-0x1, (%rcx)
movq %rdx, 0x40(%rsp)
orl $-0x1, (%rdx)
leaq 0x88(%rsp), %rax
andq $0x0, (%rax)
leaq 0x90(%rsp), %rax
andq $0x0, (%rax)
leaq 0x98(%rsp), %rax
andq $0x0, (%rax)
xorl %ecx, %ecx
xorl %r12d, %r12d
xorl %esi, %esi
movq $0x0, 0x18(%rsp)
movq $0x0, 0x10(%rsp)
movq %rdi, 0x68(%rsp)
cmpq 0x48(%rdi), %rcx
jae 0x1431e6
movq 0x58(%rdi), %rax
movq %rcx, 0x70(%rsp)
shlq $0x5, %rcx
movl 0x18(%rax,%rcx), %edx
addq %rdx, %rdx
addq 0x70(%rdi), %rdx
movq %rdx, 0xb0(%rsp)
movl 0x1c(%rax,%rcx), %edx
movq %rdx, 0xa8(%rsp)
movq %rdx, 0xa0(%rsp)
movslq (%rax,%rcx), %r14
movslq 0x8(%rax,%rcx), %r15
xorl %ebx, %ebx
movq %r12, 0x80(%rsp)
movq %rsi, 0x78(%rsp)
subq %rsi, %r12
movl %ebx, %eax
cmpq %rax, 0xa0(%rsp)
jbe 0x1431c7
movq %r14, %rbp
movq %r15, %r13
leaq 0xa0(%rsp), %rdi
movl %ebx, %esi
leaq 0xa(%rsp), %rdx
leaq 0xc(%rsp), %rcx
callq 0x53a7d
movzwl 0xa(%rsp), %ecx
movl 0xc(%rsp), %esi
leaq (%r14,%rsi), %rdx
cmpw $0x2, %cx
movq %rdx, %r14
cmoveq %rbp, %r14
cmpw $0x3, %cx
movl $0x0, %r15d
cmovneq %rsi, %r15
movl %eax, %ebx
addq %r13, %r15
movq %r14, %rax
subq %rbp, %rax
jbe 0x1430d5
cmpq %r12, %rax
jbe 0x1430d5
testw %cx, %cx
jne 0x1430d5
movq 0x60(%rsp), %rdi
movq %rbp, %rsi
movq %r13, %rcx
leaq 0x98(%rsp), %r9
leaq 0x28(%rsp), %rax
pushq %rax
leaq 0x28(%rsp), %rax
pushq %rax
leaq 0x48(%rsp), %rax
pushq %rax
leaq 0x48(%rsp), %rax
pushq %rax
leaq 0xa8(%rsp), %rax
pushq %rax
leaq 0xb8(%rsp), %rax
pushq %rax
callq 0x142cbc
addq $0x30, %rsp
testl %eax, %eax
je 0x1430d5
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %rcx
movq %rcx, %rdx
subq %rax, %rdx
cmpq %r12, %rdx
jbe 0x1430d5
movq 0x20(%rsp), %rdx
movq %rdx, 0x10(%rsp)
movq 0x28(%rsp), %rdx
movq %rdx, 0x18(%rsp)
movq %rcx, %r12
movq %rax, %rsi
jmp 0x1430c5
movq 0x70(%rsp), %rcx
incq %rcx
movq 0x68(%rsp), %rdi
movq 0x80(%rsp), %r12
movq 0x78(%rsp), %rsi
jmp 0x14307c
cmpq %rsi, %r12
jbe 0x143217
movq 0x40(%rsp), %rax
movl %esi, (%rax)
movq 0x48(%rsp), %rax
movl %r12d, (%rax)
movq 0x50(%rsp), %rax
movq 0x10(%rsp), %rcx
movl %ecx, (%rax)
movq 0x58(%rsp), %rax
movq 0x18(%rsp), %rcx
movl %ecx, (%rax)
pushq $0x1
popq %rax
jmp 0x143219
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq