// File: 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
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 0x18(%rbp), %rax
movq (%rax), %rax
movl -0xc(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movq 0x18(%rbp), %rcx
movq 0x80(%rcx), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jae 0x1cd88d
movq 0x18(%rbp), %rax
movq (%rax), %rax
movl -0x1c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movq 0x18(%rbp), %rcx
movq 0x80(%rcx), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jae 0x1cd88d
movq 0x18(%rbp), %rax
movq (%rax), %rax
movl -0xc(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movq 0x18(%rbp), %rcx
movq (%rcx), %rcx
movl -0x1c(%rbp), %edx
shrl %edx
movl %edx, %edx
cmpl (%rcx,%rdx,4), %eax
jne 0x1cd88d
movl -0xc(%rbp), %edi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
movq -0x38(%rbp), %r9
movq 0x10(%rbp), %r11
movq 0x18(%rbp), %r10
movq 0x20(%rbp), %rax
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x1cd2c0
movq %rax, -0x8(%rbp)
jmp 0x1cdcaf
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rdx
shlq $0x0, %rdx
movl $0xff, %esi
callq 0x2100
movq $-0x1, -0x50(%rbp)
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 0x1cd951
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, -0x48(%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, -0x50(%rbp)
movq -0x30(%rbp), %rax
movq -0x48(%rbp), %rcx
movzbl (%rax,%rcx), %eax
sarl $0x4, %eax
movb %al, %dl
movq -0x30(%rbp), %rax
movq -0x48(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1cd8b8
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 0x1cd9f7
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, -0x48(%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, -0x50(%rbp)
movq -0x30(%rbp), %rax
movq -0x48(%rbp), %rcx
movzbl (%rax,%rcx), %eax
sarl $0x4, %eax
movb %al, %dl
movq -0x30(%rbp), %rax
movq -0x48(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1cd95e
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq $-0x1, -0x88(%rbp)
movl $0xffffffff, -0x8c(%rbp) # imm = 0xFFFFFFFF
movq 0x20(%rbp), %rax
movq $-0x1, (%rax)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpq -0x38(%rbp), %rax
jae 0x1cdc84
movq -0x30(%rbp), %rax
movl -0x3c(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0x1cda5b
movq -0x30(%rbp), %rax
movl -0x3c(%rbp), %ecx
movb $-0x1, (%rax,%rcx)
jmp 0x1cdc76
jmp 0x1cda5d
movq -0x68(%rbp), %rax
movq 0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0xc(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rcx
cmpq (%rcx), %rax
jae 0x1cdaf0
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0xc(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq -0x68(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
shrq %cl, %rax
movq %rax, -0x48(%rbp)
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0xc(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq -0x68(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x10(%rbp), %rcx
andq 0x20(%rcx), %rax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rax
movl -0x3c(%rbp), %ecx
cmpq %rcx, %rax
jne 0x1cdadd
jmp 0x1cdaf0
jmp 0x1cdadf
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x1cda5d
movq -0x68(%rbp), %rax
movq 0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0xc(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rcx
cmpq (%rcx), %rax
jne 0x1cdb3b
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0xc(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
cmpq $0x0, (%rax)
je 0x1cdb3b
movq 0x1f64b6(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x1be7f0(%rip), %rsi    # 0x38c324
movb $0x0, %al
callq 0x22a0
movq -0x50(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x70(%rbp), %rax
movq 0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x1c(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rcx
cmpq (%rcx), %rax
jae 0x1cdbd6
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x1c(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
shrq %cl, %rax
movq %rax, -0x48(%rbp)
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x1c(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x10(%rbp), %rcx
andq 0x20(%rcx), %rax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rax
movl -0x3c(%rbp), %ecx
cmpq %rcx, %rax
jne 0x1cdbc3
jmp 0x1cdbd6
jmp 0x1cdbc5
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x1cdb43
movq -0x70(%rbp), %rax
movq 0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x1c(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rcx
cmpq (%rcx), %rax
jne 0x1cdc21
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x1c(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
cmpq $0x0, (%rax)
je 0x1cdc21
movq 0x1f63d0(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x1be713(%rip), %rsi    # 0x38c32d
movb $0x0, %al
callq 0x22a0
movq -0x50(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x78(%rbp), %rdi
movq -0x80(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x28(%rbp), %rcx
leaq -0x59(%rbp), %r8
callq 0x1cd220
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x88(%rbp), %rax
jae 0x1cdc74
movq -0x58(%rbp), %rax
movq %rax, -0x88(%rbp)
movzbl -0x59(%rbp), %eax
movl %eax, %ecx
movq 0x20(%rbp), %rax
movq %rcx, (%rax)
movl -0x3c(%rbp), %eax
movl %eax, -0x8c(%rbp)
jmp 0x1cdc76
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1cda2e
movl -0x8c(%rbp), %eax
cmpl -0xc(%rbp), %eax
je 0x1cdc9a
movl -0x8c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jne 0x1cdca4
movq $-0x1, -0x8(%rbp)
jmp 0x1cdcaf
movq -0x88(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xb0, %rsp
popq %rbp
retq
nopl (%rax)