// File: /workspace/github_cpp_makefile_star2_O3/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, -0x18(%rsp)
movq %rdx, -0x20(%rsp)
movq 0x60(%rsp), %rbx
movq 0x58(%rsp), %r14
movq 0x50(%rsp), %rdx
movq 0x40(%rsp), %rax
movq 0x38(%rsp), %r8
movq (%rax), %rax
movq (%rdi), %r10
testq %rax, %rax
sets %cl
movq %r10, -0x10(%rsp)
cmpq %r10, %rax
setg %r11b
xorl %r10d, %r10d
orb %cl, %r11b
movq (%r8), %r12
cmovneq %r10, %r12
movq %r9, -0x8(%rsp)
movq (%r9), %rbp
cmovneq %r10, %rbp
movq %r10, (%rbx)
movq %r10, (%r14)
movq %r10, (%rdx)
movq 0x48(%rsp), %r13
movq %r10, (%r13)
cmoveq %rax, %r10
testq %r10, %r10
setg %cl
movq %rsi, -0x28(%rsp)
cmpq %rsi, %rbp
setge %sil
andb %cl, %sil
cmpb $0x1, %sil
jne 0x18713a
movq 0x10(%rdi), %rcx
movl $0xfff, %r8d            # imm = 0xFFF
xorl %r9d, %r9d
movzwl -0x2(%rcx,%rax,2), %ebx
movl %ebx, %r14d
shrl $0xe, %r14d
movl $0x3ff, %r11d           # imm = 0x3FF
cmpw $0x1, %r14w
je 0x187101
movl $0x3fff, %r11d          # imm = 0x3FFF
testw %bx, %bx
cmovsl %r8d, %r11d
leaq -0x1(%rax), %r10
andl %ebx, %r11d
cmpw $0x2, %r14w
movq %r11, %rbx
cmoveq %r9, %rbx
subq %rbx, %rbp
cmpw $0x3, %r14w
cmoveq %r9, %r11
subq %r11, %r12
cmpq $0x2, %rax
jb 0x18713a
movq %r10, %rax
cmpq -0x28(%rsp), %rbp
jge 0x1870e2
cmpq -0x10(%rsp), %r10
jge 0x18733b
cmpq -0x20(%rsp), %rbp
jge 0x18733b
movq 0x10(%rdi), %r11
movq -0x28(%rsp), %rax
subq %rax, -0x18(%rsp)
movl %r10d, %eax
movzwl (%r11,%rax,2), %eax
movl %eax, %r14d
shrl $0xe, %r14d
movw $0xffff, %cx            # imm = 0xFFFF
movw %cx, -0x2c(%rsp)
movw %cx, -0x2a(%rsp)
leal -0x2(%r14), %ecx
movzwl %ax, %edx
cmpl $0x2, %ecx
jae 0x187197
movw $0xfff, %r9w            # imm = 0xFFF
movw $0xc, %cx
leaq -0x2c(%rsp), %r8
jmp 0x1871bc
movw $0x3fff, %r9w           # imm = 0x3FFF
testl %r14d, %r14d
je 0x1871c7
movl %eax, %ecx
shrl $0xc, %ecx
andl $0x3, %ecx
movw %cx, -0x2c(%rsp)
movw $0x3ff, %r9w            # imm = 0x3FF
movw $0xa, %cx
leaq -0x2a(%rsp), %r8
movl %edx, %ebx
shrl %cl, %ebx
andl $0x3, %ebx
movw %bx, (%r8)
movq %r13, %rsi
movzwl %r9w, %ecx
andl %eax, %ecx
movq %rdi, %r9
movq (%rdi), %r8
incl %r10d
cmpq %r10, %r8
jbe 0x18725b
movzwl -0x2a(%rsp), %eax
movzwl -0x2c(%rsp), %r15d
movzwl (%r11,%r10,2), %r13d
movl %r13d, %edi
shrl $0xe, %edi
cmpl %r14d, %edi
jne 0x18725b
movzwl %r14w, %edi
leal -0x2(%rdi), %ebx
cmpl $0x2, %ebx
jae 0x187217
movl %r13d, %edi
shrl $0xc, %edi
andl $0x3, %edi
andl $0xfff, %r13d           # imm = 0xFFF
jmp 0x187226
testl %edi, %edi
jne 0x18722d
andl $0x3fff, %r13d          # imm = 0x3FFF
movw $0xffff, %di            # imm = 0xFFFF
movl $0xffff, %ebx           # imm = 0xFFFF
jmp 0x187246
movl %r13d, %edi
shrl $0xc, %edi
andl $0x3, %edi
movl %r13d, %ebx
shrl $0xa, %ebx
andl $0x3, %ebx
andl $0x3ff, %r13d           # imm = 0x3FF
cmpl %eax, %ebx
jne 0x18725b
cmpw %di, %r15w
jne 0x18725b
addl %r13d, %ecx
incl %r10d
cmpq %r10, %r8
ja 0x1871e9
movl %ecx, %eax
cmpw $0x3, %r14w
jne 0x187288
movzwl -0x2c(%rsp), %ecx
movw %cx, -0x2a(%rsp)
movw $0xffff, -0x2c(%rsp)    # imm = 0xFFFF
addq %rax, %rbp
movq 0x50(%rsp), %rdx
movq %r9, %rdi
movq %rsi, %r13
jmp 0x187326
leaq (%rax,%rbp), %r8
cmpw $0x2, %r14w
movq %r8, %rcx
cmoveq %rbp, %rcx
addq %r12, %rax
cmpl $0x3fff, %edx           # imm = 0x3FFF
movq %r9, %rdi
movq %rsi, %r13
ja 0x1872d6
movq -0x28(%rsp), %r15
cmpq %r15, %rbp
cmovgq %rbp, %r15
movq -0x20(%rsp), %r9
cmpq %r9, %r8
cmovgeq %r9, %r8
cmpq %r15, %r8
setle %bl
movl %r8d, %r9d
subl %r15d, %r9d
sete %r14b
orb %bl, %r14b
je 0x1872e3
movq %rax, %r12
movq %rcx, %rbp
movq 0x50(%rsp), %rdx
jmp 0x187326
movq 0x50(%rsp), %rdx
movq (%rdx), %rbx
subq (%r13), %rbx
cmpq %rbx, %r9
jbe 0x187320
subq %rbp, %r12
leaq (%r12,%r15), %r9
movq %r9, (%r13)
addq %r8, %r12
movq %r12, (%rdx)
movq -0x18(%rsp), %r9
addq %r9, %r15
movq 0x58(%rsp), %rsi
movq %r15, (%rsi)
addq %r9, %r8
movq 0x60(%rsp), %rsi
movq %r8, (%rsi)
movq %rax, %r12
movq %rcx, %rbp
movl %r10d, %r10d
cmpq %r10, -0x10(%rsp)
jle 0x18733b
cmpq -0x20(%rsp), %rbp
jl 0x18715e
movq -0x8(%rsp), %rax
movq %rbp, (%rax)
movq 0x38(%rsp), %rax
movq %r12, (%rax)
movq 0x40(%rsp), %rax
movq %r10, (%rax)
movq (%rdx), %rcx
xorl %eax, %eax
cmpq (%r13), %rcx
seta %al
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq