// File: /workspace/github_c_makefile_star2_O2/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, %r14d
xorl %edi, %edi
callq 0x3220
cmpl $0x3, %r14d
jle 0x48f0b
movq 0x10(%r15), %r13
movq 0x18(%r15), %rax
movq %rax, 0x4e0(%rsp)
movq 0x20(%r15), %rax
movq %rax, 0x4d8(%rsp)
movq 0x28(%r15), %rdi
callq 0x3400
movl %eax, 0x4c0(%rsp)
leaq 0x9c2c(%rip), %rdx      # 0x523ba
pushq $0x1
popq %rbp
movl %r14d, %edi
movq %r15, %rsi
movl %ebp, %ecx
callq 0x4041
movl %eax, 0x4d0(%rsp)
leaq 0x9c15(%rip), %rdx      # 0x523c1
movl %r14d, %edi
movq %r15, %rsi
movl %ebp, %ecx
callq 0x4041
movl %eax, 0x4fc(%rsp)
leaq 0x9c00(%rip), %rdx      # 0x523c7
movl %r14d, %edi
movq %r15, %rsi
movl %ebp, %ecx
callq 0x4041
movl %eax, 0x4c4(%rsp)
leaq 0x90fd(%rip), %rdx      # 0x518df
pushq $0xa
popq %rcx
movl %r14d, %edi
movq %r15, %rsi
callq 0x4041
movl %eax, %r12d
leaq 0x9bd5(%rip), %rdx      # 0x523cf
pushq $0x4
popq %rcx
movl %r14d, %edi
movq %r15, %rsi
callq 0x4041
movl %eax, 0x4f8(%rsp)
leaq 0x9bc2(%rip), %rdx      # 0x523d8
movss 0x37ea(%rip), %xmm0     # 0x4c008
movl %r14d, %edi
movq %r15, %rsi
callq 0x40c0
movss %xmm0, 0x4cc(%rsp)
leaq 0x9ba5(%rip), %rdx      # 0x523de
movss 0x9aff(%rip), %xmm0     # 0x52340
movl %r14d, %edi
movq %r15, %rsi
callq 0x40c0
movss %xmm0, 0x4d4(%rsp)
leaq 0x97c3(%rip), %rdx      # 0x5201f
movl %r14d, %edi
movq %r15, %rsi
movss 0x379e(%rip), %xmm0     # 0x4c008
callq 0x40c0
movss %xmm0, 0x4f4(%rsp)
leaq 0x9b65(%rip), %rdx      # 0x523e4
xorps %xmm0, %xmm0
movl %r14d, %edi
movq %r15, %rsi
callq 0x40c0
movss %xmm0, 0x4ec(%rsp)
leaq 0x9b4f(%rip), %rdx      # 0x523ec
movss 0x7057(%rip), %xmm0     # 0x4f8fc
movl %r14d, %edi
movq %r15, %rsi
callq 0x40c0
movss %xmm0, 0x4f0(%rsp)
leaq 0x9b36(%rip), %rdx      # 0x523f6
movss 0x6ce0(%rip), %xmm0     # 0x4f5a8
movl %r14d, %edi
movq %r15, %rsi
callq 0x40c0
leaq 0x973d(%rip), %rdx      # 0x52017
movl %r14d, %edi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x4151
movq %rax, 0x530(%rsp)
leaq 0x9b08(%rip), %rdx      # 0x523fe
movl %r14d, %edi
movq %r15, %rsi
callq 0x3fdd
movl %eax, %ebx
leaq 0x9b01(%rip), %rdx      # 0x5240b
movl %r14d, %edi
movq %r15, %rsi
movl %ebp, %ecx
callq 0x4041
movq %r13, %rdi
movq 0x4e0(%rsp), %rsi
xorl %edx, %edx
callq 0x22658
movq %rax, %r15
movq %r13, %rdi
movl 0x4c0(%rsp), %r13d
callq 0x41c8
movq %rax, 0x508(%rsp)
movq 0x4d8(%rsp), %r14
movq %r14, %rdi
callq 0x41c8
movq %rax, 0x500(%rsp)
movq %r15, %rdi
movl %ebp, %esi
callq 0x22f00
leaq 0x4a0(%rsp), %rdi
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xbf36
movl $0x0, %eax
movq %rax, 0x4d8(%rsp)
testl %ebx, %ebx
je 0x48ab3
movl %r13d, (%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 0xe7d0
movq 0x10(%r14), %rsi
movq %rsi, 0x4b0(%rsp)
movups (%r14), %xmm0
movaps %xmm0, 0x4a0(%rsp)
movq %r15, %rdi
callq 0x233e2
movq 0x20(%r15), %rax
movslq (%r15), %rcx
imulq $0x4a0, %rcx, %rcx      # imm = 0x4A0
cmpl $0x16, -0x4a0(%rax,%rcx)
jne 0x48a36
leaq 0x9a51(%rip), %rdi      # 0x52459
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 0x31e8d
leaq 0x568(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x2330a
movq 0x10(%r14), %rax
movq %rax, 0x10(%rsp)
movups (%r14), %xmm0
movups %xmm0, (%rsp)
leaq 0x580(%rsp), %r14
movq %r14, %rdi
callq 0xb85b
movl 0x4(%r14), %esi
imull (%r14), %esi
imull 0x8(%r14), %esi
leaq 0x9995(%rip), %rdi      # 0x52413
xorl %eax, %eax
callq 0x3160
movq 0x10(%r14), %rax
movq %rax, 0x4d8(%rsp)
movl 0x4a0(%rsp), %esi
movl 0x4a4(%rsp), %edx
movl 0x4a8(%rsp), %ecx
leaq 0x550(%rsp), %rdi
callq 0xb45a
pushq $-0x2
popq %rcx
movl 0x4d0(%rsp), %eax
cltd
idivl %ecx
addl %r13d, %eax
movq %rax, 0x4e0(%rsp)
cvtss2sd 0x4cc(%rsp), %xmm0
movsd 0x364c(%rip), %xmm1     # 0x4c128
testl %r12d, %r12d
movl $0x0, %ecx
cmovlel %ecx, %r12d
movl 0x4c4(%rsp), %eax
testl %eax, %eax
cmovlel %ecx, %eax
subsd %xmm0, %xmm1
movsd %xmm1, 0x528(%rsp)
movq 0x1b4f0(%rip), %r13     # 0x63ff8
pushq $0x1
popq %rdx
xorl %ecx, %ecx
movl %eax, 0x4c4(%rsp)
cmpl %eax, %ecx
je 0x48ef9
movl %ecx, 0x4c8(%rsp)
movq (%r13), %rcx
leaq 0x98f2(%rip), %rdi      # 0x52420
pushq $0xb
popq %rsi
callq 0x3440
movq (%r13), %rdi
callq 0x3310
xorl %r14d, %r14d
movq (%r13), %rcx
cmpl %r14d, %r12d
je 0x48c83
movq %rcx, %rdi
leaq 0x847c(%rip), %rsi      # 0x50fd5
movl %r14d, %edx
xorl %eax, %eax
callq 0x3270
movq (%r13), %rdi
callq 0x3310
testl %ebx, %ebx
jne 0x48bfb
callq 0x34e0
cltd
idivl 0x4d0(%rsp)
movq 0x4e0(%rsp), %rax
leal (%rdx,%rax), %ebp
callq 0x34e0
cltd
idivl 0x4f8(%rsp)
cvtsi2sd %edx, %xmm1
movsd 0x97a3(%rip), %xmm0     # 0x52348
callq 0x3110
movsd 0x3576(%rip), %xmm1     # 0x4c128
divsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm0
movq 0x4b0(%rsp), %rax
movq %rax, 0x10(%rsp)
movaps 0x4a0(%rsp), %xmm1
movups %xmm1, (%rsp)
movq %r15, %rdi
movl %ebp, %esi
movss 0x4d4(%rsp), %xmm1
movss 0x4f4(%rsp), %xmm2
movl 0x4fc(%rsp), %edx
callq 0x4804e
jmp 0x48c7b
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 0x4d8(%rsp), %rsi
movss 0x4d4(%rsp), %xmm0
movss 0x4f0(%rsp), %xmm1
pushq $0x1
popq %rcx
callq 0x485c7
movq 0x4b0(%rsp), %rax
movq %rax, 0x10(%rsp)
movaps 0x4a0(%rsp), %xmm0
movups %xmm0, (%rsp)
leaq 0x97b9(%rip), %rdi      # 0x5242c
pushq $0xa
popq %rsi
callq 0xd4c7
incl %r14d
jmp 0x48b42
leaq 0x97b1(%rip), %rdi      # 0x5243b
pushq $0x5
popq %rsi
pushq $0x1
popq %rdx
callq 0x3440
movq 0x530(%rsp), %rdx
testq %rdx, %rdx
je 0x48ce1
movl 0x4c8(%rsp), %r14d
movl %r14d, (%rsp)
leaq 0x580(%rsp), %rbp
movq %rbp, %rdi
leaq 0x9781(%rip), %rsi      # 0x52441
movq 0x500(%rsp), %rcx
movq 0x508(%rsp), %r8
movl 0x4c0(%rsp), %r9d
xorl %eax, %eax
callq 0x3420
jmp 0x48d1d
leaq 0x580(%rsp), %rbp
movq %rbp, %rdi
leaq 0x9751(%rip), %rsi      # 0x52444
movq 0x500(%rsp), %rdx
movq 0x508(%rsp), %rcx
movl 0x4c0(%rsp), %r8d
movl 0x4c8(%rsp), %r14d
movl %r14d, %r9d
xorl %eax, %eax
callq 0x3420
leaq 0x972e(%rip), %rdi      # 0x52452
movl %r14d, %esi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x3160
movq 0x4b0(%rsp), %rax
movq %rax, 0x10(%rsp)
movaps 0x4a0(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %rbp, %rdi
callq 0xd514
movss 0x4ec(%rsp), %xmm0
ucomiss 0x3316(%rip), %xmm0     # 0x4c078
leaq 0x510(%rsp), %rbp
jne 0x48d6e
jnp 0x48dcf
movq 0x4b0(%rsp), %rax
movq %rax, 0x10(%rsp)
movapd 0x4a0(%rsp), %xmm1
movupd %xmm1, (%rsp)
movq %rbp, %rdi
callq 0xdf99
movq 0x4b0(%rsp), %rax
movq %rax, 0x10(%rsp)
movaps 0x4a0(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0xba88
movq 0x520(%rsp), %rax
movq %rax, 0x4b0(%rsp)
movups 0x510(%rsp), %xmm0
movaps %xmm0, 0x4a0(%rsp)
movl 0x4a0(%rsp), %eax
movl 0x4a4(%rsp), %edi
cvtsi2sd %eax, %xmm0
movsd 0x528(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd 0x332a(%rip), %xmm2     # 0x4c120
mulsd %xmm2, %xmm0
cvttsd2si %xmm0, %esi
cvtsi2sd %edi, %xmm0
mulsd %xmm1, %xmm0
mulsd %xmm2, %xmm0
cvttsd2si %xmm0, %edx
cvtsi2ss %eax, %xmm0
movss 0x4cc(%rsp), %xmm1
mulss %xmm1, %xmm0
cvttss2si %xmm0, %ecx
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 %rbp, %rdi
callq 0xdba7
movl 0x4a0(%rsp), %esi
movl 0x4a4(%rsp), %edx
movq 0x520(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x510(%rsp), %xmm0
movups %xmm0, (%rsp)
leaq 0x538(%rsp), %rdi
callq 0xc3ee
movq 0x4b0(%rsp), %rax
movq %rax, 0x10(%rsp)
movaps 0x4a0(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0xba88
movq 0x520(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x510(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0xba88
movq 0x548(%rsp), %rax
movq %rax, 0x4b0(%rsp)
movups 0x538(%rsp), %xmm0
movaps %xmm0, 0x4a0(%rsp)
movl 0x4c8(%rsp), %ecx
incl %ecx
movl 0x4c4(%rsp), %eax
pushq $0x1
popq %rdx
jmp 0x48b14
addq $0x688, %rsp            # imm = 0x688
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1b0e6(%rip), %rax     # 0x63ff8
movq (%rax), %rdi
movq (%r15), %rdx
movq 0x8(%r15), %rcx
leaq 0x9453(%rip), %rsi      # 0x52376
xorl %eax, %eax
addq $0x688, %rsp            # imm = 0x688
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3270
nop