// File: 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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x58(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movl $0x0, -0x40(%rbp)
movq -0x58(%rbp), %rcx
movl -0x40(%rbp), %eax
cmpl 0x8(%rcx), %eax
jge 0x8cc5
cmpl $0x0, -0x40(%rbp)
jne 0x8ba8
movq -0x58(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x3c(%rbp)
movslq -0x3c(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2030
movq %rax, -0x38(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jge 0x8ba3
movq -0x18(%rbp), %rax
movslq -0x44(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x38(%rbp), %rax
movslq -0x44(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x8b76
jmp 0x8c35
movq -0x58(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x40(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x3c(%rbp)
movslq -0x3c(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2030
movq %rax, -0x38(%rbp)
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jge 0x8c1c
movq -0x30(%rbp), %rax
movslq -0x48(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x38(%rbp), %rax
movslq -0x48(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x8bef
movq -0x30(%rbp), %rax
movq %rax, -0x60(%rbp)
cmpq $0x0, %rax
je 0x8c33
movq -0x60(%rbp), %rdi
callq 0x22e0
jmp 0x8c35
movq -0x58(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x40(%rbp), %rcx
movslq (%rax,%rcx,4), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2030
movq %rax, %rcx
movq -0x58(%rbp), %rax
movq %rcx, -0x30(%rbp)
movq 0x28(%rax), %rcx
movslq -0x40(%rbp), %rdx
movq (%rcx,%rdx,8), %rdi
movq -0x38(%rbp), %rsi
movq -0x30(%rbp), %rdx
movl -0x3c(%rbp), %r8d
movq 0x10(%rax), %rax
movslq -0x40(%rbp), %rcx
movl (%rax,%rcx,4), %r9d
xorl %eax, %eax
movl %eax, %ecx
callq 0x5e60
movq -0x38(%rbp), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, %rax
je 0x8cb5
movq -0x68(%rbp), %rdi
callq 0x22e0
jmp 0x8cb7
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x8b26
movq -0x30(%rbp), %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, %rax
je 0x8cdc
movq -0x70(%rbp), %rdi
callq 0x22e0
movl $0x0, -0x4c(%rbp)
movq -0x58(%rbp), %rcx
movl -0x4c(%rbp), %eax
movq 0x30(%rcx), %rcx
cmpl 0x8(%rcx), %eax
jge 0x8da2
movq -0x20(%rbp), %rax
movslq -0x4c(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl $0x0, -0x50(%rbp)
movq -0x58(%rbp), %rcx
movl -0x50(%rbp), %eax
movq 0x30(%rcx), %rcx
cmpl 0x4(%rcx), %eax
jge 0x8d6d
movq -0x58(%rbp), %rax
movq 0x30(%rax), %rax
movq 0x18(%rax), %rax
movslq -0x4c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x50(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x30(%rbp), %rax
movslq -0x50(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movq -0x20(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x8d0e
movq -0x58(%rbp), %rax
movq 0x30(%rax), %rax
movq 0x20(%rax), %rax
movslq -0x4c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x20(%rbp), %rax
movslq -0x4c(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x8ce3
movq -0x58(%rbp), %rax
movq 0x30(%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x6ba0
movq -0x58(%rbp), %rax
movq -0x20(%rbp), %rdi
movq 0x30(%rax), %rax
movl 0x8(%rax), %esi
callq 0x4af0
cmpl -0x24(%rbp), %eax
jne 0x8dd5
movl $0x1, -0x4(%rbp)
jmp 0x8ddc
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)