// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movl %edx, 0x58(%rsp)
movss %xmm2, 0x54(%rsp)
movss %xmm1, 0x5c(%rsp)
movss %xmm0, 0x24(%rsp)
movq %rdi, %r12
incl %esi
movl %esi, (%rdi)
callq 0x34e0
leal 0xf(%rax), %ebx
testl %eax, %eax
cmovnsl %eax, %ebx
andl $-0x10, %ebx
movl %ebx, %ecx
negl %ecx
movq %rax, 0x48(%rsp)
addl %ecx, %eax
addl $-0x8, %eax
movl %eax, 0x50(%rsp)
callq 0x34e0
leal 0xf(%rax), %r14d
testl %eax, %eax
cmovnsl %eax, %r14d
andl $-0x10, %r14d
movl %r14d, %ecx
negl %ecx
movq %rax, 0x40(%rsp)
leal (%rax,%rcx), %ebp
addl $-0x8, %ebp
callq 0x34e0
movl %eax, %r15d
movl 0x110(%rsp), %ecx
movl 0x114(%rsp), %r8d
movq 0x120(%rsp), %rax
movq %rax, 0x10(%rsp)
movaps 0x110(%rsp), %xmm0
movups %xmm0, (%rsp)
leaq 0xa8(%rsp), %r13
movq %r13, %rdi
movl 0x50(%rsp), %esi
movl %ebp, %edx
callq 0x18a89
xorps %xmm0, %xmm0
cvtsi2ssl 0x110(%rsp), %xmm0
movss 0x24(%rsp), %xmm1
mulss %xmm1, %xmm0
cvttss2si %xmm0, %esi
xorps %xmm0, %xmm0
cvtsi2ssl 0x114(%rsp), %xmm0
mulss %xmm1, %xmm0
cvttss2si %xmm0, %edx
movq 0x10(%r13), %rax
movq %rax, 0x10(%rsp)
movups (%r13), %xmm0
movups %xmm0, (%rsp)
leaq 0x28(%rsp), %rdi
callq 0x170fa
movl %r15d, 0x24(%rsp)
testb $0x1, %r15b
je 0x50c76
movq 0x38(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x28(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x1786a
leaq 0x110(%rsp), %r13
movq 0x48(%rsp), %rax
subl %ebx, %eax
movq %rax, 0x48(%rsp)
movq 0x40(%rsp), %rax
subl %r14d, %eax
movq %rax, 0x40(%rsp)
movl 0x28(%rsp), %esi
movl 0x2c(%rsp), %edx
movq %r12, %rdi
callq 0x321b7
movq 0x20(%r12), %rax
movslq (%r12), %rcx
imulq $0x4a0, %rcx, %rcx      # imm = 0x4A0
movl -0x448(%rax,%rcx), %r15d
movq -0x240(%rax,%rcx), %rbx
movq -0x238(%rax,%rcx), %rbp
movl 0x28(%rsp), %esi
movl 0x2c(%rsp), %edx
movl 0x30(%rsp), %ecx
leaq 0x78(%rsp), %r14
movq %r14, %rdi
callq 0x15d43
movl 0x94(%r12), %esi
movl 0x80(%r12), %ecx
movl 0x90(%r12), %edx
movl 0x28(%rsp), %r8d
movl 0x2c(%rsp), %r9d
movl 0x30(%rsp), %eax
movl %eax, (%rsp)
leaq 0xa778(%rip), %rdi      # 0x5b490
xorl %eax, %eax
callq 0x3160
movl 0x80(%r12), %edi
movq 0x38(%rsp), %rsi
movq 0xd8(%r12), %rcx
movl $0x1, %edx
movl $0x1, %r8d
callq 0x29fe0
movq 0x10(%r14), %rax
movq %rax, 0xe8(%r12)
movq %r12, %rdi
callq 0x319d7
movl %r15d, %edi
movq %rbp, %rsi
movl $0x1, %edx
movq %rbx, %rcx
movl $0x1, %r8d
callq 0x29fe0
movq %rbp, %rdi
movq %rbx, %rsi
movl %r15d, %edx
movss 0x54(%rsp), %xmm0
callq 0x50a6b
movq %r12, %rdi
callq 0x31c65
testb $0x1, 0x24(%rsp)
je 0x50daf
movq 0x88(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x78(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x1786a
movl (%r13), %esi
movl 0x4(%r13), %edx
movq 0x88(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x78(%rsp), %xmm0
movups %xmm0, (%rsp)
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
callq 0x170fa
movl $0x8, %edx
movl $0x8, %esi
subl 0x48(%rsp), %esi
subl 0x40(%rsp), %edx
movl (%r13), %ecx
movl 0x4(%r13), %r8d
movq 0x10(%rbx), %rax
movq %rax, 0x10(%rsp)
movups (%rbx), %xmm0
movups %xmm0, (%rsp)
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
callq 0x18a89
movl (%rbx), %esi
movl 0x4(%rbx), %edx
movl 0x8(%rbx), %ecx
leaq 0xc0(%rsp), %r14
movq %r14, %rdi
callq 0x15d43
movq 0x10(%r14), %rax
movq %rax, 0x10(%rsp)
movups (%r14), %xmm0
movups %xmm0, (%rsp)
movss 0x441f(%rip), %xmm0     # 0x55264
callq 0x1918e
movl 0x4(%r13), %edi
imull (%r13), %edi
imull 0x8(%r13), %edi
movq 0x10(%r13), %rsi
movq 0x10(%r14), %rcx
movss 0x442c(%rip), %xmm0     # 0x55294
movl $0x1, %edx
movl $0x1, %r8d
callq 0x29d31
movl 0x4(%r13), %edi
imull (%r13), %edi
imull 0x8(%r13), %edi
movq 0x10(%r14), %rsi
movq 0x10(%rbx), %rcx
movss 0x4402(%rip), %xmm0     # 0x55298
movl $0x1, %edx
movl $0x1, %r8d
callq 0x29d31
cmpl $0x0, 0x58(%rsp)
je 0x50ec5
movq 0x70(%rsp), %rdi
movl 0x64(%rsp), %esi
imull 0x60(%rsp), %esi
imull 0x68(%rsp), %esi
callq 0xf86c
movl 0x4(%r13), %edi
imull (%r13), %edi
imull 0x8(%r13), %edi
movq 0x70(%rsp), %rsi
movq 0x10(%r13), %rcx
movss 0x5c(%rsp), %xmm0
movl $0x1, %edx
movl $0x1, %r8d
callq 0x29d31
movq 0x10(%r13), %rax
movq %rax, 0x10(%rsp)
movups (%r13), %xmm0
movups %xmm0, (%rsp)
callq 0x18031
movq 0xb8(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0xa8(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x1657b
movq 0x38(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x28(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x1657b
movq 0x88(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x78(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x1657b
movq 0xa0(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x90(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x1657b
movq 0x70(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x60(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x1657b
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq