// File: /workspace/github_c_makefile_star2_O2/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, 0x48(%rsp)
movss %xmm2, 0x44(%rsp)
movss %xmm1, 0x4c(%rsp)
movss %xmm0, 0x24(%rsp)
movq %rdi, %r12
incl %esi
movl %esi, (%rdi)
callq 0x34e0
pushq $0x10
popq %rbp
cltd
idivl %ebp
movq %rdx, 0x88(%rsp)
leal -0x8(%rdx), %ebx
callq 0x34e0
cltd
idivl %ebp
movq %rdx, 0x80(%rsp)
leal -0x8(%rdx), %r14d
callq 0x34e0
movl %eax, %ebp
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), %r15
movq %r15, %rdi
movl %ebx, %esi
movl %r14d, %edx
callq 0xdba7
cvtsi2ssl 0x110(%rsp), %xmm0
movss 0x24(%rsp), %xmm1
mulss %xmm1, %xmm0
cvttss2si %xmm0, %esi
cvtsi2ssl 0x114(%rsp), %xmm0
mulss %xmm1, %xmm0
cvttss2si %xmm0, %edx
movq 0x10(%r15), %rax
movq %rax, 0x10(%rsp)
movups (%r15), %xmm0
movups %xmm0, (%rsp)
leaq 0x28(%rsp), %rdi
callq 0xc3ee
movl %ebp, 0x24(%rsp)
testb $0x1, %bpl
je 0x4814f
movq 0x38(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x28(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0xcb93
leaq 0x110(%rsp), %r15
movl 0x28(%rsp), %esi
movl 0x2c(%rsp), %edx
movq %r12, %rdi
callq 0x22f25
movq 0x20(%r12), %rax
movslq (%r12), %rcx
imulq $0x4a0, %rcx, %rcx      # imm = 0x4A0
movl -0x448(%rax,%rcx), %r13d
movq -0x240(%rax,%rcx), %rbx
movq -0x238(%rax,%rcx), %r14
movl 0x28(%rsp), %esi
movl 0x2c(%rsp), %edx
movl 0x30(%rsp), %ecx
leaq 0x68(%rsp), %rdi
callq 0xb45a
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 0xa17b(%rip), %rdi      # 0x52350
xorl %eax, %eax
callq 0x3160
movl 0x80(%r12), %edi
movq 0x38(%rsp), %rsi
movq 0xd8(%r12), %rcx
pushq $0x1
popq %rbp
movl %ebp, %edx
movl %ebp, %r8d
callq 0x1b5a3
leaq 0x68(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0xe8(%r12)
movq %r12, %rdi
callq 0x2294b
movl %r13d, %edi
movq %r14, %rsi
movl %ebp, %edx
movq %rbx, %rcx
movl %ebp, %r8d
callq 0x1b5a3
movq %r14, %rdi
movq %rbx, %rsi
movl %r13d, %edx
movss 0x44(%rsp), %xmm0
callq 0x47f75
movq %r12, %rdi
callq 0x22bd7
testb $0x1, 0x24(%rsp)
je 0x48265
movq 0x78(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x68(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0xcb93
movl (%r15), %esi
movl 0x4(%r15), %edx
movq 0x78(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x68(%rsp), %xmm0
movups %xmm0, (%rsp)
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
callq 0xc3ee
pushq $0x8
popq %rdx
movl %edx, %esi
subl 0x88(%rsp), %esi
subl 0x80(%rsp), %edx
movl (%r15), %ecx
movl 0x4(%r15), %r8d
movq 0x10(%rbx), %rax
movq %rax, 0x10(%rsp)
movups (%rbx), %xmm0
movups %xmm0, (%rsp)
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
callq 0xdba7
movl (%rbx), %esi
movl 0x4(%rbx), %edx
movl 0x8(%rbx), %ecx
leaq 0xc0(%rsp), %r14
movq %r14, %rdi
callq 0xb45a
movq 0x10(%r14), %rax
movq %rax, 0x10(%rsp)
movups (%r14), %xmm0
movups %xmm0, (%rsp)
movss 0x3f3d(%rip), %xmm0     # 0x4c234
callq 0xe178
movl 0x4(%r15), %edi
imull (%r15), %edi
imull 0x8(%r15), %edi
movq 0x10(%r15), %rsi
movq 0x10(%r14), %rcx
movss 0x3f4b(%rip), %xmm0     # 0x4c264
pushq $0x1
popq %rbp
movl %ebp, %edx
movl %ebp, %r8d
callq 0x1b3ed
movl 0x4(%r15), %edi
imull (%r15), %edi
imull 0x8(%r15), %edi
movq 0x10(%r14), %rsi
movq 0x10(%rbx), %rcx
movss 0x3f25(%rip), %xmm0     # 0x4c268
movl %ebp, %edx
movl %ebp, %r8d
callq 0x1b3ed
cmpl $0x0, 0x48(%rsp)
je 0x4836c
movq 0x60(%rsp), %rdi
movl 0x54(%rsp), %esi
imull 0x50(%rsp), %esi
imull 0x58(%rsp), %esi
callq 0x4a9f
movl 0x4(%r15), %edi
imull (%r15), %edi
imull 0x8(%r15), %edi
movq 0x60(%rsp), %rsi
movq 0x10(%r15), %rcx
pushq $0x1
popq %rdx
movss 0x4c(%rsp), %xmm0
movl %edx, %r8d
callq 0x1b3ed
movq 0x10(%r15), %rax
movq %rax, 0x10(%rsp)
movups (%r15), %xmm0
movups %xmm0, (%rsp)
callq 0xd231
movq 0xb8(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0xa8(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0xba88
movq 0x38(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x28(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0xba88
movq 0x78(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x68(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0xba88
movq 0xa0(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x90(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0xba88
movq 0x60(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x50(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0xba88
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq