// File: /workspace/github_c_makefile_star2_O3/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 0xa4c8
movq %r12, %rdi
callq 0xf166
movq %rax, 0x40(%rsp)
movq %rax, %rdi
callq 0x3140
movq %rax, 0x10(%rsp)
movq $0x0, 0x38(%rsp)
jmp 0xa4e3
leaq 0x10(%rsp), %rsi
movq %r12, %rdi
callq 0x493b3
movq %rax, 0x38(%rsp)
movq $0x0, 0x40(%rsp)
movq %r15, %rdi
callq 0xef02
movq 0x61b06(%rip), %r12     # 0x6bff8
movq (%r12), %rdi
leaq 0x4e5ad(%rip), %rsi     # 0x58aaa
movq %rax, 0x18(%rsp)
movq %rax, %rdx
xorl %eax, %eax
callq 0x3270
movq %r15, %rdi
movq %r14, %rsi
movl %ebx, %edx
callq 0x316bc
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 0x50560(%rip), %rsi     # 0x5aaa3
movl %edx, 0xc(%rsp)
movb $0x3, %al
callq 0x3270
movslq 0x4(%r13), %rax
movq %rax, 0x20(%rsp)
movl 0x4c(%r13), %eax
movl %eax, 0x4(%rsp)
movq 0x8(%r13), %rax
testl %ebx, %ebx
je 0xa56f
andq $0x0, (%rax)
xorl %eax, %eax
jmp 0xa572
movq (%rax), %rax
xorl %edx, %edx
movq 0x20(%rsp), %rcx
divq %rcx
movq %rax, %rbx
movl %ecx, %eax
cltd
idivl 0x4(%rsp)
movl %eax, %ebp
movslq %eax, %rdi
pushq $0x8
popq %rsi
callq 0x3250
movq %rax, %r14
movl %ebp, %r12d
movl %ebp, 0x8(%rsp)
testl %ebp, %ebp
jle 0xa5c0
movq 0x10(%rsp), %r15
xorl %ebp, %ebp
xorl %eax, %eax
callq 0xfbf0
xorl %edx, %edx
divq %r15
movq %rdx, (%r14,%rbp,8)
incq %rbp
cmpq %rbp, %r12
jne 0xa5a8
movq %r13, %rdi
callq 0x316fa
movslq 0x54(%r13), %rcx
cmpq %rcx, %rax
jae 0xa890
cvtsi2ssl 0x20(%rsp), %xmm0
movss %xmm0, 0x2c(%rsp)
movss 0x4acab(%rip), %xmm0    # 0x55294
movaps %xmm0, 0x50(%rsp)
callq 0x3120
movq %rax, 0x78(%rsp)
movq 0x10(%rsp), %rcx
cmpl $0x0, 0x30(%rsp)
movq %rbx, 0x48(%rsp)
jne 0xa626
movq 0x40(%rsp), %rdi
movq %r14, %rsi
movl 0xc(%rsp), %edx
movl 0x8(%rsp), %r8d
movl 0x4(%rsp), %r9d
callq 0x497e4
jmp 0xa641
movq 0x38(%rsp), %rdi
movq %r14, %rsi
movl 0xc(%rsp), %edx
movl 0x8(%rsp), %r8d
movl 0x4(%rsp), %r9d
callq 0x49568
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 0x29fe0
movl 0x4(%r13), %edi
imull 0x88(%r13), %edi
movq 0xe0(%r13), %rcx
movq %r15, %rsi
movl %ebx, %edx
movl %ebx, %r8d
callq 0x29fe0
movq %r13, %rdi
callq 0x31d98
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 0x4a9e2(%rip), %xmm0    # 0x550a0
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 0x4df9b(%rip), %xmm0    # 0x58680
movsd %xmm0, 0x70(%rsp)
mulsd 0x4f8d5(%rip), %xmm1    # 0x59fc8
movsd %xmm1, 0x50(%rsp)
movq %r13, %rdi
callq 0x316fa
movq %rax, %rbp
imulq 0x20(%rsp), %rbp
movq 0x618e7(%rip), %rax     # 0x6bff8
movq (%rax), %r15
movq %r13, %rdi
callq 0x31715
movss %xmm0, 0x34(%rsp)
callq 0x3120
subq 0x78(%rsp), %rax
movq %rax, %rdi
callq 0xf0bf
testq %rbp, %rbp
js 0xa740
cvtsi2ss %rbp, %xmm4
jmp 0xa755
movq %rbp, %rax
shrq %rax
andl $0x1, %ebp
orq %rax, %rbp
cvtsi2ss %rbp, %xmm4
addss %xmm4, %xmm4
movq 0x48(%rsp), %rbx
movq 0x10(%rsp), %rbp
testq %rbp, %rbp
js 0xa76b
cvtsi2ss %rbp, %xmm1
jmp 0xa782
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 0x50328(%rip), %rsi     # 0x5aae1
movl %ebx, %edx
movsd 0x60(%rsp), %xmm0
movb $0x5, %al
callq 0x3270
cmpl $0x0, 0x8(%rsp)
jle 0xa7fe
xorl %r15d, %r15d
callq 0x34e0
testb $0x3f, %al
jne 0xa7f6
xorl %eax, %eax
callq 0xfbf0
xorl %edx, %edx
divq %rbp
movq %rdx, (%r14,%r15,8)
movq %r13, %rdi
movl %r15d, %esi
callq 0x31713
incq %r15
cmpq %r15, %r12
jne 0xa7d2
movl %ebx, %eax
cltd
movl $0x2710, %ecx           # imm = 0x2710
idivl %ecx
testl %edx, %edx
jne 0xa83f
leaq 0x80(%rsp), %r15
movq %r15, %rdi
leaq 0x4f9ef(%rip), %rsi     # 0x5a20d
leaq 0x4f9a0(%rip), %rdx     # 0x5a1c5
movq 0x18(%rsp), %rcx
movl %ebx, %r8d
xorl %eax, %eax
callq 0x3420
movq %r13, %rdi
movq %r15, %rsi
callq 0x6879
movl %ebx, %eax
cltd
pushq $0x64
popq %rcx
idivl %ecx
testl %edx, %edx
jne 0xa87b
leaq 0x80(%rsp), %r15
movq %r15, %rdi
leaq 0x4f9c1(%rip), %rsi     # 0x5a21e
leaq 0x4f961(%rip), %rdx     # 0x5a1c5
movq 0x18(%rsp), %rcx
xorl %eax, %eax
callq 0x3420
movq %r13, %rdi
movq %r15, %rsi
callq 0x6879
movq %r13, %rdi
callq 0x316fa
movslq 0x54(%r13), %rcx
cmpq %rcx, %rax
jb 0xa5ee
leaq 0x4f994(%rip), %rsi     # 0x5a22b
leaq 0x4f927(%rip), %rdx     # 0x5a1c5
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movq 0x18(%rsp), %rcx
xorl %eax, %eax
callq 0x3420
movq %r13, %rdi
movq %rbx, %rsi
callq 0x6879
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq