// File: /workspace/github_cpp_makefile_star2_O3/xbai0624[P]dnn/src/Network.cpp
// Function: Network::ConstructLayers(TrainingType)
void Network::ConstructLayers(TrainingType _training_type) // test purelly fully connected, deep network, 100 layers
{
    // Network structure: {Image->Input->FC->FC->FC->Output}
    TrainingType training_type = _training_type;
    training_type = TrainingType::NewTraining;
    double learning_rate = 0.1;

    // 1) Data interface, this is a tool class, for data prepare
    DataInterface *data_interface = new DataInterface("simulation_data/data_signal_train.dat", "simulation_data/data_cosmic_train.dat", LayerDimension::_1D, std::pair<int, int>(900, 1), 200);

    // 3) input layer   ID=0
    LayerParameterList p_list0(LayerType::input, LayerDimension::_1D, data_interface, 0, 0, 
            std::pair<size_t, size_t>(0, 0), learning_rate, false, 0, 0, Regularization::Undefined, 0, ActuationFuncType::Undefined, training_type, false);
    Layer* layer_input = new ConstructLayer(p_list0);
    // NOTE: a data_interface class pointer must be passed to input layer before calling input_layer->Init() function
    //       because Initialization rely on data_interface
    layer_input->Init();

    // 4) 49 middle layers: fc layer 
    const int NLayer = 20;
    Layer *l_mid[NLayer];
    // layer 0
    LayerParameterList p_list1(LayerType::fullyConnected, LayerDimension::_1D, data_interface, 890, 0, 
            std::pair<size_t, size_t>(0, 0), learning_rate, false, 2, 0.5, Regularization::L2, 0.1, ActuationFuncType::Relu, training_type, true);
    l_mid[0] = new ConstructLayer(p_list1);
    l_mid[0]->SetPrevLayer(layer_input);
    l_mid[0]->Init();
    // layer 1 - layer 46
    for(int i=1;i<NLayer;i++)
    {
        int n_neurons = 890 - ((890-4)/(NLayer-1))*i;
        LayerParameterList p_list_mid(LayerType::fullyConnected, LayerDimension::_1D, data_interface, n_neurons, 0, 
                std::pair<size_t, size_t>(0, 0), learning_rate, false, 2, 0.5, Regularization::L2, 0.1, ActuationFuncType::Relu, training_type, true);
        l_mid[i] = new ConstructLayer(p_list_mid);
        l_mid[i]->SetPrevLayer(l_mid[i-1]);
        l_mid[i]->Init();
    }


    // 5) output layer ID = 7
    LayerParameterList p_list_output(LayerType::output, LayerDimension::_1D, data_interface, 2, 0, 
            std::pair<size_t, size_t>(0, 0), learning_rate, false, 0, 0., Regularization::L2, 0.1, ActuationFuncType::SoftMax, training_type, false);
    Layer* layer_output = new ConstructLayer(p_list_output);
    layer_output -> SetPrevLayer(l_mid[NLayer-1]);
    layer_output -> Init();

    // 6) connect all layers; SetNextLayer must be after all layers have finished initialization
    //                        input layer not needed to set, input layer has no update on w & b
    //                        input layer is just for data transfer (prepare)
    //l1->SetNextLayer(l2);
    //l2->SetNextLayer(l3);
    //l3->SetNextLayer(layer_output); // This line is ugly, to be improved
    for(int i=0;i<NLayer-1;i++)
        l_mid[i]->SetNextLayer(l_mid[i+1]);
    l_mid[NLayer-1]->SetNextLayer(layer_output);

    // 7) save all constructed layers
    __inputLayer = layer_input;
    __outputLayer = layer_output;
    //__middleAndOutputLayers.push_back(l1); // must be pushed in order
    //__middleAndOutputLayers.push_back(l2); // must be pushed in order
    //__middleAndOutputLayers.push_back(l3);
    //__middleAndOutputLayers.push_back(layer_output);
    for(int i=0;i<NLayer;i++)
        __middleAndOutputLayers.push_back(l_mid[i]);
    __middleAndOutputLayers.push_back(layer_output);
    //cout<<"total number of layers: "<<__middleAndOutputLayers.size()<<endl;
    __dataInterface = data_interface;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2c8, %rsp            # imm = 0x2C8
movq %rdi, %r14
movl $0xc8, %edi
callq 0x4270
movq %rax, %rbx
leaq 0xb581(%rip), %rsi      # 0x2a9f7
leaq 0xb5a0(%rip), %rdx      # 0x2aa1d
movabsq $0x100000384, %r8       # imm = 0x100000384
movq %rax, %rdi
xorl %ecx, %ecx
movl $0xc8, %r9d
callq 0x19726
movl $0x490, %edi            # imm = 0x490
callq 0x4270
movq %rax, %r12
leaq 0x260(%rsp), %rsi
movq $0x3, (%rsi)
movq %rbx, 0x8(%rsi)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsi)
movups %xmm0, 0x20(%rsi)
movabsq $0x3fb999999999999a, %r13 # imm = 0x3FB999999999999A
movq %r13, 0x30(%rsi)
xorl %eax, %eax
movb %al, 0x38(%rsi)
movl $0x0, 0x3c(%rsi)
movq %rax, 0x40(%rsi)
movl $0x2, 0x48(%rsi)
movq %rax, 0x50(%rsi)
movq $0x4, 0x58(%rsi)
movb %al, 0x60(%rsi)
movl $0xd, %ecx
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
movq %r12, %rdi
callq 0x4a54
movq (%r12), %rax
movq %r12, %rdi
callq *0x10(%rax)
movl $0x490, %edi            # imm = 0x490
callq 0x4270
movq %rax, %r15
xorl %eax, %eax
leaq 0x1f8(%rsp), %rsi
movq %rax, (%rsi)
movq %rbx, 0x68(%rsp)
movq %rbx, 0x8(%rsi)
movq $0x37a, 0x10(%rsi)      # imm = 0x37A
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rsi)
movq %rax, 0x28(%rsi)
movq %r13, 0x30(%rsi)
movb $0x0, 0x38(%rsi)
movl $0x2, 0x3c(%rsi)
movabsq $0x3fe0000000000000, %rax # imm = 0x3FE0000000000000
movq %rax, 0x40(%rsi)
movl $0x0, 0x48(%rsi)
movq %r13, 0x50(%rsi)
movq $0x3, 0x58(%rsi)
movb $0x1, 0x60(%rsi)
movl $0xd, %ecx
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
movq %r15, %rdi
callq 0x4a54
movq %r14, 0x78(%rsp)
leaq 0xf8(%rsp), %rbx
movq %r15, -0x8(%rbx)
movq (%r15), %rax
movq %r15, %rdi
movq %r12, 0x70(%rsp)
movq %r12, %rsi
callq *0x1e8(%rax)
movq (%r15), %rax
movq %r15, %rdi
callq *0x10(%rax)
movl $0x34c, %r14d           # imm = 0x34C
xorl %r13d, %r13d
movq %r15, %r12
movl $0x490, %edi            # imm = 0x490
callq 0x4270
movq %rax, %rbp
movq %r13, 0x88(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x90(%rsp)
movq %r14, 0x98(%rsp)
leaq 0xa0(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %r13, 0x10(%rax)
movabsq $0x3fb999999999999a, %rax # imm = 0x3FB999999999999A
movq %rax, 0xb8(%rsp)
movb $0x0, 0xc0(%rsp)
movl $0x2, 0xc4(%rsp)
movabsq $0x3fe0000000000000, %rcx # imm = 0x3FE0000000000000
movq %rcx, 0xc8(%rsp)
movl $0x0, 0xd0(%rsp)
movq %rax, 0xd8(%rsp)
movq $0x3, 0xe0(%rsp)
movb $0x1, 0xe8(%rsp)
movl $0xd, %ecx
movq %rsp, %rdi
leaq 0x88(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rbp, %rdi
callq 0x4a54
movq %rbp, (%rbx)
movq (%rbp), %rax
movq %rbp, %rdi
movq %r12, %rsi
callq *0x1e8(%rax)
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x10(%rax)
addq $-0x2e, %r14
addq $0x8, %rbx
movq %rbp, %r12
cmpq $-0x1e, %r14
jne 0x1f5ca
movl $0x490, %edi            # imm = 0x490
callq 0x4270
movq %rax, %r13
leaq 0x190(%rsp), %rsi
movq $0x4, (%rsi)
movq 0x68(%rsp), %r14
movq %r14, 0x8(%rsi)
movq $0x2, 0x10(%rsi)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rsi)
movq $0x0, 0x28(%rsi)
movabsq $0x3fb999999999999a, %rcx # imm = 0x3FB999999999999A
movq %rcx, 0x30(%rsi)
xorl %eax, %eax
movb %al, 0x38(%rsi)
movups %xmm0, 0x3c(%rsi)
movq %rcx, 0x50(%rsi)
movq $0x1, 0x58(%rsi)
movb %al, 0x60(%rsi)
movl $0xd, %ecx
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
movq %r13, %rdi
callq 0x4a54
movq %r13, 0x80(%rsp)
movq 0x188(%rsp), %r12
movq (%r13), %rax
movq %r13, %rdi
movq %r12, %rsi
callq *0x1e8(%rax)
movq (%r13), %rax
movq %r13, %rdi
callq *0x10(%rax)
movl $0x1, %ebx
movq 0xf0(%rsp,%rbx,8), %rbp
movq (%r15), %rax
movq %r15, %rdi
movq %rbp, %rsi
callq *0x1f0(%rax)
incq %rbx
movq %rbp, %r15
cmpq $0x14, %rbx
jne 0x1f755
movq (%r12), %rax
movq %r12, %rdi
movq %r13, %rsi
callq *0x1f0(%rax)
movq 0x78(%rsp), %r15
movq 0x70(%rsp), %rax
movq %rax, 0x18(%r15)
movq %r13, 0x20(%r15)
xorl %ebx, %ebx
leaq (%rsp,%rbx), %rdx
addq $0xf0, %rdx
movq 0x8(%r15), %rsi
cmpq 0x10(%r15), %rsi
je 0x1f7be
movq (%rdx), %rax
movq %rax, (%rsi)
addq $0x8, 0x8(%r15)
jmp 0x1f7c6
movq %r15, %rdi
callq 0x1ffec
addq $0x8, %rbx
cmpq $0xa0, %rbx
jne 0x1f79c
movq 0x8(%r15), %rsi
cmpq 0x10(%r15), %rsi
je 0x1f7e7
movq %r13, (%rsi)
addq $0x8, 0x8(%r15)
jmp 0x1f7f7
leaq 0x80(%rsp), %rdx
movq %r15, %rdi
callq 0x1ffec
movq %r14, 0x28(%r15)
addq $0x2c8, %rsp            # imm = 0x2C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movq %r13, %rdi
jmp 0x1f833
movq %rax, %r14
movq %r15, %rdi
jmp 0x1f833
movq %rax, %r14
movq %r12, %rdi
jmp 0x1f833
movq %rax, %r14
movq %rbx, %rdi
jmp 0x1f833
movq %rax, %r14
movq %rbp, %rdi
callq 0x4230
movq %r14, %rdi
callq 0x43e0