// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x420, %rsp            # imm = 0x420
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x3290
movl %eax, %edi
callq 0x31e0
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0x5e5f7
movq -0x18(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x5deb0
movq %rax, -0x30(%rbp)
jmp 0x5e611
movq -0x18(%rbp), %rdi
callq 0x4c90
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x3110
movq %rax, -0x38(%rbp)
leaq 0x1735d(%rip), %rax     # 0x75975
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
callq 0x4850
movq %rax, -0x48(%rbp)
movq 0x249c8(%rip), %rax     # 0x82ff8
movq (%rax), %rdi
movq -0x48(%rbp), %rdx
leaq 0x15d5f(%rip), %rsi     # 0x7439d
movb $0x0, %al
callq 0x3240
movss 0x15a8f(%rip), %xmm0    # 0x740dc
movss %xmm0, -0x4c(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq 0x39a70
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movl 0x80(%rax), %eax
movl %eax, -0x5c(%rbp)
movq 0x2497e(%rip), %rax     # 0x82ff8
movq (%rax), %rdi
movq -0x58(%rbp), %rax
movss 0x34(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x58(%rbp), %rax
movss 0x38(%rax), %xmm1
cvtss2sd %xmm1, %xmm1
movq -0x58(%rbp), %rax
movss 0x3c(%rax), %xmm2
cvtss2sd %xmm2, %xmm2
movl -0x5c(%rbp), %edx
movq -0x58(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x58(%rbp), %rax
movl 0x4c(%rax), %r8d
leaq 0x17b2e(%rip), %rsi     # 0x761eb
movb $0x3, %al
callq 0x3240
movq -0x58(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x60(%rbp)
movq -0x58(%rbp), %rax
movl 0x4c(%rax), %eax
movl %eax, -0x64(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x5e6ed
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movq $0x0, (%rax)
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq -0x58(%rbp), %rcx
movslq 0x4(%rcx), %rcx
xorl %edx, %edx
divq %rcx
movl %eax, -0x68(%rbp)
movl -0x60(%rbp), %eax
cltd
idivl -0x64(%rbp)
movl %eax, -0x6c(%rbp)
movslq -0x6c(%rbp), %rdi
movl $0x8, %esi
callq 0x3220
movq %rax, -0x78(%rbp)
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpl -0x6c(%rbp), %eax
jge 0x5e759
movb $0x0, %al
callq 0x5e80
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x38(%rbp)
movq -0x78(%rbp), %rax
movslq -0x7c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x5e72b
jmp 0x5e75b
movq -0x58(%rbp), %rdi
callq 0x39ad0
movq -0x58(%rbp), %rcx
movslq 0x54(%rcx), %rcx
cmpq %rcx, %rax
jae 0x5eb5b
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
callq 0x30f0
movq %rax, -0x88(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0x5e7d8
movq -0x30(%rbp), %rdi
movq -0x78(%rbp), %rsi
movl -0x5c(%rbp), %edx
movq -0x38(%rbp), %rcx
movl -0x6c(%rbp), %r8d
movl -0x64(%rbp), %r9d
callq 0x5e0b0
movq %rax, -0xa8(%rbp)
movq %rdx, -0xa0(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0xa0(%rbp), %rax
movq %rax, -0x90(%rbp)
jmp 0x5e81e
movq -0x28(%rbp), %rdi
movq -0x78(%rbp), %rsi
movl -0x5c(%rbp), %edx
movq -0x38(%rbp), %rcx
movl -0x6c(%rbp), %r8d
movl -0x64(%rbp), %r9d
callq 0x5e3e0
movq %rax, -0xb8(%rbp)
movq %rdx, -0xb0(%rbp)
movq -0xb8(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x58(%rbp), %rax
movl 0x80(%rax), %edi
movq -0x58(%rbp), %rax
imull 0x4(%rax), %edi
movq -0x98(%rbp), %rsi
movq -0x58(%rbp), %rax
movq 0xd8(%rax), %rcx
movl $0x1, %r8d
movl %r8d, %edx
callq 0x2f960
movq -0x58(%rbp), %rax
movl 0x88(%rax), %edi
movq -0x58(%rbp), %rax
imull 0x4(%rax), %edi
movq -0x90(%rbp), %rsi
movq -0x58(%rbp), %rax
movq 0xe0(%rax), %rcx
movl $0x1, %r8d
movl %r8d, %edx
callq 0x2f960
movq -0x58(%rbp), %rdi
callq 0x3a700
cvtsi2ssl -0x60(%rbp), %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0xbc(%rbp)
movq -0x98(%rbp), %rdi
callq 0x3030
movq -0x90(%rbp), %rdi
callq 0x3030
xorps %xmm0, %xmm0
ucomiss -0x4c(%rbp), %xmm0
jbe 0x5e8ca
movss -0xbc(%rbp), %xmm0
movss %xmm0, -0x4c(%rbp)
movss -0x4c(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movss -0xbc(%rbp), %xmm1
cvtss2sd %xmm1, %xmm1
movsd 0x157d1(%rip), %xmm2    # 0x740b8
mulsd %xmm2, %xmm1
movsd 0x16e6d(%rip), %xmm2    # 0x75760
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x4c(%rbp)
movq -0x58(%rbp), %rdi
callq 0x39ad0
movslq -0x60(%rbp), %rcx
imulq %rcx, %rax
movq %rax, -0xc8(%rbp)
movq 0x246d5(%rip), %rax     # 0x82ff8
movq (%rax), %rax
movq %rax, -0x408(%rbp)
movl -0x68(%rbp), %eax
movl %eax, -0x3fc(%rbp)
movss -0xbc(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x3f8(%rbp)
movss -0x4c(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x3f0(%rbp)
movq -0x58(%rbp), %rdi
callq 0x39b60
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x3e8(%rbp)
callq 0x30f0
movq %rax, %rdi
movq -0x88(%rbp), %rax
subq %rax, %rdi
callq 0x4b50
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x3e0(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, %rdx
shrq %rdx
movl %eax, %ecx
andl $0x1, %ecx
orq %rdx, %rcx
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
cvtsi2ss %rax, %xmm1
movss %xmm1, -0x3d8(%rbp)
testq %rax, %rax
movss %xmm0, -0x3d4(%rbp)
js 0x5e9db
movss -0x3d8(%rbp), %xmm0
movss %xmm0, -0x3d4(%rbp)
movss -0x3d4(%rbp), %xmm0
movss %xmm0, -0x414(%rbp)
movq -0x38(%rbp), %rax
movq %rax, %rdx
shrq %rdx
movl %eax, %ecx
andl $0x1, %ecx
orq %rdx, %rcx
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
cvtsi2ss %rax, %xmm1
movss %xmm1, -0x410(%rbp)
testq %rax, %rax
movss %xmm0, -0x40c(%rbp)
js 0x5ea30
movss -0x410(%rbp), %xmm0
movss %xmm0, -0x40c(%rbp)
movsd -0x3e0(%rbp), %xmm3
movsd -0x3e8(%rbp), %xmm2
movsd -0x3f0(%rbp), %xmm1
movsd -0x3f8(%rbp), %xmm0
movl -0x3fc(%rbp), %edx
movq -0x408(%rbp), %rdi
movss -0x414(%rbp), %xmm4
movss -0x40c(%rbp), %xmm5
divss %xmm5, %xmm4
cvtss2sd %xmm4, %xmm4
leaq 0x177ad(%rip), %rsi     # 0x76229
movb $0x5, %al
callq 0x3240
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpl -0x6c(%rbp), %eax
jge 0x5ead8
callq 0x34b0
movl $0x40, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x5eacb
movb $0x0, %al
callq 0x5e80
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x38(%rbp)
movq -0x78(%rbp), %rax
movslq -0x7c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x58(%rbp), %rdi
movl -0x7c(%rbp), %esi
callq 0x39b10
jmp 0x5eacd
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x5ea8a
movl -0x68(%rbp), %eax
movl $0x2710, %ecx           # imm = 0x2710
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x5eb19
leaq -0x1d0(%rbp), %rdi
movq -0x40(%rbp), %rdx
movq -0x48(%rbp), %rcx
movl -0x68(%rbp), %r8d
leaq 0x16ebb(%rip), %rsi     # 0x759bd
movb $0x0, %al
callq 0x33f0
movq -0x58(%rbp), %rdi
leaq -0x1d0(%rbp), %rsi
callq 0x41980
movl -0x68(%rbp), %eax
movl $0x64, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x5eb56
leaq -0x2d0(%rbp), %rdi
movq -0x40(%rbp), %rdx
movq -0x48(%rbp), %rcx
leaq 0x16e8f(%rip), %rsi     # 0x759ce
movb $0x0, %al
callq 0x33f0
movq -0x58(%rbp), %rdi
leaq -0x2d0(%rbp), %rsi
callq 0x41980
jmp 0x5e75b
leaq -0x3d0(%rbp), %rdi
movq -0x40(%rbp), %rdx
movq -0x48(%rbp), %rcx
leaq 0x16e6a(%rip), %rsi     # 0x759db
movb $0x0, %al
callq 0x33f0
movq -0x58(%rbp), %rdi
leaq -0x3d0(%rbp), %rsi
callq 0x41980
addq $0x420, %rsp            # imm = 0x420
popq %rbp
retq
nopw %cs:(%rax,%rax)