// File: /workspace/github_c_makefile_star2_O1/SimpleDoger[P]Darknet_CPU_Only/segmenter.c
// Function: train_segmenter
void train_segmenter(char *datacfg, char *cfgfile, char *weightfile, int *gpus, int ngpus, int clear, int display)
{
    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];
    image pred = get_network_image(net);

    int div = net->w/pred.w;
    assert(pred.w * div == net->w);
    assert(pred.h * div == net->h);

    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/");
    char *train_list = option_find_str(options, "train", "data/train.list");

    list *plist = get_paths(train_list);
    char **paths = (char **)list_to_array(plist);
    printf("%d\n", plist->size);
    int N = plist->size;

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

    args.min = net->min_crop;
    args.max = net->max_crop;
    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.classes = 80;

    args.paths = paths;
    args.n = imgs;
    args.m = N;
    args.type = SEGMENTATION_DATA;

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

    int epoch = (*net->seen)/N;
    while(get_current_batch(net) < net->max_batches || net->max_batches == 0){
        double 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(display){
            image tr = float_to_image(net->w/div, net->h/div, 80, train.y.vals[net->batch*(net->subdivisions-1)]);
            image im = float_to_image(net->w, net->h, net->c, train.X.vals[net->batch*(net->subdivisions-1)]);
            image mask = mask_to_rgb(tr);
            image prmask = mask_to_rgb(pred);
            show_image(im, "input", 1);
            show_image(prmask, "pred", 1);
            show_image(mask, "truth", 100);
            free_image(mask);
            free_image(prmask);
        }
        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)%100 == 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);

    free_network(net);
    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 $0x3a8, %rsp            # imm = 0x3A8
movl %r9d, 0xe0(%rsp)
movl %r8d, %ebx
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, 0xa8(%rsp)
movq %rsi, %rdi
callq 0xe717
movq %rax, 0xc0(%rsp)
movq %rax, %rdi
callq 0x30c0
leaq 0x10934(%rip), %rdi     # 0x58441
movl %ebx, %esi
xorl %eax, %eax
callq 0x3170
movl %ebx, 0xd0(%rsp)
movslq %ebx, %rbx
movl $0x8, %esi
movq %rbx, %rdi
callq 0x3260
movq %rax, %r13
xorl %edi, %edi
callq 0x32d0
movl %eax, %edi
callq 0x3230
callq 0x34f0
testl %ebx, %ebx
jle 0x47b9c
movl %eax, %r15d
movl 0xd0(%rsp), %eax
cvtsi2ss %eax, %xmm0
movss %xmm0, 0xb0(%rsp)
movl %eax, %ebx
xorl %ebp, %ebp
movl %r15d, %edi
callq 0x3230
movq %r12, %rdi
movq %r14, %rsi
movl 0xe0(%rsp), %edx
callq 0x2f9e4
movq %rax, (%r13,%rbp,8)
movss 0x34(%rax), %xmm0
mulss 0xb0(%rsp), %xmm0
movss %xmm0, 0x34(%rax)
incq %rbp
cmpq %rbp, %rbx
jne 0x47b62
xorl %edi, %edi
callq 0x32d0
movl %eax, %edi
callq 0x3230
movq (%r13), %r14
leaq 0x190(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x30708
movl 0x94(%r14), %ecx
movl (%rbx), %esi
movl %ecx, %eax
cltd
idivl %esi
imull %eax, %esi
cmpl %ecx, %esi
jne 0x48315
movl %eax, %ebp
movl 0x194(%rsp), %eax
imull %ebp, %eax
cmpl 0x90(%r14), %eax
jne 0x48334
movl 0xd0(%rsp), %r13d
imull 0x4(%r14), %r13d
imull 0x1c(%r14), %r13d
xorps %xmm0, %xmm0
cvtss2sd 0x34(%r14), %xmm0
cvtss2sd 0x38(%r14), %xmm1
cvtss2sd 0x3c(%r14), %xmm2
leaq 0x10277(%rip), %rdi     # 0x57e98
movb $0x3, %al
callq 0x3170
movq 0xa8(%rsp), %rdi
callq 0x34b9c
movq %rax, %rbx
leaq 0x10716(%rip), %rsi     # 0x58355
leaq 0x10455(%rip), %rdx     # 0x5809b
movq %rax, %rdi
callq 0x34d5b
movq %rax, 0xb8(%rsp)
leaq 0x1067f(%rip), %rsi     # 0x582dc
leaq 0x10ebc(%rip), %rdx     # 0x58b20
movq %rbx, %rdi
callq 0x34d5b
movq %rax, %rdi
callq 0x296f8
movq %rax, %rbx
movq %rax, %rdi
callq 0x10fa0
movq %rax, %r12
movl (%rbx), %esi
leaq 0x107b6(%rip), %rdi     # 0x58441
xorl %eax, %eax
callq 0x3170
movq %rbx, 0x108(%rsp)
movslq (%rbx), %rbx
leaq 0x300(%rsp), %r15
movl $0xa8, %edx
movq %r15, %rdi
xorl %esi, %esi
callq 0x31d0
movl 0x94(%r14), %eax
movl %eax, 0x2c(%r15)
movl 0x90(%r14), %ecx
movl %ecx, 0x28(%r15)
movl $0x20, (%r15)
movl %ebp, 0x58(%r15)
movl 0xa0(%r14), %ecx
movl %ecx, 0x44(%r15)
movl 0x9c(%r14), %ecx
movl %ecx, 0x48(%r15)
movss 0xb0(%r14), %xmm0
movss %xmm0, 0x68(%r15)
movss 0xb4(%r14), %xmm0
movss %xmm0, 0x6c(%r15)
movss 0xb8(%r14), %xmm0
movss %xmm0, 0x74(%r15)
movss 0xbc(%r14), %xmm0
movss %xmm0, 0x70(%r15)
movss 0xc0(%r14), %xmm0
movss %xmm0, 0x78(%r15)
movl %eax, 0x4c(%r15)
movl $0x50, 0x50(%r15)
movq %r12, 0x100(%rsp)
movq %r12, 0x8(%r15)
movl %r13d, 0x18(%r15)
movq %rbx, 0xf8(%rsp)
movl %ebx, 0x1c(%r15)
movl $0xf, 0x98(%r15)
leaq 0x1c0(%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(%r14), %rax
movq (%rax), %rbx
movq %r14, %rdi
callq 0x2fa22
movslq 0x54(%r14), %rcx
testq %rcx, %rcx
je 0x47daf
cmpq %rcx, %rax
jae 0x4829d
movq 0xf8(%rsp), %rcx
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
movss %xmm0, 0xcc(%rsp)
movq %rbx, %rax
xorl %edx, %edx
divq %rcx
movq %rax, %rbx
movss 0xb48e(%rip), %xmm0     # 0x53268
leaq 0x200(%rsp), %r15
movaps %xmm0, 0xe0(%rsp)
xorl %eax, %eax
callq 0xdfdc
movsd %xmm0, 0xb0(%rsp)
movq %r13, %rdi
xorl %esi, %esi
callq 0x3480
movupd 0x1c0(%rsp), %xmm0
movups 0x1d0(%rsp), %xmm1
movups 0x1e0(%rsp), %xmm2
movups 0x1f0(%rsp), %xmm3
movapd %xmm0, 0x120(%rsp)
movaps %xmm1, 0x130(%rsp)
movaps %xmm2, 0x140(%rsp)
movaps %xmm3, 0x150(%rsp)
movl $0x15, %ecx
movq %rsp, %rdi
leaq 0x300(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x2e089
movq %rax, %r13
xorl %eax, %eax
callq 0xdfdc
subsd 0xb0(%rsp), %xmm0
leaq 0x1008e(%rip), %rdi     # 0x57f06
movb $0x1, %al
callq 0x3170
xorl %eax, %eax
callq 0xdfdc
movsd %xmm0, 0xb0(%rsp)
movaps 0x120(%rsp), %xmm0
movaps 0x130(%rsp), %xmm1
movaps 0x140(%rsp), %xmm2
movaps 0x150(%rsp), %xmm3
movups %xmm3, 0x30(%rsp)
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x301c6
cmpl $0x0, 0x3e0(%rsp)
je 0x4806d
movl 0x90(%r14), %ecx
movl 0x94(%r14), %eax
cltd
idivl %ebp
movl %eax, %esi
movl %ecx, %eax
cltd
idivl %ebp
movq 0x140(%rsp), %rcx
movslq 0x4(%r14), %rdx
movslq 0x1c(%r14), %rdi
decq %rdi
imulq %rdx, %rdi
movq (%rcx,%rdi,8), %r8
movq %r15, %rdi
movl %eax, %edx
movl $0x50, %ecx
movaps %xmm0, 0xd0(%rsp)
callq 0x1675b
movl 0x90(%r14), %edx
movl 0x94(%r14), %esi
movl 0x98(%r14), %ecx
movq 0x130(%rsp), %rax
movslq 0x4(%r14), %rdi
movslq 0x1c(%r14), %r8
decq %r8
imulq %rdi, %r8
movq (%rax,%r8,8), %r8
leaq 0x1a8(%rsp), %rdi
callq 0x1675b
movq 0x210(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x200(%rsp), %xmm0
movups %xmm0, (%rsp)
leaq 0x178(%rsp), %rdi
callq 0x150aa
movq 0x1a0(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x190(%rsp), %xmm0
movups %xmm0, (%rsp)
leaq 0x160(%rsp), %rdi
callq 0x150aa
movq 0x1b8(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x1a8(%rsp), %xmm0
movups %xmm0, (%rsp)
leaq 0x10b65(%rip), %rdi     # 0x58b30
movl $0x1, %esi
callq 0x3f2e
movq 0x170(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x160(%rsp), %xmm0
movups %xmm0, (%rsp)
leaq 0x10b41(%rip), %rdi     # 0x58b36
movl $0x1, %esi
callq 0x3f2e
movq 0x188(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x178(%rsp), %xmm0
movups %xmm0, (%rsp)
leaq 0xf4ae(%rip), %rdi      # 0x574cd
movl $0x64, %esi
callq 0x3f2e
movq 0x188(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x178(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x15b37
movq 0x170(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x160(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x15b37
movaps 0xd0(%rsp), %xmm0
movaps 0xe0(%rsp), %xmm2
movaps %xmm2, %xmm3
cmpeqss 0xb1e7(%rip), %xmm3     # 0x53268
movaps %xmm3, %xmm1
andps %xmm0, %xmm1
andnps %xmm2, %xmm3
orps %xmm1, %xmm3
xorps %xmm1, %xmm1
cvtss2sd %xmm3, %xmm1
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0xd0(%rsp)
mulsd 0xe507(%rip), %xmm0     # 0x565b0
movsd %xmm0, 0xa8(%rsp)
mulsd 0xfdd6(%rip), %xmm1     # 0x57e90
movsd %xmm1, 0xe0(%rsp)
movq %r14, %rdi
callq 0x2fa22
movq %rax, %r12
movq 0x8(%r14), %rax
movq (%rax), %rax
testq %rax, %rax
js 0x480e4
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
jmp 0x480fc
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
divss 0xcc(%rsp), %xmm0
movsd 0xe0(%rsp), %xmm1
addsd 0xa8(%rsp), %xmm1
cvtsd2ss %xmm1, %xmm1
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x118(%rsp)
movaps %xmm1, 0xe0(%rsp)
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
movsd %xmm0, 0xa8(%rsp)
movq %r14, %rdi
callq 0x2fa3d
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x110(%rsp)
xorl %eax, %eax
callq 0xdfdc
movaps %xmm0, %xmm4
subsd 0xb0(%rsp), %xmm4
movq 0x8(%r14), %rax
movq (%rax), %rdx
leaq 0xffbe(%rip), %rdi      # 0x58134
movq %r12, %rsi
movsd 0x118(%rsp), %xmm0
movsd 0xd0(%rsp), %xmm1
movsd 0xa8(%rsp), %xmm2
movsd 0x110(%rsp), %xmm3
movb $0x5, %al
callq 0x3170
movaps 0x120(%rsp), %xmm0
movaps 0x130(%rsp), %xmm1
movaps 0x140(%rsp), %xmm2
movaps 0x150(%rsp), %xmm3
movups %xmm3, 0x30(%rsp)
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x2b6fd
movq 0x8(%r14), %rax
movq (%rax), %rax
xorl %edx, %edx
divq 0xf8(%rsp)
movq %rax, %r12
movslq %ebx, %rax
cmpq %rax, %r12
jbe 0x4822a
movq %r15, %rdi
leaq 0xfed3(%rip), %rsi      # 0x580d5
movq 0xb8(%rsp), %rdx
movq 0xc0(%rsp), %rcx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x3430
movq %r14, %rdi
movq %r15, %rsi
callq 0x66cd
movl %r12d, %ebx
movq %r14, %rdi
callq 0x2fa22
movabsq $-0x70a3d70a3d70a3d7, %rcx # imm = 0x8F5C28F5C28F5C29
imulq %rcx, %rax
rorq $0x2, %rax
movabsq $0x28f5c28f5c28f5c, %rcx # imm = 0x28F5C28F5C28F5C
cmpq %rcx, %rax
ja 0x4827f
movq %r15, %rdi
leaq 0xfe89(%rip), %rsi      # 0x580e6
movq 0xb8(%rsp), %rdx
movq 0xc0(%rsp), %rcx
xorl %eax, %eax
callq 0x3430
movq %r14, %rdi
movq %r15, %rsi
callq 0x66cd
movq %r14, %rdi
callq 0x2fa22
movslq 0x54(%r14), %rcx
testq %rcx, %rcx
je 0x47dea
cmpq %rcx, %rax
jb 0x47dea
leaq 0xfec9(%rip), %rsi      # 0x5816d
leaq 0x200(%rsp), %rbx
movq %rbx, %rdi
movq 0xb8(%rsp), %rdx
movq 0xc0(%rsp), %r15
movq %r15, %rcx
xorl %eax, %eax
callq 0x3430
movq %r14, %rdi
movq %rbx, %rsi
callq 0x66cd
movq %r14, %rdi
callq 0x314d2
movq 0x108(%rsp), %rbx
movl (%rbx), %esi
movq 0x100(%rsp), %rdi
callq 0xeb09
movq %rbx, %rdi
callq 0x10f56
movq %r15, %rdi
callq 0x3040
addq $0x3a8, %rsp            # imm = 0x3A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x10793(%rip), %rdi     # 0x58aaf
leaq 0x11127(%rip), %rsi     # 0x5944a
leaq 0x1079c(%rip), %rcx     # 0x58ac6
movl $0x1b, %edx
callq 0x31c0
leaq 0x107ce(%rip), %rdi     # 0x58b09
leaq 0x11108(%rip), %rsi     # 0x5944a
leaq 0x1077d(%rip), %rcx     # 0x58ac6
movl $0x1c, %edx
callq 0x31c0