// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/hic.cpp
// Function: next_hap_label_id(block_phase_type*, G_partition*, bubble_type*, ma_utg_t*, int, long long*, long long*)
uint32_t next_hap_label_id(block_phase_type* b, G_partition* g_p, bubble_type* bub, ma_utg_t *u, 
int is_forward, long long* c_bid, long long* c_uid)
{
    uint32_t beg, sink, uid, *a, n, gid, pre_gid;
    while (1) ///while(b->bid < (long long)u->n)
    {
        if(is_forward == 1 && b->bid >= (long long)u->n) break;
        if(is_forward == 0 && b->bid < 0) break;

        get_bubbles(bub, u->a[b->bid]>>33, &beg, &sink, &a, &n, NULL);
        while (1) ///while (b->uid < (long long)n)
        {
            if(is_forward == 1 && b->uid >= (long long)n) break;
            if(is_forward == 0 && b->uid < 0) break;

            uid = a[b->uid]>>1;
            gid = (uint32_t)-1;
            if(g_p->index[uid] != (uint32_t)-1)
            {
                gid = g_p->index[uid]>>1;
            }
            if(c_bid) (*c_bid) = b->bid;
            if(c_uid) (*c_uid) = b->uid;

            if(is_forward == 1) b->uid++;
            if(is_forward == 0) b->uid--;
            if(gid == (uint32_t)-1) continue;


            pre_gid = uid = (uint32_t)-1;
            if(is_forward == 1 && (b->uid >= 2)) uid = a[b->uid - 2]>>1;
            if(is_forward == 0 && (b->uid + 2 < n)) uid = a[b->uid + 2]>>1;
            if(uid != (uint32_t)-1 && g_p->index[uid] != (uint32_t)-1) pre_gid = g_p->index[uid]>>1;

            if(pre_gid == gid) continue;

            return gid;
        }
        
        if(is_forward == 1) b->bid++, b->uid = 0;
        if(is_forward == 0) b->bid--, b->uid = (long long)n - (long long)1;
    }

    return (uint32_t)-1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, %rbx
movl %r8d, %ebp
movq %rcx, 0x10(%rsp)
movq %rdx, 0x20(%rsp)
movq %rdi, %r13
movq 0x70(%rsp), %r12
pushq $-0x1
popq %r10
movq %rsi, 0x18(%rsp)
movq 0x20(%r13), %rax
cmpl $0x1, %ebp
je 0x10dffb
testl %ebp, %ebp
jne 0x10e00c
movb $0x1, %r15b
testq %rax, %rax
jns 0x10e00f
jmp 0x10e149
movq 0x10(%rsp), %rcx
movl 0x10(%rcx), %ecx
cmpq %rcx, %rax
jge 0x10e149
xorl %r15d, %r15d
movq 0x10(%rsp), %rcx
movq 0x18(%rcx), %rcx
movq (%rcx,%rax,8), %rsi
shrq $0x21, %rsi
andq $0x0, (%rsp)
movq 0x20(%rsp), %rdi
leaq 0x34(%rsp), %rdx
leaq 0x30(%rsp), %rcx
leaq 0x28(%rsp), %r8
leaq 0xc(%rsp), %r9
movl %r10d, %r14d
callq 0xf6ab5
movl %r14d, %r10d
movq 0x18(%rsp), %r14
movl 0xc(%rsp), %ecx
movq 0x28(%rsp), %rdx
cmpl $0x1, %ebp
setne %sil
movq 0x28(%r13), %rax
cmpq %rcx, %rax
setl %dil
orb %sil, %dil
cmpb $0x1, %dil
jne 0x10e116
testq %rax, %rax
sets %sil
testb %sil, %r15b
jne 0x10e132
movl (%rdx,%rax,4), %eax
shrl %eax
movq 0x18(%r14), %rsi
movl (%rsi,%rax,4), %eax
testq %rbx, %rbx
je 0x10e09e
movq 0x20(%r13), %rdi
movq %rdi, (%rbx)
testq %r12, %r12
je 0x10e0ab
movq 0x28(%r13), %rdi
movq %rdi, (%r12)
cmpl $0x1, %ebp
jne 0x10e0b4
incq 0x28(%r13)
testb %r15b, %r15b
je 0x10e0bd
decq 0x28(%r13)
cmpl $-0x1, %eax
je 0x10e057
movl %r10d, %edi
cmpl $0x1, %ebp
jne 0x10e0de
movq 0x28(%r13), %r8
movl %r10d, %edi
cmpq $0x2, %r8
jl 0x10e0de
movl -0x8(%rdx,%r8,4), %edi
shrl %edi
shrl %eax
testb %r15b, %r15b
je 0x10e0fb
movq 0x28(%r13), %r8
leaq 0x2(%r8), %r9
cmpq %rcx, %r9
jge 0x10e0fb
movl 0x8(%rdx,%r8,4), %edi
shrl %edi
jmp 0x10e100
cmpl $-0x1, %edi
je 0x10e14c
movl %edi, %edi
movl (%rsi,%rdi,4), %esi
cmpl $-0x1, %esi
je 0x10e14c
shrl %esi
cmpl %eax, %esi
je 0x10e057
jmp 0x10e14c
movq 0x20(%r13), %rax
leaq 0x1(%rax), %rdx
movq %rdx, 0x20(%r13)
andq $0x0, 0x28(%r13)
testb %r15b, %r15b
je 0x10dfe1
jmp 0x10e139
movq 0x20(%r13), %rax
decq %rax
movq %rax, 0x20(%r13)
decq %rcx
movq %rcx, 0x28(%r13)
jmp 0x10dfe1
movl %r10d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq