// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/hic.cpp
// Function: get_LCA_bubble(unsigned int, unsigned long, unsigned int, unsigned long, unsigned char*, unsigned long, MT*, bubble_type*, unsigned long*)
uint64_t get_LCA_bubble(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)
{
    uint32_t j, v, k;
    uint64_t u, d = (uint64_t)-1, tmp;
    uint8_t rev;
    uint32_t root[2], a_n, *a;
    get_bubbles(bub, bub->index[x>>1], &root[0], &root[1], &a, &a_n, NULL);
    root[0] ^= 1; root[1] ^= 1;
    if(root[0] > root[1])
    {
        k = root[0];
        root[0] = root[1];
        root[1] = k;
    }

    dis[root[0]] = (uint8_t)-1;
    dis[root[1]] = (uint8_t)-1;

    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 (k = 0; k < 2; k++)
    {
        j = root[k];
        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 $0x68, %rsp
movq %r8, 0x28(%rsp)
movq %rcx, 0x20(%rsp)
movl %edx, %r14d
movq %rsi, 0x18(%rsp)
movl %edi, %ebp
movq 0xa8(%rsp), %rdi
movq (%rdi), %rax
movl %ebp, %ecx
shrl %ecx
movl (%rax,%rcx,4), %esi
leaq 0x14(%rsp), %rcx
andq $0x0, (%rsp)
leaq 0x10(%rsp), %r13
leaq 0x60(%rsp), %r8
leaq 0x5c(%rsp), %r9
movq %r13, %rdx
callq 0xf6ab5
movl (%r13), %ecx
movl 0x4(%r13), %eax
xorl $0x1, %ecx
movl %ecx, (%r13)
xorl $0x1, %eax
movl %eax, 0x4(%r13)
cmpl %eax, %ecx
jbe 0xfb18b
movl %eax, 0x10(%rsp)
movl %ecx, 0x14(%rsp)
movl %ecx, %edx
jmp 0xfb18f
movl %eax, %edx
movl %ecx, %eax
movq 0xa0(%rsp), %rbx
movl %eax, %eax
movb $-0x1, %cl
movq 0x28(%rsp), %r9
movb %cl, (%r9,%rax)
movl %edx, %eax
movb %cl, (%r9,%rax)
movl %ebp, %esi
xorl %eax, %eax
pushq $-0x1
popq %r12
shlq $0x5, %rsi
movq 0x20(%rsp), %r10
movq 0x18(%rsp), %r11
movl %eax, %edx
movq 0x10(%rbx), %rax
cmpq %rdx, (%rax,%rsi)
jbe 0xfb1ee
addq %rsi, %rax
movq 0x10(%rax), %rax
movq (%rax,%rdx,8), %r12
movb 0x18(%rbx), %cl
movq %r12, %rax
shrq %cl, %rax
andq 0x20(%rbx), %r12
shrb $0x4, (%r9,%rax)
leal 0x1(%rdx), %eax
jmp 0xfb1c0
movl %r14d, %edi
xorl %ecx, %ecx
shlq $0x5, %rdi
movl %ecx, %edx
cmpq %rdx, (%rax,%rdi)
jbe 0xfb225
addq %rdi, %rax
movq 0x10(%rax), %rax
movq (%rax,%rdx,8), %r12
movb 0x18(%rbx), %cl
movq %r12, %rax
shrq %cl, %rax
andq 0x20(%rbx), %r12
shrb $0x4, (%r9,%rax)
leal 0x1(%rdx), %ecx
movq 0x10(%rbx), %rax
jmp 0xfb1f7
movq %rdi, 0x48(%rsp)
movq %rsi, 0x50(%rsp)
movl %r14d, 0x38(%rsp)
movl %ebp, 0x3c(%rsp)
movq 0xb0(%rsp), %rax
orq $-0x1, (%rax)
pushq $-0x1
popq %rax
movl %eax, 0xc(%rsp)
pushq $-0x1
popq %rax
movq %rax, 0x30(%rsp)
xorl %r13d, %r13d
xorl %ebp, %ebp
xorl %r14d, %r14d
cmpq $0x2, %r13
je 0xfb3e5
movl 0x10(%rsp,%r13,4), %r15d
cmpb $0x0, (%r9,%r15)
je 0xfb27b
movb $-0x1, (%r9,%r15)
jmp 0xfb369
movq 0x10(%rbx), %rax
movq 0x50(%rsp), %rcx
leaq (%rax,%rcx), %rsi
movq (%rax,%rcx), %rdx
cmpq %rdx, %rbp
movq %rdx, %rdi
cmovaq %rbp, %rdi
cmpq %rdx, %rbp
jae 0xfb2ba
movq 0x10(%rsi), %rcx
movq (%rcx,%rbp,8), %r12
movb 0x18(%rbx), %cl
movq %r12, %r8
shrq %cl, %r8
andq 0x20(%rbx), %r12
cmpq %r15, %r8
je 0xfb2bd
incq %rbp
jmp 0xfb296
movq %rdi, %rbp
movq %r12, %r8
cmpq %rdx, %rbp
setne %cl
testq %rdx, %rdx
sete %dl
orb %cl, %dl
je 0xfb371
movq 0x48(%rsp), %rcx
movq (%rax,%rcx), %rdx
addq %rcx, %rax
cmpq %rdx, %r14
movq %rdx, %rsi
cmovaq %r14, %rsi
movq %r8, %r12
cmpq %rdx, %r14
jae 0xfb311
movq 0x10(%rax), %rcx
movq (%rcx,%r14,8), %r12
movb 0x18(%rbx), %cl
movq %r12, %rdi
shrq %cl, %rdi
andq 0x20(%rbx), %r12
cmpq %r15, %rdi
je 0xfb314
incq %r14
jmp 0xfb2ed
movq %rsi, %r14
cmpq %rdx, %r14
setne %al
testq %rdx, %rdx
sete %cl
orb %al, %cl
je 0xfb3ab
movq %r11, %rax
subq %r8, %rax
movq %r10, %rcx
subq %r12, %rcx
cmpq %r12, %r8
cmovlq %rax, %rcx
movq %r12, %rax
cmovgq %r8, %rax
addq %rcx, %rax
cmpq 0x30(%rsp), %rax
jae 0xfb369
xorl %ecx, %ecx
cmpq %r12, %r8
setl %cl
movq 0xb0(%rsp), %rdx
movq %rcx, (%rdx)
movq %rax, 0x30(%rsp)
movl %r15d, 0xc(%rsp)
incq %r13
jmp 0xfb25b
movq 0x157c68(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0xf2275(%rip), %rdi     # 0x1ed5f7
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
movq %r8, %r12
callq 0x2410
movq %r12, %r8
movq 0x18(%rsp), %r11
movq 0x20(%rsp), %r10
movq 0x28(%rsp), %r9
movq 0x10(%rbx), %rax
jmp 0xfb2d4
movq 0x157c2e(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0xf2244(%rip), %rdi     # 0x1ed600
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
movq %r8, 0x40(%rsp)
callq 0x2410
movq 0x40(%rsp), %r8
movq 0x18(%rsp), %r11
movq 0x20(%rsp), %r10
movq 0x28(%rsp), %r9
jmp 0xfb328
movl 0xc(%rsp), %edx
cmpl 0x38(%rsp), %edx
pushq $-0x1
popq %rcx
movq 0x30(%rsp), %rax
cmoveq %rcx, %rax
cmpl 0x3c(%rsp), %edx
cmoveq %rcx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq