// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/ecovlp.cpp
// Function: extract_max_exact_sub(asg16_v*, long, long, long, long, long*, long*, long*, unsigned long*, unsigned long*, unsigned long*, unsigned long*)
uint32_t extract_max_exact_sub(asg16_v *in, int64_t xs0, int64_t xe0, int64_t ys0, int64_t ye0, int64_t *exk, int64_t *eyk, int64_t *eck, uint64_t *rxs, uint64_t *rxe, uint64_t *rys, uint64_t *rye)
{
    int64_t xk = *exk, yk = *eyk, ck = *eck, cn = in->n, ol, wx[2], wy[2], os, oe; uint16_t op, bq, bt; uint32_t cl, ovlp;
    *rxs = *rxe = *rys = *rye = 0;

    if((ck < 0) || (ck > cn)) {//(*ck) == cn is allowed
        ck = 0; xk = 0; yk = 0;
    }

    while (ck > 0 && xk >= xs0) {///x -> t; y -> p; first insertion and then match/mismatch
        --ck;
        op = in->a[ck]>>14;
        // ol = (((op == 1) || (op == 2))?(in->a[ck]&(0xfff)):(in->a[ck]&(0x3fff)));
        if((op == 2) || (op == 3)) {
            ol = in->a[ck]&(0xfff);
        } else if(op == 1) {
            ol = in->a[ck]&(0x3ff);
        } else {
            ol = in->a[ck]&(0x3fff);
        }
        if(op != 2) xk -= ol;
        if(op != 3) yk -= ol;
    }

    while (ck < cn && xk < xe0) {
        wx[0] = xk; wy[0] = yk; 
        // ck = pop_trace_bp(in, ck, &op, &b, &cl);
        ck = pop_trace_bp_f(in, ck, &op, &bq, &bt, &cl);
        if(op != 2) xk += cl;
        if(op != 3) yk += cl;
        wx[1] = xk; wy[1] = yk;  
        if(op == 0) {
            os = MAX(xs0, wx[0]); oe = MIN(xe0, wx[1]);
            ovlp = ((oe>os)? (oe-os):0);
            if((ovlp > 0) && (ovlp > (*rxe) - (*rxs))) {
                (*rxs) = wy[0] + os - wx[0]; 
                (*rxe) = wy[0] + oe - wx[0]; 

                (*rys) = ys0 + os - xs0; 
                (*rye) = ys0 + oe - xs0; 
            }
        }
    }

    *exk = xk; *eyk = yk; *eck = ck;
    if((*rxe) > (*rxs)) return 1;
    return 0; 
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, 0x18(%rsp)
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %r13
movq 0x68(%rsp), %rax
movq 0x60(%rsp), %rdx
movq (%rax), %rsi
movq (%rdi), %rbx
testq %rsi, %rsi
sets %al
cmpq %rbx, %rsi
setg %cl
orb %al, %cl
movq %r9, 0x20(%rsp)
jne 0x142d05
movq (%rdx), %rax
movq (%r9), %rcx
jmp 0x142d0b
xorl %eax, %eax
xorl %ecx, %ecx
xorl %esi, %esi
movq 0x88(%rsp), %rdx
andq $0x0, (%rdx)
movq 0x80(%rsp), %rdx
andq $0x0, (%rdx)
movq 0x78(%rsp), %rdx
andq $0x0, (%rdx)
movq 0x70(%rsp), %rdx
andq $0x0, (%rdx)
movl $0xfff, %edx            # imm = 0xFFF
xorl %edi, %edi
testq %rsi, %rsi
jle 0x142d9b
cmpq %r12, %rcx
jl 0x142d9b
movq 0x10(%r13), %r8
movzwl -0x2(%r8,%rsi,2), %r10d
movl %r10d, %r8d
shrl $0xe, %r8d
movw %r8w, 0xe(%rsp)
movl $0x3ff, %r9d            # imm = 0x3FF
cmpw $0x1, %r8w
je 0x142d70
movl $0x3fff, %r9d           # imm = 0x3FFF
decq %rsi
testw %r10w, %r10w
cmovsl %edx, %r9d
andl %r10d, %r9d
cmpw $0x2, %r8w
movq %r9, %r10
cmoveq %rdi, %r10
subq %r10, %rcx
cmpw $0x3, %r8w
cmoveq %rdi, %r9
subq %r9, %rax
jmp 0x142d3c
subq %r12, 0x18(%rsp)
movq %rcx, %rbp
movq %rax, %r14
cmpq %rbx, %rsi
jge 0x142e88
cmpq %r15, %rbp
jge 0x142e88
movq %r13, %rdi
leaq 0xe(%rsp), %rdx
leaq 0x16(%rsp), %rcx
leaq 0x14(%rsp), %r8
leaq 0x10(%rsp), %r9
callq 0xeeaa9
movl %eax, %esi
movzwl 0xe(%rsp), %edi
movl 0x10(%rsp), %eax
leaq (%rax,%rbp), %rdx
cmpw $0x2, %di
movq %rdx, %rcx
cmoveq %rbp, %rcx
cmpw $0x3, %di
movl $0x0, %r8d
cmoveq %r8, %rax
addq %r14, %rax
testw %di, %di
jne 0x142da0
cmpq %r12, %rbp
movq %r12, %rdi
cmovgq %rbp, %rdi
cmpq %r15, %rdx
cmovgeq %r15, %rdx
cmpq %rdi, %rdx
setle %r9b
movl %edx, %r8d
subl %edi, %r8d
sete %r10b
orb %r9b, %r10b
jne 0x142da0
movq 0x78(%rsp), %r9
movq (%r9), %r9
movq 0x70(%rsp), %r10
subq (%r10), %r9
cmpq %r9, %r8
jbe 0x142da0
subq %rbp, %r14
leaq (%r14,%rdi), %r8
movq 0x70(%rsp), %r9
movq %r8, (%r9)
addq %rdx, %r14
movq 0x78(%rsp), %r8
movq %r14, (%r8)
movq 0x18(%rsp), %r8
addq %r8, %rdi
movq 0x80(%rsp), %r9
movq %rdi, (%r9)
addq %r8, %rdx
movq 0x88(%rsp), %rdi
movq %rdx, (%rdi)
jmp 0x142da0
movq 0x20(%rsp), %rax
movq %rbp, (%rax)
movq 0x60(%rsp), %rax
movq %r14, (%rax)
movq 0x68(%rsp), %rax
movq %rsi, (%rax)
movq 0x78(%rsp), %rax
movq (%rax), %rcx
xorl %eax, %eax
movq 0x70(%rsp), %rdx
cmpq (%rdx), %rcx
seta %al
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq