// File: /workspace/github_c_makefile_star2_O1/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 $0x14d8, %rsp           # imm = 0x14D8
leaq 0x19b0(%rsp), %r13
leaq 0x1510(%rsp), %r14
leaq 0x5e8(%rsp), %rbx
movl $0x110, %edx            # imm = 0x110
movq %rbx, %rdi
movq %r13, %rsi
callq 0x32c0
movl 0xf8(%r13), %eax
movl %eax, 0xf8(%rbx)
movq 0x390(%r14), %rsi
leaq 0xb98(%rsp), %r12
movl $0x4a0, %edx            # imm = 0x4A0
movq %r12, %rdi
callq 0x32c0
movq 0x398(%r14), %rsi
leaq 0x6f8(%rsp), %r15
movl $0x4a0, %edx            # imm = 0x4A0
movq %r15, %rdi
callq 0x32c0
movq 0x3a0(%r14), %rsi
leaq 0x1038(%rsp), %rbx
movl $0x4a0, %edx            # imm = 0x4A0
movq %rbx, %rdi
callq 0x32c0
movslq 0xc0(%r14), %rax
leal -0x1(%rax), %ecx
movl 0x58(%r12), %edx
imull %ecx, %edx
imull 0x48(%r12), %edx
movslq %edx, %rdx
shlq $0x2, %rdx
addq %rdx, 0x268(%r12)
addq %rdx, 0x260(%r12)
addq %rdx, 0x2c0(%r12)
addq %rdx, 0x2c8(%r12)
movl 0x58(%r15), %edx
imull %ecx, %edx
imull 0x48(%r15), %edx
movslq %edx, %rdx
shlq $0x2, %rdx
addq %rdx, 0x268(%r15)
addq %rdx, 0x260(%r15)
addq %rdx, 0x2c0(%r15)
addq %rdx, 0x2c8(%r15)
imull 0x58(%rbx), %ecx
imull 0x48(%rbx), %ecx
movslq %ecx, %rcx
shlq $0x2, %rcx
addq %rcx, 0x268(%rbx)
addq %rcx, 0x260(%rbx)
addq %rcx, 0x2c0(%rbx)
addq %rcx, 0x2c8(%rbx)
movl 0x48(%r14), %edi
movl 0x58(%r14), %edx
imull %edi, %edx
movl %edx, %ecx
imull %eax, %ecx
movslq %ecx, %rbx
shlq $0x2, %rbx
addq 0x1e0(%r14), %rbx
movq %rbx, 0x1e0(%r14)
testq %rax, %rax
jle 0x3905e
movl %edx, 0x5b4(%rsp)
movslq %edx, %rcx
movq %rcx, %rdx
negq %rdx
movl 0x44(%r14), %esi
movl %esi, 0x5b8(%rsp)
movq 0xd8(%r13), %rsi
movq %rsi, 0x5d8(%rsp)
movq 0xe8(%r13), %rsi
movq %rsi, 0x5d0(%rsp)
imull 0x54(%r14), %edi
movq %rax, %r14
incq %r14
leal -0x1(%rax), %r12d
imull %edi, %r12d
shlq $0x2, %rcx
negq %rcx
movq %rcx, 0x5e0(%rsp)
leaq 0x5e8(%rsp), %r13
movq %rdx, 0x5c0(%rsp)
leaq (,%rdx,4), %rax
movq %rax, 0x5c8(%rsp)
movl %edi, 0x5bc(%rsp)
movl 0x5b4(%rsp), %ebp
movq 0xe00(%rsp), %rsi
movl %ebp, %edi
movl $0x1, %edx
movq %rbx, %rcx
movl $0x1, %r8d
callq 0x2879c
movq 0x960(%rsp), %rsi
movl %ebp, %edi
movss 0x1a1e4(%rip), %xmm0    # 0x53008
movl $0x1, %edx
movq %rbx, %rcx
movl $0x1, %r8d
callq 0x285e9
movq %rbx, 0x6c0(%rsp)
movq 0x958(%rsp), %rax
movq %rax, 0x6d0(%rsp)
movl $0x94, %ecx
movq %rsp, %rdi
leaq 0x1038(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x4a0(%rsp), %rdi
movl $0x22, %ecx
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x31721
addq 0x5e0(%rsp), %rbx
movq %rbx, 0x6c0(%rsp)
movq 0x958(%rsp), %rax
addq 0x5c8(%rsp), %rax
cmpq $0x2, %r14
movl $0x0, %ecx
cmoveq %rcx, %rax
movq %rax, 0x6d0(%rsp)
movl $0x94, %ecx
movq %rsp, %rdi
leaq 0x6f8(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x4a0(%rsp), %rdi
movl $0x22, %ecx
movq %r13, %r15
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x31721
movq 0x958(%rsp), %rsi
movq 0xdf8(%rsp), %rcx
movl %ebp, %edi
movl $0x1, %edx
movl $0x1, %r8d
callq 0x2879c
cmpq $0x2, %r14
je 0x38f42
cmpl $0x0, 0x5b8(%rsp)
je 0x38f42
movq 0x958(%rsp), %rsi
movq 0x5c0(%rsp), %rax
leaq (%rsi,%rax,4), %rcx
movl 0x5b4(%rsp), %edi
movss 0x1a0d6(%rip), %xmm0    # 0x53008
movl $0x1, %edx
movl $0x1, %r8d
callq 0x285e9
movq 0x5d0(%rsp), %rcx
testq %rcx, %rcx
movslq %r12d, %r12
movq 0x5d8(%rsp), %rax
leaq (%rax,%r12,4), %rax
movq %rax, 0x6c0(%rsp)
leaq (%rcx,%r12,4), %rax
cmoveq %rcx, %rax
movq %rax, 0x6d0(%rsp)
movl $0x94, %ecx
movq %rsp, %rdi
leaq 0xb98(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x4a0(%rsp), %rdi
movl $0x22, %ecx
movq %r15, %r13
movq %r15, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x31721
movl 0xbf0(%rsp), %eax
imull 0xbe0(%rsp), %eax
negl %eax
cltq
shlq $0x2, %rax
addq %rax, 0xe00(%rsp)
addq %rax, 0xdf8(%rsp)
addq %rax, 0xe58(%rsp)
addq %rax, 0xe60(%rsp)
movl 0x750(%rsp), %eax
imull 0x740(%rsp), %eax
negl %eax
cltq
shlq $0x2, %rax
addq %rax, 0x960(%rsp)
addq %rax, 0x958(%rsp)
addq %rax, 0x9b8(%rsp)
addq %rax, 0x9c0(%rsp)
movl 0x1090(%rsp), %eax
imull 0x1080(%rsp), %eax
negl %eax
cltq
shlq $0x2, %rax
addq %rax, 0x12a0(%rsp)
addq %rax, 0x1298(%rsp)
addq %rax, 0x12f8(%rsp)
addq %rax, 0x1300(%rsp)
decq %r14
movl 0x5bc(%rsp), %edi
subl %edi, %r12d
cmpq $0x1, %r14
jg 0x38df5
leaq 0x1510(%rsp), %rax
movq %rbx, 0x1e0(%rax)
addq $0x14d8, %rsp           # imm = 0x14D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq