// File: /workspace/github_cpp_makefile_star2_O1/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
movq %rcx, -0x20(%rsp)
movq %rdx, -0x28(%rsp)
movq 0x60(%rsp), %r14
movq 0x58(%rsp), %r15
movq 0x50(%rsp), %rbx
movq 0x40(%rsp), %rax
movq 0x38(%rsp), %r8
movq (%rax), %rax
movq (%rdi), %rdx
testq %rax, %rax
sets %cl
movq %rdx, -0x18(%rsp)
cmpq %rdx, %rax
setg %dl
xorl %r10d, %r10d
orb %cl, %dl
movq (%r8), %r11
cmovneq %r10, %r11
movq %r9, -0x10(%rsp)
movq (%r9), %r13
cmovneq %r10, %r13
movq %r10, (%r14)
movq %r10, (%r15)
movq %r10, (%rbx)
movq 0x48(%rsp), %rcx
movq %r10, (%rcx)
cmovneq %r10, %rax
testq %rax, %rax
setg %cl
movq %rsi, -0x30(%rsp)
cmpq %rsi, %r13
setge %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x17fc6d
movq 0x10(%rdi), %rcx
movl $0xfff, %r10d           # imm = 0xFFF
xorl %r8d, %r8d
movzwl -0x2(%rcx,%rax,2), %esi
movl %esi, %edx
shrl $0xe, %edx
movl $0x3ff, %r9d            # imm = 0x3FF
cmpw $0x1, %dx
je 0x17fc34
movl $0x3fff, %r9d           # imm = 0x3FFF
testw %si, %si
cmovsl %r10d, %r9d
leaq -0x1(%rax), %rbp
andl %esi, %r9d
cmpw $0x2, %dx
movq %r9, %rsi
cmoveq %r8, %rsi
subq %rsi, %r13
cmpw $0x3, %dx
cmoveq %r8, %r9
subq %r9, %r11
cmpq $0x2, %rax
jl 0x17fc70
movq %rbp, %rax
cmpq -0x30(%rsp), %r13
jge 0x17fc18
jmp 0x17fc70
movq %rax, %rbp
cmpq -0x18(%rsp), %rbp
jge 0x17fe35
cmpq -0x28(%rsp), %r13
jge 0x17fe35
movq 0x10(%rdi), %rsi
movq -0x30(%rsp), %rax
subq %rax, -0x20(%rsp)
movq %rdi, -0x8(%rsp)
movl %ebp, %eax
movzwl (%rsi,%rax,2), %r9d
movl %r9d, %r15d
shrl $0xe, %r15d
leal -0x2(%r15), %eax
cmpl $0x2, %eax
jae 0x17fcc4
movl %r9d, %ebx
shrl $0xc, %ebx
andl $0x3, %ebx
movl %r9d, %ecx
andl $0xfff, %ecx            # imm = 0xFFF
jmp 0x17fcd6
testl %r15d, %r15d
jne 0x17fcdd
movl %r9d, %ecx
andl $0x3fff, %ecx           # imm = 0x3FFF
movw $0xffff, %bx            # imm = 0xFFFF
movl $0xffff, %eax           # imm = 0xFFFF
jmp 0x17fcf8
movl %r9d, %ebx
shrl $0xc, %ebx
andl $0x3, %ebx
movl %r9d, %eax
shrl $0xa, %eax
andl $0x3, %eax
movl %r9d, %ecx
andl $0x3ff, %ecx            # imm = 0x3FF
movzwl %cx, %r14d
movq (%rdi), %r12
incl %ebp
cmpq %rbp, %r12
jbe 0x17fd7c
movzwl %r15w, %ecx
leal -0x2(%rcx), %edi
movzwl (%rsi,%rbp,2), %r10d
movl %r10d, %edx
shrl $0xe, %edx
cmpl %r15d, %edx
jne 0x17fd7c
cmpl $0x2, %edi
jae 0x17fd36
movl %r10d, %r8d
shrl $0xc, %r8d
andl $0x3, %r8d
andl $0xfff, %r10d           # imm = 0xFFF
jmp 0x17fd46
testl %ecx, %ecx
jne 0x17fd4d
andl $0x3fff, %r10d          # imm = 0x3FFF
movw $0xffff, %r8w           # imm = 0xFFFF
movl $0xffff, %edx           # imm = 0xFFFF
jmp 0x17fd68
movl %r10d, %r8d
shrl $0xc, %r8d
andl $0x3, %r8d
movl %r10d, %edx
shrl $0xa, %edx
andl $0x3, %edx
andl $0x3ff, %r10d           # imm = 0x3FF
cmpl %eax, %edx
jne 0x17fd7c
cmpw %r8w, %bx
jne 0x17fd7c
addl %r10d, %r14d
incl %ebp
cmpq %rbp, %r12
ja 0x17fd0d
cmpw $0x2, %r15w
movl %r14d, %ecx
movq %rcx, %rax
movl $0x0, %edx
cmoveq %rdx, %rax
addq %r13, %rax
cmpw $0x3, %r15w
cmoveq %rdx, %rcx
cmpl $0x3fff, %r9d           # imm = 0x3FFF
ja 0x17fe14
movq -0x30(%rsp), %rdi
cmpq %rdi, %r13
cmovgq %r13, %rdi
movq -0x28(%rsp), %r8
cmpq %r8, %rax
cmovlq %rax, %r8
cmpq %rdi, %r8
jle 0x17fe14
movl %r8d, %edx
subl %edi, %edx
testq %rdx, %rdx
je 0x17fe14
movq 0x50(%rsp), %r9
movq (%r9), %r9
movq 0x48(%rsp), %r10
subq (%r10), %r9
cmpq %r9, %rdx
jbe 0x17fe14
movq %r11, %rdx
subq %r13, %rdx
leaq (%rdx,%rdi), %r9
movq %r9, (%r10)
addq %r8, %rdx
movq 0x50(%rsp), %r9
movq %rdx, (%r9)
movq -0x20(%rsp), %rdx
addq %rdx, %rdi
movq 0x58(%rsp), %r9
movq %rdi, (%r9)
addq %rdx, %r8
movq 0x60(%rsp), %rdx
movq %r8, (%rdx)
movl %ebp, %ebp
addq %rcx, %r11
cmpq %rbp, -0x18(%rsp)
movq -0x8(%rsp), %rdi
jle 0x17fe38
movq %rax, %r13
cmpq -0x28(%rsp), %rax
jl 0x17fc99
jmp 0x17fe38
movq %r13, %rax
movq -0x10(%rsp), %rcx
movq %rax, (%rcx)
movq 0x38(%rsp), %rax
movq %r11, (%rax)
movq 0x40(%rsp), %rax
movq %rbp, (%rax)
movq 0x50(%rsp), %rax
movq (%rax), %rcx
xorl %eax, %eax
movq 0x48(%rsp), %rdx
cmpq (%rdx), %rcx
seta %al
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq