// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rdi, -0x28(%rbp)
movq %rsi, -0x30(%rbp)
movq %rdx, -0x38(%rbp)
movq %rcx, -0x40(%rbp)
movq %r8, -0x48(%rbp)
movq %r9, -0x50(%rbp)
movq -0x50(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq -0x48(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq -0x40(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq -0x38(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq $0x0, -0x110(%rbp)
movq $0x0, -0x108(%rbp)
movq $0x0, -0x100(%rbp)
movq $0x0, -0xc8(%rbp)
movq $0x0, -0xd0(%rbp)
movq $0x0, -0xb8(%rbp)
movq $0x0, -0xc0(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq 0x48(%rcx), %rax
jae 0x25644c
movq -0x28(%rbp), %rax
movq 0x70(%rax), %rax
movq -0x28(%rbp), %rcx
movq 0x58(%rcx), %rcx
movq -0x58(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movl 0x18(%rcx), %ecx
shlq %rcx
addq %rcx, %rax
movq %rax, -0xe8(%rbp)
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x58(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x1c(%rax), %eax
movq %rax, -0xf0(%rbp)
movq %rax, -0xf8(%rbp)
movl $0x0, -0xd8(%rbp)
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x58(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq (%rax), %rax
movq %rax, -0x88(%rbp)
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x58(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq 0x8(%rax), %rax
movq %rax, -0x90(%rbp)
movl -0xd8(%rbp), %eax
cmpq -0xf8(%rbp), %rax
jae 0x256439
movq -0x88(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0xb0(%rbp)
movl -0xd8(%rbp), %esi
leaq -0xf8(%rbp), %rdi
leaq -0xda(%rbp), %rdx
leaq -0xd4(%rbp), %rcx
callq 0xa1ee0
movl %eax, -0xd8(%rbp)
movzwl -0xda(%rbp), %eax
cmpl $0x2, %eax
je 0x2562f0
movl -0xd4(%rbp), %eax
addq -0x88(%rbp), %rax
movq %rax, -0x88(%rbp)
movzwl -0xda(%rbp), %eax
cmpl $0x3, %eax
je 0x256310
movl -0xd4(%rbp), %eax
addq -0x90(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x98(%rbp), %rax
cmpq -0xa0(%rbp), %rax
ja 0x256341
jmp 0x25627b
movq -0x98(%rbp), %rax
subq -0xa0(%rbp), %rax
movq -0xb8(%rbp), %rcx
subq -0xc0(%rbp), %rcx
cmpq %rcx, %rax
ja 0x256367
jmp 0x25627b
movzwl -0xda(%rbp), %eax
cmpl $0x0, %eax
jne 0x256434
movq -0x30(%rbp), %rdi
movq -0xa0(%rbp), %rsi
movq -0x98(%rbp), %rdx
movq -0xb0(%rbp), %rcx
movq -0xa8(%rbp), %r8
leaq -0x70(%rbp), %rbx
leaq -0x70(%rbp), %r11
addq $0x8, %r11
leaq -0x80(%rbp), %r10
leaq -0x80(%rbp), %rax
addq $0x8, %rax
leaq -0x100(%rbp), %r9
leaq -0x108(%rbp), %r15
leaq -0x110(%rbp), %r14
movq %r15, (%rsp)
movq %r14, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movq %r11, 0x18(%rsp)
movq %r10, 0x20(%rsp)
movq %rax, 0x28(%rsp)
callq 0x255b60
cmpl $0x0, %eax
je 0x256434
movq -0x68(%rbp), %rax
subq -0x70(%rbp), %rax
movq -0xb8(%rbp), %rcx
subq -0xc0(%rbp), %rcx
cmpq %rcx, %rax
jbe 0x256432
movq -0x70(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0xc8(%rbp)
jmp 0x256434
jmp 0x25627b
jmp 0x25643b
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x2561d5
movq -0xb8(%rbp), %rax
cmpq -0xc0(%rbp), %rax
jbe 0x2564a2
movq -0xc0(%rbp), %rax
movl %eax, %ecx
movq -0x38(%rbp), %rax
movl %ecx, (%rax)
movq -0xb8(%rbp), %rax
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, (%rax)
movq -0xd0(%rbp), %rax
movl %eax, %ecx
movq -0x48(%rbp), %rax
movl %ecx, (%rax)
movq -0xc8(%rbp), %rax
movl %eax, %ecx
movq -0x50(%rbp), %rax
movl %ecx, (%rax)
movq $0x1, -0x20(%rbp)
jmp 0x2564aa
movq $0x0, -0x20(%rbp)
movq -0x20(%rbp), %rax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)