// File: /workspace/github_c_makefile_star2_O2/SimpleDoger[P]Darknet_CPU_Only/rnn_layer.c
// Function: backward_rnn_layer
void backward_rnn_layer(layer l, network net)
{
    network s = net;
    s.train = net.train;
    int i;
    layer input_layer = *(l.input_layer);
    layer self_layer = *(l.self_layer);
    layer output_layer = *(l.output_layer);

    increment_layer(&input_layer, l.steps-1);
    increment_layer(&self_layer, l.steps-1);
    increment_layer(&output_layer, l.steps-1);

    l.state += l.outputs*l.batch*l.steps;
    for (i = l.steps-1; i >= 0; --i) {
        copy_cpu(l.outputs * l.batch, input_layer.output, 1, l.state, 1);
        axpy_cpu(l.outputs * l.batch, 1, self_layer.output, 1, l.state, 1);

        s.input = l.state;
        s.delta = self_layer.delta;
        backward_connected_layer(output_layer, s);

        l.state -= l.outputs*l.batch;
        /*
           if(i > 0){
           copy_cpu(l.outputs * l.batch, input_layer.output - l.outputs*l.batch, 1, l.state, 1);
           axpy_cpu(l.outputs * l.batch, 1, self_layer.output - l.outputs*l.batch, 1, l.state, 1);
           }else{
           fill_cpu(l.outputs * l.batch, 0, l.state, 1);
           }
         */

        s.input = l.state;
        s.delta = self_layer.delta - l.outputs*l.batch;
        if (i == 0) s.delta = 0;
        backward_connected_layer(self_layer, s);

        copy_cpu(l.outputs*l.batch, self_layer.delta, 1, input_layer.delta, 1);
        if (i > 0 && l.shortcut) axpy_cpu(l.outputs*l.batch, 1, self_layer.delta, 1, self_layer.delta - l.outputs*l.batch, 1);
        s.input = net.input + i*l.inputs*l.batch;
        if(net.delta) s.delta = net.delta + i*l.inputs*l.batch;
        else s.delta = 0;
        backward_connected_layer(input_layer, s);

        increment_layer(&input_layer, -1);
        increment_layer(&self_layer, -1);
        increment_layer(&output_layer, -1);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x14e8, %rsp           # imm = 0x14E8
leaq 0x5f8(%rsp), %rbx
leaq 0x19c0(%rsp), %r12
movl $0x110, %edx            # imm = 0x110
movq %rbx, %rdi
movq %r12, %rsi
callq 0x32b0
movl 0xf8(%r12), %eax
movl %eax, 0xf8(%rbx)
movq 0x18b0(%rsp), %rsi
leaq 0xba8(%rsp), %r15
movl $0x4a0, %edx            # imm = 0x4A0
movq %r15, %rdi
callq 0x32b0
movq 0x18b8(%rsp), %rsi
leaq 0x708(%rsp), %r13
movl $0x4a0, %edx            # imm = 0x4A0
movq %r13, %rdi
callq 0x32b0
movq 0x18c0(%rsp), %rsi
leaq 0x1048(%rsp), %r14
movl $0x4a0, %edx            # imm = 0x4A0
movq %r14, %rdi
callq 0x32b0
movslq 0x15e0(%rsp), %rbx
leal -0x1(%rbx), %r8d
movl 0x58(%r15), %eax
imull %r8d, %eax
imull 0x48(%r15), %eax
cltq
shlq $0x2, %rax
movq 0x268(%r15), %rsi
addq %rax, %rsi
movq %rsi, 0x268(%r15)
addq %rax, 0x260(%r15)
addq %rax, 0x2c0(%r15)
addq %rax, 0x2c8(%r15)
movl 0x58(%r13), %eax
imull %r8d, %eax
imull 0x48(%r13), %eax
cltq
shlq $0x2, %rax
movq 0x268(%r13), %r15
addq %rax, %r15
movq %r15, 0x268(%r13)
movq 0x260(%r13), %rbp
addq %rax, %rbp
movq %rbp, 0x260(%r13)
addq %rax, 0x2c0(%r13)
addq %rax, 0x2c8(%r13)
movl 0x58(%r14), %eax
imull %r8d, %eax
imull 0x48(%r14), %eax
cltq
shlq $0x2, %rax
addq %rax, 0x268(%r14)
addq %rax, 0x260(%r14)
addq %rax, 0x2c0(%r14)
addq %rax, 0x2c8(%r14)
movl 0x1568(%rsp), %ecx
movl 0x1578(%rsp), %edx
imull %ecx, %edx
movl %edx, %eax
imull %ebx, %eax
cltq
shlq $0x2, %rax
addq 0x1700(%rsp), %rax
movq %rax, 0x5c0(%rsp)
movl 0x1564(%rsp), %eax
movl %eax, 0x5b8(%rsp)
movl %edx, 0x5b4(%rsp)
movslq %edx, %rax
movq %rax, %rdx
negq %rdx
movq 0xd8(%r12), %rdi
movq %rdi, 0x5e8(%rsp)
movq 0xe8(%r12), %rdi
movq %rdi, 0x5e0(%rsp)
imull 0x1574(%rsp), %ecx
movl %ecx, 0x5bc(%rsp)
imull %ecx, %r8d
shlq $0x2, %rax
negq %rax
movq %rax, 0x5f0(%rsp)
movq %rdx, 0x5d0(%rsp)
leaq (,%rdx,4), %rax
movq %rax, 0x5d8(%rsp)
movl 0x5b4(%rsp), %r14d
testq %rbx, %rbx
jle 0x2e11d
movl %r14d, %edi
movq %r8, %r13
movq %rbx, 0x5c8(%rsp)
pushq $0x1
popq %rbx
movl %ebx, %edx
movq 0x5c0(%rsp), %r12
movq %r12, %rcx
movl %ebx, %r8d
callq 0x1b5a3
movl %r14d, %edi
movss 0x1e144(%rip), %xmm0    # 0x4c008
movq %r15, %rsi
movl %ebx, %edx
movq %r12, %rcx
movl %ebx, %r8d
callq 0x1b3ed
movq %r12, 0x6d0(%rsp)
movq %rbp, 0x6e0(%rsp)
movl $0x94, %ecx
movq %rsp, %rdi
leaq 0x1048(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x4a0(%rsp), %rdi
pushq $0x22
popq %rbp
movq %rbp, %rcx
leaq 0x5f8(%rsp), %r15
movq %r15, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x245c7
addq 0x5f0(%rsp), %r12
movq %r12, 0x5c0(%rsp)
movq %r12, 0x6d0(%rsp)
movq 0x968(%rsp), %rax
addq 0x5d8(%rsp), %rax
cmpq $0x1, 0x5c8(%rsp)
movl $0x0, %ecx
cmoveq %rcx, %rax
movq %rax, 0x6e0(%rsp)
movl $0x94, %ecx
movq %rsp, %rdi
leaq 0x708(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x4a0(%rsp), %rdi
movq %rbp, %rcx
movq %r15, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x245c7
movq 0x968(%rsp), %r15
movq 0xe08(%rsp), %rcx
movl %r14d, %edi
movq %r15, %rsi
movl %ebx, %edx
movl %ebx, %r8d
movq 0x5c8(%rsp), %rbx
callq 0x1b5a3
cmpq $0x1, %rbx
je 0x2dfe4
cmpl $0x0, 0x5b8(%rsp)
je 0x2dfe4
movq 0x5d0(%rsp), %rax
leaq (%r15,%rax,4), %rcx
movl 0x5b4(%rsp), %edi
movss 0x1e032(%rip), %xmm0    # 0x4c008
movq %r15, %rsi
pushq $0x1
popq %rdx
movl %edx, %r8d
callq 0x1b3ed
movq 0x5e0(%rsp), %rcx
testq %rcx, %rcx
movslq %r13d, %r13
movq 0x5e8(%rsp), %rax
leaq (%rax,%r13,4), %rax
movq %rax, 0x6d0(%rsp)
leaq (%rcx,%r13,4), %rax
cmoveq %rcx, %rax
movq %rax, 0x6e0(%rsp)
movl $0x94, %ecx
movq %rsp, %rdi
leaq 0xba8(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x4a0(%rsp), %rdi
pushq $0x22
popq %rcx
leaq 0x5f8(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x245c7
movl 0xc00(%rsp), %eax
imull 0xbf0(%rsp), %eax
negl %eax
cltq
shlq $0x2, %rax
movq 0xe10(%rsp), %rsi
addq %rax, %rsi
movq %rsi, 0xe10(%rsp)
addq %rax, 0xe08(%rsp)
addq %rax, 0xe68(%rsp)
addq %rax, 0xe70(%rsp)
movl 0x760(%rsp), %eax
imull 0x750(%rsp), %eax
negl %eax
cltq
shlq $0x2, %rax
movq 0x970(%rsp), %r15
addq %rax, %r15
movq %r15, 0x970(%rsp)
movq 0x968(%rsp), %rbp
addq %rax, %rbp
movq %rbp, 0x968(%rsp)
addq %rax, 0x9c8(%rsp)
addq %rax, 0x9d0(%rsp)
movl 0x10a0(%rsp), %eax
imull 0x1090(%rsp), %eax
negl %eax
cltq
shlq $0x2, %rax
addq %rax, 0x12b0(%rsp)
addq %rax, 0x12a8(%rsp)
addq %rax, 0x1308(%rsp)
addq %rax, 0x1310(%rsp)
subl 0x5bc(%rsp), %r13d
decq %rbx
movq %r13, %r8
jmp 0x2de8a
addq $0x14e8, %rsp           # imm = 0x14E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq