// File: /workspace/github_c_makefile_star2_O2/SimpleDoger[P]Darknet_CPU_Only/classifier.c
// Function: train_classifier
void train_classifier(char *datacfg, char *cfgfile, char *weightfile, 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;
    }
    srand(time(0));
    network *net = nets[0];

    int imgs = net->batch * net->subdivisions * ngpus;

    printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net->learning_rate, net->momentum, net->decay);
    list *options = read_data_cfg(datacfg);

    char *backup_directory = option_find_str(options, "backup", "/backup/");
    int tag = option_find_int_quiet(options, "tag", 0);
    char *label_list = option_find_str(options, "labels", "data/labels.list");
    char *train_list = option_find_str(options, "train", "data/train.list");
    char *tree = option_find_str(options, "tree", 0);
    if (tree) net->hierarchy = read_tree(tree);
    int classes = option_find_int(options, "classes", 2);

    char **labels = 0;
    if(!tag){
        labels = get_labels(label_list);
    }
    list *plist = get_paths(train_list);
    char **paths = (char **)list_to_array(plist);
    printf("%d\n", plist->size);
    int N = plist->size;
    double time;

    load_args args = {0};
    args.w = net->w;
    args.h = net->h;
    args.threads = 32;
    args.hierarchy = net->hierarchy;

    args.min = net->min_ratio*net->w;
    args.max = net->max_ratio*net->w;
    printf("%d %d\n", args.min, args.max);
    args.angle = net->angle;
    args.aspect = net->aspect;
    args.exposure = net->exposure;
    args.saturation = net->saturation;
    args.hue = net->hue;
    args.size = net->w;

    args.paths = paths;
    args.classes = classes;
    args.n = imgs;
    args.m = N;
    args.labels = labels;
    if (tag){
        args.type = TAG_DATA;
    } else {
        args.type = CLASSIFICATION_DATA;
    }

    data train;
    data buffer;
    pthread_t load_thread;
    args.d = &buffer;
    load_thread = load_data(args);

    int count = 0;
    int epoch = (*net->seen)/N;
    while(get_current_batch(net) < net->max_batches || net->max_batches == 0){
        if(net->random && count++%40 == 0){
            printf("Resizing\n");
            int dim = (rand() % 11 + 4) * 32;
            //if (get_current_batch(net)+200 > net->max_batches) dim = 608;
            //int dim = (rand() % 4 + 16) * 32;
            printf("%d\n", dim);
            args.w = dim;
            args.h = dim;
            args.size = dim;
            args.min = net->min_ratio*dim;
            args.max = net->max_ratio*dim;
            printf("%d %d\n", args.min, args.max);

            pthread_join(load_thread, 0);
            train = buffer;
            free_data(train);
            load_thread = load_data(args);

            for(i = 0; i < ngpus; ++i){
                resize_network(nets[i], dim, dim);
            }
            net = nets[0];
        }
        time = what_time_is_it_now();

        pthread_join(load_thread, 0);
        train = buffer;
        load_thread = load_data(args);

        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);
        if(avg_loss == -1) avg_loss = loss;
        avg_loss = avg_loss*.9 + loss*.1;
        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);
        free_data(train);
        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);
        }
    }
    char buff[256];
    sprintf(buff, "%s/%s.weights", backup_directory, base);
    save_weights(net, buff);
    pthread_join(load_thread, 0);

    free_network(net);
    if(labels) free_ptrs((void**)labels, classes);
    free_ptrs((void**)paths, plist->size);
    free_list(plist);
    free(base);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x368, %rsp            # imm = 0x368
movl %r9d, 0xa8(%rsp)
movl %r8d, %ebp
movl %r8d, 0xb0(%rsp)
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, 0xe0(%rsp)
movq %rsi, %rdi
callq 0x41c8
movq %rax, 0xc0(%rsp)
movq %rax, %rdi
callq 0x30b0
leaq 0x113bc(%rip), %rdi     # 0x51451
xorl %ebx, %ebx
movl %ebp, %esi
xorl %eax, %eax
callq 0x3160
movslq %ebp, %rbp
pushq $0x8
popq %rsi
movq %rbp, %rdi
callq 0x3250
movq %rax, %r14
xorl %edi, %edi
callq 0x32c0
movl %eax, %edi
callq 0x3220
callq 0x34e0
movl %eax, %r15d
cvtsi2ss %ebp, %xmm0
movss %xmm0, 0xf0(%rsp)
testl %ebp, %ebp
movl $0x0, %ebp
cmovgl 0xb0(%rsp), %ebp
cmpq %rbx, %rbp
je 0x4011e
movl %r15d, %edi
callq 0x3220
movq %r13, %rdi
movq %r12, %rsi
movl 0xa8(%rsp), %edx
callq 0x22658
movq %rax, (%r14,%rbx,8)
movss 0x34(%rax), %xmm0
mulss 0xf0(%rsp), %xmm0
movss %xmm0, 0x34(%rax)
incq %rbx
jmp 0x400e3
xorl %edi, %edi
callq 0x32c0
movl %eax, %edi
callq 0x3220
movq (%r14), %r12
movl 0xb0(%rsp), %eax
imull 0x4(%r12), %eax
imull 0x1c(%r12), %eax
movl %eax, 0xb0(%rsp)
cvtss2sd 0x34(%r12), %xmm0
cvtss2sd 0x38(%r12), %xmm1
cvtss2sd 0x3c(%r12), %xmm2
leaq 0x10d6b(%rip), %rdi     # 0x50ed0
movb $0x3, %al
callq 0x3160
movq 0xe0(%rsp), %rdi
callq 0x29598
movq %rax, %r15
leaq 0x111e2(%rip), %rsi     # 0x51365
leaq 0x10f49(%rip), %rdx     # 0x510d3
movq %rax, %rdi
callq 0x29737
movq %rax, 0xd0(%rsp)
leaq 0x119df(%rip), %rsi     # 0x51b80
movq %r15, %rdi
xorl %edx, %edx
callq 0x29884
movl %eax, %ebx
leaq 0xf7f2(%rip), %rsi      # 0x4f9a6
leaq 0x119c9(%rip), %rdx     # 0x51b84
movq %r15, %rdi
callq 0x29737
movq %rax, 0xa8(%rsp)
leaq 0x11142(%rip), %rsi     # 0x51314
leaq 0x11957(%rip), %rdx     # 0x51b30
movq %r15, %rdi
callq 0x29737
movq %rax, %r13
leaq 0xffec(%rip), %rsi      # 0x501d7
movq %r15, %rdi
xorl %edx, %edx
callq 0x29737
testq %rax, %rax
je 0x4020a
movq %rax, %rdi
callq 0x325a2
movq %rax, 0xd0(%r12)
leaq 0xffda(%rip), %rsi      # 0x501eb
pushq $0x2
popq %rdx
movq %r15, %rdi
callq 0x2977d
movl %eax, 0xbc(%rsp)
testl %ebx, %ebx
je 0x4023f
pushq $0x8
popq %rax
movl %eax, 0xa8(%rsp)
movq $0x0, 0xc8(%rsp)
jmp 0x4025f
movq 0xa8(%rsp), %rdi
callq 0x1e673
movq %rax, 0xc8(%rsp)
movl $0x0, 0xa8(%rsp)
movq %r13, %rdi
callq 0x1c8c8
movq %rax, %r13
movq %rax, %rdi
callq 0x72f5
movq %rax, %r15
movl (%r13), %esi
leaq 0x111d1(%rip), %rdi     # 0x51451
xorl %eax, %eax
callq 0x3160
movq %r13, 0x158(%rsp)
movslq (%r13), %r13
leaq 0x1b8(%rsp), %rbx
movl $0xa0, %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x31c0
movl 0x94(%r12), %eax
movl %eax, 0x2c(%rbx)
movl 0x90(%r12), %ecx
movl %ecx, 0x28(%rbx)
movl $0x20, (%rbx)
movq 0xd0(%r12), %rcx
movq %rcx, 0xa0(%rbx)
cvtsi2ss %eax, %xmm0
movsd 0xa4(%r12), %xmm1
shufps $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
mulps %xmm1, %xmm0
cvttps2dq %xmm0, %xmm0
pshufd $0xe1, %xmm0, %xmm1     # xmm1 = xmm0[1,0,2,3]
movq %xmm1, 0x44(%rbx)
movd %xmm0, %edx
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, %esi
leaq 0x105d0(%rip), %rdi     # 0x508dc
xorl %eax, %eax
callq 0x3160
movups 0xb0(%r12), %xmm0
shufps $0xb4, %xmm0, %xmm0     # xmm0 = xmm0[0,1,3,2]
movups %xmm0, 0x68(%rbx)
movss 0xc0(%r12), %xmm0
movss %xmm0, 0x78(%rbx)
movl 0x94(%r12), %eax
movl %eax, 0x4c(%rbx)
movq %r15, 0x150(%rsp)
movq %r15, 0x8(%rbx)
movl 0xbc(%rsp), %eax
movl %eax, 0x50(%rbx)
movl 0xb0(%rsp), %eax
movl %eax, 0x18(%rbx)
movl %r13d, 0x1c(%rbx)
movq 0xc8(%rsp), %rax
movq %rax, 0x20(%rbx)
movl 0xa8(%rsp), %eax
movl %eax, 0x98(%rbx)
leaq 0x178(%rsp), %rax
movq %rax, 0x80(%rbx)
pushq $0x15
popq %rcx
movq %rsp, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x20e7b
movq %rax, 0xa8(%rsp)
cvtsi2ss %r13d, %xmm0
movss %xmm0, 0xec(%rsp)
movq 0x8(%r12), %rax
movq (%rax), %rax
xorl %edx, %edx
movq %r13, 0x160(%rsp)
divq %r13
movq %rax, 0x108(%rsp)
movss 0xbe8e(%rip), %xmm0     # 0x4c264
movl $0x3e8, %r15d           # imm = 0x3E8
movaps %xmm0, 0xf0(%rsp)
xorl %r13d, %r13d
movq %r12, %rdi
callq 0x22693
movslq 0x54(%r12), %rcx
testq %rcx, %rcx
je 0x40402
cmpq %rcx, %rax
jae 0x40882
cmpl $0x0, 0xc4(%r12)
je 0x4057d
leal 0x1(%r13), %ebx
movl %r13d, %eax
cltd
pushq $0x28
popq %rcx
idivl %ecx
testl %edx, %edx
jne 0x4057a
leaq 0x11890(%rip), %rdi     # 0x51cbd
callq 0x30b0
callq 0x34e0
cltd
pushq $0xb
popq %rcx
idivl %ecx
movl %edx, %r13d
shll $0x5, %r13d
subl $-0x80, %r13d
leaq 0x11002(%rip), %rdi     # 0x51451
movl %r13d, %esi
xorl %eax, %eax
callq 0x3160
movl %r13d, 0x1e4(%rsp)
movl %r13d, 0x1e0(%rsp)
movl %r13d, 0x204(%rsp)
cvtsi2ss %r13d, %xmm0
movsd 0xa4(%r12), %xmm1
shufps $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
mulps %xmm1, %xmm0
cvttps2dq %xmm0, %xmm0
pshufd $0xe1, %xmm0, %xmm1     # xmm1 = xmm0[1,0,2,3]
movq %xmm1, 0x1fc(%rsp)
movd %xmm0, %edx
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, %esi
leaq 0x1042f(%rip), %rdi     # 0x508dc
xorl %eax, %eax
callq 0x3160
movq 0xa8(%rsp), %rdi
xorl %esi, %esi
callq 0x3470
movups 0x178(%rsp), %xmm0
movups 0x188(%rsp), %xmm1
movups 0x198(%rsp), %xmm2
movups 0x1a8(%rsp), %xmm3
movaps %xmm0, 0x110(%rsp)
movaps %xmm1, 0x120(%rsp)
movaps %xmm2, 0x130(%rsp)
movaps %xmm3, 0x140(%rsp)
movups 0x178(%rsp), %xmm0
movdqu 0x188(%rsp), %xmm1
movups 0x198(%rsp), %xmm2
movups 0x1a8(%rsp), %xmm3
movups %xmm3, 0x30(%rsp)
movups %xmm2, 0x20(%rsp)
movdqu %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x1e69d
pushq $0x15
popq %rcx
movq %rsp, %rdi
leaq 0x1b8(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x20e7b
movq %rax, 0xa8(%rsp)
xorl %r12d, %r12d
cmpq %r12, %rbp
je 0x40577
movq (%r14,%r12,8), %rdi
movl %r13d, %esi
movl %r13d, %edx
callq 0x22f25
incq %r12
jmp 0x4055e
movq (%r14), %r12
movl %ebx, %r13d
xorl %eax, %eax
callq 0x3bc0
movsd %xmm0, 0xb0(%rsp)
movq 0xa8(%rsp), %rdi
xorl %esi, %esi
callq 0x3470
movupd 0x178(%rsp), %xmm0
movups 0x188(%rsp), %xmm1
movups 0x198(%rsp), %xmm2
movups 0x1a8(%rsp), %xmm3
movapd %xmm0, 0x110(%rsp)
movaps %xmm1, 0x120(%rsp)
movaps %xmm2, 0x130(%rsp)
movaps %xmm3, 0x140(%rsp)
pushq $0x15
popq %rcx
movq %rsp, %rdi
leaq 0x1b8(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x20e7b
movq %rax, 0xa8(%rsp)
xorl %eax, %eax
callq 0x3bc0
subsd 0xb0(%rsp), %xmm0
leaq 0x1092b(%rip), %rdi     # 0x50f3e
movb $0x1, %al
callq 0x3160
xorl %eax, %eax
callq 0x3bc0
movsd %xmm0, 0xb0(%rsp)
movaps 0x110(%rsp), %xmm0
movaps 0x120(%rsp), %xmm1
movaps 0x130(%rsp), %xmm2
movaps 0x140(%rsp), %xmm3
movups %xmm3, 0x30(%rsp)
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r12, %rdi
callq 0x22df8
movaps 0xf0(%rsp), %xmm3
movaps %xmm3, %xmm1
cmpeqss 0xbbeb(%rip), %xmm1     # 0x4c264
movaps %xmm1, %xmm2
andnps %xmm3, %xmm2
andps %xmm0, %xmm1
orps %xmm2, %xmm1
cvtss2sd %xmm1, %xmm1
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0xe0(%rsp)
mulsd 0xef32(%rip), %xmm0     # 0x4f5d0
movsd %xmm0, 0xd8(%rsp)
mulsd 0x10819(%rip), %xmm1    # 0x50ec8
movsd %xmm1, 0xf0(%rsp)
movq %r12, %rdi
callq 0x22693
movq %rax, %rbx
movq 0x8(%r12), %rax
movq (%rax), %rax
testq %rax, %rax
js 0x406d7
cvtsi2ss %rax, %xmm0
jmp 0x406ec
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
divss 0xec(%rsp), %xmm0
movsd 0xf0(%rsp), %xmm1
addsd 0xd8(%rsp), %xmm1
cvtsd2ss %xmm1, %xmm1
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x170(%rsp)
movaps %xmm1, 0xf0(%rsp)
cvtss2sd %xmm1, %xmm0
movsd %xmm0, 0xd8(%rsp)
movq %r12, %rdi
callq 0x226ae
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x168(%rsp)
xorl %eax, %eax
callq 0x3bc0
movaps %xmm0, %xmm4
subsd 0xb0(%rsp), %xmm4
movq 0x8(%r12), %rax
movq (%rax), %rdx
leaq 0x10a08(%rip), %rdi     # 0x5116c
movq %rbx, %rsi
movsd 0x170(%rsp), %xmm0
movsd 0xe0(%rsp), %xmm1
movsd 0xd8(%rsp), %xmm2
movsd 0x168(%rsp), %xmm3
movb $0x5, %al
callq 0x3160
movaps 0x110(%rsp), %xmm0
movdqa 0x120(%rsp), %xmm1
movaps 0x130(%rsp), %xmm2
movaps 0x140(%rsp), %xmm3
movups %xmm3, 0x30(%rsp)
movups %xmm2, 0x20(%rsp)
movdqu %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x1e69d
movq 0x8(%r12), %rax
movq (%rax), %rax
xorl %edx, %edx
divq 0x160(%rsp)
movq %rax, %rbx
movslq 0x108(%rsp), %rax
cmpq %rax, %rbx
jbe 0x40833
leaq 0x260(%rsp), %r15
movq %r15, %rdi
leaq 0x1090d(%rip), %rsi     # 0x5110d
movq 0xd0(%rsp), %rdx
movq 0xc0(%rsp), %rcx
movl %ebx, %r8d
xorl %eax, %eax
callq 0x3420
movq %r12, %rdi
movq %r15, %rsi
movl $0x3e8, %r15d           # imm = 0x3E8
callq 0x28aed
movq %rbx, 0x108(%rsp)
movq %r12, %rdi
callq 0x22693
xorl %edx, %edx
divq %r15
testq %rdx, %rdx
jne 0x403e7
leaq 0x260(%rsp), %rbx
movq %rbx, %rdi
leaq 0x108c3(%rip), %rsi     # 0x5111e
movq 0xd0(%rsp), %rdx
movq 0xc0(%rsp), %rcx
xorl %eax, %eax
callq 0x3420
movq %r12, %rdi
movq %rbx, %rsi
callq 0x28aed
jmp 0x403e7
leaq 0x1091c(%rip), %rsi     # 0x511a5
leaq 0x260(%rsp), %rbx
movq %rbx, %rdi
movq 0xd0(%rsp), %rdx
movq 0xc0(%rsp), %rcx
xorl %eax, %eax
callq 0x3420
movq %r12, %rdi
movq %rbx, %rsi
callq 0x28aed
movq 0xa8(%rsp), %rdi
xorl %esi, %esi
callq 0x3470
movq %r12, %rdi
callq 0x24067
movq 0xc8(%rsp), %rdi
testq %rdi, %rdi
je 0x408e6
movl 0xbc(%rsp), %esi
callq 0x4582
movq 0x158(%rsp), %rbx
movl (%rbx), %esi
movq 0x150(%rsp), %rdi
callq 0x4582
movq %rbx, %rdi
callq 0x72c6
movq 0xc0(%rsp), %rdi
callq 0x3040
addq $0x368, %rsp            # imm = 0x368
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq