// File: /workspace/github_cpp_makefile_star2_O3/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 %rsi, 0x70(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, (%r9)
movl %eax, (%r8)
movl %eax, (%rcx)
movl %eax, (%rdx)
xorl %eax, %eax
movq %rax, 0xa0(%rsp)
movq %rax, 0xa8(%rsp)
movq %rax, 0xb0(%rsp)
movq 0x48(%rdi), %r11
testq %r11, %r11
je 0x187874
movq %rdx, 0x50(%rsp)
movq %rcx, 0x58(%rsp)
movq %r8, 0x60(%rsp)
movq %r9, 0x68(%rsp)
movq 0x58(%rdi), %rdx
movq 0x70(%rdi), %rax
movq %rax, 0x80(%rsp)
addq $0x2, %rax
movq %rax, 0x78(%rsp)
movq $0x0, 0x20(%rsp)
movq $0x0, 0x18(%rsp)
xorl %edi, %edi
xorl %r15d, %r15d
xorl %eax, %eax
movq %rdx, 0x88(%rsp)
movq %r11, 0x28(%rsp)
movq %rax, 0x90(%rsp)
shlq $0x5, %rax
movl 0x1c(%rdx,%rax), %r13d
testl %r13d, %r13d
je 0x187823
movl 0x18(%rdx,%rax), %ecx
movq 0x80(%rsp), %rsi
leaq (%rsi,%rcx,2), %rbx
movslq 0x8(%rdx,%rax), %rbp
movslq (%rdx,%rax), %r14
movq 0x78(%rsp), %rax
leaq (%rax,%rcx,2), %rax
movq %rax, 0x98(%rsp)
xorl %r12d, %r12d
movq %r15, 0x10(%rsp)
movq %rdi, 0x8(%rsp)
subq %rdi, %r15
movq %r14, %rsi
movq %rbp, %rcx
movl %r12d, %r8d
movzwl (%rbx,%r8,2), %eax
movl %eax, %edi
shrl $0xe, %edi
movl %eax, %edx
andl $0x3fff, %edx           # imm = 0x3FFF
incl %r12d
cmpl %r12d, %r13d
jbe 0x18772a
movq 0x98(%rsp), %r9
leaq (%r9,%r8,2), %r8
movzwl (%r8), %r9d
movl %r9d, %r10d
shrl $0xe, %r10d
cmpl %edi, %r10d
jne 0x18772a
andl $0x3fff, %r9d           # imm = 0x3FFF
addl %r9d, %edx
incl %r12d
addq $0x2, %r8
cmpl %r12d, %r13d
jne 0x187701
movl %r13d, %r12d
movl %edx, %r8d
leaq (%rsi,%r8), %rdx
cmpw $0x2, %di
movq %rdx, %r14
cmoveq %rsi, %r14
cmpw $0x3, %di
movl $0x0, %ebp
cmovneq %r8, %rbp
addq %rcx, %rbp
movq %r14, %rdi
subq %rsi, %rdi
jbe 0x187759
cmpq %r15, %rdi
ja 0x18776d
movq %r14, %rsi
movq %rbp, %rcx
cmpl %r12d, %r13d
ja 0x1876d8
jmp 0x187819
movzwl %ax, %eax
cmpl $0x3fff, %eax           # imm = 0x3FFF
ja 0x187804
movq 0x70(%rsp), %rdi
leaq 0xb0(%rsp), %r9
leaq 0x38(%rsp), %rax
pushq %rax
leaq 0x38(%rsp), %rax
pushq %rax
leaq 0x58(%rsp), %rax
pushq %rax
leaq 0x58(%rsp), %rax
pushq %rax
leaq 0xc0(%rsp), %rax
pushq %rax
leaq 0xd0(%rsp), %rax
pushq %rax
callq 0x187046
addq $0x30, %rsp
testl %eax, %eax
je 0x1877ff
movq 0x40(%rsp), %rax
movq 0x48(%rsp), %rcx
movq %rcx, %rdx
subq %rax, %rdx
cmpq %r15, %rdx
movq 0x28(%rsp), %r11
movq 0x10(%rsp), %r15
movq 0x8(%rsp), %rdi
jbe 0x18780e
movq 0x30(%rsp), %rdx
movq %rdx, 0x20(%rsp)
movq 0x38(%rsp), %rdx
movq %rdx, 0x18(%rsp)
movq %rcx, %r15
movq %rax, %rdi
jmp 0x18780e
movq 0x28(%rsp), %r11
movq 0x10(%rsp), %r15
movq 0x8(%rsp), %rdi
cmpl %r12d, %r13d
ja 0x1876c5
jmp 0x187823
movq 0x10(%rsp), %r15
movq 0x8(%rsp), %rdi
movq 0x90(%rsp), %rax
incq %rax
cmpq %r11, %rax
movq 0x88(%rsp), %rdx
jne 0x18767e
cmpq %rdi, %r15
jbe 0x187872
movq 0x50(%rsp), %rax
movl %edi, (%rax)
movq 0x58(%rsp), %rax
movl %r15d, (%rax)
movq 0x60(%rsp), %rax
movq 0x20(%rsp), %rcx
movl %ecx, (%rax)
movq 0x68(%rsp), %rax
movq 0x18(%rsp), %rcx
movl %ecx, (%rax)
movl $0x1, %eax
jmp 0x187874
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq