// File: /workspace/github_c_makefile_star2_O0/SimpleDoger[P]Darknet_CPU_Only/nightmare.c
// Function: optimize_picture
void optimize_picture(network *net, image orig, int max_layer, float scale, float rate, float thresh, int norm)
{
    //scale_image(orig, 2);
    //translate_image(orig, -1);
    net->n = max_layer + 1;

    int dx = rand()%16 - 8;
    int dy = rand()%16 - 8;
    int flip = rand()%2;

    image crop = crop_image(orig, dx, dy, orig.w, orig.h);
    image im = resize_image(crop, (int)(orig.w * scale), (int)(orig.h * scale));
    if(flip) flip_image(im);

    resize_network(net, im.w, im.h);
    layer last = net->layers[net->n-1];
    //net->layers[net->n - 1].activation = LINEAR;

    image delta = make_image(im.w, im.h, im.c);

    printf("\nnet: %d %d %d im: %d %d %d\n", net->w, net->h, net->inputs, im.w, im.h, im.c);
    copy_cpu(net->inputs, im.data, 1, net->input, 1);
    net->delta = delta.data;
    forward_network(net);
    copy_cpu(last.outputs, last.output, 1, last.delta, 1);
    calculate_loss(last.output, last.delta, last.outputs, thresh);
    backward_network(net);

    if(flip) flip_image(delta);
    //normalize_array(delta.data, delta.w*delta.h*delta.c);
    image resized = resize_image(delta, orig.w, orig.h);
    image out = crop_image(resized, -dx, -dy, orig.w, orig.h);

    /*
       image g = grayscale_image(out);
       free_image(out);
       out = g;
     */

    //rate = rate / abs_mean(out.data, out.w*out.h*out.c);
    image gray = make_image(out.w, out.h, out.c);
    fill_image(gray, .5);
    axpy_cpu(orig.w*orig.h*orig.c, -1, orig.data, 1, gray.data, 1);
    axpy_cpu(orig.w*orig.h*orig.c, .1, gray.data, 1, out.data, 1);

    if(norm) normalize_array(out.data, out.w*out.h*out.c);
    axpy_cpu(orig.w*orig.h*orig.c, rate, out.data, 1, orig.data, 1);

    /*
       normalize_array(orig.data, orig.w*orig.h*orig.c);
       scale_image(orig, sqrt(var));
       translate_image(orig, mean);
     */

    //translate_image(orig, 1);
    //scale_image(orig, .5);
    //normalize_image(orig);

    constrain_image(orig);

    free_image(crop);
    free_image(im);
    free_image(delta);
    free_image(resized);
    free_image(out);

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x580, %rsp            # imm = 0x580
leaq 0x10(%rbp), %rax
movq %rax, -0x560(%rbp)
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movss %xmm0, -0x10(%rbp)
movss %xmm1, -0x14(%rbp)
movss %xmm2, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl -0xc(%rbp), %ecx
addl $0x1, %ecx
movq -0x8(%rbp), %rax
movl %ecx, (%rax)
callq 0x34b0
movl $0x10, %ecx
cltd
idivl %ecx
subl $0x8, %edx
movl %edx, -0x20(%rbp)
callq 0x34b0
movl $0x10, %ecx
cltd
idivl %ecx
subl $0x8, %edx
movl %edx, -0x24(%rbp)
callq 0x34b0
movl $0x2, %ecx
cltd
idivl %ecx
movq -0x560(%rbp), %rax
movl %edx, -0x28(%rbp)
movl -0x20(%rbp), %esi
movl -0x24(%rbp), %edx
movl (%rax), %ecx
movl 0x4(%rax), %r8d
leaq -0x40(%rbp), %rdi
movq (%rax), %r9
movq %r9, (%rsp)
movq 0x8(%rax), %r9
movq %r9, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x11ca0
movq -0x560(%rbp), %rax
cvtsi2ssl (%rax), %xmm0
mulss -0x10(%rbp), %xmm0
cvttss2si %xmm0, %esi
cvtsi2ssl 0x4(%rax), %xmm0
mulss -0x10(%rbp), %xmm0
cvttss2si %xmm0, %edx
leaq -0x58(%rbp), %rdi
leaq -0x40(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0xfc00
cmpl $0x0, -0x28(%rbp)
je 0x6c5c6
leaq -0x58(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x104c0
movq -0x8(%rbp), %rdi
movl -0x58(%rbp), %esi
movl -0x54(%rbp), %edx
callq 0x3aa00
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rsi
movq -0x8(%rbp), %rax
movl (%rax), %eax
subl $0x1, %eax
cltq
imulq $0x4a0, %rax, %rax      # imm = 0x4A0
addq %rax, %rsi
leaq -0x4f8(%rbp), %rdi
movl $0x4a0, %edx            # imm = 0x4A0
callq 0x3280
movl -0x58(%rbp), %esi
movl -0x54(%rbp), %edx
movl -0x50(%rbp), %ecx
leaq -0x510(%rbp), %rdi
callq 0xe3e0
movq -0x8(%rbp), %rax
movl 0x94(%rax), %esi
movq -0x8(%rbp), %rax
movl 0x90(%rax), %edx
movq -0x8(%rbp), %rax
movl 0x80(%rax), %ecx
movl -0x58(%rbp), %r8d
movl -0x54(%rbp), %r9d
movl -0x50(%rbp), %eax
leaq 0xa440(%rip), %rdi      # 0x76a88
movl %eax, (%rsp)
movb $0x0, %al
callq 0x3130
movq -0x8(%rbp), %rax
movl 0x80(%rax), %edi
movq -0x48(%rbp), %rsi
movq -0x8(%rbp), %rax
movq 0xd8(%rax), %rcx
movl $0x1, %r8d
movl %r8d, %edx
callq 0x2f960
movq -0x500(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0xe8(%rax)
movq -0x8(%rbp), %rdi
callq 0x3a1b0
movl -0x4a0(%rbp), %edi
movq -0x290(%rbp), %rsi
movq -0x298(%rbp), %rcx
movl $0x1, %r8d
movl %r8d, %edx
callq 0x2f960
movq -0x290(%rbp), %rdi
movq -0x298(%rbp), %rsi
movl -0x4a0(%rbp), %edx
movss -0x18(%rbp), %xmm0
callq 0x6c3c0
movq -0x8(%rbp), %rdi
callq 0x3a5a0
cmpl $0x0, -0x28(%rbp)
je 0x6c708
leaq -0x510(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x104c0
movq -0x560(%rbp), %rax
movl (%rax), %esi
movl 0x4(%rax), %edx
leaq -0x528(%rbp), %rdi
leaq -0x510(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0xfc00
movq -0x560(%rbp), %rax
xorl %esi, %esi
subl -0x20(%rbp), %esi
xorl %edx, %edx
subl -0x24(%rbp), %edx
movl (%rax), %ecx
movl 0x4(%rax), %r8d
leaq -0x540(%rbp), %rdi
leaq -0x528(%rbp), %rax
movq (%rax), %r9
movq %r9, (%rsp)
movq 0x8(%rax), %r9
movq %r9, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x11ca0
movl -0x540(%rbp), %esi
movl -0x53c(%rbp), %edx
movl -0x538(%rbp), %ecx
leaq -0x558(%rbp), %rdi
callq 0xe3e0
leaq -0x558(%rbp), %rax
movss 0x4a3c(%rip), %xmm0     # 0x711ec
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x12370
movq -0x560(%rbp), %rax
movl (%rax), %edi
imull 0x4(%rax), %edi
imull 0x8(%rax), %edi
movq 0x10(%rax), %rsi
movq -0x548(%rbp), %rcx
movss 0x78ea(%rip), %xmm0     # 0x740dc
movl $0x1, %r8d
movl %r8d, %edx
callq 0x2f680
movq -0x560(%rbp), %rax
movl (%rax), %edi
imull 0x4(%rax), %edi
imull 0x8(%rax), %edi
movq -0x548(%rbp), %rsi
movq -0x530(%rbp), %rcx
movss 0x4a0d(%rip), %xmm0     # 0x71234
movl $0x1, %r8d
movl %r8d, %edx
callq 0x2f680
cmpl $0x0, -0x1c(%rbp)
je 0x6c85b
movq -0x530(%rbp), %rdi
movl -0x540(%rbp), %esi
imull -0x53c(%rbp), %esi
imull -0x538(%rbp), %esi
callq 0x58a0
movq -0x560(%rbp), %rax
movl (%rax), %edi
imull 0x4(%rax), %edi
imull 0x8(%rax), %edi
movss -0x14(%rbp), %xmm0
movq -0x530(%rbp), %rsi
movq 0x10(%rax), %rcx
movl $0x1, %r8d
movl %r8d, %edx
callq 0x2f680
movq -0x560(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x10db0
leaq -0x40(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0xedb0
leaq -0x58(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0xedb0
leaq -0x510(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0xedb0
leaq -0x528(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0xedb0
leaq -0x540(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0xedb0
addq $0x580, %rsp            # imm = 0x580
popq %rbp
retq
nopl (%rax,%rax)