// File: /workspace/github_c_makefile_star2_O3/SimpleDoger[P]Darknet_CPU_Only/coco.c
// Function: train_coco
void train_coco(char *cfgfile, char *weightfile)
{
    //char *train_images = "/home/pjreddie/data/voc/test/train.txt";
    //char *train_images = "/home/pjreddie/data/coco/train.txt";
    char *train_images = "data/coco.trainval.txt";
    //char *train_images = "data/bags.train.list";
    char *backup_directory = "/home/pjreddie/backup/";
    srand(time(0));
    char *base = basecfg(cfgfile);
    printf("%s\n", base);
    float avg_loss = -1;
    network *net = load_network(cfgfile, weightfile, 0);
    printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net->learning_rate, net->momentum, net->decay);
    int imgs = net->batch*net->subdivisions;
    int i = *net->seen/imgs;
    data train, buffer;


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

    int side = l.side;
    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 = {0};
    args.w = net->w;
    args.h = net->h;
    args.paths = paths;
    args.n = imgs;
    args.m = plist->size;
    args.classes = classes;
    args.jitter = jitter;
    args.num_boxes = side;
    args.d = &buffer;
    args.type = REGION_DATA;

    args.angle = net->angle;
    args.exposure = net->exposure;
    args.saturation = net->saturation;
    args.hue = net->hue;

    pthread_t load_thread = load_data_in_thread(args);
    clock_t time;
    //while(i*imgs < N*120){
    while(get_current_batch(net) < net->max_batches){
        i += 1;
        time=clock();
        pthread_join(load_thread, 0);
        train = buffer;
        load_thread = load_data_in_thread(args);

        printf("Loaded: %lf seconds\n", sec(clock()-time));

        /*
           image im = float_to_image(net->w, net->h, 3, train.X.vals[113]);
           image copy = copy_image(im);
           draw_coco(copy, train.y.vals[113], 7, "truth");
           cvWaitKey(0);
           free_image(copy);
         */

        time=clock();
        float loss = train_network(net, train);
        if (avg_loss < 0) avg_loss = loss;
        avg_loss = avg_loss*.9 + loss*.1;

        printf("%d: %f, %f avg, %f rate, %lf seconds, %d images\n", i, loss, avg_loss, get_current_rate(net), sec(clock()-time), i*imgs);
        if(i%1000==0 || (i < 1000 && i%100 == 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);
        }
        free_data(train);
    }
    char buff[256];
    sprintf(buff, "%s/%s_final.weights", backup_directory, base);
    save_weights(net, buff);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x328, %rsp            # imm = 0x328
movq %rsi, %rbx
movq %rdi, %r14
xorl %edi, %edi
callq 0x32c0
movl %eax, %edi
callq 0x3220
movq %r14, %rdi
callq 0xef02
movq %rax, 0xb0(%rsp)
movq %rax, %rdi
callq 0x30b0
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x316bc
movq %rax, %r14
cvtss2sd 0x34(%rax), %xmm0
cvtss2sd 0x38(%rax), %xmm1
cvtss2sd 0x3c(%rax), %xmm2
leaq 0xbb45(%rip), %rdi      # 0x59fd0
movb $0x3, %al
callq 0x3160
movslq 0x4(%r14), %rax
movslq 0x1c(%r14), %rbp
imulq %rax, %rbp
movq 0x8(%r14), %rax
movq (%rax), %rax
movq %rax, 0xb8(%rsp)
movq 0x20(%r14), %rax
movslq (%r14), %rcx
imulq $0x4a0, %rcx, %rcx      # imm = 0x4A0
movl -0x40c(%rax,%rcx), %edx
movl %edx, 0xd0(%rsp)
movss -0x3c8(%rax,%rcx), %xmm0
movss %xmm0, 0xc0(%rsp)
movl -0x3a4(%rax,%rcx), %r12d
leaq 0xcb6a(%rip), %rdi      # 0x5b054
callq 0x2af38
movq %rax, %r15
movq %rax, %rdi
callq 0x11928
movq %rax, %r13
leaq 0x178(%rsp), %rbx
movl $0xa8, %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x31c0
movq 0x90(%r14), %rax
movq %rax, 0x28(%rbx)
movq %r13, 0x8(%rbx)
movq %rbp, 0xc8(%rsp)
movl %ebp, 0x18(%rbx)
movl (%r15), %eax
movl %eax, 0x1c(%rbx)
movl %r12d, 0x50(%rbx)
movss 0xc0(%rsp), %xmm0
movss %xmm0, 0x64(%rbx)
movl 0xd0(%rsp), %eax
movl %eax, 0x40(%rbx)
leaq 0x138(%rsp), %rax
movq %rax, 0x80(%rbx)
movl $0x3, 0x98(%rbx)
movss 0xb0(%r14), %xmm0
movss %xmm0, 0x68(%rbx)
movsd 0xb8(%r14), %xmm0
shufps $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movlps %xmm0, 0x70(%rbx)
movss 0xc0(%r14), %xmm0
movss %xmm0, 0x78(%rbx)
movl $0x15, %ecx
movq %rsp, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x2f8eb
movq %rax, %r15
movq %r14, %rdi
callq 0x316fa
movslq 0x54(%r14), %rcx
cmpq %rcx, %rax
jae 0x4e89d
movq 0xb8(%rsp), %rax
xorl %edx, %edx
divq 0xc8(%rsp)
movq %rax, %r12
movss 0x6cb6(%rip), %xmm0     # 0x55294
movaps %xmm0, 0xd0(%rsp)
leaq 0x220(%rsp), %r13
leal 0x1(%r12), %ebp
callq 0x3120
movq %rax, %rbx
movq %r15, %rdi
xorl %esi, %esi
callq 0x3470
movups 0x138(%rsp), %xmm0
movups 0x148(%rsp), %xmm1
movups 0x158(%rsp), %xmm2
movups 0x168(%rsp), %xmm3
movaps %xmm0, 0xf0(%rsp)
movaps %xmm1, 0x100(%rsp)
movaps %xmm2, 0x110(%rsp)
movaps %xmm3, 0x120(%rsp)
movl $0x15, %ecx
movq %rsp, %rdi
leaq 0x178(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x2f8eb
movq %rax, %r15
callq 0x3120
subq %rbx, %rax
movq %rax, %rdi
callq 0xf0bf
cvtss2sd %xmm0, %xmm0
leaq 0xb9c3(%rip), %rdi      # 0x5a03e
movb $0x1, %al
callq 0x3160
callq 0x3120
movq %rax, %rbx
movaps 0xf0(%rsp), %xmm0
movaps 0x100(%rsp), %xmm1
movaps 0x110(%rsp), %xmm2
movaps 0x120(%rsp), %xmm3
movups %xmm3, 0x30(%rsp)
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x31ea6
movaps 0xd0(%rsp), %xmm3
movaps %xmm3, %xmm1
cmpltss 0x69c7(%rip), %xmm1     # 0x550a0
movaps %xmm1, %xmm2
andnps %xmm3, %xmm2
andps %xmm0, %xmm1
orps %xmm2, %xmm1
cvtss2sd %xmm1, %xmm1
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0xc0(%rsp)
mulsd 0x9f82(%rip), %xmm0     # 0x58680
mulsd 0xb8c2(%rip), %xmm1     # 0x59fc8
addsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
movaps %xmm0, 0xd0(%rsp)
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0xb8(%rsp)
movq %r14, %rdi
callq 0x31715
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0xe8(%rsp)
callq 0x3120
subq %rbx, %rax
movq %rax, %rdi
callq 0xf0bf
xorps %xmm3, %xmm3
cvtss2sd %xmm0, %xmm3
movl %ebp, %edx
imull 0xc8(%rsp), %edx
leaq 0xba79(%rip), %rdi      # 0x5a1dc
movl %ebp, %esi
movsd 0xc0(%rsp), %xmm0
movsd 0xb8(%rsp), %xmm1
movsd 0xe8(%rsp), %xmm2
movb $0x4, %al
callq 0x3160
imull $0x26e978d5, %r12d, %ecx # imm = 0x26E978D5
addl $0x27ef9dad, %ecx       # imm = 0x27EF9DAD
rorl $0x3, %ecx
movslq %ebp, %rax
cmpl $0x418936, %ecx         # imm = 0x418936
jbe 0x4e7d3
cmpl $0x3e7, %r12d           # imm = 0x3E7
setl %cl
imulq $0x51eb851f, %rax, %rdx # imm = 0x51EB851F
movq %rdx, %rsi
shrq $0x3f, %rsi
sarq $0x25, %rdx
addl %esi, %edx
imull $0x64, %edx, %edx
subl %edx, %eax
sete %dl
andb %cl, %dl
xorl %ebx, %ebx
cmpb $0x1, %dl
je 0x4e7ee
jmp 0x4e81e
imulq $0x51eb851f, %rax, %rcx # imm = 0x51EB851F
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x25, %rcx
addl %edx, %ecx
imull $0x64, %ecx, %ecx
subl %ecx, %eax
movl %eax, %ebx
movq %r13, %rdi
leaq 0xba15(%rip), %rsi      # 0x5a20d
leaq 0xb9c6(%rip), %rdx      # 0x5a1c5
movq 0xb0(%rsp), %rcx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x3420
movq %r14, %rdi
movq %r13, %rsi
callq 0x6879
movl %ebx, %eax
testl %eax, %eax
jne 0x4e84d
movq %r13, %rdi
leaq 0xb9f2(%rip), %rsi      # 0x5a21e
leaq 0xb992(%rip), %rdx      # 0x5a1c5
movq 0xb0(%rsp), %rcx
xorl %eax, %eax
callq 0x3420
movq %r14, %rdi
movq %r13, %rsi
callq 0x6879
movaps 0xf0(%rsp), %xmm0
movaps 0x100(%rsp), %xmm1
movaps 0x110(%rsp), %xmm2
movaps 0x120(%rsp), %xmm3
movups %xmm3, 0x30(%rsp)
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x2d296
movq %r14, %rdi
callq 0x316fa
movslq 0x54(%r14), %rcx
movl %ebp, %r12d
cmpq %rcx, %rax
jb 0x4e5ee
leaq 0xb987(%rip), %rsi      # 0x5a22b
leaq 0xb91a(%rip), %rdx      # 0x5a1c5
leaq 0x220(%rsp), %rbx
movq %rbx, %rdi
movq 0xb0(%rsp), %rcx
xorl %eax, %eax
callq 0x3420
movq %r14, %rdi
movq %rbx, %rsi
callq 0x6879
addq $0x328, %rsp            # imm = 0x328
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq