// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: Merge_DAGCon(Graph*)
void Merge_DAGCon(Graph* DAGCon)
{
    ///using the length of edge representing if it has been visited
    ///in default, the length of edge is 0
    RSet iter_node, iter_edge;
    long long flag;

    Node* currentNode;
    Node* outNode;
    Edge* edge;
    Edge* e_forward;
    Edge* e_backward;

    // int num_way = Real_Length(Output_Edges(G_Node(*DAGCon, DAGCon->s_start_nodeID)));
    // if(num_way > 2)
    // {
    //     print_graph(DAGCon);
    // }
    

    
    ///at begining, only the start node has no in-node
    currentNode = &(G_Node(*DAGCon, DAGCon->s_start_nodeID));
    Push_Node(DAGCon, &currentNode);
    

    
    while (Pop_Node(DAGCon, &currentNode))
    {
        ///merge in-node
        Merge_In_Nodes(DAGCon, currentNode);
        ///merge out-node
        Merge_Out_Nodes(DAGCon, currentNode);

        clear_RSet(&iter_edge);
        ///for all out-edges of currentNode, set as visited
        while (getOutputEdges(&iter_edge, DAGCon, currentNode, &edge))
        {
            get_bi_direction_edges(DAGCon, edge, &e_forward, &e_backward);
            Visit(*e_forward) = 1;
            Visit(*e_backward) = 1;
        }
        


        ///check all out-node of currentNode
        clear_RSet(&iter_node);
        while(getOutputNodes(&iter_node, DAGCon, currentNode, &outNode))
        {

            ///for each outNode, check if all in-edges have been visited
            flag = 0;
            clear_RSet(&iter_edge);
            while (getInputEdges(&iter_edge, DAGCon, outNode, &edge))
            {
                if(Visit(*edge) == 0)
                {
                    flag = 1;
                    break;
                }
            }
            //if all in-edges of Out_node have already been visited, push it to queue
            if(flag == 0)
            {
                Push_Node(DAGCon, &outNode);
            }
        }
    }



    // if(num_way > 2)
    // {
    //     print_graph(DAGCon);
    //     fprintf(stderr, "****************************note*****************\n\n");
    // }


    ///debug_DAGCon(DAGCon);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movq 0xa8(%rdi), %rax
shlq $0x7, %rax
addq 0x18(%rdi), %rax
movq %rsp, %rsi
movq %rax, (%rsi)
callq 0x52a74
leaq 0x10(%rsp), %r15
leaq 0x8(%rsp), %r12
leaq 0x30(%rsp), %r13
leaq 0x28(%rsp), %rbp
pushq $0x1
popq %r14
movq %rbx, %rdi
movq %rsp, %rsi
callq 0x52a89
testl %eax, %eax
je 0x1f2d3
movq (%rsp), %rsi
movq %rbx, %rdi
callq 0x1eb95
movq (%rsp), %rsi
movq %rbx, %rdi
callq 0x1e8ed
andq $0x0, 0x10(%rsp)
movq (%rsp), %rdx
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rcx
callq 0x52a35
testl %eax, %eax
je 0x1f27b
movq 0x8(%rsp), %rsi
movq %rbx, %rdi
leaq 0x20(%rsp), %rdx
leaq 0x18(%rsp), %rcx
callq 0x187aa
movq 0x20(%rsp), %rax
movq %r14, 0x20(%rax)
movq 0x18(%rsp), %rax
movq %r14, 0x20(%rax)
jmp 0x1f23a
andq $0x0, 0x30(%rsp)
movq (%rsp), %rdx
movq %r13, %rdi
movq %rbx, %rsi
movq %rbp, %rcx
callq 0x5288b
testl %eax, %eax
je 0x1f209
andq $0x0, 0x10(%rsp)
movq 0x28(%rsp), %rdx
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rcx
callq 0x18a44
testl %eax, %eax
je 0x1f2c6
movq 0x8(%rsp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x1f2a1
jmp 0x1f281
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x52a74
jmp 0x1f281
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq