// File: /workspace/github_c_makefile_star2_O0/SimpleDoger[P]Darknet_CPU_Only/detector.c
// Function: train_detector
void train_detector(char *datacfg, char *cfgfile, char *weightfile, int *gpus, int ngpus, int clear)
{
    list *options = read_data_cfg(datacfg);
    char *train_images = option_find_str(options, "train", "data/train.list");
    char *backup_directory = option_find_str(options, "backup", "/backup/");

    srand(time(0));
    char *base = basecfg(cfgfile);
    printf("%s\n", base);
    float avg_loss = -1;
    network **nets = calloc(ngpus, sizeof(network));

    srand(time(0));
    int seed = rand();
    int i;
    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);
    data train, buffer;

    layer l = net->layers[net->n - 1];

    int classes = l.classes;
    float jitter = l.jitter;

    list *plist = get_paths(train_images);
    //int N = plist->size;
    char **paths = (char **)list_to_array(plist);

    load_args args = get_base_args(net);
    args.coords = l.coords;
    args.paths = paths;
    args.n = imgs;
    args.m = plist->size;
    args.classes = classes;
    args.jitter = jitter;
    args.num_boxes = l.max_boxes;
    args.d = &buffer;
    args.type = DETECTION_DATA;
    //args.type = INSTANCE_DATA;
    args.threads = 64;

    pthread_t load_thread = load_data(args);
    double time;
    int count = 0;
    //while(i*imgs < N*120){
    while(get_current_batch(net) < net->max_batches){
        if(l.random && count++%10 == 0){
            printf("Resizing\n");
            int dim = (rand() % 10 + 10) * 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;

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

            #pragma omp parallel for
            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);

        /*
           int k;
           for(k = 0; k < l.max_boxes; ++k){
           box b = float_to_box(train.y.vals[10] + 1 + k*5);
           if(!b.x) break;
           printf("loaded: %f %f %f %f\n", b.x, b.y, b.w, b.h);
           }
         */
        /*
           int zz;
           for(zz = 0; zz < train.X.cols; ++zz){
           image im = float_to_image(net->w, net->h, 3, train.X.vals[zz]);
           int k;
           for(k = 0; k < l.max_boxes; ++k){
           box b = float_to_box(train.y.vals[zz] + k*5, 1);
           printf("%f %f %f %f\n", b.x, b.y, b.w, b.h);
           draw_bbox(im, b, 1, 1,0,0);
           }
           show_image(im, "truth11");
           cvWaitKey(0);
           save_image(im, "truth11");
           }
         */

        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 < 0) avg_loss = loss;
        avg_loss = avg_loss*.9 + loss*.1;

        i = get_current_batch(net);
        printf("%ld: %f, %f avg, %f rate, %lf seconds, %d images\n", get_current_batch(net), loss, avg_loss, get_current_rate(net), what_time_is_it_now()-time, i*imgs);
        if(i%100==0){
            char buff[256];
            sprintf(buff, "%s/%s.backup", backup_directory, base);
            save_weights(net, buff);
        }
        if(i%10000==0 || (i < 1000 && i%100 == 0)){
            char buff[256];
            sprintf(buff, "%s/%s_%d.weights", backup_directory, base, i);
            save_weights(net, buff);
        }
        free_data(train);
    }
    char buff[256];
    sprintf(buff, "%s/%s_final.weights", backup_directory, base);
    save_weights(net, buff);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa40, %rsp            # imm = 0xA40
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl %r9d, -0x28(%rbp)
movq -0x8(%rbp), %rdi
callq 0x426b0
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
leaq 0xcdc1(%rip), %rsi      # 0x75bbc
leaq 0xd5b6(%rip), %rdx      # 0x763b8
callq 0x42920
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rdi
leaq 0xcde4(%rip), %rsi      # 0x75bfa
leaq 0xcb66(%rip), %rdx      # 0x75983
callq 0x42920
movq %rax, -0x40(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x3290
movl %eax, %edi
callq 0x31e0
movq -0x10(%rbp), %rdi
callq 0x4850
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rsi
leaq 0xb54f(%rip), %rdi      # 0x7439d
movb $0x0, %al
callq 0x3130
movss 0xb27f(%rip), %xmm0     # 0x740dc
movss %xmm0, -0x4c(%rbp)
movslq -0x24(%rbp), %rdi
movl $0x110, %esi            # imm = 0x110
callq 0x3220
movq %rax, -0x58(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x3290
movl %eax, %edi
callq 0x31e0
callq 0x34b0
movl %eax, -0x5c(%rbp)
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x68ee8
movl -0x5c(%rbp), %edi
callq 0x31e0
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x28(%rbp), %edx
callq 0x39a70
movq %rax, %rdx
movq -0x58(%rbp), %rax
movslq -0x60(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
cvtsi2ssl -0x24(%rbp), %xmm0
movq -0x58(%rbp), %rax
movslq -0x60(%rbp), %rcx
movq (%rax,%rcx,8), %rax
mulss 0x34(%rax), %xmm0
movss %xmm0, 0x34(%rax)
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x68e93
xorl %eax, %eax
movl %eax, %edi
callq 0x3290
movl %eax, %edi
callq 0x31e0
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x68(%rbp), %rcx
imull 0x1c(%rcx), %eax
imull -0x24(%rbp), %eax
movl %eax, -0x6c(%rbp)
movq -0x68(%rbp), %rax
movss 0x34(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x68(%rbp), %rax
movss 0x38(%rax), %xmm1
cvtss2sd %xmm1, %xmm1
movq -0x68(%rbp), %rax
movss 0x3c(%rax), %xmm2
cvtss2sd %xmm2, %xmm2
leaq 0xc821(%rip), %rdi      # 0x75768
movb $0x3, %al
callq 0x3130
movq -0x68(%rbp), %rax
movq 0x20(%rax), %rsi
movq -0x68(%rbp), %rax
movl (%rax), %eax
subl $0x1, %eax
cltq
imulq $0x4a0, %rax, %rax      # imm = 0x4A0
addq %rax, %rsi
leaq -0x590(%rbp), %rdi
movl $0x4a0, %edx            # imm = 0x4A0
callq 0x3280
movl -0x494(%rbp), %eax
movl %eax, -0x594(%rbp)
movss -0x4b8(%rbp), %xmm0
movss %xmm0, -0x598(%rbp)
movq -0x38(%rbp), %rdi
callq 0x31450
movq %rax, -0x5a0(%rbp)
movq -0x5a0(%rbp), %rdi
callq 0x9020
movq %rax, -0x5a8(%rbp)
movq -0x68(%rbp), %rsi
leaq -0x650(%rbp), %rdi
callq 0x39990
movl -0x490(%rbp), %eax
movl %eax, -0x5f0(%rbp)
movq -0x5a8(%rbp), %rax
movq %rax, -0x648(%rbp)
movl -0x6c(%rbp), %eax
movl %eax, -0x638(%rbp)
movq -0x5a0(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x634(%rbp)
movl -0x594(%rbp), %eax
movl %eax, -0x600(%rbp)
movss -0x598(%rbp), %xmm0
movss %xmm0, -0x5ec(%rbp)
movl -0x508(%rbp), %eax
movl %eax, -0x610(%rbp)
leaq -0xf0(%rbp), %rax
movq %rax, -0x5d0(%rbp)
movl $0x1, -0x5b8(%rbp)
movl $0x40, -0x650(%rbp)
movq %rsp, %rdi
movl $0x15, %ecx
leaq -0x650(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x37900
movq %rax, -0x658(%rbp)
movl $0x0, -0x664(%rbp)
movq -0x68(%rbp), %rdi
callq 0x39ad0
movq -0x68(%rbp), %rcx
movslq 0x54(%rcx), %rcx
cmpq %rcx, %rax
jae 0x6946b
cmpl $0x0, -0x438(%rbp)
je 0x691eb
movl -0x664(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x664(%rbp)
movl $0xa, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x691eb
leaq 0xd342(%rip), %rdi      # 0x76401
movb $0x0, %al
callq 0x3130
callq 0x34b0
movl $0xa, %ecx
cltd
idivl %ecx
movl %edx, %eax
addl $0xa, %eax
shll $0x5, %eax
movl %eax, -0x668(%rbp)
movq -0x68(%rbp), %rdi
callq 0x39ad0
addq $0xc8, %rax
movq -0x68(%rbp), %rcx
movslq 0x54(%rcx), %rcx
cmpq %rcx, %rax
jbe 0x69107
movl $0x260, -0x668(%rbp)    # imm = 0x260
movl -0x668(%rbp), %esi
leaq 0xa062(%rip), %rdi      # 0x73176
movb $0x0, %al
callq 0x3130
movl -0x668(%rbp), %eax
movl %eax, -0x624(%rbp)
movl -0x668(%rbp), %eax
movl %eax, -0x628(%rbp)
movq -0x658(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x3440
leaq -0xb0(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
movl $0x40, %edx
callq 0x3280
movups -0xb0(%rbp), %xmm0
movups -0xa0(%rbp), %xmm1
movups -0x90(%rbp), %xmm2
movups -0x80(%rbp), %xmm3
movq %rsp, %rax
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x34400
movq %rsp, %rdi
movl $0x15, %ecx
leaq -0x650(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x37900
movq %rax, -0x658(%rbp)
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x691e0
movq -0x58(%rbp), %rax
movslq -0x60(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movl -0x668(%rbp), %esi
movl -0x668(%rbp), %edx
callq 0x3aa00
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x691b0
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x68(%rbp)
movb $0x0, %al
callq 0x3f10
movsd %xmm0, -0x660(%rbp)
movq -0x658(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x3440
leaq -0xb0(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
movl $0x40, %edx
callq 0x3280
movq %rsp, %rdi
movl $0x15, %ecx
leaq -0x650(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x37900
movq %rax, -0x658(%rbp)
movb $0x0, %al
callq 0x3f10
subsd -0x660(%rbp), %xmm0
leaq 0xc59e(%rip), %rdi      # 0x757f4
movb $0x1, %al
callq 0x3130
movb $0x0, %al
callq 0x3f10
movsd %xmm0, -0x660(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x66c(%rbp)
movq -0x68(%rbp), %rdi
movups -0xb0(%rbp), %xmm0
movups -0xa0(%rbp), %xmm1
movups -0x90(%rbp), %xmm2
movups -0x80(%rbp), %xmm3
movq %rsp, %rax
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x3a860
movss %xmm0, -0x66c(%rbp)
xorps %xmm0, %xmm0
ucomiss -0x4c(%rbp), %xmm0
jbe 0x692c9
movss -0x66c(%rbp), %xmm0
movss %xmm0, -0x4c(%rbp)
movss -0x4c(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movss -0x66c(%rbp), %xmm1
cvtss2sd %xmm1, %xmm1
movsd 0xadd2(%rip), %xmm2     # 0x740b8
mulsd %xmm2, %xmm1
movsd 0xc46e(%rip), %xmm2     # 0x75760
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x4c(%rbp)
movq -0x68(%rbp), %rdi
callq 0x39ad0
movl %eax, -0x60(%rbp)
movq -0x68(%rbp), %rdi
callq 0x39ad0
movq %rax, -0x990(%rbp)
movss -0x66c(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x978(%rbp)
movss -0x4c(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x988(%rbp)
movq -0x68(%rbp), %rdi
callq 0x39b60
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x980(%rbp)
movb $0x0, %al
callq 0x3f10
movq -0x990(%rbp), %rsi
movsd -0x988(%rbp), %xmm1
movsd -0x980(%rbp), %xmm2
movaps %xmm0, %xmm3
movsd -0x978(%rbp), %xmm0
subsd -0x660(%rbp), %xmm3
movl -0x60(%rbp), %edx
imull -0x6c(%rbp), %edx
leaq 0xd610(%rip), %rdi      # 0x769a8
movb $0x4, %al
callq 0x3130
movl -0x60(%rbp), %eax
movl $0x64, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x693dc
leaq -0x770(%rbp), %rdi
movq -0x40(%rbp), %rdx
movq -0x48(%rbp), %rcx
leaq 0xc609(%rip), %rsi      # 0x759ce
movb $0x0, %al
callq 0x33f0
movq -0x68(%rbp), %rdi
leaq -0x770(%rbp), %rsi
callq 0x41980
movl -0x60(%rbp), %eax
movl $0x2710, %ecx           # imm = 0x2710
cltd
idivl %ecx
cmpl $0x0, %edx
je 0x69405
cmpl $0x3e8, -0x60(%rbp)     # imm = 0x3E8
jge 0x69436
movl -0x60(%rbp), %eax
movl $0x64, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x69436
leaq -0x870(%rbp), %rdi
movq -0x40(%rbp), %rdx
movq -0x48(%rbp), %rcx
movl -0x60(%rbp), %r8d
leaq 0xc59e(%rip), %rsi      # 0x759bd
movb $0x0, %al
callq 0x33f0
movq -0x68(%rbp), %rdi
leaq -0x870(%rbp), %rsi
callq 0x41980
movups -0xb0(%rbp), %xmm0
movups -0xa0(%rbp), %xmm1
movups -0x90(%rbp), %xmm2
movups -0x80(%rbp), %xmm3
movq %rsp, %rax
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x34400
jmp 0x6906f
leaq -0x970(%rbp), %rdi
movq -0x40(%rbp), %rdx
movq -0x48(%rbp), %rcx
leaq 0xc55a(%rip), %rsi      # 0x759db
movb $0x0, %al
callq 0x33f0
movq -0x68(%rbp), %rdi
leaq -0x970(%rbp), %rsi
callq 0x41980
addq $0xa40, %rsp            # imm = 0xA40
popq %rbp
retq
nopw %cs:(%rax,%rax)