// File: /workspace/github_cpp_makefile_star2_O1/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, 0x14(%rsp)
movq %rdx, %r14
movq %rsi, 0x20(%rsp)
movq %rdi, 0x8(%rsp)
cmpl $0x0, 0x8(%rdi)
jle 0x6313
xorl %r15d, %r15d
xorl %r12d, %r12d
testq %r15, %r15
je 0x626a
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movslq -0x4(%rax,%r15,4), %rbp
leaq (,%rbp,8), %r13
testq %rbp, %rbp
movq %r13, %rdi
movq $-0x1, %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, %rbx
testq %rbp, %rbp
jle 0x6256
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x2160
testq %r12, %r12
movq %rbx, 0x18(%rsp)
je 0x62ab
movq %r12, %rdi
callq 0x2210
jmp 0x62ab
movq 0x8(%rsp), %rax
movslq 0x4(%rax), %rbp
leaq (,%rbp,8), %r12
testq %rbp, %rbp
movq %r12, %rdi
movq $-0x1, %rax
cmovsq %rax, %rdi
callq 0x2030
testq %rbp, %rbp
movq %rax, 0x18(%rsp)
jle 0x62ab
movq %rax, %rdi
movq 0x20(%rsp), %rsi
movq %r12, %rdx
callq 0x2160
movq 0x8(%rsp), %rbx
movq 0x10(%rbx), %r13
movslq (%r13,%r15,4), %rax
leaq (,%rax,8), %rdi
testq %rax, %rax
movq $-0x1, %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, %r12
movq 0x28(%rbx), %rax
movq (%rax,%r15,8), %rdi
movl (%r13,%r15,4), %r9d
movq 0x18(%rsp), %r13
movq %r13, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
movl %ebp, %r8d
callq 0x44f6
movq %r13, %rdi
callq 0x2210
incq %r15
movslq 0x8(%rbx), %rax
cmpq %rax, %r15
jl 0x620f
jmp 0x6316
xorl %r12d, %r12d
testq %r12, %r12
je 0x6323
movq %r12, %rdi
callq 0x2210
movq 0x8(%rsp), %rax
movq 0x30(%rax), %rdi
movslq 0x8(%rdi), %rax
testq %rax, %rax
jle 0x6392
movl 0x4(%rdi), %ecx
movq 0x20(%rdi), %rdx
xorl %esi, %esi
movq $0x0, (%r14,%rsi,8)
testl %ecx, %ecx
jle 0x6379
movq 0x18(%rdi), %r8
movq (%r8,%rsi,8), %r8
movsd (%r14,%rsi,8), %xmm0
xorl %r9d, %r9d
movsd (%r8,%r9,8), %xmm1
mulsd (%r12,%r9,8), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, (%r14,%rsi,8)
incq %r9
cmpq %r9, %rcx
jne 0x635b
movsd (%rdx,%rsi,8), %xmm0
addsd (%r14,%rsi,8), %xmm0
movsd %xmm0, (%r14,%rsi,8)
incq %rsi
cmpq %rax, %rsi
jne 0x633e
movq %r14, %rsi
callq 0x4cee
movq 0x8(%rsp), %rax
movq 0x30(%rax), %rax
movslq 0x8(%rax), %rax
testq %rax, %rax
jle 0x63f3
movsd (%r14), %xmm1
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
movsd (%r14,%rcx,8), %xmm2
xorps %xmm3, %xmm3
cvtsi2sd %ecx, %xmm3
movapd %xmm2, %xmm4
maxsd %xmm1, %xmm4
cmpltsd %xmm2, %xmm1
andpd %xmm1, %xmm3
andnpd %xmm0, %xmm1
orpd %xmm3, %xmm1
movapd %xmm1, %xmm0
incq %rcx
movapd %xmm4, %xmm1
cmpq %rcx, %rax
jne 0x63b7
cvttsd2si %xmm0, %ecx
jmp 0x63f5
xorl %ecx, %ecx
xorl %eax, %eax
cmpl 0x14(%rsp), %ecx
sete %al
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop