// File: 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
movq %rsp, %rbp
subq $0x70, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movq %r9, -0x38(%rbp)
cmpl $0x1, -0x2c(%rbp)
jne 0x1f0ae3
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x28(%rbp), %rcx
movl 0x10(%rcx), %ecx
cmpq %rcx, %rax
jl 0x1f0ae3
jmp 0x1f0cfa
cmpl $0x0, -0x2c(%rbp)
jne 0x1f0af9
movq -0x10(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jge 0x1f0af9
jmp 0x1f0cfa
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x20(%rcx), %rcx
movq (%rax,%rcx,8), %rsi
shrq $0x21, %rsi
leaq -0x3c(%rbp), %rdx
leaq -0x40(%rbp), %rcx
leaq -0x50(%rbp), %r8
leaq -0x54(%rbp), %r9
xorl %eax, %eax
movq $0x0, (%rsp)
callq 0x1c4b20
cmpl $0x1, -0x2c(%rbp)
jne 0x1f0b4f
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x54(%rbp), %ecx
cmpq %rcx, %rax
jl 0x1f0b4f
jmp 0x1f0cac
cmpl $0x0, -0x2c(%rbp)
jne 0x1f0b65
movq -0x10(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jge 0x1f0b65
jmp 0x1f0cac
movq -0x50(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl (%rax,%rcx,4), %eax
shrl %eax
movl %eax, -0x44(%rbp)
movl $0xffffffff, -0x58(%rbp) # imm = 0xFFFFFFFF
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x44(%rbp), %ecx
cmpl $-0x1, (%rax,%rcx,4)
je 0x1f0ba4
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x44(%rbp), %ecx
movl (%rax,%rcx,4), %eax
shrl %eax
movl %eax, -0x58(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x1f0bba
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x38(%rbp), %rax
movq %rcx, (%rax)
cmpq $0x0, 0x10(%rbp)
je 0x1f0bd0
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rcx
movq 0x10(%rbp), %rax
movq %rcx, (%rax)
cmpl $0x1, -0x2c(%rbp)
jne 0x1f0be6
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x28(%rax)
cmpl $0x0, -0x2c(%rbp)
jne 0x1f0bfc
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x28(%rax)
cmpl $-0x1, -0x58(%rbp)
jne 0x1f0c07
jmp 0x1f0b34
movl $0xffffffff, -0x44(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x5c(%rbp) # imm = 0xFFFFFFFF
cmpl $0x1, -0x2c(%rbp)
jne 0x1f0c3e
movq -0x10(%rbp), %rax
cmpq $0x2, 0x28(%rax)
jl 0x1f0c3e
movq -0x50(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x28(%rcx), %rcx
subq $0x2, %rcx
movl (%rax,%rcx,4), %eax
shrl %eax
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x1f0c6d
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
addq $0x2, %rax
movl -0x54(%rbp), %ecx
cmpq %rcx, %rax
jge 0x1f0c6d
movq -0x50(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl 0x8(%rax,%rcx,4), %eax
shrl %eax
movl %eax, -0x44(%rbp)
cmpl $-0x1, -0x44(%rbp)
je 0x1f0c97
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x44(%rbp), %ecx
cmpl $-0x1, (%rax,%rcx,4)
je 0x1f0c97
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x44(%rbp), %ecx
movl (%rax,%rcx,4), %eax
shrl %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x58(%rbp), %eax
jne 0x1f0ca4
jmp 0x1f0b34
movl -0x58(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x1f0d01
cmpl $0x1, -0x2c(%rbp)
jne 0x1f0cce
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x20(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x28(%rax)
cmpl $0x0, -0x2c(%rbp)
jne 0x1f0cf5
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x20(%rax)
movl -0x54(%rbp), %eax
movl %eax, %ecx
subq $0x1, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
jmp 0x1f0ac4
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw (%rax,%rax)