// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: if_recoverable(asg_t*, ma_ug_t*, bubble_type*, unsigned int, kvec_t_u32_warp*, unsigned char*)
int if_recoverable(asg_t *sg, ma_ug_t *ug, bubble_type* bub, uint32_t bid, kvec_t_u32_warp* stack, uint8_t* vis_flag)
{
    uint32_t beg_utg, sink_utg, *a = NULL, n, begRid, sinkRid, i;
    get_bubbles(bub, bid, &beg_utg, &sink_utg, &a, &n, NULL);
    if(beg_utg == (uint32_t)-1 || sink_utg == (uint32_t)-1) return 0;
    if(beg_utg&1)
    {
        begRid = ug->u.a[beg_utg>>1].start^1;
    }
    else
    {
        begRid = ug->u.a[beg_utg>>1].end^1;
    }

    if(sink_utg&1)
    {
        sinkRid = ug->u.a[sink_utg>>1].start;
    }
    else
    {
        sinkRid = ug->u.a[sink_utg>>1].end;
    }

    asg_arc_t *acur = NULL;
    uint32_t cur, ncur, v, n_vx = sg->n_seq<<1;
    stack->a.n = 0; 
    memset(vis_flag, 0, n_vx);

    kv_push(uint32_t, stack->a, begRid);
    while (stack->a.n > 0)
    {
        stack->a.n--;
        cur = stack->a.a[stack->a.n];
        ncur = asg_arc_n(sg, cur);
        acur = asg_arc_a(sg, cur);
        vis_flag[cur] |= 1;
        for (i = 0; i < ncur; i++)
        {
            if(acur[i].del) continue;
            if(vis_flag[acur[i].v]&1) return 0;
            if(vis_flag[acur[i].v^1]&1) return 0;
            if(acur[i].v == sinkRid) continue;
            kv_push(uint32_t, stack->a, acur[i].v);
        }
    }
    vis_flag[sinkRid] |= 1;



    begRid ^= 1; sinkRid ^= 1; v = begRid; begRid = sinkRid; sinkRid = v;
    
    stack->a.n = 0; 
    kv_push(uint32_t, stack->a, begRid);
    while (stack->a.n > 0)
    {
        stack->a.n--;
        cur = stack->a.a[stack->a.n];
        ncur = asg_arc_n(sg, cur);
        acur = asg_arc_a(sg, cur);
        vis_flag[cur] |= 2;
        for (i = 0; i < ncur; i++)
        {
            if(acur[i].del) continue;
            if(vis_flag[acur[i].v]&2) return 0;
            if(vis_flag[acur[i].v]&1) return 0;
            if(vis_flag[acur[i].v^1]&2) return 0;
            if(acur[i].v == sinkRid) continue;
            kv_push(uint32_t, stack->a, acur[i].v);
        }
    }
    vis_flag[sinkRid] |= 2;



    begRid ^= 1; sinkRid ^= 1; v = begRid; begRid = sinkRid; sinkRid = v;

    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, %rbx
movq %r8, %r14
movq %rsi, %r12
movq %rdi, 0x10(%rsp)
leaq 0x30(%rsp), %r8
andq $0x0, (%r8)
movl %ecx, %esi
andq $0x0, (%rsp)
leaq 0x2c(%rsp), %r13
leaq 0x28(%rsp), %rbp
leaq 0x24(%rsp), %r9
movq %rdx, %rdi
movq %r13, %rdx
movq %rbp, %rcx
callq 0xf6ab5
movl (%r13), %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, %rcx
sete %sil
movl (%rbp), %edx
cmpl $-0x1, %edx
sete %dil
xorl %eax, %eax
orb %sil, %dil
je 0xb7f05
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %edx, %eax
xorl %edx, %edx
testb $0x1, %cl
sete %dl
movq 0x10(%r12), %rsi
shrl %ecx
imulq $0x28, %rcx, %rcx
addq %rsi, %rcx
movl 0x4(%rcx,%rdx,4), %edx
movq %rdx, 0x18(%rsp)
movl %edx, %r15d
xorl $0x1, %r15d
movl %eax, %ecx
shrl %ecx
imulq $0x28, %rcx, %rcx
movl %eax, %edx
shrl %edx
imulq $0x28, %rdx, %rdx
testb $0x1, %al
leaq 0x4(%rsi,%rcx), %rax
leaq 0x8(%rsi,%rdx), %rcx
cmovneq %rax, %rcx
movl (%rcx), %ebp
movq 0x10(%rsp), %rax
movl 0x14(%rax), %edx
addl %edx, %edx
andq $0x0, (%r14)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x20e0
movq (%r14), %rcx
pushq $0x2
popq %rax
cmpq 0x8(%r14), %rcx
jne 0xb7f98
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x2, %rsi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
jmp 0xb7f9c
movq 0x10(%r14), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
movl %r15d, (%rax,%rcx,4)
testq %rdx, %rdx
je 0xb806f
leaq -0x1(%rdx), %rax
movq %rax, (%r14)
movq 0x10(%r14), %rax
movl -0x4(%rax,%rdx,4), %eax
movq 0x10(%rsp), %rdx
movq 0x8(%rdx), %rcx
movq 0x28(%rdx), %rdx
movq (%rdx,%rax,8), %rdx
movl %edx, %esi
shrq $0x20, %rdx
orb $0x1, (%rbx,%rax)
imulq $0x18, %rsi, %r12
imulq $0x18, %rdx, %rax
leaq (%rcx,%rax), %r13
addq $0x8, %r13
xorl %r15d, %r15d
cmpq %r15, %r12
je 0xb8067
cmpl $0x0, 0x4(%r13,%r15)
js 0xb8061
movl (%r13,%r15), %ecx
xorl %eax, %eax
testb $0x1, (%rbx,%rcx)
jne 0xb7ef6
movq %rcx, %rdx
xorq $0x1, %rdx
testb $0x1, (%rbx,%rdx)
jne 0xb7ef6
cmpl %ebp, %ecx
je 0xb8061
movq (%r14), %rdx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rdx
jne 0xb8057
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movl (%r13,%r15), %ecx
movq (%r14), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, (%r14)
movl %ecx, (%rax,%rdx,4)
addq $0x18, %r15
jmp 0xb7fed
movq (%r14), %rdx
jmp 0xb7fa7
orb $0x1, (%rbx,%rbp)
andq $0x0, (%r14)
xorl $0x1, %ebp
cmpq $0x0, 0x8(%r14)
movq 0x10(%r14), %rax
je 0xb8089
xorl %edx, %edx
jmp 0xb80a3
movq $0x2, 0x8(%r14)
pushq $0x8
popq %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rdx
leaq 0x1(%rdx), %rcx
movq %rcx, (%r14)
movl %ebp, (%rax,%rdx,4)
testq %rcx, %rcx
je 0xb8177
leaq -0x1(%rcx), %rax
movq %rax, (%r14)
movq 0x10(%r14), %rax
movl -0x4(%rax,%rcx,4), %eax
movq 0x10(%rsp), %rdx
movq 0x8(%rdx), %rcx
movq 0x28(%rdx), %rdx
movq (%rdx,%rax,8), %rdx
movl %edx, %esi
shrq $0x20, %rdx
orb $0x2, (%rbx,%rax)
imulq $0x18, %rsi, %r12
imulq $0x18, %rdx, %rax
leaq (%rcx,%rax), %r13
addq $0x8, %r13
xorl %r15d, %r15d
cmpq %r15, %r12
je 0xb816f
cmpl $0x0, 0x4(%r13,%r15)
js 0xb8169
movl (%r13,%r15), %ecx
xorl %eax, %eax
testb $0x3, (%rbx,%rcx)
jne 0xb7ef6
movq %rcx, %rdx
xorq $0x1, %rdx
testb $0x2, (%rbx,%rdx)
jne 0xb7ef6
cmpl 0x18(%rsp), %ecx
je 0xb8169
movq (%r14), %rdx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rdx
jne 0xb815f
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movl (%r13,%r15), %ecx
movq (%r14), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, (%r14)
movl %ecx, (%rax,%rdx,4)
addq $0x18, %r15
jmp 0xb80f3
movq (%r14), %rcx
jmp 0xb80ad
movq 0x18(%rsp), %rax
orb $0x2, (%rbx,%rax)
pushq $0x1
popq %rax
jmp 0xb7ef6