// File: /workspace/github_c_makefile_star2_O3/SimpleDoger[P]Darknet_CPU_Only/nightmare.c
// Function: run_nightmare
void run_nightmare(int argc, char **argv)
{
    srand(0);
    if(argc < 4){
        fprintf(stderr, "usage: %s %s [cfg] [weights] [image] [layer] [options! (optional)]\n", argv[0], argv[1]);
        return;
    }

    char *cfg = argv[2];
    char *weights = argv[3];
    char *input = argv[4];
    int max_layer = atoi(argv[5]);

    int range = find_int_arg(argc, argv, "-range", 1);
    int norm = find_int_arg(argc, argv, "-norm", 1);
    int rounds = find_int_arg(argc, argv, "-rounds", 1);
    int iters = find_int_arg(argc, argv, "-iters", 10);
    int octaves = find_int_arg(argc, argv, "-octaves", 4);
    float zoom = find_float_arg(argc, argv, "-zoom", 1.);
    float rate = find_float_arg(argc, argv, "-rate", .04);
    float thresh = find_float_arg(argc, argv, "-thresh", 1.);
    float rotate = find_float_arg(argc, argv, "-rotate", 0);
    float momentum = find_float_arg(argc, argv, "-momentum", .9);
    float lambda = find_float_arg(argc, argv, "-lambda", .01);
    char *prefix = find_char_arg(argc, argv, "-prefix", 0);
    int reconstruct = find_arg(argc, argv, "-reconstruct");
    int smooth_size = find_int_arg(argc, argv, "-smooth", 1);

    network *net = load_network(cfg, weights, 0);
    char *cfgbase = basecfg(cfg);
    char *imbase = basecfg(input);

    set_batch_network(net, 1);
    image im = load_image_color(input, 0, 0);
    if(0){
        float scale = 1;
        if(im.w > 512 || im.h > 512){
            if(im.w > im.h) scale = 512.0/im.w;
            else scale = 512.0/im.h;
        }
        image resized = resize_image(im, scale*im.w, scale*im.h);
        free_image(im);
        im = resized;
    }
    //im = letterbox_image(im, net->w, net->h);

    float *features = 0;
    image update;
    if (reconstruct){
        net->n = max_layer;
        im = letterbox_image(im, net->w, net->h);
        //resize_network(&net, im.w, im.h);

        network_predict(net, im.data);
        if(net->layers[net->n-1].type == REGION){
            printf("region!\n");
            zero_objectness(net->layers[net->n-1]);
        }
        image out_im = copy_image(get_network_image(net));
        /*
           image crop = crop_image(out_im, zz, zz, out_im.w-2*zz, out_im.h-2*zz);
        //flip_image(crop);
        image f_im = resize_image(crop, out_im.w, out_im.h);
        free_image(crop);
         */
        printf("%d features\n", out_im.w*out_im.h*out_im.c);

        features = out_im.data;

        /*
        int i;
           for(i = 0; i < 14*14*512; ++i){
        //features[i] += rand_uniform(-.19, .19);
        }
        free_image(im);
        im = make_random_image(im.w, im.h, im.c);
         */
        update = make_image(im.w, im.h, im.c);
    }

    int e;
    int n;
    for(e = 0; e < rounds; ++e){
        fprintf(stderr, "Iteration: ");
        fflush(stderr);
        for(n = 0; n < iters; ++n){  
            fprintf(stderr, "%d, ", n);
            fflush(stderr);
            if(reconstruct){
                reconstruct_picture(net, features, im, update, rate, momentum, lambda, smooth_size, 1);
                //if ((n+1)%30 == 0) rate *= .5;
                show_image(im, "reconstruction", 10);
            }else{
                int layer = max_layer + rand()%range - range/2;
                int octave = rand()%octaves;
                optimize_picture(net, im, layer, 1/pow(1.33333333, octave), rate, thresh, norm);
            }
        }
        fprintf(stderr, "done\n");
        if(0){
            image g = grayscale_image(im);
            free_image(im);
            im = g;
        }
        char buff[256];
        if (prefix){
            sprintf(buff, "%s/%s_%s_%d_%06d",prefix, imbase, cfgbase, max_layer, e);
        }else{
            sprintf(buff, "%s_%s_%d_%06d",imbase, cfgbase, max_layer, e);
        }
        printf("%d %s\n", e, buff);
        save_image(im, buff);
        //show_image(im, buff, 0);

        if(rotate){
            image rot = rotate_image(im, rotate);
            free_image(im);
            im = rot;
        }
        image crop = crop_image(im, im.w * (1. - zoom)/2., im.h * (1.-zoom)/2., im.w*zoom, im.h*zoom);
        image resized = resize_image(crop, im.w, im.h);
        free_image(im);
        free_image(crop);
        im = resized;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x688, %rsp            # imm = 0x688
movq %rsi, %r15
movl %edi, %r13d
xorl %edi, %edi
callq 0x3220
cmpl $0x3, %r13d
jle 0x51ad4
movq 0x10(%r15), %rbp
movq 0x18(%r15), %r12
movq 0x20(%r15), %r14
movq 0x28(%r15), %rdi
callq 0x3400
movl %eax, 0x4c4(%rsp)
leaq 0xa1ae(%rip), %rdx      # 0x5b4fa
movl %r13d, %edi
movq %r15, %rsi
movl $0x1, %ecx
callq 0xed18
movl %eax, 0x4d0(%rsp)
leaq 0xa197(%rip), %rdx      # 0x5b501
movl %r13d, %edi
movq %r15, %rsi
movl $0x1, %ecx
callq 0xed18
movl %eax, 0x4ec(%rsp)
leaq 0xa17f(%rip), %rdx      # 0x5b507
movl %r13d, %edi
movq %r15, %rsi
movl $0x1, %ecx
callq 0xed18
movl %eax, 0x4cc(%rsp)
leaq 0x9681(%rip), %rdx      # 0x5aa27
movl %r13d, %edi
movq %r15, %rsi
movl $0xa, %ecx
callq 0xed18
movl %eax, 0x4d4(%rsp)
leaq 0xa14b(%rip), %rdx      # 0x5b50f
movl %r13d, %edi
movq %r15, %rsi
movl $0x4, %ecx
callq 0xed18
movl %eax, 0x4e8(%rsp)
leaq 0xa136(%rip), %rdx      # 0x5b518
movss 0x3c1e(%rip), %xmm0     # 0x55008
movl %r13d, %edi
movq %r15, %rsi
callq 0xedb7
movss %xmm0, 0x4c8(%rsp)
leaq 0xa119(%rip), %rdx      # 0x5b51e
movss 0xa073(%rip), %xmm0     # 0x5b480
movl %r13d, %edi
movq %r15, %rsi
callq 0xedb7
movss %xmm0, 0x4d8(%rsp)
leaq 0x9d37(%rip), %rdx      # 0x5b15f
movl %r13d, %edi
movq %r15, %rsi
movss 0x3bd2(%rip), %xmm0     # 0x55008
callq 0xedb7
movss %xmm0, 0x4e4(%rsp)
leaq 0xa0d9(%rip), %rdx      # 0x5b524
xorps %xmm0, %xmm0
movl %r13d, %edi
movq %r15, %rsi
callq 0xedb7
movss %xmm0, 0x4dc(%rsp)
leaq 0xa0c3(%rip), %rdx      # 0x5b52c
movss 0x753b(%rip), %xmm0     # 0x589ac
movl %r13d, %edi
movq %r15, %rsi
callq 0xedb7
movss %xmm0, 0x4e0(%rsp)
leaq 0xa0aa(%rip), %rdx      # 0x5b536
movss 0x71c4(%rip), %xmm0     # 0x58658
movl %r13d, %edi
movq %r15, %rsi
callq 0xedb7
leaq 0x9cb1(%rip), %rdx      # 0x5b157
movq $0x0, 0x500(%rsp)
movl %r13d, %edi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0xee68
movq %rax, 0x528(%rsp)
leaq 0xa070(%rip), %rdx      # 0x5b53e
movl %r13d, %edi
movq %r15, %rsi
callq 0xeca2
movl %eax, %ebx
leaq 0xa069(%rip), %rdx      # 0x5b54b
movl %r13d, %edi
movq %r15, %rsi
movl $0x1, %ecx
callq 0xed18
movq %rbp, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x316bc
movq %rax, %r15
movq %rbp, %rdi
callq 0xef02
movq %rax, 0x4f8(%rsp)
movq %r14, %rdi
callq 0xef02
movq %rax, 0x4f0(%rsp)
movq %r15, %rdi
movl $0x1, %esi
callq 0x320a7
leaq 0x4a0(%rsp), %rdi
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x16ac9
testl %ebx, %ebx
je 0x51676
movl 0x4c4(%rsp), %eax
movl %eax, (%r15)
movl 0x90(%r15), %edx
movl 0x94(%r15), %esi
movq 0x4b0(%rsp), %rax
movq %rax, 0x10(%rsp)
movaps 0x4a0(%rsp), %xmm0
movups %xmm0, (%rsp)
leaq 0x580(%rsp), %r14
movq %r14, %rdi
callq 0x19916
movq 0x10(%r14), %rsi
movq %rsi, 0x4b0(%rsp)
movups (%r14), %xmm0
movaps %xmm0, 0x4a0(%rsp)
movq %r15, %rdi
callq 0x32690
movq 0x20(%r15), %rax
movslq (%r15), %rcx
imulq $0x4a0, %rcx, %rcx      # imm = 0x4A0
cmpl $0x16, -0x4a0(%rax,%rcx)
jne 0x515f9
leaq 0x9fce(%rip), %rdi      # 0x5b599
callq 0x30b0
movq 0x20(%r15), %rax
movslq (%r15), %rcx
imulq $0x4a0, %rcx, %rcx      # imm = 0x4A0
leaq (%rax,%rcx), %rsi
addq $-0x4a0, %rsi           # imm = 0xFB60
movl $0x94, %ecx
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
callq 0x3e876
leaq 0x568(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x325ae
movq 0x10(%r14), %rax
movq %rax, 0x10(%rsp)
movups (%r14), %xmm0
movups %xmm0, (%rsp)
leaq 0x580(%rsp), %r14
movq %r14, %rdi
callq 0x1626c
movl 0x4(%r14), %esi
imull (%r14), %esi
imull 0x8(%r14), %esi
leaq 0x9f12(%rip), %rdi      # 0x5b553
xorl %eax, %eax
callq 0x3160
movq 0x10(%r14), %rax
movq %rax, 0x500(%rsp)
movl 0x4a0(%rsp), %esi
movl 0x4a4(%rsp), %edx
movl 0x4a8(%rsp), %ecx
leaq 0x550(%rsp), %rdi
callq 0x15d43
cmpl $0x0, 0x4cc(%rsp)
movl 0x4d4(%rsp), %r13d
jle 0x51ac2
movl 0x4d0(%rsp), %ecx
movl %ecx, %eax
shrl $0x1f, %eax
addl %ecx, %eax
sarl %eax
movl 0x4c4(%rsp), %ecx
subl %eax, %ecx
movq %rcx, 0x530(%rsp)
xorps %xmm0, %xmm0
cvtss2sd 0x4c8(%rsp), %xmm0
movsd 0x3a8f(%rip), %xmm1     # 0x55150
subsd %xmm0, %xmm1
movsd %xmm1, 0x520(%rsp)
movq 0x1a923(%rip), %r12     # 0x6bff8
xorl %ebp, %ebp
movq (%r12), %rcx
movl $0xb, %esi
movl $0x1, %edx
leaq 0x9e74(%rip), %rdi      # 0x5b560
callq 0x3440
movq (%r12), %rdi
callq 0x3310
testl %r13d, %r13d
jle 0x51853
xorl %r14d, %r14d
movq (%r12), %rdi
leaq 0x89c4(%rip), %rsi      # 0x5a0d5
movl %r14d, %edx
xorl %eax, %eax
callq 0x3270
movq (%r12), %rdi
callq 0x3310
testl %ebx, %ebx
jne 0x517c3
callq 0x34e0
cltd
idivl 0x4d0(%rsp)
movq 0x530(%rsp), %rax
leal (%rdx,%rax), %r13d
callq 0x34e0
cltd
idivl 0x4e8(%rsp)
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
movsd 0x9d27(%rip), %xmm0     # 0x5b488
callq 0x3110
movsd 0x39e2(%rip), %xmm1     # 0x55150
divsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
movq 0x4b0(%rsp), %rax
movq %rax, 0x10(%rsp)
movaps 0x4a0(%rsp), %xmm1
movups %xmm1, (%rsp)
movq %r15, %rdi
movl %r13d, %esi
movl 0x4d4(%rsp), %r13d
movss 0x4d8(%rsp), %xmm1
movss 0x4e4(%rsp), %xmm2
movl 0x4ec(%rsp), %edx
callq 0x50b50
jmp 0x51847
movq 0x560(%rsp), %rax
movq %rax, 0x28(%rsp)
movups 0x550(%rsp), %xmm0
movups %xmm0, 0x18(%rsp)
movq 0x4b0(%rsp), %rax
movq %rax, 0x10(%rsp)
movaps 0x4a0(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r15, %rdi
movq 0x500(%rsp), %rsi
movss 0x4d8(%rsp), %xmm0
movss 0x4e0(%rsp), %xmm1
movl $0x1, %ecx
callq 0x51195
movq 0x4b0(%rsp), %rax
movq %rax, 0x10(%rsp)
movaps 0x4a0(%rsp), %xmm0
movups %xmm0, (%rsp)
leaq 0x9d2f(%rip), %rdi      # 0x5b56c
movl $0xa, %esi
callq 0x40f9
incl %r14d
cmpl %r14d, %r13d
jne 0x51706
movq (%r12), %rcx
movl $0x5, %esi
movl $0x1, %edx
leaq 0x9d13(%rip), %rdi      # 0x5b57b
callq 0x3440
movq 0x528(%rsp), %rdx
testq %rdx, %rdx
je 0x518b0
movl %ebp, (%rsp)
leaq 0x580(%rsp), %r14
movq %r14, %rdi
leaq 0x9cf2(%rip), %rsi      # 0x5b581
movq 0x4f0(%rsp), %rcx
movq 0x4f8(%rsp), %r8
movl 0x4c4(%rsp), %r9d
xorl %eax, %eax
callq 0x3420
jmp 0x518e4
leaq 0x580(%rsp), %r14
movq %r14, %rdi
leaq 0x9cc2(%rip), %rsi      # 0x5b584
movq 0x4f0(%rsp), %rdx
movq 0x4f8(%rsp), %rcx
movl 0x4c4(%rsp), %r8d
movl %ebp, %r9d
xorl %eax, %eax
callq 0x3420
leaq 0x9ca7(%rip), %rdi      # 0x5b592
movl %ebp, %esi
movq %r14, %rdx
xorl %eax, %eax
callq 0x3160
movq 0x4b0(%rsp), %rax
movq %rax, 0x10(%rsp)
movaps 0x4a0(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x18301
movss 0x4dc(%rsp), %xmm0
ucomiss 0x3778(%rip), %xmm0     # 0x550a0
leaq 0x508(%rsp), %r14
jne 0x51934
jnp 0x51995
movq 0x4b0(%rsp), %rax
movq %rax, 0x10(%rsp)
movapd 0x4a0(%rsp), %xmm1
movupd %xmm1, (%rsp)
movq %r14, %rdi
callq 0x18f38
movq 0x4b0(%rsp), %rax
movq %rax, 0x10(%rsp)
movaps 0x4a0(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x1657b
movq 0x518(%rsp), %rax
movq %rax, 0x4b0(%rsp)
movups 0x508(%rsp), %xmm0
movaps %xmm0, 0x4a0(%rsp)
movl 0x4a0(%rsp), %eax
movl 0x4a4(%rsp), %edi
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd 0x520(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd 0x3789(%rip), %xmm2     # 0x55148
mulsd %xmm2, %xmm0
cvttsd2si %xmm0, %esi
xorps %xmm0, %xmm0
cvtsi2sd %edi, %xmm0
mulsd %xmm1, %xmm0
mulsd %xmm2, %xmm0
cvttsd2si %xmm0, %edx
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movss 0x4c8(%rsp), %xmm1
mulss %xmm1, %xmm0
cvttss2si %xmm0, %ecx
xorps %xmm0, %xmm0
cvtsi2ss %edi, %xmm0
mulss %xmm1, %xmm0
cvttss2si %xmm0, %r8d
movq 0x4b0(%rsp), %rax
movq %rax, 0x10(%rsp)
movaps 0x4a0(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x18a89
movl 0x4a0(%rsp), %esi
movl 0x4a4(%rsp), %edx
movq 0x518(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x508(%rsp), %xmm0
movups %xmm0, (%rsp)
leaq 0x538(%rsp), %rdi
callq 0x170fa
movq 0x4b0(%rsp), %rax
movq %rax, 0x10(%rsp)
movaps 0x4a0(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x1657b
movq 0x518(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x508(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x1657b
movq 0x548(%rsp), %rax
movq %rax, 0x4b0(%rsp)
movups 0x538(%rsp), %xmm0
movaps %xmm0, 0x4a0(%rsp)
incl %ebp
cmpl 0x4cc(%rsp), %ebp
jne 0x516d7
addq $0x688, %rsp            # imm = 0x688
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
callq 0xe043
jmp 0x51ac2
nop