// File: /workspace/github_c_makefile_star2_O1/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 0x32d0
movl %eax, %edi
callq 0x3230
movl %ebp, 0x30(%rsp)
testl %ebp, %ebp
jne 0x9ece
movq %r12, %rdi
callq 0xe97b
movq %rax, 0x40(%rsp)
movq %rax, %rdi
callq 0x3150
movq %rax, 0x10(%rsp)
movq $0x0, 0x38(%rsp)
jmp 0x9ee9
leaq 0x10(%rsp), %rsi
movq %r12, %rdi
callq 0x470a3
movq %rax, 0x38(%rsp)
movq $0x0, 0x40(%rsp)
movq %r15, %rdi
callq 0xe717
movq 0x60100(%rip), %r12     # 0x69ff8
movq (%r12), %rdi
leaq 0x4caa7(%rip), %rsi     # 0x569aa
movq %rax, 0x18(%rsp)
movq %rax, %rdx
xorl %eax, %eax
callq 0x3280
movq %r15, %rdi
movq %r14, %rsi
movl %ebx, %edx
callq 0x2f9e4
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 0x4ea0a(%rip), %rsi     # 0x58953
movl %edx, 0xc(%rsp)
movb $0x3, %al
callq 0x3280
movslq 0x4(%r13), %rax
movq %rax, 0x20(%rsp)
movl 0x4c(%r13), %eax
movl %eax, 0x4(%rsp)
testl %ebx, %ebx
je 0x9f71
movq 0x8(%r13), %rax
andq $0x0, (%rax)
movq 0x8(%r13), %rax
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 0x3260
movq %rax, %r14
movl %ebp, %r12d
movl %ebp, 0x8(%rsp)
testl %ebp, %ebp
jle 0x9fc6
movq 0x10(%rsp), %r15
xorl %ebp, %ebp
xorl %eax, %eax
callq 0xf3e1
xorl %edx, %edx
divq %r15
movq %rdx, (%r14,%rbp,8)
incq %rbp
cmpq %rbp, %r12
jne 0x9fae
movq %r13, %rdi
callq 0x2fa22
movslq 0x54(%r13), %rcx
cmpq %rcx, %rax
jae 0xa296
cvtsi2ssl 0x20(%rsp), %xmm0
movss %xmm0, 0x2c(%rsp)
movss 0x49279(%rip), %xmm0    # 0x53268
movaps %xmm0, 0x50(%rsp)
callq 0x3130
movq %rax, 0x78(%rsp)
movq 0x10(%rsp), %rcx
cmpl $0x0, 0x30(%rsp)
movq %rbx, 0x48(%rsp)
jne 0xa02c
movq 0x40(%rsp), %rdi
movq %r14, %rsi
movl 0xc(%rsp), %edx
movl 0x8(%rsp), %r8d
movl 0x4(%rsp), %r9d
callq 0x474d4
jmp 0xa047
movq 0x38(%rsp), %rdi
movq %r14, %rsi
movl 0xc(%rsp), %edx
movl 0x8(%rsp), %r8d
movl 0x4(%rsp), %r9d
callq 0x47258
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 0x2879c
movl 0x4(%r13), %edi
imull 0x88(%r13), %edi
movq 0xe0(%r13), %rcx
movq %r15, %rsi
movl %ebx, %edx
movl %ebx, %r8d
callq 0x2879c
movq %r13, %rdi
callq 0x300b8
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 0x48fb4(%rip), %xmm0    # 0x53078
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 0x4c4c5(%rip), %xmm0    # 0x565b0
movsd %xmm0, 0x70(%rsp)
mulsd 0x4dd97(%rip), %xmm1    # 0x57e90
movsd %xmm1, 0x50(%rsp)
movq %r13, %rdi
callq 0x2fa22
movq %rax, %rbp
imulq 0x20(%rsp), %rbp
movq 0x5fee1(%rip), %rax     # 0x69ff8
movq (%rax), %r15
movq %r13, %rdi
callq 0x2fa3d
movss %xmm0, 0x34(%rsp)
callq 0x3130
subq 0x78(%rsp), %rax
movq %rax, %rdi
callq 0xe8d4
testq %rbp, %rbp
js 0xa146
cvtsi2ss %rbp, %xmm4
jmp 0xa15b
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 0xa171
cvtsi2ss %rbp, %xmm1
jmp 0xa188
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 0x4e7d2(%rip), %rsi     # 0x58991
movl %ebx, %edx
movsd 0x60(%rsp), %xmm0
movb $0x5, %al
callq 0x3280
cmpl $0x0, 0x8(%rsp)
jle 0xa204
xorl %r15d, %r15d
callq 0x34f0
testb $0x3f, %al
jne 0xa1fc
xorl %eax, %eax
callq 0xf3e1
xorl %edx, %edx
divq %rbp
movq %rdx, (%r14,%r15,8)
movq %r13, %rdi
movl %r15d, %esi
callq 0x2fa3b
incq %r15
cmpq %r15, %r12
jne 0xa1d8
movl %ebx, %eax
cltd
movl $0x2710, %ecx           # imm = 0x2710
idivl %ecx
testl %edx, %edx
jne 0xa245
leaq 0x80(%rsp), %r15
movq %r15, %rdi
leaq 0x4deb1(%rip), %rsi     # 0x580d5
leaq 0x4de62(%rip), %rdx     # 0x5808d
movq 0x18(%rsp), %rcx
movl %ebx, %r8d
xorl %eax, %eax
callq 0x3430
movq %r13, %rdi
movq %r15, %rsi
callq 0x66cd
movl %ebx, %eax
cltd
pushq $0x64
popq %rcx
idivl %ecx
testl %edx, %edx
jne 0xa281
leaq 0x80(%rsp), %r15
movq %r15, %rdi
leaq 0x4de83(%rip), %rsi     # 0x580e6
leaq 0x4de23(%rip), %rdx     # 0x5808d
movq 0x18(%rsp), %rcx
xorl %eax, %eax
callq 0x3430
movq %r13, %rdi
movq %r15, %rsi
callq 0x66cd
movq %r13, %rdi
callq 0x2fa22
movslq 0x54(%r13), %rcx
cmpq %rcx, %rax
jb 0x9ff4
leaq 0x4de56(%rip), %rsi     # 0x580f3
leaq 0x4dde9(%rip), %rdx     # 0x5808d
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movq 0x18(%rsp), %rcx
xorl %eax, %eax
callq 0x3430
movq %r13, %rdi
movq %rbx, %rsi
callq 0x66cd
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq