// File: /workspace/github_cpp_makefile_star2_O1/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 $0xa8, %rsp
movq %rsi, 0x68(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, (%r9)
movl %eax, (%r8)
movl %eax, (%rcx)
movl %eax, (%rdx)
xorl %r11d, %r11d
movq %r11, 0x90(%rsp)
movq %r11, 0x98(%rsp)
movq %r11, 0xa0(%rsp)
movq 0x48(%rdi), %r10
movl $0x0, %ebx
movl $0x0, %esi
movl $0x0, %eax
movq %r10, 0x80(%rsp)
testq %r10, %r10
je 0x18033f
movq %rdx, 0x48(%rsp)
movq %rcx, 0x50(%rsp)
movq %r8, 0x58(%rsp)
movq %r9, 0x60(%rsp)
movq 0x58(%rdi), %rdx
movq 0x70(%rdi), %rax
movq %rax, 0x70(%rsp)
movq $0x0, 0x10(%rsp)
movq $0x0, 0x8(%rsp)
xorl %edi, %edi
xorl %r11d, %r11d
xorl %eax, %eax
movq %rdx, 0x78(%rsp)
movq %rax, 0x88(%rsp)
shlq $0x5, %rax
movl 0x1c(%rdx,%rax), %r12d
testl %r12d, %r12d
je 0x180300
movl 0x18(%rdx,%rax), %ecx
movq 0x70(%rsp), %rsi
leaq (%rsi,%rcx,2), %r14
movslq 0x8(%rdx,%rax), %r13
movslq (%rdx,%rax), %rbx
xorl %r15d, %r15d
movq %r11, %rbp
movq %rdi, (%rsp)
subq %rdi, %rbp
movq %rbx, %rsi
movq %r13, %rcx
movl %r15d, %eax
movzwl (%r14,%rax,2), %eax
movl %eax, %edx
shrl $0xe, %edx
movl %eax, %edi
andl $0x3fff, %edi           # imm = 0x3FFF
incl %r15d
cmpl %r15d, %r12d
jbe 0x180213
movl %r15d, %r8d
leaq (%r14,%r8,2), %r8
movzwl (%r8), %r9d
movl %r9d, %r10d
shrl $0xe, %r10d
cmpl %edx, %r10d
jne 0x180213
andl $0x3fff, %r9d           # imm = 0x3FFF
addl %r9d, %edi
incl %r15d
addq $0x2, %r8
cmpl %r15d, %r12d
jne 0x1801ea
movl %r12d, %r15d
cmpw $0x2, %dx
movl %edi, %r13d
movq %r13, %rdi
movl $0x0, %r8d
cmoveq %r8, %rdi
leaq (%rdi,%rsi), %rbx
cmpw $0x3, %dx
cmoveq %r8, %r13
addq %rcx, %r13
cmpq %rsi, %rbx
jbe 0x180240
cmpq %rbp, %rdi
ja 0x180254
movq %rbx, %rsi
movq %r13, %rcx
cmpl %r15d, %r12d
ja 0x1801c6
jmp 0x1802fc
movzwl %ax, %eax
cmpl $0x3fff, %eax           # imm = 0x3FFF
ja 0x1802ed
movq %r11, 0x18(%rsp)
movq 0x68(%rsp), %rdi
movq %rbx, %rdx
leaq 0xa0(%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 0xb0(%rsp), %rax
pushq %rax
leaq 0xc0(%rsp), %rax
pushq %rax
callq 0x17fb82
addq $0x30, %rsp
testl %eax, %eax
je 0x1802e8
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %rcx
movq %rcx, %rdx
subq %rax, %rdx
cmpq %rbp, %rdx
movq 0x18(%rsp), %r11
movq (%rsp), %rdi
jbe 0x1802f1
movq 0x20(%rsp), %rdx
movq %rdx, 0x10(%rsp)
movq 0x28(%rsp), %rdx
movq %rdx, 0x8(%rsp)
movq %rcx, %r11
movq %rax, %rdi
jmp 0x1802f1
movq 0x18(%rsp), %r11
movq (%rsp), %rdi
cmpl %r15d, %r12d
ja 0x1801b6
jmp 0x180300
movq (%rsp), %rdi
movq 0x88(%rsp), %rax
incq %rax
cmpq 0x80(%rsp), %rax
movq 0x78(%rsp), %rdx
jne 0x180183
movq 0x60(%rsp), %r9
movq 0x58(%rsp), %r8
movq 0x50(%rsp), %rcx
movq 0x48(%rsp), %rdx
movq 0x10(%rsp), %rax
movq 0x8(%rsp), %rsi
movq %rdi, %rbx
cmpq %rbx, %r11
jbe 0x180356
movl %ebx, (%rdx)
movl %r11d, (%rcx)
movl %eax, (%r8)
movl %esi, (%r9)
movl $0x1, %eax
jmp 0x180358
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq