// File: /workspace/github_c_makefile_star2_O1/SimpleDoger[P]Darknet_CPU_Only/captcha.c
// Function: train_captcha
void train_captcha(char *cfgfile, char *weightfile)
{
    srand(time(0));
    float avg_loss = -1;
    char *base = basecfg(cfgfile);
    printf("%s\n", base);
    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 = 1024;
    int i = *net->seen/imgs;
    int solved = 1;
    list *plist;
    char **labels = get_labels("/data/captcha/reimgs.labels.list");
    if (solved){
        plist = get_paths("/data/captcha/reimgs.solved.list");
    }else{
        plist = get_paths("/data/captcha/reimgs.raw.list");
    }
    char **paths = (char **)list_to_array(plist);
    printf("%d\n", plist->size);
    clock_t time;
    pthread_t load_thread;
    data train;
    data buffer;

    load_args args = {0};
    args.w = net->w;
    args.h = net->h;
    args.paths = paths;
    args.classes = 26;
    args.n = imgs;
    args.m = plist->size;
    args.labels = labels;
    args.d = &buffer;
    args.type = CLASSIFICATION_DATA;

    load_thread = load_data_in_thread(args);
    while(1){
        ++i;
        time=clock();
        pthread_join(load_thread, 0);
        train = buffer;
        fix_data_captcha(train, solved);

        /*
           image im = float_to_image(256, 256, 3, train.X.vals[114]);
           show_image(im, "training");
           cvWaitKey(0);
         */

        load_thread = load_data_in_thread(args);
        printf("Loaded: %lf seconds\n", sec(clock()-time));
        time=clock();
        float loss = train_network(net, train);
        if(avg_loss == -1) avg_loss = loss;
        avg_loss = avg_loss*.9 + loss*.1;
        printf("%d: %f, %f avg, %lf seconds, %ld images\n", i, loss, avg_loss, sec(clock()-time), *net->seen);
        free_data(train);
        if(i%100==0){
            char buff[256];
            sprintf(buff, "/home/pjreddie/imagenet_backup/%s_%d.weights",base, i);
            save_weights(net, buff);
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x308, %rsp            # imm = 0x308
movq %rsi, %rbx
movq %rdi, %r14
xorl %edi, %edi
callq 0x32d0
movl %eax, %edi
callq 0x3230
movq %r14, %rdi
callq 0xe717
movq %rax, 0xa8(%rsp)
movq %rax, %rdi
callq 0x30c0
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x2f9e4
movq %rax, %r14
cvtss2sd 0x34(%rax), %xmm0
cvtss2sd 0x38(%rax), %xmm1
cvtss2sd 0x3c(%rax), %xmm2
leaq 0x174da(%rip), %rdi     # 0x57e98
movb $0x3, %al
callq 0x3170
movq 0x8(%r14), %rax
movq (%rax), %r15
shrq $0xa, %r15
leaq 0x174ed(%rip), %rdi     # 0x57ec4
callq 0x2b6d3
movq %rax, %rbx
leaq 0x174ff(%rip), %rdi     # 0x57ee5
callq 0x296f8
movq %rax, %r12
movq %rax, %rdi
callq 0x10fa0
movq %rax, %r13
movl (%r12), %esi
leaq 0x17a3d(%rip), %rdi     # 0x58441
xorl %eax, %eax
callq 0x3170
leaq 0x158(%rsp), %rbp
movl $0xa8, %edx
movq %rbp, %rdi
xorl %esi, %esi
callq 0x31d0
movl 0x94(%r14), %eax
movl %eax, 0x2c(%rbp)
movl 0x90(%r14), %eax
movl %eax, 0x28(%rbp)
movq %r13, 0x8(%rbp)
movl $0x1a, 0x50(%rbp)
movl $0x400, 0x18(%rbp)      # imm = 0x400
movl (%r12), %eax
movl %eax, 0x1c(%rbp)
movq %rbx, 0x20(%rbp)
leaq 0x118(%rsp), %rax
movq %rax, 0x80(%rbp)
movl $0x0, 0x98(%rbp)
movl $0x15, %ecx
movq %rsp, %rdi
movq %rbp, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x2dc84
movq %rax, %rbx
movabsq $-0x3b65bfff3b65c000, %r13 # imm = 0xC49A4000C49A4000
leaq 0x17488(%rip), %rbp     # 0x57f1b
movss 0x127cd(%rip), %xmm0    # 0x53268
movaps %xmm0, 0x100(%rsp)
callq 0x3130
movq %rax, %r12
movq %rbx, %rdi
xorl %esi, %esi
callq 0x3480
movups 0x118(%rsp), %xmm0
movups 0x128(%rsp), %xmm1
movups 0x138(%rsp), %xmm2
movups 0x148(%rsp), %xmm3
movaps %xmm1, 0xd0(%rsp)
movaps %xmm2, 0xe0(%rsp)
movaps %xmm0, 0xc0(%rsp)
movaps %xmm3, 0xf0(%rsp)
movslq 0xd8(%rsp), %rax
testq %rax, %rax
xorps %xmm1, %xmm1
jle 0x40b5c
movq 0xe0(%rsp), %rcx
movslq 0xdc(%rsp), %rdx
xorl %esi, %esi
testl %edx, %edx
jle 0x40b54
movq (%rcx,%rsi,8), %rdi
xorl %r8d, %r8d
movss (%rdi,%r8,4), %xmm0
ucomiss %xmm1, %xmm0
jne 0x40b2f
jnp 0x40b47
movss 0x4(%rdi,%r8,4), %xmm0
ucomiss %xmm1, %xmm0
jne 0x40b3d
jnp 0x40b4b
movl $0x0, (%rdi,%r8,4)
jmp 0x40b4b
movq %r13, (%rdi,%r8,4)
addq $0x2, %r8
cmpq %rdx, %r8
jl 0x40b22
incq %rsi
cmpq %rax, %rsi
jne 0x40b17
incl %r15d
movl $0x15, %ecx
movq %rsp, %rdi
leaq 0x158(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x2dc84
movq %rax, %rbx
callq 0x3130
subq %r12, %rax
movq %rax, %rdi
callq 0xe8d4
cvtss2sd %xmm0, %xmm0
leaq 0x17371(%rip), %rdi     # 0x57f06
movb $0x1, %al
callq 0x3170
callq 0x3130
movq %rax, %r12
movaps 0xc0(%rsp), %xmm0
movaps 0xd0(%rsp), %xmm1
movaps 0xe0(%rsp), %xmm2
movaps 0xf0(%rsp), %xmm3
movups %xmm3, 0x30(%rsp)
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x301c6
movaps 0x100(%rsp), %xmm3
movaps %xmm3, %xmm1
cmpeqss 0x12675(%rip), %xmm1    # 0x53268
movaps %xmm1, %xmm2
andnps %xmm3, %xmm2
andps %xmm0, %xmm1
orps %xmm2, %xmm1
cvtss2sd %xmm1, %xmm1
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0xb8(%rsp)
mulsd 0x15998(%rip), %xmm0    # 0x565b0
mulsd 0x17270(%rip), %xmm1    # 0x57e90
addsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
movaps %xmm0, 0x100(%rsp)
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0xb0(%rsp)
callq 0x3130
subq %r12, %rax
movq %rax, %rdi
callq 0xe8d4
xorps %xmm2, %xmm2
cvtss2sd %xmm0, %xmm2
movq 0x8(%r14), %rax
movq (%rax), %rdx
movq %rbp, %rdi
movl %r15d, %esi
movsd 0xb8(%rsp), %xmm0
movsd 0xb0(%rsp), %xmm1
movb $0x3, %al
callq 0x3170
movaps 0xc0(%rsp), %xmm0
movaps 0xd0(%rsp), %xmm1
movaps 0xe0(%rsp), %xmm2
movaps 0xf0(%rsp), %xmm3
movups %xmm3, 0x30(%rsp)
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x2b6fd
imull $0xc28f5c29, %r15d, %eax # imm = 0xC28F5C29
addl $0x51eb850, %eax        # imm = 0x51EB850
rorl $0x2, %eax
cmpl $0x28f5c28, %eax        # imm = 0x28F5C28
ja 0x40aa3
leaq 0x200(%rsp), %r12
movq %r12, %rdi
leaq 0x17263(%rip), %rsi     # 0x57f44
movq 0xa8(%rsp), %rdx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x3430
movq %r14, %rdi
movq %r12, %rsi
callq 0x66cd
jmp 0x40aa3