// File: 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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0xa8(%rcx), %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x9fd40
movq -0x8(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x9fd80
cmpl $0x0, %eax
je 0x38e69
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x38600
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x382f0
leaq -0x18(%rbp), %rdi
callq 0x2cce0
movq -0x8(%rbp), %rsi
movq -0x28(%rbp), %rdx
leaq -0x18(%rbp), %rdi
leaq -0x38(%rbp), %rcx
callq 0x9fc50
cmpl $0x0, %eax
je 0x38de9
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
leaq -0x40(%rbp), %rdx
leaq -0x48(%rbp), %rcx
callq 0x2c720
movq -0x40(%rbp), %rax
movq $0x1, 0x20(%rax)
movq -0x48(%rbp), %rax
movq $0x1, 0x20(%rax)
jmp 0x38da0
leaq -0x10(%rbp), %rdi
callq 0x2cce0
movq -0x8(%rbp), %rsi
movq -0x28(%rbp), %rdx
leaq -0x10(%rbp), %rdi
leaq -0x30(%rbp), %rcx
callq 0x9f8b0
cmpl $0x0, %eax
je 0x38e64
movq $0x0, -0x20(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x2cce0
movq -0x8(%rbp), %rsi
movq -0x30(%rbp), %rdx
leaq -0x18(%rbp), %rdi
leaq -0x38(%rbp), %rcx
callq 0x2cd00
cmpl $0x0, %eax
je 0x38e4e
movq -0x38(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x38e4c
movq $0x1, -0x20(%rbp)
jmp 0x38e4e
jmp 0x38e1d
cmpq $0x0, -0x20(%rbp)
jne 0x38e62
movq -0x8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x9fd40
jmp 0x38df2
jmp 0x38d67
addq $0x50, %rsp
popq %rbp
retq
nop