// File: /workspace/github_c_makefile_star2_O1/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 0x3230
cmpl $0x3, %r13d
jle 0x4f862
movq 0x10(%r15), %rbp
movq 0x18(%r15), %r12
movq 0x20(%r15), %r14
movq 0x28(%r15), %rdi
callq 0x3410
movl %eax, 0x4c4(%rsp)
leaq 0xa2c0(%rip), %rdx      # 0x5939a
movl %r13d, %edi
movq %r15, %rsi
movl $0x1, %ecx
callq 0xe4b3
movl %eax, 0x4d0(%rsp)
leaq 0xa2a9(%rip), %rdx      # 0x593a1
movl %r13d, %edi
movq %r15, %rsi
movl $0x1, %ecx
callq 0xe4b3
movl %eax, 0x4ec(%rsp)
leaq 0xa291(%rip), %rdx      # 0x593a7
movl %r13d, %edi
movq %r15, %rsi
movl $0x1, %ecx
callq 0xe4b3
movl %eax, 0x4cc(%rsp)
leaq 0x979b(%rip), %rdx      # 0x588cf
movl %r13d, %edi
movq %r15, %rsi
movl $0xa, %ecx
callq 0xe4b3
movl %eax, 0x4d4(%rsp)
leaq 0xa25d(%rip), %rdx      # 0x593af
movl %r13d, %edi
movq %r15, %rsi
movl $0x4, %ecx
callq 0xe4b3
movl %eax, 0x4e8(%rsp)
leaq 0xa248(%rip), %rdx      # 0x593b8
movss 0x3e90(%rip), %xmm0     # 0x53008
movl %r13d, %edi
movq %r15, %rsi
callq 0xe57a
movss %xmm0, 0x4c8(%rsp)
leaq 0xa22b(%rip), %rdx      # 0x593be
movss 0xa185(%rip), %xmm0     # 0x59320
movl %r13d, %edi
movq %r15, %rsi
callq 0xe57a
movss %xmm0, 0x4d8(%rsp)
leaq 0x9e51(%rip), %rdx      # 0x59007
movl %r13d, %edi
movq %r15, %rsi
movss 0x3e44(%rip), %xmm0     # 0x53008
callq 0xe57a
movss %xmm0, 0x4e4(%rsp)
leaq 0xa1eb(%rip), %rdx      # 0x593c4
xorps %xmm0, %xmm0
movl %r13d, %edi
movq %r15, %rsi
callq 0xe57a
movss %xmm0, 0x4dc(%rsp)
leaq 0xa1d5(%rip), %rdx      # 0x593cc
movss 0x76dd(%rip), %xmm0     # 0x568dc
movl %r13d, %edi
movq %r15, %rsi
callq 0xe57a
movss %xmm0, 0x4e0(%rsp)
leaq 0xa1bc(%rip), %rdx      # 0x593d6
movss 0x7366(%rip), %xmm0     # 0x56588
movl %r13d, %edi
movq %r15, %rsi
callq 0xe57a
leaq 0x9dcb(%rip), %rdx      # 0x58fff
movq $0x0, 0x500(%rsp)
movl %r13d, %edi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0xe64d
movq %rax, 0x528(%rsp)
leaq 0xa182(%rip), %rdx      # 0x593de
movl %r13d, %edi
movq %r15, %rsi
callq 0xe439
movl %eax, %ebx
leaq 0xa17b(%rip), %rdx      # 0x593eb
movl %r13d, %edi
movq %r15, %rsi
movl $0x1, %ecx
callq 0xe4b3
movq %rbp, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x2f9e4
movq %rax, %r15
movq %rbp, %rdi
callq 0xe717
movq %rax, 0x4f8(%rsp)
movq %r14, %rdi
callq 0xe717
movq %rax, 0x4f0(%rsp)
movq %r15, %rdi
movl $0x1, %esi
callq 0x302d4
leaq 0x4a0(%rsp), %rdi
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x160b6
testl %ebx, %ebx
je 0x4f404
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 0x1905c
movq 0x10(%r14), %rsi
movq %rsi, 0x4b0(%rsp)
movups (%r14), %xmm0
movaps %xmm0, 0x4a0(%rsp)
movq %r15, %rdi
callq 0x307ea
movq 0x20(%r15), %rax
movslq (%r15), %rcx
imulq $0x4a0, %rcx, %rcx      # imm = 0x4A0
cmpl $0x16, -0x4a0(%rax,%rcx)
jne 0x4f387
leaq 0xa0e0(%rip), %rdi      # 0x59439
callq 0x30c0
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 0x3c6dc
leaq 0x568(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x30708
movq 0x10(%r14), %rax
movq %rax, 0x10(%rsp)
movups (%r14), %xmm0
movups %xmm0, (%rsp)
leaq 0x580(%rsp), %r14
movq %r14, %rdi
callq 0x15841
movl 0x4(%r14), %esi
imull (%r14), %esi
imull 0x8(%r14), %esi
leaq 0xa024(%rip), %rdi      # 0x593f3
xorl %eax, %eax
callq 0x3170
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 0x1529e
cmpl $0x0, 0x4cc(%rsp)
movl 0x4d4(%rsp), %r13d
jle 0x4f850
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 0x3cd9(%rip), %xmm1     # 0x53128
subsd %xmm0, %xmm1
movsd %xmm1, 0x520(%rsp)
movq 0x1ab95(%rip), %r12     # 0x69ff8
xorl %ebp, %ebp
movq (%r12), %rcx
movl $0xb, %esi
movl $0x1, %edx
leaq 0x9f86(%rip), %rdi      # 0x59400
callq 0x3450
movq (%r12), %rdi
callq 0x3320
testl %r13d, %r13d
jle 0x4f5e1
xorl %r14d, %r14d
movq (%r12), %rdi
leaq 0x8afe(%rip), %rsi      # 0x57f9d
movl %r14d, %edx
xorl %eax, %eax
callq 0x3280
movq (%r12), %rdi
callq 0x3320
testl %ebx, %ebx
jne 0x4f551
callq 0x34f0
cltd
idivl 0x4d0(%rsp)
movq 0x530(%rsp), %rax
leal (%rdx,%rax), %r13d
callq 0x34f0
cltd
idivl 0x4e8(%rsp)
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
movsd 0x9e39(%rip), %xmm0     # 0x59328
callq 0x3120
movsd 0x3c2c(%rip), %xmm1     # 0x53128
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 0x4e8e4
jmp 0x4f5d5
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 0x4ef23
movq 0x4b0(%rsp), %rax
movq %rax, 0x10(%rsp)
movaps 0x4a0(%rsp), %xmm0
movups %xmm0, (%rsp)
leaq 0x9e41(%rip), %rdi      # 0x5940c
movl $0xa, %esi
callq 0x3f2e
incl %r14d
cmpl %r14d, %r13d
jne 0x4f494
movq (%r12), %rcx
movl $0x5, %esi
movl $0x1, %edx
leaq 0x9e25(%rip), %rdi      # 0x5941b
callq 0x3450
movq 0x528(%rsp), %rdx
testq %rdx, %rdx
je 0x4f63e
movl %ebp, (%rsp)
leaq 0x580(%rsp), %r14
movq %r14, %rdi
leaq 0x9e04(%rip), %rsi      # 0x59421
movq 0x4f0(%rsp), %rcx
movq 0x4f8(%rsp), %r8
movl 0x4c4(%rsp), %r9d
xorl %eax, %eax
callq 0x3430
jmp 0x4f672
leaq 0x580(%rsp), %r14
movq %r14, %rdi
leaq 0x9dd4(%rip), %rsi      # 0x59424
movq 0x4f0(%rsp), %rdx
movq 0x4f8(%rsp), %rcx
movl 0x4c4(%rsp), %r8d
movl %ebp, %r9d
xorl %eax, %eax
callq 0x3430
leaq 0x9db9(%rip), %rdi      # 0x59432
movl %ebp, %esi
movq %r14, %rdx
xorl %eax, %eax
callq 0x3170
movq 0x4b0(%rsp), %rax
movq %rax, 0x10(%rsp)
movaps 0x4a0(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x17a72
movss 0x4dc(%rsp), %xmm0
ucomiss 0x39c2(%rip), %xmm0     # 0x53078
leaq 0x508(%rsp), %r14
jne 0x4f6c2
jnp 0x4f723
movq 0x4b0(%rsp), %rax
movq %rax, 0x10(%rsp)
movapd 0x4a0(%rsp), %xmm1
movupd %xmm1, (%rsp)
movq %r14, %rdi
callq 0x18754
movq 0x4b0(%rsp), %rax
movq %rax, 0x10(%rsp)
movaps 0x4a0(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x15b37
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 0x39d3(%rip), %xmm2     # 0x53120
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 0x181f7
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 0x1676b
movq 0x4b0(%rsp), %rax
movq %rax, 0x10(%rsp)
movaps 0x4a0(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x15b37
movq 0x518(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x508(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x15b37
movq 0x548(%rsp), %rax
movq %rax, 0x4b0(%rsp)
movups 0x538(%rsp), %xmm0
movaps %xmm0, 0x4a0(%rsp)
incl %ebp
cmpl 0x4cc(%rsp), %ebp
jne 0x4f465
addq $0x688, %rsp            # imm = 0x688
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
callq 0xd883
jmp 0x4f850