// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %rax
movq %rdi, %rbx
movq 0xa8(%rdi), %rax
shlq $0x7, %rax
movq 0x18(%rdi), %rcx
movq 0x78(%rdi), %r15
movq (%rcx,%rax), %r14
movq 0x88(%rdi), %rsi
cmpq %rsi, %r15
jl 0x4c496
movq 0x70(%rbx), %rax
testq %rax, %rax
je 0x4c45d
subq %rax, %r15
leaq (,%r15,8), %rdx
movq 0x90(%rbx), %rdi
leaq (%rdi,%rax,8), %rsi
cmpq %rax, %r15
jle 0x4c485
callq 0x2360
jmp 0x4c48a
leaq (%rsi,%rsi), %rax
movq %rax, 0x88(%rbx)
movq 0x90(%rbx), %rdi
shlq $0x4, %rsi
callq 0x2370
movq %rax, 0x90(%rbx)
movq 0x78(%rbx), %r15
jmp 0x4c496
callq 0x2160
movq $0x0, 0x70(%rbx)
movq %r15, 0x78(%rbx)
movq 0x90(%rbx), %rax
movq %r14, (%rax,%r15,8)
movq 0x70(%rbx), %rax
movq 0x78(%rbx), %rcx
incq %rcx
movq %rcx, 0x78(%rbx)
cmpq %rcx, %rax
je 0x4c6cc
movl $0x1, %r15d
movq 0x18(%rbx), %r12
movq 0x90(%rbx), %rcx
movq (%rcx,%rax,8), %r13
incq %rax
movq %rax, 0x70(%rbx)
shlq $0x7, %r13
leaq (%r12,%r13), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4bb5b
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4b4c7
movq 0x50(%r12,%r13), %rax
testq %rax, %rax
jle 0x4c6be
movq 0x40(%r14), %rcx
xorl %edx, %edx
imulq $0x38, %rdx, %rsi
movq 0x8(%rcx,%rsi), %rdi
cmpq $-0x1, %rdi
je 0x4c581
leaq (%rcx,%rsi), %r10
movq (%rcx,%rsi), %r11
movq 0x28(%rcx,%rsi), %r8
movq 0x18(%rbx), %r9
movq %r11, %r12
shlq $0x7, %r12
movq 0x40(%r9,%r12), %rsi
cmpq 0x50(%r9,%r12), %r8
jae 0x4c559
imulq $0x38, %r8, %r12
cmpq %r11, (%rsi,%r12)
jne 0x4c559
addq %rsi, %r12
cmpq %rdi, 0x8(%r12)
jne 0x4c559
shlq $0x7, %rdi
imulq $0x38, 0x30(%r10), %r11
jmp 0x4c565
imulq $0x38, %r8, %r11
movq 0x30(%r10), %r8
shlq $0x7, %rdi
addq 0x60(%r9,%rdi), %r11
incq %rdx
imulq $0x38, %r8, %rdi
movq %r15, 0x20(%rsi,%rdi)
movq %r15, 0x20(%r11)
cmpq %rax, %rdx
jl 0x4c507
jmp 0x4c58d
incq %rdx
cmpq %rax, %rdx
jne 0x4c507
testq %rax, %rax
jle 0x4c6be
xorl %r12d, %r12d
movq 0x40(%r14), %rdx
movq %rax, %rcx
negq %rcx
imulq $0x38, %r12, %rsi
incq %r12
addq %rsi, %rdx
addq $0x8, %rdx
movq (%rdx), %rsi
cmpq $-0x1, %rsi
jne 0x4c5d3
leaq (%rcx,%r12), %rsi
incq %rsi
incq %r12
addq $0x38, %rdx
cmpq $0x1, %rsi
jne 0x4c5b1
jmp 0x4c6be
movq 0x18(%rbx), %rdx
shlq $0x7, %rsi
leaq (%rdx,%rsi), %rcx
movq 0x70(%rdx,%rsi), %rdx
movq %rdx, %rsi
negq %rsi
xorl %edi, %edi
cmpq %rdx, %rdi
jge 0x4c62b
movq 0x60(%rcx), %r8
imulq $0x38, %rdi, %r9
addq %r9, %r8
addq $0x20, %r8
incq %rdi
cmpq $-0x1, -0x18(%r8)
jne 0x4c620
addq $0x38, %r8
leaq (%rsi,%rdi), %r9
incq %r9
incq %rdi
cmpq $0x1, %r9
jne 0x4c603
jmp 0x4c62b
cmpq $0x0, (%r8)
jne 0x4c5ec
jmp 0x4c6b5
movq (%rcx), %r13
movq 0x78(%rbx), %rbp
movq 0x88(%rbx), %rsi
cmpq %rsi, %rbp
jl 0x4c6a2
movq 0x70(%rbx), %rax
testq %rax, %rax
je 0x4c669
subq %rax, %rbp
leaq (,%rbp,8), %rdx
movq 0x90(%rbx), %rdi
leaq (%rdi,%rax,8), %rsi
cmpq %rax, %rbp
jle 0x4c691
callq 0x2360
jmp 0x4c696
leaq (%rsi,%rsi), %rax
movq %rax, 0x88(%rbx)
movq 0x90(%rbx), %rdi
shlq $0x4, %rsi
callq 0x2370
movq %rax, 0x90(%rbx)
movq 0x78(%rbx), %rbp
jmp 0x4c6a2
callq 0x2160
movq $0x0, 0x70(%rbx)
movq %rbp, 0x78(%rbx)
movq 0x90(%rbx), %rax
movq %r13, (%rax,%rbp,8)
incq 0x78(%rbx)
movq 0x50(%r14), %rax
cmpq %rax, %r12
jl 0x4c599
movq 0x70(%rbx), %rax
cmpq 0x78(%rbx), %rax
jne 0x4c4bf
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq