// File: /workspace/github_c_makefile_star2_O2/SimpleDoger[P]Darknet_CPU_Only/rnn.c
// Function: train_char_rnn
void train_char_rnn(char *cfgfile, char *weightfile, char *filename, int clear, int tokenized)
{
    srand(time(0));
    unsigned char *text = 0;
    int *tokens = 0;
    size_t size;
    if(tokenized){
        tokens = read_tokenized_data(filename, &size);
    } else {
        text = read_file(filename);
        size = strlen((const char*)text);
    }

    char *backup_directory = "/home/pjreddie/backup/";
    char *base = basecfg(cfgfile);
    fprintf(stderr, "%s\n", base);
    float avg_loss = -1;
    network *net = load_network(cfgfile, weightfile, clear);

    int inputs = net->inputs;
    fprintf(stderr, "Learning Rate: %g, Momentum: %g, Decay: %g, Inputs: %d %d %d\n", net->learning_rate, net->momentum, net->decay, inputs, net->batch, net->time_steps);
    int batch = net->batch;
    int steps = net->time_steps;
    if(clear) *net->seen = 0;
    int i = (*net->seen)/net->batch;

    int streams = batch/steps;
    size_t *offsets = calloc(streams, sizeof(size_t));
    int j;
    for(j = 0; j < streams; ++j){
        offsets[j] = rand_size_t()%size;
    }

    clock_t time;
    while(get_current_batch(net) < net->max_batches){
        i += 1;
        time=clock();
        float_pair p;
        if(tokenized){
            p = get_rnn_token_data(tokens, offsets, inputs, size, streams, steps);
        }else{
            p = get_rnn_data(text, offsets, inputs, size, streams, steps);
        }

        copy_cpu(net->inputs*net->batch, p.x, 1, net->input, 1);
        copy_cpu(net->truths*net->batch, p.y, 1, net->truth, 1);
        float loss = train_network_datum(net) / (batch);
        free(p.x);
        free(p.y);
        if (avg_loss < 0) avg_loss = loss;
        avg_loss = avg_loss*.9 + loss*.1;

        size_t chars = get_current_batch(net)*batch;
        fprintf(stderr, "%d: %f, %f avg, %f rate, %lf seconds, %f epochs\n", i, loss, avg_loss, get_current_rate(net), sec(clock()-time), (float) chars/size);

        for(j = 0; j < streams; ++j){
            //printf("%d\n", j);
            if(rand()%64 == 0){
                //fprintf(stderr, "Reset\n");
                offsets[j] = rand_size_t()%size;
                reset_network_state(net, j);
            }
        }

        if(i%10000==0){
            char buff[256];
            sprintf(buff, "%s/%s_%d.weights", backup_directory, base, i);
            save_weights(net, buff);
        }
        if(i%100==0){
            char buff[256];
            sprintf(buff, "%s/%s.backup", backup_directory, base);
            save_weights(net, buff);
        }
    }
    char buff[256];
    sprintf(buff, "%s/%s_final.weights", backup_directory, base);
    save_weights(net, buff);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
movl %r8d, %ebp
movl %ecx, %ebx
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %r15
xorl %edi, %edi
callq 0x32c0
movl %eax, %edi
callq 0x3220
movl %ebp, 0x30(%rsp)
testl %ebp, %ebp
jne 0x3d9d1
movq %r12, %rdi
callq 0x4404
movq %rax, 0x40(%rsp)
movq %rax, %rdi
callq 0x3140
movq %rax, 0x20(%rsp)
movq $0x0, 0x38(%rsp)
jmp 0x3d9ec
leaq 0x20(%rsp), %rsi
movq %r12, %rdi
callq 0x3d43f
movq %rax, 0x38(%rsp)
movq $0x0, 0x40(%rsp)
movq %r15, %rdi
callq 0x41c8
movq 0x265fd(%rip), %r12     # 0x63ff8
movq (%r12), %rdi
leaq 0x11ff4(%rip), %rsi     # 0x4f9fa
movq %rax, 0x10(%rsp)
movq %rax, %rdx
xorl %eax, %eax
callq 0x3270
movq %r15, %rdi
movq %r14, %rsi
movl %ebx, %edx
callq 0x22658
movq %rax, %r13
movl 0x80(%rax), %edx
movq (%r12), %rdi
cvtss2sd 0x34(%rax), %xmm0
cvtss2sd 0x38(%rax), %xmm1
cvtss2sd 0x3c(%rax), %xmm2
movl 0x4(%rax), %ecx
movl 0x4c(%rax), %r8d
leaq 0x13f17(%rip), %rsi     # 0x51963
movl %edx, 0xc(%rsp)
movb $0x3, %al
callq 0x3270
movslq 0x4(%r13), %rax
movq %rax, 0x18(%rsp)
movl 0x4c(%r13), %eax
movl %eax, 0x4(%rsp)
movq 0x8(%r13), %rax
testl %ebx, %ebx
je 0x3da78
andq $0x0, (%rax)
xorl %eax, %eax
jmp 0x3da7b
movq (%rax), %rax
xorl %r15d, %r15d
xorl %edx, %edx
movq 0x18(%rsp), %rcx
divq %rcx
movq %rax, %rbx
movl %ecx, %eax
cltd
idivl 0x4(%rsp)
movl %eax, 0x8(%rsp)
movslq %eax, %r14
pushq $0x8
popq %rsi
movq %r14, %rdi
callq 0x3250
movq %rax, %r12
movq 0x20(%rsp), %rbp
testl %r14d, %r14d
cmovlel %r15d, %r14d
xorl %r15d, %r15d
cmpq %r15, %r14
je 0x3dad0
xorl %eax, %eax
callq 0x4dcd
xorl %edx, %edx
divq %rbp
movq %rdx, (%r12,%r15,8)
incq %r15
jmp 0x3dab6
cvtsi2ssl 0x18(%rsp), %xmm0
movss %xmm0, 0x2c(%rsp)
movss 0xe780(%rip), %xmm0     # 0x4c264
movaps %xmm0, 0x50(%rsp)
movq %r13, %rdi
callq 0x22693
movslq 0x54(%r13), %rcx
cmpq %rcx, %rax
jae 0x3dd90
callq 0x3120
movq %rax, 0x78(%rsp)
cmpl $0x0, 0x30(%rsp)
movq %rbx, 0x48(%rsp)
jne 0x3db34
movq 0x40(%rsp), %rdi
movq %r12, %rsi
movl 0xc(%rsp), %edx
movq %rbp, %rcx
movl 0x8(%rsp), %r8d
movl 0x4(%rsp), %r9d
callq 0x3d83f
jmp 0x3db52
movq 0x38(%rsp), %rdi
movq %r12, %rsi
movl 0xc(%rsp), %edx
movq %rbp, %rcx
movl 0x8(%rsp), %r8d
movl 0x4(%rsp), %r9d
callq 0x3d5c4
movq %rax, %rbp
movq %rdx, %r15
movl 0x4(%r13), %edi
imull 0x80(%r13), %edi
movq 0xd8(%r13), %rcx
movq %rax, %rsi
pushq $0x1
popq %rbx
movl %ebx, %edx
movl %ebx, %r8d
callq 0x1b5a3
movl 0x4(%r13), %edi
imull 0x88(%r13), %edi
movq 0xe0(%r13), %rcx
movq %r15, %rsi
movl %ebx, %edx
movl %ebx, %r8d
callq 0x1b5a3
movq %r13, %rdi
callq 0x22ced
divss 0x2c(%rsp), %xmm0
movaps %xmm0, 0x60(%rsp)
movq %rbp, %rdi
callq 0x3040
movq %r15, %rdi
callq 0x3040
movaps 0x50(%rsp), %xmm2
movaps %xmm2, %xmm0
cmpltss 0xe4a9(%rip), %xmm0     # 0x4c078
movaps %xmm0, %xmm1
movaps 0x60(%rsp), %xmm3
andps %xmm3, %xmm1
andnps %xmm2, %xmm0
orps %xmm1, %xmm0
cvtss2sd %xmm0, %xmm1
cvtss2sd %xmm3, %xmm0
movsd %xmm0, 0x60(%rsp)
mulsd 0x119da(%rip), %xmm0    # 0x4f5d0
movsd %xmm0, 0x70(%rsp)
mulsd 0x132c4(%rip), %xmm1    # 0x50ec8
movsd %xmm1, 0x50(%rsp)
movq %r13, %rdi
callq 0x22693
movq %rax, %rbp
imulq 0x18(%rsp), %rbp
movq 0x263d6(%rip), %rax     # 0x63ff8
movq (%rax), %r15
movq %r13, %rdi
callq 0x226ae
movss %xmm0, 0x34(%rsp)
callq 0x3120
subq 0x78(%rsp), %rax
movq %rax, %rdi
callq 0x437c
testq %rbp, %rbp
js 0x3dc51
cvtsi2ss %rbp, %xmm4
jmp 0x3dc66
movq %rbp, %rax
shrq %rax
andl $0x1, %ebp
orq %rax, %rbp
cvtsi2ss %rbp, %xmm4
addss %xmm4, %xmm4
movq 0x48(%rsp), %rbx
movq 0x20(%rsp), %rbp
testq %rbp, %rbp
js 0x3dc7c
cvtsi2ss %rbp, %xmm1
jmp 0x3dc93
movq %rbp, %rax
shrq %rax
movl %ebp, %ecx
andl $0x1, %ecx
orq %rax, %rcx
cvtsi2ss %rcx, %xmm1
addss %xmm1, %xmm1
divss %xmm1, %xmm4
incl %ebx
movsd 0x50(%rsp), %xmm1
addsd 0x70(%rsp), %xmm1
cvtsd2ss %xmm1, %xmm1
movaps %xmm1, 0x50(%rsp)
cvtss2sd %xmm1, %xmm1
cvtss2sd 0x34(%rsp), %xmm2
cvtss2sd %xmm0, %xmm3
cvtss2sd %xmm4, %xmm4
movq %r15, %rdi
leaq 0x13cd7(%rip), %rsi     # 0x519a1
movl %ebx, %edx
movsd 0x60(%rsp), %xmm0
movb $0x5, %al
callq 0x3270
xorl %r15d, %r15d
cmpq %r15, %r14
je 0x3dd0a
callq 0x34e0
testb $0x3f, %al
jne 0x3dd05
xorl %eax, %eax
callq 0x4dcd
xorl %edx, %edx
divq %rbp
movq %rdx, (%r12,%r15,8)
movq %r13, %rdi
movl %r15d, %esi
callq 0x226ac
incq %r15
jmp 0x3dcdc
movl %ebx, %eax
cltd
movl $0x2710, %ecx           # imm = 0x2710
idivl %ecx
testl %edx, %edx
jne 0x3dd4b
leaq 0x80(%rsp), %r15
movq %r15, %rdi
leaq 0x133e3(%rip), %rsi     # 0x5110d
leaq 0x13394(%rip), %rdx     # 0x510c5
movq 0x10(%rsp), %rcx
movl %ebx, %r8d
xorl %eax, %eax
callq 0x3420
movq %r13, %rdi
movq %r15, %rsi
callq 0x28aed
movl %ebx, %eax
cltd
pushq $0x64
popq %rcx
idivl %ecx
testl %edx, %edx
jne 0x3dae9
leaq 0x80(%rsp), %r15
movq %r15, %rdi
leaq 0x133b1(%rip), %rsi     # 0x5111e
leaq 0x13351(%rip), %rdx     # 0x510c5
movq 0x10(%rsp), %rcx
xorl %eax, %eax
callq 0x3420
movq %r13, %rdi
movq %r15, %rsi
callq 0x28aed
jmp 0x3dae9
leaq 0x13394(%rip), %rsi     # 0x5112b
leaq 0x13327(%rip), %rdx     # 0x510c5
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movq 0x10(%rsp), %rcx
xorl %eax, %eax
callq 0x3420
movq %r13, %rdi
movq %rbx, %rsi
callq 0x28aed
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq