// File: chhylp123[P]hifiasm/Correct.cpp
// Function: Merge_Out_Nodes(Graph*, Node*)
void Merge_Out_Nodes(Graph* DAGCon, Node* currentNode)
{
    ///if this node does not have any output, directly return
    if(Real_Length(Output_Edges((*currentNode))) == 0)
    {
        return;
    }

    RSet buf, out_buf;
    char Bases[4] = {'A', 'C', 'G', 'T'};
    char base;
    long long base_i, weight;
    int flag = 0;
    Node* get_node_1 = NULL;
    Node* out_node_of_get_node_1 = NULL;
    Node* consensus_node_1 = NULL;
    Edge* e_forward_1 = NULL;
    Edge* e_backward_1 = NULL;

    ///merge all base for each base
    for (base_i = 0; base_i < 4; base_i++)
    {
        base = Bases[base_i];

        clear_RSet(&buf);
        flag = 0;
        weight = 0;
        ///should use getOutputEdges, instead of getOutputNodes
        ///check all out-nodes of currentNode
        while(getOutputNodes(&buf, DAGCon, currentNode, &get_node_1))
        {
            ///check the corresponding node, this node must only have one in-node
            ///note this is the Real_Length, instead of the Input_Edges.length
            if((*get_node_1).base == base && Real_Length(Input_Edges(*get_node_1)) == 1)
            {
                if(flag == 0)
                {
                    flag = 1;
                    ///add a new node to merge all out-node
                    consensus_node_1 = get_node_1;
                    ///link consensus_node to currentNode
                    ///set the new edge to be visited
                    if(get_bi_Edge(DAGCon, currentNode, consensus_node_1, &e_forward_1, &e_backward_1))
                    {
                        Visit(*e_forward_1) = 1;
                        Visit(*e_backward_1) = 1;
                    }
                    else
                    {
                        fprintf(stderr, "error\n");
                    }

                    weight = (*e_forward_1).weight;
                }
                else
                {
                    flag++;
                    ///add the weight of get_node->currentNode
                    if(get_bi_Edge(DAGCon, currentNode, get_node_1, &e_forward_1, &e_backward_1))
                    {
                        weight = weight + (*e_forward_1).weight;
                    }
                    else
                    {
                        fprintf(stderr, "error\n");
                    }

                    ///process the out-nodes of get_node
                    clear_RSet(&out_buf);
                    while(getOutputNodes(&out_buf, DAGCon, get_node_1, &out_node_of_get_node_1))
                    {
                        ///link consensus_node to the out-nodes of get_node
                        if(get_bi_Edge(DAGCon, consensus_node_1, out_node_of_get_node_1, &e_forward_1, &e_backward_1))
                        {
                            Visit(*e_forward_1) = 1;
                            Visit(*e_backward_1) = 1;
                            (*e_forward_1).weight += get_Edge_Weight(DAGCon, get_node_1, out_node_of_get_node_1);
                            (*e_backward_1).weight = (*e_forward_1).weight;
                        }
                        else
                        {
                            add_bi_direction_edge(DAGCon, consensus_node_1, out_node_of_get_node_1, 
                            get_Edge_Weight(DAGCon, get_node_1, out_node_of_get_node_1), 1);
                        }
                    }

                    delete_Node_DAGCon(DAGCon, get_node_1);
                }
            }
        }

        if(flag > 1)
        {
            get_bi_Edge(DAGCon, currentNode, consensus_node_1, &e_forward_1, &e_backward_1);
            (*e_forward_1).weight = weight;
            (*e_backward_1).weight = (*e_forward_1).weight;
        }

        if(flag > 0)
        {
            Merge_Out_Nodes(DAGCon, consensus_node_1);   
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x10(%rbp), %rcx
subq 0x58(%rcx), %rax
cmpq $0x0, %rax
jne 0x3831e
jmp 0x385ee
movl 0x34e501(%rip), %eax    # 0x386825
movl %eax, -0x24(%rbp)
movl $0x0, -0x3c(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x30(%rbp)
cmpq $0x4, -0x30(%rbp)
jge 0x385ee
movq -0x30(%rbp), %rax
movb -0x24(%rbp,%rax), %al
movb %al, -0x25(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x2cce0
movl $0x0, -0x3c(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x8(%rbp), %rsi
movq -0x10(%rbp), %rdx
leaq -0x18(%rbp), %rdi
leaq -0x48(%rbp), %rcx
callq 0x9f8b0
cmpl $0x0, %eax
je 0x3858d
movq -0x48(%rbp), %rax
movsbl 0x18(%rax), %eax
movsbl -0x25(%rbp), %ecx
cmpl %ecx, %eax
jne 0x38588
movq -0x48(%rbp), %rax
movq 0x70(%rax), %rax
movq -0x48(%rbp), %rcx
subq 0x78(%rcx), %rax
cmpq $0x1, %rax
jne 0x38588
cmpl $0x0, -0x3c(%rbp)
jne 0x3844f
movl $0x1, -0x3c(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x58(%rbp), %rdx
leaq -0x60(%rbp), %rcx
leaq -0x68(%rbp), %r8
callq 0x2c680
cmpq $0x0, %rax
je 0x38426
movq -0x60(%rbp), %rax
movq $0x1, 0x20(%rax)
movq -0x68(%rbp), %rax
movq $0x1, 0x20(%rax)
jmp 0x3843e
movq 0x38bbb3(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x34a1ae(%rip), %rsi    # 0x3825e5
movb $0x0, %al
callq 0x22a0
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0x38586
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x48(%rbp), %rdx
leaq -0x60(%rbp), %rcx
leaq -0x68(%rbp), %r8
callq 0x2c680
cmpq $0x0, %rax
je 0x38489
movq -0x38(%rbp), %rax
movq -0x60(%rbp), %rcx
addq 0x10(%rcx), %rax
movq %rax, -0x38(%rbp)
jmp 0x384a1
movq 0x38bb50(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x34a14b(%rip), %rsi    # 0x3825e5
movb $0x0, %al
callq 0x22a0
leaq -0x20(%rbp), %rdi
callq 0x2cce0
movq -0x8(%rbp), %rsi
movq -0x48(%rbp), %rdx
leaq -0x20(%rbp), %rdi
leaq -0x50(%rbp), %rcx
callq 0x9f8b0
cmpl $0x0, %eax
je 0x38579
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x50(%rbp), %rdx
leaq -0x60(%rbp), %rcx
leaq -0x68(%rbp), %r8
callq 0x2c680
cmpq $0x0, %rax
je 0x38531
movq -0x60(%rbp), %rax
movq $0x1, 0x20(%rax)
movq -0x68(%rbp), %rax
movq $0x1, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x50(%rbp), %rdx
callq 0x9f9b0
movq %rax, %rcx
movq -0x60(%rbp), %rax
addq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x68(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x38574
movq -0x8(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x50(%rbp), %rdx
callq 0x9f9b0
movq -0x80(%rbp), %rdi
movq -0x78(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq %rax, %rcx
movl $0x1, %r8d
callq 0x2bbf0
jmp 0x384aa
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x9fa00
jmp 0x38588
jmp 0x3838c
cmpl $0x1, -0x3c(%rbp)
jle 0x385c8
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x58(%rbp), %rdx
leaq -0x60(%rbp), %rcx
leaq -0x68(%rbp), %r8
callq 0x2c680
movq -0x38(%rbp), %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x68(%rbp), %rax
movq %rcx, 0x10(%rax)
cmpl $0x0, -0x3c(%rbp)
jle 0x385db
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x382f0
jmp 0x385dd
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x3835e
addq $0x80, %rsp
popq %rbp
retq
nopw (%rax,%rax)