// File: /workspace/github_c_makefile_star2_O1/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 $0x378, %rsp            # imm = 0x378
movl %r9d, 0xc0(%rsp)
movl %r8d, %ebx
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, 0xf8(%rsp)
movq %rsi, %rdi
callq 0xe717
movq %rax, 0xe0(%rsp)
movq %rax, %rdi
callq 0x30c0
leaq 0xf3a1(%rip), %rdi      # 0x58441
movl %ebx, %esi
xorl %eax, %eax
callq 0x3170
movslq %ebx, %rdi
movq %rdi, 0xb0(%rsp)
movl $0x8, %esi
callq 0x3260
movq %rax, %rbp
xorl %edi, %edi
callq 0x32d0
movl %eax, %edi
callq 0x3230
callq 0x34f0
movl %ebx, 0xbc(%rsp)
movl %ebx, %r14d
cmpl $0x0, 0xb0(%rsp)
jle 0x49139
movl %eax, %r13d
cvtsi2ssl 0xbc(%rsp), %xmm0
movss %xmm0, 0xb0(%rsp)
xorl %ebx, %ebx
movl %r13d, %edi
callq 0x3230
movq %r12, %rdi
movq %r15, %rsi
movl 0xc0(%rsp), %edx
callq 0x2f9e4
movq %rax, (%rbp,%rbx,8)
movss 0x34(%rax), %xmm0
mulss 0xb0(%rsp), %xmm0
movss %xmm0, 0x34(%rax)
incq %rbx
cmpq %rbx, %r14
jne 0x490ff
movq %r14, 0x170(%rsp)
xorl %edi, %edi
callq 0x32d0
movl %eax, %edi
callq 0x3230
movq %rbp, 0x168(%rsp)
movq (%rbp), %r12
movl 0x4(%r12), %ebp
imull 0xbc(%rsp), %ebp
imull 0x1c(%r12), %ebp
xorps %xmm0, %xmm0
cvtss2sd 0x34(%r12), %xmm0
cvtss2sd 0x38(%r12), %xmm1
cvtss2sd 0x3c(%r12), %xmm2
leaq 0xed0b(%rip), %rdi      # 0x57e98
movb $0x3, %al
callq 0x3170
movq 0xf8(%rsp), %rdi
callq 0x34b9c
movq %rax, %r13
leaq 0xf1aa(%rip), %rsi      # 0x58355
leaq 0xeee9(%rip), %rdx      # 0x5809b
movq %rax, %rdi
callq 0x34d5b
movq %rax, 0xe8(%rsp)
leaq 0xf986(%rip), %rsi      # 0x58b4f
movq %r13, %rdi
xorl %edx, %edx
callq 0x34e93
movl %eax, %r15d
leaq 0xd779(%rip), %rsi      # 0x56956
leaq 0xf96f(%rip), %rdx      # 0x58b53
movq %r13, %rdi
callq 0x34d5b
movq %rax, 0xc0(%rsp)
leaq 0xf0e1(%rip), %rsi      # 0x582dc
leaq 0xf91e(%rip), %rdx      # 0x58b20
movq %r13, %rdi
callq 0x34d5b
movq %rax, %rbx
leaq 0xdf73(%rip), %rsi      # 0x57187
movq %r13, %rdi
xorl %edx, %edx
callq 0x34d5b
testq %rax, %rax
je 0x49233
movq %rax, %rdi
callq 0x3cd97
movq %rax, 0xd0(%r12)
leaq 0xdf61(%rip), %rsi      # 0x5719b
movq %r13, %rdi
movl $0x2, %edx
callq 0x34d9f
movl %eax, %r14d
testl %r15d, %r15d
je 0x49268
movl $0x8, 0xc0(%rsp)
movq $0x0, 0xd8(%rsp)
jmp 0x49288
movq 0xc0(%rsp), %rdi
callq 0x2b6d3
movq %rax, 0xd8(%rsp)
movl $0x0, 0xc0(%rsp)
movq %rbx, %rdi
callq 0x296f8
movq %rax, %r15
movq %rax, %rdi
callq 0x10fa0
movq %rax, %rbx
movl (%r15), %esi
leaq 0xf199(%rip), %rdi      # 0x58441
xorl %eax, %eax
callq 0x3170
movq %r15, 0x160(%rsp)
movslq (%r15), %r13
leaq 0x1c8(%rsp), %r15
movl $0xa8, %edx
movq %r15, %rdi
xorl %esi, %esi
callq 0x31d0
movl 0x94(%r12), %eax
movl %eax, 0x2c(%r15)
movl 0x90(%r12), %ecx
movl %ecx, 0x28(%r15)
movl $0x20, (%r15)
movq 0xd0(%r12), %rcx
movq %rcx, 0xa0(%r15)
cvtsi2ss %eax, %xmm0
movss 0xa8(%r12), %xmm1
mulss %xmm0, %xmm1
cvttss2si %xmm1, %esi
movl %esi, 0x44(%r15)
mulss 0xa4(%r12), %xmm0
cvttss2si %xmm0, %edx
movl %edx, 0x48(%r15)
leaq 0xe552(%rip), %rdi      # 0x57884
xorl %eax, %eax
callq 0x3170
movss 0xb0(%r12), %xmm0
movss %xmm0, 0x68(%r15)
movss 0xb4(%r12), %xmm0
movss %xmm0, 0x6c(%r15)
movss 0xb8(%r12), %xmm0
movss %xmm0, 0x74(%r15)
movss 0xbc(%r12), %xmm0
movss %xmm0, 0x70(%r15)
movss 0xc0(%r12), %xmm0
movss %xmm0, 0x78(%r15)
movl 0x94(%r12), %eax
movl %eax, 0x4c(%r15)
movq %rbx, 0x158(%rsp)
movq %rbx, 0x8(%r15)
movl %r14d, 0x100(%rsp)
movl %r14d, 0x50(%r15)
movl %ebp, 0x18(%r15)
movq %r13, 0x108(%rsp)
movl %r13d, 0x1c(%r15)
movq 0xd8(%rsp), %rax
movq %rax, 0x20(%r15)
movl 0xc0(%rsp), %eax
movl %eax, 0x98(%r15)
leaq 0x188(%rsp), %rax
movq %rax, 0x80(%r15)
movl $0x15, %ecx
movq %rsp, %rdi
movq %r15, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x2e089
movq %rax, %r13
movq 0x8(%r12), %rax
movq (%rax), %r15
movq %r12, %rdi
callq 0x2fa22
movslq 0x54(%r12), %rcx
testq %rcx, %rcx
movq 0xe0(%rsp), %rbx
je 0x49427
cmpq %rcx, %rax
jae 0x49935
movq %r15, %rax
xorl %edx, %edx
movq 0x108(%rsp), %rcx
divq %rcx
movq %rax, %r14
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
movss %xmm0, 0x104(%rsp)
movss 0x9e16(%rip), %xmm0     # 0x53268
xorl %ebp, %ebp
movaps %xmm0, 0xc0(%rsp)
cmpl $0x0, 0xc4(%r12)
je 0x49618
imull $0xcccccccd, %ebp, %eax # imm = 0xCCCCCCCD
incl %ebp
addl $0x19999998, %eax       # imm = 0x19999998
rorl $0x3, %eax
cmpl $0x6666666, %eax        # imm = 0x6666666
ja 0x49618
movq %r14, 0xb0(%rsp)
leaq 0xf7f7(%rip), %rdi      # 0x58c8c
callq 0x30c0
callq 0x34f0
movslq %eax, %r15
imulq $0x2e8ba2e9, %r15, %rax # imm = 0x2E8BA2E9
movq %rax, %rcx
shrq $0x3f, %rcx
sarq $0x21, %rax
addl %ecx, %eax
leal (%rax,%rax,4), %ecx
leal (%rax,%rcx,2), %eax
subl %eax, %r15d
shll $0x5, %r15d
subl $-0x80, %r15d
leaq 0xef73(%rip), %rdi      # 0x58441
movl %r15d, %esi
xorl %eax, %eax
callq 0x3170
movl %r15d, 0x1f4(%rsp)
movl %r15d, 0x1f0(%rsp)
movl %r15d, 0x214(%rsp)
xorps %xmm0, %xmm0
cvtsi2ss %r15d, %xmm0
movss 0xa8(%r12), %xmm1
mulss %xmm0, %xmm1
cvttss2si %xmm1, %esi
movl %esi, 0x20c(%rsp)
mulss 0xa4(%r12), %xmm0
cvttss2si %xmm0, %edx
movl %edx, 0x210(%rsp)
leaq 0xe357(%rip), %rdi      # 0x57884
xorl %eax, %eax
callq 0x3170
movq %r13, %rdi
xorl %esi, %esi
callq 0x3480
movups 0x188(%rsp), %xmm0
movups 0x198(%rsp), %xmm1
movups 0x1a8(%rsp), %xmm2
movups 0x1b8(%rsp), %xmm3
movaps %xmm0, 0x110(%rsp)
movaps %xmm1, 0x120(%rsp)
movaps %xmm2, 0x130(%rsp)
movaps %xmm3, 0x140(%rsp)
movups 0x188(%rsp), %xmm0
movups 0x198(%rsp), %xmm1
movups 0x1a8(%rsp), %xmm2
movups 0x1b8(%rsp), %xmm3
movups %xmm3, 0x30(%rsp)
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x2b6fd
movl $0x15, %ecx
movq %rsp, %rdi
leaq 0x1c8(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x2e089
movq %rax, %r13
cmpl $0x0, 0xbc(%rsp)
movq 0x170(%rsp), %r14
movq 0x168(%rsp), %r12
jle 0x49604
xorl %ebx, %ebx
movq (%r12,%rbx,8), %rdi
movl %r15d, %esi
movl %r15d, %edx
callq 0x302fd
incq %rbx
cmpq %rbx, %r14
jne 0x495ed
movq (%r12), %r12
movq 0xe0(%rsp), %rbx
movq 0xb0(%rsp), %r14
xorl %eax, %eax
callq 0xdfdc
movsd %xmm0, 0xb0(%rsp)
movq %r13, %rdi
xorl %esi, %esi
callq 0x3480
movupd 0x188(%rsp), %xmm0
movups 0x198(%rsp), %xmm1
movups 0x1a8(%rsp), %xmm2
movups 0x1b8(%rsp), %xmm3
movapd %xmm0, 0x110(%rsp)
movaps %xmm1, 0x120(%rsp)
movaps %xmm2, 0x130(%rsp)
movaps %xmm3, 0x140(%rsp)
movl $0x15, %ecx
movq %rsp, %rdi
leaq 0x1c8(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x2e089
movq %rax, %r13
xorl %eax, %eax
callq 0xdfdc
subsd 0xb0(%rsp), %xmm0
leaq 0xe860(%rip), %rdi      # 0x57f06
movb $0x1, %al
callq 0x3170
xorl %eax, %eax
callq 0xdfdc
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 0x301c6
movaps 0xc0(%rsp), %xmm3
movaps %xmm3, %xmm1
cmpeqss 0x9b5c(%rip), %xmm1     # 0x53268
movaps %xmm1, %xmm2
andnps %xmm3, %xmm2
andps %xmm0, %xmm1
orps %xmm2, %xmm1
cvtss2sd %xmm1, %xmm1
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0xf8(%rsp)
mulsd 0xce7f(%rip), %xmm0     # 0x565b0
movsd %xmm0, 0xf0(%rsp)
mulsd 0xe74e(%rip), %xmm1     # 0x57e90
movsd %xmm1, 0xc0(%rsp)
movq %r12, %rdi
callq 0x2fa22
movq %rax, %r15
movq 0x8(%r12), %rax
movq (%rax), %rax
testq %rax, %rax
js 0x4976d
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
jmp 0x49785
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
divss 0x104(%rsp), %xmm0
movsd 0xc0(%rsp), %xmm1
addsd 0xf0(%rsp), %xmm1
cvtsd2ss %xmm1, %xmm1
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x180(%rsp)
movaps %xmm1, 0xc0(%rsp)
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
movsd %xmm0, 0xf0(%rsp)
movq %r12, %rdi
callq 0x2fa3d
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x178(%rsp)
xorl %eax, %eax
callq 0xdfdc
movaps %xmm0, %xmm4
subsd 0xb0(%rsp), %xmm4
movq 0x8(%r12), %rax
movq (%rax), %rdx
leaq 0xe934(%rip), %rdi      # 0x58134
movq %r15, %rsi
movsd 0x180(%rsp), %xmm0
movsd 0xf8(%rsp), %xmm1
movsd 0xf0(%rsp), %xmm2
movsd 0x178(%rsp), %xmm3
movb $0x5, %al
callq 0x3170
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)
callq 0x2b6fd
movq 0x8(%r12), %rax
movq (%rax), %rax
xorl %edx, %edx
divq 0x108(%rsp)
movq %rax, %r15
movslq %r14d, %rax
cmpq %rax, %r15
jbe 0x498b8
leaq 0x270(%rsp), %r14
movq %r14, %rdi
leaq 0xe840(%rip), %rsi      # 0x580d5
movq 0xe8(%rsp), %rdx
movq %rbx, %rcx
movl %r15d, %r8d
xorl %eax, %eax
callq 0x3430
movq %r12, %rdi
movq %r14, %rsi
callq 0x66cd
movl %r15d, %r14d
movq %r12, %rdi
callq 0x2fa22
movabsq $0x1cac083126e978d5, %rcx # imm = 0x1CAC083126E978D5
imulq %rcx, %rax
rorq $0x3, %rax
movabsq $0x4189374bc6a7ef, %rcx # imm = 0x4189374BC6A7EF
cmpq %rcx, %rax
ja 0x49916
movq %r14, %r15
leaq 0x270(%rsp), %r14
movq %r14, %rdi
leaq 0xe7f0(%rip), %rsi      # 0x580e6
movq 0xe8(%rsp), %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x3430
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %r14
callq 0x66cd
movq %r12, %rdi
callq 0x2fa22
movslq 0x54(%r12), %rcx
testq %rcx, %rcx
je 0x4945c
cmpq %rcx, %rax
jb 0x4945c
leaq 0xe831(%rip), %rsi      # 0x5816d
movq %rbx, %rcx
leaq 0x270(%rsp), %rbx
movq %rbx, %rdi
movq 0xe8(%rsp), %rdx
xorl %eax, %eax
callq 0x3430
movq %r12, %rdi
movq %rbx, %rsi
callq 0x66cd
movq %r13, %rdi
xorl %esi, %esi
callq 0x3480
movq %r12, %rdi
callq 0x314d2
movq 0xd8(%rsp), %rdi
testq %rdi, %rdi
je 0x4998f
movl 0x100(%rsp), %esi
callq 0xeb09
movq 0x160(%rsp), %rbx
movl (%rbx), %esi
movq 0x158(%rsp), %rdi
callq 0xeb09
movq %rbx, %rdi
callq 0x10f56
movq 0xe0(%rsp), %rdi
callq 0x3040
addq $0x378, %rsp            # imm = 0x378
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq