// File: /workspace/github_cpp_makefile_star2_O2/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, %r14d
movq %rsi, 0x40(%rsp)
movl %edi, %ebx
movq 0xa0(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x98(%rsp), %rdx
movq 0x90(%rsp), %rbp
movq (%rdx), %rax
movq 0x80(%rdx), %rdx
movl %edi, %esi
shrl %esi
movl (%rax,%rsi,4), %esi
incq %rdx
cmpq %rsi, %rdx
jbe 0xfb485
movl %r14d, %edx
shrl %edx
cmpl (%rax,%rdx,4), %esi
jne 0xfb485
movl %ebx, %edi
movq 0x40(%rsp), %rsi
movl %r14d, %edx
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xfb110
movq %rcx, 0x18(%rsp)
movq %r8, %rdi
movl $0xff, %esi
movq %r9, 0x10(%rsp)
movq %r9, %rdx
movq %r8, 0x20(%rsp)
callq 0x20e0
movq 0x20(%rsp), %r9
movl %ebx, %esi
xorl %eax, %eax
pushq $-0x1
popq %r12
shlq $0x5, %rsi
movl %eax, %edx
movq 0x10(%rbp), %rax
cmpq %rdx, (%rax,%rsi)
jbe 0xfb4e3
addq %rsi, %rax
movq 0x10(%rax), %rax
movq (%rax,%rdx,8), %r12
movb 0x18(%rbp), %cl
movq %r12, %rax
shrq %cl, %rax
andq 0x20(%rbp), %r12
shrb $0x4, (%r9,%rax)
leal 0x1(%rdx), %eax
jmp 0xfb4b5
movq %rsi, 0x50(%rsp)
movl %r14d, %esi
xorl %ecx, %ecx
shlq $0x5, %rsi
movq 0x18(%rsp), %r10
movq 0x10(%rsp), %r11
movl %ecx, %edx
cmpq %rdx, (%rax,%rsi)
jbe 0xfb529
addq %rsi, %rax
movq 0x10(%rax), %rax
movq (%rax,%rdx,8), %r12
movb 0x18(%rbp), %cl
movq %r12, %rax
shrq %cl, %rax
andq 0x20(%rbp), %r12
shrb $0x4, (%r9,%rax)
leal 0x1(%rdx), %ecx
movq 0x10(%rbp), %rax
jmp 0xfb4fb
movq %rsi, 0x48(%rsp)
movl %r14d, 0x30(%rsp)
movl %ebx, 0x34(%rsp)
movq 0x38(%rsp), %rax
orq $-0x1, (%rax)
pushq $-0x1
popq %rax
movl %eax, 0x8(%rsp)
pushq $-0x1
popq %rax
movq %rax, 0x28(%rsp)
xorl %r15d, %r15d
xorl %r13d, %r13d
xorl %eax, %eax
movl %eax, %ebx
cmpq %r11, %rbx
jae 0xfb6d8
cmpb $0x0, (%r9,%rbx)
je 0xfb573
movb $-0x1, (%r9,%rbx)
jmp 0xfb66d
movl %eax, 0xc(%rsp)
movq 0x10(%rbp), %rax
movq 0x50(%rsp), %rcx
leaq (%rax,%rcx), %rsi
movq (%rax,%rcx), %rdx
cmpq %rdx, %r15
movq %rdx, %rdi
cmovaq %r15, %rdi
cmpq %rdx, %r15
jae 0xfb5b6
movq 0x10(%rsi), %rcx
movq (%rcx,%r15,8), %r12
movb 0x18(%rbp), %cl
movq %r12, %r8
shrq %cl, %r8
andq 0x20(%rbp), %r12
cmpq %rbx, %r8
je 0xfb5b9
incq %r15
jmp 0xfb592
movq %rdi, %r15
movq %r12, %r14
cmpq %rdx, %r15
setne %cl
testq %rdx, %rdx
sete %dl
orb %cl, %dl
je 0xfb674
movq 0x48(%rsp), %rcx
movq (%rax,%rcx), %rdx
addq %rcx, %rax
cmpq %rdx, %r13
movq %rdx, %rsi
cmovaq %r13, %rsi
movq %r14, %r12
cmpq %rdx, %r13
jae 0xfb60d
movq 0x10(%rax), %rcx
movq (%rcx,%r13,8), %r12
movb 0x18(%rbp), %cl
movq %r12, %rdi
shrq %cl, %rdi
andq 0x20(%rbp), %r12
cmpq %rbx, %rdi
je 0xfb610
incq %r13
jmp 0xfb5e9
movq %rsi, %r13
cmpq %rdx, %r13
setne %al
testq %rdx, %rdx
sete %cl
orb %al, %cl
je 0xfb6a8
movq 0x40(%rsp), %rax
subq %r14, %rax
movq %r10, %rcx
subq %r12, %rcx
cmpq %r12, %r14
cmovlq %rax, %rcx
movq %r12, %rax
cmovgq %r14, %rax
addq %rcx, %rax
cmpq 0x28(%rsp), %rax
jae 0xfb669
xorl %ecx, %ecx
cmpq %r12, %r14
setl %cl
movq 0x38(%rsp), %rdx
movq %rcx, (%rdx)
movq %rax, 0x28(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x8(%rsp)
jmp 0xfb66d
movl 0xc(%rsp), %eax
incl %eax
jmp 0xfb557
movq 0x157965(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0xf1f72(%rip), %rdi     # 0x1ed5f7
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
callq 0x2410
movq 0x10(%rsp), %r11
movq 0x18(%rsp), %r10
movq 0x20(%rsp), %r9
movq 0x10(%rbp), %rax
jmp 0xfb5d0
movq 0x157931(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0xf1f47(%rip), %rdi     # 0x1ed600
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
callq 0x2410
movq 0x10(%rsp), %r11
movq 0x18(%rsp), %r10
movq 0x20(%rsp), %r9
jmp 0xfb624
movl 0x8(%rsp), %edx
cmpl 0x30(%rsp), %edx
pushq $-0x1
popq %rcx
movq 0x28(%rsp), %rax
cmoveq %rcx, %rax
cmpl 0x34(%rsp), %edx
cmoveq %rcx, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq