// File: 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
movq %rsp, %rbp
subq $0xb0, %rsp
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movl %edi, -0xc(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq $-0x1, -0x58(%rbp)
movq 0x18(%rbp), %rdi
movq 0x18(%rbp), %rax
movq (%rax), %rax
movl -0xc(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movl %eax, %esi
leaq -0x6c(%rbp), %rdx
leaq -0x6c(%rbp), %rcx
addq $0x4, %rcx
leaq -0x78(%rbp), %r8
leaq -0x70(%rbp), %r9
xorl %eax, %eax
movq $0x0, (%rsp)
callq 0x1c4b20
movl -0x6c(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0x6c(%rbp)
movl -0x68(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0x68(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x68(%rbp), %eax
jbe 0x1cd35b
movl -0x6c(%rbp), %eax
movl %eax, -0x44(%rbp)
movl -0x68(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl -0x44(%rbp), %eax
movl %eax, -0x68(%rbp)
movq -0x30(%rbp), %rax
movl -0x6c(%rbp), %ecx
movb $-0x1, (%rax,%rcx)
movq -0x30(%rbp), %rax
movl -0x68(%rbp), %ecx
movb $-0x1, (%rax,%rcx)
movl -0xc(%rbp), %eax
movl %eax, -0x40(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq 0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x40(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rcx
cmpq (%rcx), %rax
jae 0x1cd417
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x40(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movl -0x3c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq 0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
shrq %cl, %rax
movq %rax, -0x50(%rbp)
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x40(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movl -0x3c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq 0x10(%rbp), %rcx
andq 0x20(%rcx), %rax
movq %rax, -0x58(%rbp)
movq -0x30(%rbp), %rax
movq -0x50(%rbp), %rcx
movzbl (%rax,%rcx), %eax
sarl $0x4, %eax
movb %al, %dl
movq -0x30(%rbp), %rax
movq -0x50(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1cd37e
movl -0x1c(%rbp), %eax
movl %eax, -0x40(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq 0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x40(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rcx
cmpq (%rcx), %rax
jae 0x1cd4bd
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x40(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movl -0x3c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq 0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
shrq %cl, %rax
movq %rax, -0x50(%rbp)
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x40(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movl -0x3c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq 0x10(%rbp), %rcx
andq 0x20(%rcx), %rax
movq %rax, -0x58(%rbp)
movq -0x30(%rbp), %rax
movq -0x50(%rbp), %rcx
movzbl (%rax,%rcx), %eax
sarl $0x4, %eax
movb %al, %dl
movq -0x30(%rbp), %rax
movq -0x50(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1cd424
movq $0x0, -0x80(%rbp)
movq $0x0, -0x88(%rbp)
movq $-0x1, -0xa0(%rbp)
movl $0xffffffff, -0xa4(%rbp) # imm = 0xFFFFFFFF
movq 0x20(%rbp), %rax
movq $-0x1, (%rax)
movl $0x0, -0x44(%rbp)
cmpl $0x2, -0x44(%rbp)
jae 0x1cd776
movl -0x44(%rbp), %eax
movl -0x6c(%rbp,%rax,4), %eax
movl %eax, -0x3c(%rbp)
movq -0x30(%rbp), %rax
movl -0x3c(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0x1cd52b
movq -0x30(%rbp), %rax
movl -0x3c(%rbp), %ecx
movb $-0x1, (%rax,%rcx)
jmp 0x1cd768
jmp 0x1cd52d
movq -0x80(%rbp), %rax
movq 0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0xc(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rcx
cmpq (%rcx), %rax
jae 0x1cd5c0
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0xc(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq -0x80(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
shrq %cl, %rax
movq %rax, -0x50(%rbp)
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0xc(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq -0x80(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x10(%rbp), %rcx
andq 0x20(%rcx), %rax
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rax
movl -0x3c(%rbp), %ecx
cmpq %rcx, %rax
jne 0x1cd5ad
jmp 0x1cd5c0
jmp 0x1cd5af
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x1cd52d
movq -0x80(%rbp), %rax
movq 0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0xc(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rcx
cmpq (%rcx), %rax
jne 0x1cd60b
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0xc(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
cmpq $0x0, (%rax)
je 0x1cd60b
movq 0x1f69e6(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x1bed20(%rip), %rsi    # 0x38c324
movb $0x0, %al
callq 0x22a0
movq -0x58(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x88(%rbp), %rax
movq 0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x1c(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rcx
cmpq (%rcx), %rax
jae 0x1cd6bc
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x1c(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq -0x88(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
shrq %cl, %rax
movq %rax, -0x50(%rbp)
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x1c(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq -0x88(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x10(%rbp), %rcx
andq 0x20(%rcx), %rax
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rax
movl -0x3c(%rbp), %ecx
cmpq %rcx, %rax
jne 0x1cd6a3
jmp 0x1cd6bc
jmp 0x1cd6a5
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
jmp 0x1cd616
movq -0x88(%rbp), %rax
movq 0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x1c(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rcx
cmpq (%rcx), %rax
jne 0x1cd70a
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x1c(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
cmpq $0x0, (%rax)
je 0x1cd70a
movq 0x1f68e7(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x1bec2a(%rip), %rsi    # 0x38c32d
movb $0x0, %al
callq 0x22a0
movq -0x58(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x90(%rbp), %rdi
movq -0x98(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x28(%rbp), %rcx
leaq -0x61(%rbp), %r8
callq 0x1cd220
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0xa0(%rbp), %rax
jae 0x1cd766
movq -0x60(%rbp), %rax
movq %rax, -0xa0(%rbp)
movzbl -0x61(%rbp), %eax
movl %eax, %ecx
movq 0x20(%rbp), %rax
movq %rcx, (%rax)
movl -0x3c(%rbp), %eax
movl %eax, -0xa4(%rbp)
jmp 0x1cd768
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x1cd4f7
movl -0xa4(%rbp), %eax
cmpl -0xc(%rbp), %eax
je 0x1cd78c
movl -0xa4(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jne 0x1cd796
movq $-0x1, -0x8(%rbp)
jmp 0x1cd7a1
movq -0xa0(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xb0, %rsp
popq %rbp
retq
nop