// File: /workspace/github_c_makefile_star2_O2/SimpleDoger[P]Darknet_CPU_Only/go.c
// Function: train_go
void train_go(char *cfgfile, char *weightfile, char *filename, int *gpus, int ngpus, int clear)
{
    int i;
    float avg_loss = -1;
    char *base = basecfg(cfgfile);
    printf("%s\n", base);
    printf("%d\n", ngpus);
    network **nets = calloc(ngpus, sizeof(network*));

    srand(time(0));
    int seed = rand();
    for(i = 0; i < ngpus; ++i){
        srand(seed);
        nets[i] = load_network(cfgfile, weightfile, clear);
        nets[i]->learning_rate *= ngpus;
    }
    network *net = nets[0];
    printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net->learning_rate, net->momentum, net->decay);

    char *backup_directory = "/home/pjreddie/backup/";

    char buff[256];
    moves m = load_go_moves(filename);
    //moves m = load_go_moves("games.txt");

    int N = m.n;
    printf("Moves: %d\n", N);
    int epoch = (*net->seen)/N;
    while(get_current_batch(net) < net->max_batches || net->max_batches == 0){
        double time=what_time_is_it_now();

        data train = random_go_moves(m, net->batch*net->subdivisions*ngpus);
        printf("Loaded: %lf seconds\n", what_time_is_it_now() - time);
        time=what_time_is_it_now();

        float loss = 0;
        loss = train_network(net, train);
        free_data(train);

        if(avg_loss == -1) avg_loss = loss;
        avg_loss = avg_loss*.95 + loss*.05;
        printf("%ld, %.3f: %f, %f avg, %f rate, %lf seconds, %ld images\n", get_current_batch(net), (float)(*net->seen)/N, loss, avg_loss, get_current_rate(net), what_time_is_it_now()-time, *net->seen);
        if(*net->seen/N > epoch){
            epoch = *net->seen/N;
            char buff[256];
            sprintf(buff, "%s/%s_%d.weights", backup_directory,base, epoch);
            save_weights(net, buff);

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

    free_network(net);
    free(base);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f8, %rsp            # imm = 0x1F8
movl %r9d, 0x70(%rsp)
movl %r8d, %ebx
movl %r8d, 0x54(%rsp)
movq %rdx, 0x80(%rsp)
movq %rsi, %r14
movq %rdi, %r12
callq 0x41c8
movq %rax, 0x58(%rsp)
movq %rax, %rdi
callq 0x30b0
leaq 0x177c7(%rip), %rdi     # 0x51451
xorl %r15d, %r15d
movl %ebx, %esi
xorl %eax, %eax
callq 0x3160
movslq %ebx, %rbp
pushq $0x8
popq %rsi
movq %rbp, %rdi
callq 0x3250
movq %rax, %r13
xorl %edi, %edi
callq 0x32c0
movl %eax, %edi
callq 0x3220
callq 0x34e0
movl %eax, %ebx
cvtsi2ss %ebp, %xmm0
movss %xmm0, 0x48(%rsp)
testl %ebp, %ebp
movl $0x0, %ebp
cmovgl 0x54(%rsp), %ebp
cmpq %r15, %rbp
je 0x39d07
movl %ebx, %edi
callq 0x3220
movq %r12, %rdi
movq %r14, %rsi
movl 0x70(%rsp), %edx
callq 0x22658
movq %rax, (%r13,%r15,8)
movss 0x34(%rax), %xmm0
mulss 0x48(%rsp), %xmm0
movss %xmm0, 0x34(%rax)
incq %r15
jmp 0x39cd2
movq (%r13), %r14
cvtss2sd 0x34(%r14), %xmm0
cvtss2sd 0x38(%r14), %xmm1
cvtss2sd 0x3c(%r14), %xmm2
leaq 0x171ac(%rip), %rdi     # 0x50ed0
movb $0x3, %al
callq 0x3160
movq 0x80(%rsp), %rdi
callq 0x3984e
movq %rax, 0x98(%rsp)
movl %edx, %r12d
leaq 0x17606(%rip), %rdi     # 0x51350
movl %edx, %esi
xorl %eax, %eax
callq 0x3160
movslq %r12d, %r15
cvtsi2ss %r15d, %xmm0
movss %xmm0, 0x6c(%rsp)
movq 0x8(%r14), %rax
movq (%rax), %rax
xorl %edx, %edx
divq %r15
movq %rax, %rbp
movss 0x124ec(%rip), %xmm0    # 0x4c264
leaq 0xf0(%rsp), %r13
movapd %xmm0, 0x70(%rsp)
movq %r14, %rdi
callq 0x22693
movslq 0x54(%r14), %rcx
testq %rcx, %rcx
je 0x39da0
cmpq %rcx, %rax
jae 0x3a061
xorl %eax, %eax
callq 0x3bc0
movsd %xmm0, 0x48(%rsp)
movl 0x4(%r14), %ecx
imull 0x54(%rsp), %ecx
imull 0x1c(%r14), %ecx
leaq 0xb0(%rsp), %rdi
movq 0x98(%rsp), %rsi
movl %r12d, %edx
callq 0x39a09
xorl %eax, %eax
callq 0x3bc0
subsd 0x48(%rsp), %xmm0
leaq 0x17157(%rip), %rdi     # 0x50f3e
movb $0x1, %al
callq 0x3160
xorl %eax, %eax
callq 0x3bc0
movsd %xmm0, 0x48(%rsp)
movups 0xb0(%rsp), %xmm0
movups 0xc0(%rsp), %xmm1
movups 0xd0(%rsp), %xmm2
movups 0xe0(%rsp), %xmm3
movups %xmm3, 0x30(%rsp)
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x22df8
movaps %xmm0, 0x80(%rsp)
movups 0xb0(%rsp), %xmm0
movups 0xc0(%rsp), %xmm1
movups 0xd0(%rsp), %xmm2
movups 0xe0(%rsp), %xmm3
movups %xmm3, 0x30(%rsp)
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x1e69d
movaps 0x70(%rsp), %xmm2
movaps %xmm2, %xmm0
cmpeqss 0x123dd(%rip), %xmm0    # 0x4c264
movaps %xmm0, %xmm1
andnps %xmm2, %xmm1
movaps 0x80(%rsp), %xmm2
andps %xmm2, %xmm0
orps %xmm1, %xmm0
cvtss2sd %xmm0, %xmm1
cvtss2sd %xmm2, %xmm0
movsd %xmm0, 0x80(%rsp)
mulsd 0x17324(%rip), %xmm0    # 0x511d8
movsd %xmm0, 0x60(%rsp)
mulsd 0x1731e(%rip), %xmm1    # 0x511e0
movsd %xmm1, 0x70(%rsp)
movq %r14, %rdi
callq 0x22693
movq %rax, %rbx
movq 0x8(%r14), %rax
movq (%rax), %rax
testq %rax, %rax
js 0x39ee6
cvtsi2ss %rax, %xmm0
jmp 0x39efb
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
divss 0x6c(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
addsd 0x60(%rsp), %xmm1
cvtsd2ss %xmm1, %xmm1
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0xa8(%rsp)
movaps %xmm1, 0x70(%rsp)
cvtss2sd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
movq %r14, %rdi
callq 0x226ae
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0xa0(%rsp)
xorl %eax, %eax
callq 0x3bc0
movaps %xmm0, %xmm4
subsd 0x48(%rsp), %xmm4
movq 0x8(%r14), %rax
movq (%rax), %rdx
leaq 0x1720c(%rip), %rdi     # 0x5116c
movq %rbx, %rsi
movsd 0xa8(%rsp), %xmm0
movsd 0x80(%rsp), %xmm1
movsd 0x60(%rsp), %xmm2
movsd 0xa0(%rsp), %xmm3
movb $0x5, %al
callq 0x3160
movq 0x8(%r14), %rax
movq (%rax), %rax
xorl %edx, %edx
divq %r15
movq %rax, %rbx
movslq %ebp, %rax
cmpq %rax, %rbx
jbe 0x39fcf
movq %r13, %rdi
leaq 0x17161(%rip), %rsi     # 0x5110d
leaq 0x17112(%rip), %rdx     # 0x510c5
movq 0x58(%rsp), %rcx
movl %ebx, %r8d
xorl %eax, %eax
callq 0x3420
movq %r14, %rdi
movq %r13, %rsi
callq 0x28aed
movl %ebx, %ebp
movq %r14, %rdi
callq 0x22693
xorl %edx, %edx
movl $0x3e8, %ecx            # imm = 0x3E8
divq %rcx
testq %rdx, %rdx
jne 0x3a00e
movq %r13, %rdi
leaq 0x1712e(%rip), %rsi     # 0x5111e
leaq 0x170ce(%rip), %rdx     # 0x510c5
movq 0x58(%rsp), %rcx
xorl %eax, %eax
callq 0x3420
movq %r14, %rdi
movq %r13, %rsi
callq 0x28aed
movq %r14, %rdi
callq 0x22693
xorl %edx, %edx
movl $0x2710, %ecx           # imm = 0x2710
divq %rcx
testq %rdx, %rdx
jne 0x39d86
movq %r14, %rdi
callq 0x22693
movq %r13, %rdi
leaq 0x17320(%rip), %rsi     # 0x5135b
leaq 0x17083(%rip), %rdx     # 0x510c5
movq 0x58(%rsp), %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x3420
movq %r14, %rdi
movq %r13, %rsi
callq 0x28aed
jmp 0x39d86
leaq 0x1713d(%rip), %rsi     # 0x511a5
leaq 0x17056(%rip), %rdx     # 0x510c5
leaq 0xf0(%rsp), %rbx
movq %rbx, %rdi
movq 0x58(%rsp), %r15
movq %r15, %rcx
xorl %eax, %eax
callq 0x3420
movq %r14, %rdi
movq %rbx, %rsi
callq 0x28aed
movq %r14, %rdi
callq 0x24067
movq %r15, %rdi
callq 0x3040
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq