// File: /workspace/github_cpp_makefile_star2_O1/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 %r12
pushq %rbx
movq 0x30(%rsp), %r10
cmpl $0x1, %r8d
jne 0x14eb32
movl 0x10(%rcx), %eax
cmpq %rax, 0x20(%rdi)
jge 0x14ec63
testl %r8d, %r8d
jne 0x14eb42
cmpq $0x0, 0x20(%rdi)
js 0x14ec63
movq 0x18(%rcx), %rax
movq 0x20(%rdi), %r11
movq (%rax,%r11,8), %rax
movq 0x28(%rdx), %rbx
movq 0x40(%rdx), %r11
shrq $0x21, %rax
movl (%r11,%rax,4), %r14d
movl 0x4(%r11,%rax,4), %r11d
leaq (%rbx,%r14,4), %rbx
subl %r14d, %r11d
addl $-0x2, %r11d
cmpl $0x1, %r8d
jne 0x14eb7e
cmpq %r11, 0x28(%rdi)
jge 0x14ec38
testl %r8d, %r8d
jne 0x14eb8e
cmpq $0x0, 0x28(%rdi)
js 0x14ec38
movq 0x28(%rdi), %rax
movl 0x8(%rbx,%rax,4), %eax
shrl %eax
movq 0x18(%rsi), %r14
movl (%r14,%rax,4), %ebp
movl %ebp, %eax
shrl %eax
cmpl $-0x1, %ebp
cmovel %ebp, %eax
testq %r9, %r9
je 0x14ebb6
movq 0x20(%rdi), %r15
movq %r15, (%r9)
testq %r10, %r10
je 0x14ebc2
movq 0x28(%rdi), %r15
movq %r15, (%r10)
cmpl $0x1, %r8d
jne 0x14ebcc
incq 0x28(%rdi)
testl %r8d, %r8d
jne 0x14ebd5
decq 0x28(%rdi)
cmpl $-0x1, %ebp
je 0x14eb6e
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x1, %r8d
jne 0x14ebf5
movq 0x28(%rdi), %r15
cmpq $0x2, %r15
jl 0x14ebf5
movl (%rbx,%r15,4), %ebp
shrl %ebp
testl %r8d, %r8d
jne 0x14ec0e
movq 0x28(%rdi), %r15
leaq 0x2(%r15), %r12
cmpq %r11, %r12
jge 0x14ec0e
movl 0x10(%rbx,%r15,4), %ebp
shrl %ebp
cmpl $-0x1, %ebp
je 0x14ec29
movl %ebp, %r15d
movl (%r14,%r15,4), %r14d
movl %r14d, %ebp
shrl %ebp
cmpl $-0x1, %r14d
cmovel %r14d, %ebp
jmp 0x14ec2e
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl %eax, %ebp
je 0x14eb6e
jmp 0x14ec68
cmpl $0x1, %r8d
jne 0x14ec4a
incq 0x20(%rdi)
movq $0x0, 0x28(%rdi)
testl %r8d, %r8d
jne 0x14eb1f
decq 0x20(%rdi)
decq %r11
movq %r11, 0x28(%rdi)
jmp 0x14eb1f
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq