// File: /workspace/github_cpp_makefile_star2_O2/lipiji[P]PG_DEEP/deep.cpp
// Function: DBN::predict(double*, double*, int)
int DBN::predict(double *x, double *pred_y, int true_label)
{ 
    double *layer_input = NULL;
    double *pre_layer_input = NULL;
    int pre_size;

    // some x may be computed many times
    // which may be replaced by DP algoritham
    for(int j=0; j<n_layers; j++)
    {
        if(j == 0)
        {
            pre_size = n_features;
            pre_layer_input = new double[pre_size];
            for(int f=0; f<pre_size; f++)
                pre_layer_input[f] = x[f];
        }
        else
        {
            pre_size = hidden_layer_size[j-1];
            pre_layer_input = new double[pre_size];

            for(int f=0; f<pre_size; f++)
                pre_layer_input[f] = layer_input[f];
            delete[] layer_input;

        }
        layer_input = new double[hidden_layer_size[j]];
        rbm_layers[j]->activate_hidden(pre_layer_input, layer_input, NULL, pre_size, hidden_layer_size[j]);
        delete[] pre_layer_input;
    }
    delete[] layer_input;

    for(int i=0; i<lr_layer->n_labels; i++)
    {
        pred_y[i] = 0;
        for(int j=0; j<lr_layer->n_features; j++)
        {
            pred_y[i] += lr_layer->W[i][j] * layer_input[j];
        }
        pred_y[i] += lr_layer->b[i];
    }
    lr_layer->softmax(pred_y);
    /*
       cout <<true_label<<": ";
       for(int i=0; i<lr_layer->n_labels; i++)
       cout<<pred_y[i]<<" ";
       cout<<max_i_(pred_y, lr_layer->n_labels)<<"?????"<<true_label<<endl;
       */
    if(max_i_(pred_y, lr_layer->n_labels) == true_label)
        return 1;
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %ecx, 0xc(%rsp)
movq %rdx, %r14
movq %rsi, 0x18(%rsp)
movq %rdi, %rbp
xorl %r13d, %r13d
xorl %r12d, %r12d
movslq 0x8(%rbp), %rax
cmpq %rax, %r13
jge 0x571d
testq %r13, %r13
movq %r13, 0x20(%rsp)
je 0x56cf
movq 0x10(%rbp), %rax
movslq -0x4(%rax,%r13,4), %r15
leaq (,%r15,8), %rdi
testq %r15, %r15
pushq $-0x1
popq %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, %rbx
testl %r15d, %r15d
movl $0x0, %eax
movq %r15, 0x10(%rsp)
cmovgl %r15d, %eax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x566d
movsd (%r12,%rcx,8), %xmm0
movsd %xmm0, (%rbx,%rcx,8)
incq %rcx
jmp 0x5658
testq %r12, %r12
je 0x567a
movq %r12, %rdi
callq 0x2200
movq 0x10(%rbp), %r15
movq 0x20(%rsp), %r13
movslq (%r15,%r13,4), %rax
leaq (,%rax,8), %rdi
testq %rax, %rax
pushq $-0x1
popq %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, %r12
movq 0x28(%rbp), %rax
movq (%rax,%r13,8), %rdi
movl (%r15,%r13,4), %r9d
movq %rbx, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
movq 0x10(%rsp), %r8
callq 0x3dac
movq %rbx, %rdi
callq 0x2200
incq %r13
jmp 0x5607
movslq 0x4(%rbp), %r15
leaq (,%r15,8), %rdi
testq %r15, %r15
pushq $-0x1
popq %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, %rbx
testl %r15d, %r15d
movl $0x0, %eax
movq %r15, 0x10(%rsp)
cmovgl %r15d, %eax
xorl %ecx, %ecx
movq 0x18(%rsp), %rdx
cmpq %rcx, %rax
je 0x567a
movsd (%rdx,%rcx,8), %xmm0
movsd %xmm0, (%rbx,%rcx,8)
incq %rcx
jmp 0x5705
testq %r12, %r12
je 0x572a
movq %r12, %rdi
callq 0x2200
movq 0x30(%rbp), %rdi
movl 0x8(%rdi), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
xorl %edx, %edx
cmpq %rax, %rdx
je 0x578f
andq $0x0, (%r14,%rdx,8)
movl 0x4(%rdi), %esi
testl %esi, %esi
cmovlel %ecx, %esi
xorpd %xmm0, %xmm0
xorl %r8d, %r8d
cmpq %r8, %rsi
je 0x577b
movq 0x18(%rdi), %r9
movq (%r9,%rdx,8), %r9
movsd (%r9,%r8,8), %xmm1
mulsd (%r12,%r8,8), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, (%r14,%rdx,8)
incq %r8
jmp 0x5753
movq 0x20(%rdi), %rsi
addsd (%rsi,%rdx,8), %xmm0
movsd %xmm0, (%r14,%rdx,8)
incq %rdx
jmp 0x573a
movq %r14, %rsi
callq 0x44d4
movq 0x30(%rbp), %rax
movl 0x8(%rax), %esi
movq %r14, %rdi
callq 0x31e9
xorl %ecx, %ecx
cmpl 0xc(%rsp), %eax
sete %cl
movl %ecx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq