// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/hic.cpp
// Function: get_LCA(unsigned int, unsigned long, unsigned int, unsigned long, unsigned char*, unsigned long, MT*, bubble_type*, unsigned long*)
uint64_t get_LCA(uint32_t x, uint64_t xLen, uint32_t y, uint64_t yLen, uint8_t* dis, uint64_t n, MT* M, bubble_type* bub, uint64_t* min_rev)
{
    if(IF_BUB(x>>1, *bub) && IF_BUB(y>>1, *bub) && bub->index[x>>1] == bub->index[y>>1])
    {
        return get_LCA_bubble(x, xLen, y, yLen, dis, n, M, bub, min_rev);
    }
    else
    {
        memset(dis, -1, sizeof(uint8_t)*n);
    }

    uint32_t j, v;
    uint64_t u, d = (uint64_t)-1, tmp;
    uint8_t rev;
    
    v = x;
    for (j = 0; j < M->matrix.a[v].a.n; j++)
    {
        u = M->matrix.a[v].a.a[j] >> M->uID_shift;
        d = M->matrix.a[v].a.a[j] & M->dis_mode;
        dis[u] = dis[u] >> 4;
    }

    v = y;
    for (j = 0; j < M->matrix.a[v].a.n; j++)
    {
        u = M->matrix.a[v].a.a[j] >> M->uID_shift;
        d = M->matrix.a[v].a.a[j] & M->dis_mode;
        dis[u] = dis[u] >> 4;
    }

    uint64_t x_i = 0, y_i = 0, d_x, d_y, min_d = (uint64_t)-1;
    uint32_t min_j = (uint32_t)-1;
    (*min_rev) = (uint64_t)-1;
    for (j = 0; j < n; j++)
    {
        if(dis[j] != 0)
        {
            dis[j] = (uint8_t)-1;
            continue;
        }

        for (; x_i < M->matrix.a[x].a.n; x_i++)
        {
            u = M->matrix.a[x].a.a[x_i] >> M->uID_shift;
            d = M->matrix.a[x].a.a[x_i] & M->dis_mode;
            if(u == j) break;
        }
        if(x_i == M->matrix.a[x].a.n && M->matrix.a[x].a.n != 0) fprintf(stderr, "ERROR X\n");
        d_x = d;

        for (; y_i < M->matrix.a[y].a.n; y_i++)
        {
            u = M->matrix.a[y].a.a[y_i] >> M->uID_shift;
            d = M->matrix.a[y].a.a[y_i] & M->dis_mode;
            if(u == j) break;
        }
        if(y_i == M->matrix.a[y].a.n && M->matrix.a[y].a.n != 0) fprintf(stderr, "ERROR Y\n");
        d_y = d;


        tmp = LCA_distance(d_x, d_y, xLen, yLen, &rev);
        if(tmp < min_d) min_d = tmp, (*min_rev) = rev, min_j = j;
    }

    if(min_j == x || min_j == y) return (uint64_t)-1;

    return min_d;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %edx, %ebx
movl %edi, %r14d
movq 0xa0(%rsp), %r11
movq 0x98(%rsp), %rax
movq 0x90(%rsp), %rbp
movq (%rax), %rdi
movq 0x80(%rax), %rax
movl %r14d, %edx
shrl %edx
movl (%rdi,%rdx,4), %edx
incq %rax
cmpq %rdx, %rax
jbe 0x13cbf9
movl %ebx, %r10d
shrl %r10d
movl (%rdi,%r10,4), %edi
cmpq %rdi, %rax
setbe %al
cmpl %edi, %edx
setne %dl
orb %al, %dl
jne 0x13cbf9
movl %r14d, %edi
movl %ebx, %edx
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x13c89f
movq %r11, 0x10(%rsp)
movq %rsi, 0x18(%rsp)
movq %rcx, 0x28(%rsp)
movq %r8, 0x30(%rsp)
movq %r8, %rdi
movl $0xff, %esi
movq %r9, 0x20(%rsp)
movq %r9, %rdx
callq 0x20e0
movl %r14d, 0x40(%rsp)
movl %r14d, %r13d
movq 0x10(%rbp), %rcx
shlq $0x5, %r13
cmpq $0x0, (%rcx,%r13)
je 0x13cc72
movl $0x1, %eax
xorl %edx, %edx
movq 0x30(%rsp), %rdi
movq 0x10(%rcx,%r13), %rcx
movq (%rcx,%rdx,8), %rsi
movb 0x18(%rbp), %cl
movq %rsi, %rdx
shrq %cl, %rdx
movq 0x20(%rbp), %r14
shrb $0x4, (%rdi,%rdx)
movl %eax, %edx
movq 0x10(%rbp), %rcx
incl %eax
cmpq %rdx, (%rcx,%r13)
ja 0x13cc45
andq %rsi, %r14
jmp 0x13cc7e
movq $-0x1, %r14
movq 0x30(%rsp), %rdi
movq 0x28(%rsp), %r8
movq 0x20(%rsp), %r9
movq 0x18(%rsp), %r10
movq 0x10(%rsp), %r11
movl %ebx, %r15d
movq 0x10(%rbp), %rcx
shlq $0x5, %r15
cmpq $0x0, (%rcx,%r15)
je 0x13ccd6
movl $0x1, %eax
xorl %edx, %edx
movq 0x10(%rcx,%r15), %rcx
movq (%rcx,%rdx,8), %rsi
movb 0x18(%rbp), %cl
movq %rsi, %rdx
shrq %cl, %rdx
movq 0x20(%rbp), %r14
shrb $0x4, (%rdi,%rdx)
movl %eax, %edx
movq 0x10(%rbp), %rcx
incl %eax
cmpq %rdx, (%rcx,%r15)
ja 0x13ccab
andq %rsi, %r14
movl %ebx, 0x44(%rsp)
movq $-0x1, (%r11)
testq %r9, %r9
je 0x13ce4f
movq %r15, 0x50(%rsp)
xorl %r12d, %r12d
movq $-0x1, 0x38(%rsp)
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
xorl %ebx, %ebx
xorl %r15d, %r15d
movq %r13, 0x48(%rsp)
cmpb $0x0, (%rdi,%r12)
je 0x13cd1e
movb $-0x1, (%rdi,%r12)
jmp 0x13cdfb
movq 0x10(%rbp), %rcx
movq (%rcx,%r13), %rax
cmpq %rax, %r15
jae 0x13cd56
addq %r13, %rcx
movq 0x10(%rcx), %rdx
movq 0x18(%rbp), %rcx
movq (%rdx,%r15,8), %r13
movq %r13, %rsi
shrq %cl, %rsi
cmpq %r12, %rsi
je 0x13cd50
incq %r15
cmpq %r15, %rax
jne 0x13cd36
movq %rax, %r15
andq 0x20(%rbp), %r13
jmp 0x13cd59
movq %r14, %r13
cmpq %rax, %r15
setne %cl
testq %rax, %rax
sete %al
orb %cl, %al
je 0x13ce09
movq 0x10(%rbp), %rcx
movq 0x50(%rsp), %rdx
movq (%rcx,%rdx), %rax
cmpq %rax, %rbx
jae 0x13cdaa
addq %rdx, %rcx
movq 0x10(%rcx), %rdx
movq 0x18(%rbp), %rcx
movq (%rdx,%rbx,8), %r14
movq %r14, %rsi
shrq %cl, %rsi
cmpq %r12, %rsi
je 0x13cda4
incq %rbx
cmpq %rbx, %rax
jne 0x13cd8a
movq %rax, %rbx
andq 0x20(%rbp), %r14
jmp 0x13cdad
movq %r13, %r14
cmpq %rax, %rbx
setne %cl
testq %rax, %rax
sete %al
orb %cl, %al
je 0x13ce2c
movq %r10, %rax
subq %r13, %rax
movq %r8, %rcx
subq %r14, %rcx
cmpq %r14, %r13
cmovlq %rax, %rcx
movq %r14, %rax
cmovgq %r13, %rax
addq %rcx, %rax
cmpq 0x38(%rsp), %rax
jae 0x13cdf6
xorl %ecx, %ecx
cmpq %r14, %r13
setl %cl
movq %rcx, (%r11)
movq %rax, 0x38(%rsp)
movl %r12d, 0xc(%rsp)
movq 0x48(%rsp), %r13
incl %r12d
cmpq %r9, %r12
jb 0x13cd0d
jmp 0x13ce60
callq 0x1caa8
movq 0x10(%rsp), %r11
movq 0x18(%rsp), %r10
movq 0x20(%rsp), %r9
movq 0x28(%rsp), %r8
movq 0x30(%rsp), %rdi
jmp 0x13cd6d
callq 0x1cac4
movq 0x10(%rsp), %r11
movq 0x18(%rsp), %r10
movq 0x20(%rsp), %r9
movq 0x28(%rsp), %r8
movq 0x30(%rsp), %rdi
jmp 0x13cdbd
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
movq $-0x1, 0x38(%rsp)
movl 0xc(%rsp), %edx
cmpl 0x44(%rsp), %edx
movq $-0x1, %rcx
movq 0x38(%rsp), %rax
cmoveq %rcx, %rax
cmpl 0x40(%rsp), %edx
cmoveq %rcx, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq