// File: /workspace/github_cpp_makefile_star2_O1/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 $0x48, %rsp
movq %rcx, %r10
movq 0x88(%rsp), %rax
movl %edi, %ecx
shrl %ecx
movq (%rax), %r9
movq 0x28(%rax), %r11
movl (%r9,%rcx,4), %ecx
movq 0x40(%rax), %rax
movl (%rax,%rcx,4), %ecx
movl (%r11,%rcx,4), %eax
movl %eax, 0x4(%rsp)
incl %ecx
movl (%r11,%rcx,4), %ecx
xorl $0x1, %eax
movl %eax, 0x4(%rsp)
xorl $0x1, %ecx
movl %ecx, 0x8(%rsp)
cmpl %ecx, %eax
jbe 0x13c8f6
movl %ecx, 0x4(%rsp)
movl %eax, 0x8(%rsp)
movl 0x4(%rsp), %eax
movb $-0x1, %cl
movb %cl, (%r8,%rax)
movl 0x8(%rsp), %eax
movb %cl, (%r8,%rax)
movl %edi, %r15d
movq 0x80(%rsp), %r11
movq 0x10(%r11), %rcx
shlq $0x5, %r15
cmpq $0x0, (%rcx,%r15)
je 0x13c95f
movl $0x1, %eax
xorl %r9d, %r9d
movq %r11, %rbx
movq 0x10(%rcx,%r15), %rcx
movq (%rcx,%r9,8), %r11
movb 0x18(%rbx), %cl
movq %r11, %r9
shrq %cl, %r9
movq 0x20(%rbx), %r14
shrb $0x4, (%r8,%r9)
movl %eax, %r9d
movq 0x10(%rbx), %rcx
incl %eax
cmpq %r9, (%rcx,%r15)
ja 0x13c92d
andq %r11, %r14
movq %rbx, %r11
jmp 0x13c966
movq $-0x1, %r14
movl %edi, 0x1c(%rsp)
movq 0x90(%rsp), %r9
movl %edx, %ebx
movq 0x10(%r11), %rcx
shlq $0x5, %rbx
cmpq $0x0, (%rcx,%rbx)
je 0x13c9bf
movl $0x1, %eax
xorl %edi, %edi
movq 0x10(%rcx,%rbx), %rcx
movq (%rcx,%rdi,8), %r9
movb 0x18(%r11), %cl
movq %r9, %rdi
shrq %cl, %rdi
movq 0x20(%r11), %r14
shrb $0x4, (%r8,%rdi)
movl %eax, %edi
movq 0x10(%r11), %rcx
incl %eax
cmpq %rdi, (%rcx,%rbx)
ja 0x13c98a
andq %r9, %r14
movq 0x90(%rsp), %r9
movq %rbx, 0x40(%rsp)
movl %edx, 0x18(%rsp)
movq $-0x1, (%r9)
movq $-0x1, 0x10(%rsp)
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
xorl %r12d, %r12d
xorl %r13d, %r13d
xorl %ebx, %ebx
movq %r15, 0x30(%rsp)
movq %r8, 0x28(%rsp)
movq %rsi, 0x20(%rsp)
movl 0x4(%rsp,%r12,4), %ebp
cmpb $0x0, (%r8,%rbp)
je 0x13ca0d
movb $-0x1, (%r8,%rbp)
jmp 0x13cae9
movq 0x10(%r11), %rcx
movq (%rcx,%r15), %rax
cmpq %rax, %rbx
jae 0x13ca45
addq %r15, %rcx
movq 0x10(%rcx), %rdx
movq 0x18(%r11), %rcx
movq (%rdx,%rbx,8), %r15
movq %r15, %rdi
shrq %cl, %rdi
cmpq %rbp, %rdi
je 0x13ca3f
incq %rbx
cmpq %rbx, %rax
jne 0x13ca25
movq %rax, %rbx
andq 0x20(%r11), %r15
jmp 0x13ca48
movq %r14, %r15
cmpq %rax, %rbx
setne %cl
testq %rax, %rax
sete %al
orb %cl, %al
je 0x13cafc
movq 0x10(%r11), %rcx
movq 0x40(%rsp), %rdx
movq (%rcx,%rdx), %rax
cmpq %rax, %r13
jae 0x13ca99
addq %rdx, %rcx
movq 0x10(%rcx), %rdx
movq 0x18(%r11), %rcx
movq (%rdx,%r13,8), %r14
movq %r14, %rdi
shrq %cl, %rdi
cmpq %rbp, %rdi
je 0x13ca93
incq %r13
cmpq %r13, %rax
jne 0x13ca79
movq %rax, %r13
andq 0x20(%r11), %r14
jmp 0x13ca9c
movq %r15, %r14
cmpq %rax, %r13
setne %cl
testq %rax, %rax
sete %al
orb %cl, %al
je 0x13cb26
movq %rsi, %rax
subq %r15, %rax
movq %r10, %rcx
subq %r14, %rcx
cmpq %r14, %r15
cmovlq %rax, %rcx
movq %r14, %rax
cmovgq %r15, %rax
addq %rcx, %rax
cmpq 0x10(%rsp), %rax
jae 0x13cae4
xorl %ecx, %ecx
cmpq %r14, %r15
setl %cl
movq %rcx, (%r9)
movq %rax, 0x10(%rsp)
movl %ebp, 0xc(%rsp)
movq 0x30(%rsp), %r15
leaq 0x1(%r12), %rax
testq %r12, %r12
movq %rax, %r12
je 0x13c9f7
jmp 0x13cb54
movq %r10, %r14
callq 0x1ca70
movq 0x90(%rsp), %r9
movq 0x80(%rsp), %r11
movq 0x20(%rsp), %rsi
movq %r14, %r10
movq 0x28(%rsp), %r8
jmp 0x13ca5c
movq %r10, 0x38(%rsp)
callq 0x1ca8c
movq 0x90(%rsp), %r9
movq 0x80(%rsp), %r11
movq 0x20(%rsp), %rsi
movq 0x38(%rsp), %r10
movq 0x28(%rsp), %r8
jmp 0x13caac
movl 0xc(%rsp), %edx
cmpl 0x18(%rsp), %edx
movq $-0x1, %rcx
movq 0x10(%rsp), %rax
cmoveq %rcx, %rax
cmpl 0x1c(%rsp), %edx
cmoveq %rcx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq