// File: /workspace/github_cpp_makefile_star2_O1/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, %r13
movq %rdi, 0x8(%rsp)
xorl %r12d, %r12d
leaq 0x30(%rsp), %r8
movq %r12, (%r8)
movl %ecx, %esi
movq %r12, (%rsp)
leaq 0x2c(%rsp), %rbp
leaq 0x28(%rsp), %r15
leaq 0x24(%rsp), %r9
movq %rdx, %rdi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x13812e
movl (%rbp), %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %rax
sete %dl
movl (%r15), %ecx
cmpl $-0x1, %ecx
sete %sil
orb %dl, %sil
jne 0xf5131
movl %ecx, %ecx
xorl %edx, %edx
testb $0x1, %al
sete %dl
movq 0x10(%r13), %rsi
shrl %eax
leaq (%rax,%rax,4), %rax
leaq (%rsi,%rax,8), %rax
movl 0x4(%rax,%rdx,4), %edx
movq %rdx, 0x18(%rsp)
movl %edx, %ebp
xorl $0x1, %ebp
xorl %eax, %eax
testb $0x1, %cl
sete %al
shrl %ecx
leaq (%rcx,%rcx,4), %rcx
leaq (%rsi,%rcx,8), %rcx
movl 0x4(%rcx,%rax,4), %eax
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rax
movl 0x14(%rax), %edx
addl %edx, %edx
movq $0x0, (%r14)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x20e0
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
jne 0xf50ba
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x2, %rsi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
movq 0x10(%r14), %rdx
movq %rcx, %rax
incq %rax
movq %rax, (%r14)
movl %ebp, (%rdx,%rcx,4)
movq 0x10(%rsp), %rdi
jne 0xf5143
orb $0x1, (%rbx,%rdi)
xorl $0x1, %edi
movq $0x0, (%r14)
cmpq $0x0, 0x8(%r14)
jne 0xf5109
movq $0x2, 0x8(%r14)
movq %rdi, %r15
movq 0x10(%r14), %rdi
movl $0x8, %esi
callq 0x2370
movq %r15, %rdi
movq %rax, 0x10(%r14)
movq (%r14), %rcx
movq 0x10(%r14), %rdx
movq %rcx, %rax
incq %rax
movq %rax, (%r14)
movl %edi, (%rdx,%rcx,4)
jne 0xf521d
movq 0x18(%rsp), %rax
orb $0x2, (%rbx,%rax)
movl $0x1, %r12d
movl %r12d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq -0x1(%rax), %rcx
movq %rcx, (%r14)
movq 0x10(%r14), %rcx
movl -0x4(%rcx,%rax,4), %edx
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rax
movq 0x28(%rcx), %rcx
movq (%rcx,%rdx,8), %rcx
orb $0x1, (%rbx,%rdx)
testl %ecx, %ecx
je 0xf520c
movq %rcx, %rdx
shrq $0x20, %rdx
movl %ecx, %ecx
leaq (%rdx,%rdx,2), %rdx
leaq (%rax,%rdx,8), %rbp
addq $0x8, %rbp
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %r15
xorl %r13d, %r13d
cmpl $0x0, 0x4(%rbp,%r13)
js 0xf5203
movl (%rbp,%r13), %eax
xorl %r12d, %r12d
testb $0x1, (%rbx,%rax)
jne 0xf5131
movq %rax, %rcx
xorq $0x1, %rcx
testb $0x1, (%rbx,%rcx)
jne 0xf5131
cmpl %edi, %eax
je 0xf5203
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
jne 0xf51ed
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x2, %rsi
callq 0x2370
movq 0x10(%rsp), %rdi
movq %rax, 0x10(%r14)
movl (%rbp,%r13), %eax
movq (%r14), %rcx
movq 0x10(%r14), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r14)
movl %eax, (%rdx,%rcx,4)
addq $0x18, %r13
cmpq %r13, %r15
jne 0xf518f
movq (%r14), %rax
testq %rax, %rax
jne 0xf5143
jmp 0xf50d4
leaq -0x1(%rax), %rcx
movq %rcx, (%r14)
movq 0x10(%r14), %rcx
movl -0x4(%rcx,%rax,4), %edx
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rax
movq 0x28(%rcx), %rcx
movq (%rcx,%rdx,8), %rcx
orb $0x2, (%rbx,%rdx)
testl %ecx, %ecx
je 0xf52e7
movq %rcx, %rdx
shrq $0x20, %rdx
movl %ecx, %ecx
leaq (%rdx,%rdx,2), %rdx
leaq (%rax,%rdx,8), %rbp
addq $0x8, %rbp
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %r15
xorl %r13d, %r13d
cmpl $0x0, 0x4(%rbp,%r13)
js 0xf52de
movl (%rbp,%r13), %eax
xorl %r12d, %r12d
testb $0x3, (%rbx,%rax)
jne 0xf5131
movq %rax, %rcx
xorq $0x1, %rcx
testb $0x2, (%rbx,%rcx)
jne 0xf5131
cmpl 0x18(%rsp), %eax
je 0xf52de
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
jne 0xf52c8
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x2, %rsi
callq 0x2370
movq %rax, 0x10(%r14)
movl (%rbp,%r13), %eax
movq (%r14), %rcx
movq 0x10(%r14), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r14)
movl %eax, (%rdx,%rcx,4)
addq $0x18, %r13
cmpq %r13, %r15
jne 0xf5269
movq (%r14), %rax
testq %rax, %rax
jne 0xf521d
jmp 0xf5122