// File: /workspace/github_c_makefile_star2_O3/SimpleDoger[P]Darknet_CPU_Only/yolo_layer.c
// Function: forward_yolo_layer
void forward_yolo_layer(const layer l, network net)
{
    int i,j,b,t,n;
    memcpy(l.output, net.input, l.outputs*l.batch*sizeof(float));

    for (b = 0; b < l.batch; ++b){
        for(n = 0; n < l.n; ++n){
            int index = entry_index(l, b, n*l.w*l.h, 0);
            activate_array(l.output + index, 2*l.w*l.h, LOGISTIC);
            index = entry_index(l, b, n*l.w*l.h, 4);
            activate_array(l.output + index, (1+l.classes)*l.w*l.h, LOGISTIC);
        }
    }

    memset(l.delta, 0, l.outputs * l.batch * sizeof(float));
    if(!net.train) return;
    float avg_iou = 0;
    float recall = 0;
    float recall75 = 0;
    float avg_cat = 0;
    float avg_obj = 0;
    float avg_anyobj = 0;
    int count = 0;
    int class_count = 0;
    *(l.cost) = 0;
    for (b = 0; b < l.batch; ++b) {
        for (j = 0; j < l.h; ++j) {
            for (i = 0; i < l.w; ++i) {
                for (n = 0; n < l.n; ++n) {
                    int box_index = entry_index(l, b, n*l.w*l.h + j*l.w + i, 0);
                    box pred = get_yolo_box(l.output, l.biases, l.mask[n], box_index, i, j, l.w, l.h, net.w, net.h, l.w*l.h);
                    float best_iou = 0;
                    int best_t = 0;
                    for(t = 0; t < l.max_boxes; ++t){
                        box truth = float_to_box(net.truth + t*(4 + 1) + b*l.truths, 1);
                        if(!truth.x) break;
                        float iou = box_iou(pred, truth);
                        if (iou > best_iou) {
                            best_iou = iou;
                            best_t = t;
                        }
                    }
                    int obj_index = entry_index(l, b, n*l.w*l.h + j*l.w + i, 4);
                    avg_anyobj += l.output[obj_index];
                    l.delta[obj_index] = 0 - l.output[obj_index];
                    if (best_iou > l.ignore_thresh) {
                        l.delta[obj_index] = 0;
                    }
                    if (best_iou > l.truth_thresh) {
                        l.delta[obj_index] = 1 - l.output[obj_index];

                        int class = net.truth[best_t*(4 + 1) + b*l.truths + 4];
                        if (l.map) class = l.map[class];
                        int class_index = entry_index(l, b, n*l.w*l.h + j*l.w + i, 4 + 1);
                        delta_yolo_class(l.output, l.delta, class_index, class, l.classes, l.w*l.h, 0);
                        box truth = float_to_box(net.truth + best_t*(4 + 1) + b*l.truths, 1);
                        delta_yolo_box(truth, l.output, l.biases, l.mask[n], box_index, i, j, l.w, l.h, net.w, net.h, l.delta, (2-truth.w*truth.h), l.w*l.h);
                    }
                }
            }
        }
        for(t = 0; t < l.max_boxes; ++t){
            box truth = float_to_box(net.truth + t*(4 + 1) + b*l.truths, 1);

            if(!truth.x) break;
            float best_iou = 0;
            int best_n = 0;
            i = (truth.x * l.w);
            j = (truth.y * l.h);
            box truth_shift = truth;
            truth_shift.x = truth_shift.y = 0;
            for(n = 0; n < l.total; ++n){
                box pred = {0};
                pred.w = l.biases[2*n]/net.w;
                pred.h = l.biases[2*n+1]/net.h;
                float iou = box_iou(pred, truth_shift);
                if (iou > best_iou){
                    best_iou = iou;
                    best_n = n;
                }
            }

            int mask_n = int_index(l.mask, best_n, l.n);
            if(mask_n >= 0){
                int box_index = entry_index(l, b, mask_n*l.w*l.h + j*l.w + i, 0);
                float iou = delta_yolo_box(truth, l.output, l.biases, best_n, box_index, i, j, l.w, l.h, net.w, net.h, l.delta, (2-truth.w*truth.h), l.w*l.h);

                int obj_index = entry_index(l, b, mask_n*l.w*l.h + j*l.w + i, 4);
                avg_obj += l.output[obj_index];
                l.delta[obj_index] = 1 - l.output[obj_index];

                int class = net.truth[t*(4 + 1) + b*l.truths + 4];
                if (l.map) class = l.map[class];
                int class_index = entry_index(l, b, mask_n*l.w*l.h + j*l.w + i, 4 + 1);
                delta_yolo_class(l.output, l.delta, class_index, class, l.classes, l.w*l.h, &avg_cat);

                ++count;
                ++class_count;
                if(iou > .5) recall += 1;
                if(iou > .75) recall75 += 1;
                avg_iou += iou;
            }
        }
    }
    *(l.cost) = pow(mag_array(l.delta, l.outputs * l.batch), 2);
    printf("Region %d Avg IOU: %f, Class: %f, Obj: %f, No Obj: %f, .5R: %f, .75R: %f,  count: %d\n", net.index, avg_iou/count, avg_cat/class_count, avg_obj/count, avg_anyobj/(l.w*l.h*l.n*l.batch), recall/count, recall75/count, count);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
leaq 0x580(%rsp), %rax
leaq 0xe0(%rsp), %r13
movq 0x268(%r13), %rdi
movq 0xd8(%rax), %rsi
movslq 0x58(%r13), %rax
movslq 0x48(%r13), %rdx
imulq %rax, %rdx
shlq $0x2, %rdx
callq 0x32b0
movl 0x48(%r13), %eax
testl %eax, %eax
jle 0x410d0
movl 0x84(%r13), %ecx
xorl %ebx, %ebx
testl %ecx, %ecx
jle 0x410c6
xorl %ebp, %ebp
movl 0x6c(%r13), %esi
movl 0xfc(%r13), %edi
imull 0x70(%r13), %esi
movl %esi, %ecx
imull %ebp, %ecx
movl %ecx, %eax
cltd
idivl %esi
movl 0x58(%r13), %eax
imull %ebx, %eax
addl $0x5, %edi
subl %edx, %ecx
imull %edi, %ecx
addl %edx, %eax
addl %ecx, %eax
movslq %eax, %rdi
shlq $0x2, %rdi
addq 0x268(%r13), %rdi
addl %esi, %esi
xorl %edx, %edx
callq 0x28c99
movl 0x6c(%r13), %ecx
movl 0xfc(%r13), %esi
imull 0x70(%r13), %ecx
movl %ecx, %eax
imull %ebp, %eax
cltd
idivl %ecx
movl 0x58(%r13), %edi
imull %ebx, %edi
leal 0x5(%rsi), %r8d
imull %eax, %r8d
addl $0x4, %r8d
imull %ecx, %r8d
addl %edx, %edi
addl %r8d, %edi
movslq %edi, %rdi
shlq $0x2, %rdi
addq 0x268(%r13), %rdi
incl %esi
imull %ecx, %esi
xorl %edx, %edx
callq 0x28c99
incl %ebp
movl 0x84(%r13), %ecx
cmpl %ecx, %ebp
jl 0x4101f
movl 0x48(%r13), %eax
incl %ebx
cmpl %eax, %ebx
jl 0x41015
movq 0x260(%r13), %rdi
movslq 0x58(%r13), %rcx
movslq %eax, %rdx
imulq %rcx, %rdx
shlq $0x2, %rdx
xorl %ebx, %ebx
xorl %esi, %esi
callq 0x31c0
leaq 0x580(%rsp), %rax
cmpl $0x0, 0xf8(%rax)
je 0x41904
movl %ebx, 0x34(%rsp)
movq 0x1d8(%r13), %rax
movl %ebx, (%rax)
movl 0x48(%r13), %esi
testl %esi, %esi
jle 0x4181c
leaq 0x580(%rsp), %rax
movq 0x90(%rax), %r12
movq 0xe0(%rax), %rax
movq %rax, 0x28(%rsp)
movq %r12, %xmm0
cvtdq2ps %xmm0, %xmm1
shufps $0xe1, %xmm1, %xmm1     # xmm1 = xmm1[1,0,2,3]
movaps %xmm1, 0x90(%rsp)
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, %eax
movq %rax, 0x50(%rsp)
pxor %xmm0, %xmm0
xorl %r14d, %r14d
xorps %xmm2, %xmm2
xorps %xmm1, %xmm1
movss %xmm1, 0x20(%rsp)
movl $0x0, 0x24(%rsp)
movl $0x0, 0x30(%rsp)
movq %r12, 0x78(%rsp)
movl 0x6c(%r13), %ecx
movslq %r14d, %rax
movq %rax, 0x58(%rsp)
testl %ecx, %ecx
movq %r14, 0x38(%rsp)
jle 0x414d8
movl 0x70(%r13), %eax
movl $0x0, 0xc(%rsp)
movdqa %xmm0, 0x40(%rsp)
movss %xmm2, 0x8(%rsp)
testl %eax, %eax
jle 0x414c6
movl 0x84(%r13), %ecx
movl $0x0, 0x10(%rsp)
testl %ecx, %ecx
jle 0x414b0
xorl %ebx, %ebx
movss %xmm1, 0x14(%rsp)
movl 0x6c(%r13), %r11d
movl 0x70(%r13), %r10d
movl %r11d, %ecx
imull %ebx, %ecx
movl 0xc(%rsp), %r9d
addl %r9d, %ecx
imull %r10d, %ecx
movl 0x10(%rsp), %r8d
addl %r8d, %ecx
movq %r14, %rsi
movl %r11d, %r14d
imull %r10d, %r14d
movl %ecx, %eax
cltd
idivl %r14d
movl 0x58(%r13), %r15d
imull %esi, %r15d
movl 0xfc(%r13), %eax
addl $0x5, %eax
subl %edx, %ecx
imull %eax, %ecx
addl %edx, %r15d
addl %ecx, %r15d
movq 0x268(%r13), %rdi
movq 0x230(%r13), %rsi
movq 0x128(%r13), %rax
movl (%rax,%rbx,4), %edx
subq $0x8, %rsp
movl %r15d, %ecx
pushq %r14
pushq %r12
pushq 0x68(%rsp)
pushq %r11
pushq %r10
callq 0x419bb
addq $0x30, %rsp
movdqa %xmm0, 0x60(%rsp)
movaps %xmm1, 0x80(%rsp)
cmpl $0x0, 0x88(%r13)
jle 0x412ec
xorl %r14d, %r14d
xorps %xmm1, %xmm1
movq 0x28(%rsp), %r12
xorl %ebp, %ebp
movss %xmm1, 0x18(%rsp)
movslq 0x68(%r13), %rax
imulq 0x58(%rsp), %rax
leaq (%r12,%rax,4), %rdi
movl $0x1, %esi
callq 0x38d7b
ucomiss 0x13df9(%rip), %xmm0    # 0x550a0
jne 0x412ab
jnp 0x412f4
movaps %xmm0, %xmm2
movaps %xmm1, %xmm3
movaps 0x60(%rsp), %xmm0
movaps 0x80(%rsp), %xmm1
callq 0x389b3
movss 0x18(%rsp), %xmm1
ucomiss %xmm1, %xmm0
cmoval %r14d, %ebp
maxss %xmm1, %xmm0
incq %r14
movslq 0x88(%r13), %rax
addq $0x14, %r12
movaps %xmm0, %xmm1
cmpq %rax, %r14
jl 0x41282
jmp 0x412fa
pxor %xmm0, %xmm0
xorl %ebp, %ebp
jmp 0x41303
movss 0x18(%rsp), %xmm0
leal (%rbp,%rbp,4), %ebp
movq 0x78(%rsp), %r12
movl 0x6c(%r13), %r9d
movl 0x70(%r13), %ecx
movl %r9d, %eax
imull %ebx, %eax
addl 0xc(%rsp), %eax
imull %ecx, %eax
addl 0x10(%rsp), %eax
imull %ecx, %r9d
cltd
idivl %r9d
movl %edx, %ecx
movl 0x58(%r13), %r10d
movq 0x38(%rsp), %r14
imull %r14d, %r10d
movl 0xfc(%r13), %r8d
leal 0x5(%r8), %edx
imull %eax, %edx
leal 0x4(%rdx), %eax
imull %r9d, %eax
addl %ecx, %r10d
addl %r10d, %eax
movq 0x268(%r13), %rdi
cltq
movss (%rdi,%rax,4), %xmm3
xorps %xmm1, %xmm1
subss %xmm3, %xmm1
movq 0x260(%r13), %rsi
movss %xmm1, (%rsi,%rax,4)
ucomiss 0x15c(%r13), %xmm0
movss 0x8(%rsp), %xmm2
jbe 0x41386
movl $0x0, (%rsi,%rax,4)
ucomiss 0x160(%r13), %xmm0
movss 0x14(%rsp), %xmm1
jbe 0x41490
movss 0x13c66(%rip), %xmm0    # 0x55008
subss (%rdi,%rax,4), %xmm0
movss %xmm0, (%rsi,%rax,4)
movl 0x68(%r13), %eax
imull %r14d, %eax
addl %ebp, %eax
cltq
movq 0x28(%rsp), %rcx
cvttss2si 0x10(%rcx,%rax,4), %ecx
movq 0x1b8(%r13), %rax
testq %rax, %rax
movss %xmm3, 0x18(%rsp)
je 0x413db
movslq %ecx, %rcx
movl (%rax,%rcx,4), %ecx
addl $0x5, %edx
imull %r9d, %edx
addl %r10d, %edx
subq $0x8, %rsp
pushq $0x0
callq 0x41c88
addq $0x10, %rsp
movslq %ebp, %rax
movq 0x28(%rsp), %rcx
leaq (%rcx,%rax,4), %rax
movslq 0x68(%r13), %rcx
imulq 0x58(%rsp), %rcx
leaq (%rax,%rcx,4), %rdi
movl $0x1, %esi
callq 0x38d7b
movq 0x268(%r13), %rdi
movq 0x230(%r13), %rsi
movq 0x128(%r13), %rax
movl (%rax,%rbx,4), %edx
movl 0x6c(%r13), %r10d
movl 0x70(%r13), %eax
movaps %xmm1, %xmm3
shufps $0x55, %xmm1, %xmm3     # xmm3 = xmm3[1,1],xmm1[1,1]
mulss %xmm1, %xmm3
movss 0x13e41(%rip), %xmm2    # 0x5528c
subss %xmm3, %xmm2
movl %r10d, %r11d
imull %eax, %r11d
movl %r15d, %ecx
movl 0x10(%rsp), %r8d
movl 0xc(%rsp), %r9d
pushq %r11
pushq 0x260(%r13)
pushq %r12
pushq 0x68(%rsp)
pushq %r10
pushq %rax
callq 0x41abd
addq $0x30, %rsp
movss 0x14(%rsp), %xmm1
movss 0x8(%rsp), %xmm2
movss 0x18(%rsp), %xmm3
addss %xmm3, %xmm1
incq %rbx
movslq 0x84(%r13), %rcx
cmpq %rcx, %rbx
jl 0x411d4
movl 0x70(%r13), %eax
movaps 0x40(%rsp), %xmm0
movl 0x10(%rsp), %edx
incl %edx
movl %edx, 0x10(%rsp)
cmpl %eax, %edx
jl 0x411ca
movl 0x6c(%r13), %ecx
movl 0xc(%rsp), %edx
incl %edx
movl %edx, 0xc(%rsp)
cmpl %ecx, %edx
jl 0x411b3
movss %xmm1, 0x14(%rsp)
cmpl $0x0, 0x88(%r13)
jle 0x417ea
xorl %r15d, %r15d
movss %xmm2, 0x8(%rsp)
movaps %xmm0, 0x40(%rsp)
leaq (%r15,%r15,4), %rbx
movq 0x28(%rsp), %rax
leaq (%rax,%rbx,4), %rax
movslq 0x68(%r13), %rcx
imulq 0x58(%rsp), %rcx
leaq (%rax,%rcx,4), %rdi
movl $0x1, %esi
callq 0x38d7b
ucomiss 0x13b7a(%rip), %xmm0    # 0x550a0
jne 0x4152e
jnp 0x417df
xorps %xmm2, %xmm2
cvtsi2ssl 0x70(%r13), %xmm2
movss %xmm2, 0xc(%rsp)
xorps %xmm2, %xmm2
cvtsi2ssl 0x6c(%r13), %xmm2
movss %xmm2, 0x10(%rsp)
cmpl $0x0, 0x130(%r13)
movaps %xmm0, 0x80(%rsp)
movaps %xmm1, 0x60(%rsp)
jle 0x415b8
xorps %xmm0, %xmm0
xorl %r14d, %r14d
xorl %ebp, %ebp
movss %xmm0, 0x18(%rsp)
movq 0x230(%r13), %rax
movsd (%rax,%r14,8), %xmm1
divps 0x90(%rsp), %xmm1
xorps %xmm0, %xmm0
xorps %xmm2, %xmm2
movaps 0x60(%rsp), %xmm3
callq 0x389b3
movss 0x18(%rsp), %xmm1
ucomiss %xmm1, %xmm0
maxss %xmm1, %xmm0
cmoval %r14d, %ebp
incq %r14
movslq 0x130(%r13), %rax
cmpq %rax, %r14
jl 0x4156b
jmp 0x415ba
xorl %ebp, %ebp
movq 0x128(%r13), %rdi
movl 0x84(%r13), %edx
movl %ebp, %esi
callq 0xfac0
testl %eax, %eax
js 0x417ba
movl %eax, %r14d
movq %r15, 0x18(%rsp)
movaps 0x80(%rsp), %xmm0
movss 0xc(%rsp), %xmm1
mulss %xmm0, %xmm1
cvttss2si %xmm1, %eax
movaps %xmm0, %xmm1
shufps $0x55, %xmm0, %xmm1     # xmm1 = xmm1[1,1],xmm0[1,1]
mulss 0x10(%rsp), %xmm1
cvttss2si %xmm1, %r15d
movl 0x6c(%r13), %r11d
movl 0x70(%r13), %r10d
movl %r11d, %esi
imull %r14d, %esi
addl %r15d, %esi
imull %r10d, %esi
addl %eax, %esi
movl %eax, %r8d
movl %r11d, %r12d
imull %r10d, %r12d
movl %esi, %eax
cltd
idivl %r12d
movl 0x58(%r13), %ecx
imull 0x38(%rsp), %ecx
movl 0xfc(%r13), %eax
addl $0x5, %eax
subl %edx, %esi
imull %eax, %esi
addl %edx, %ecx
addl %esi, %ecx
movq 0x268(%r13), %rdi
movq 0x230(%r13), %rsi
movaps 0x60(%rsp), %xmm1
movaps %xmm1, %xmm3
shufps $0x55, %xmm1, %xmm3     # xmm3 = xmm3[1,1],xmm1[1,1]
mulss %xmm1, %xmm3
movss 0x13c1b(%rip), %xmm2    # 0x5528c
subss %xmm3, %xmm2
movl %ebp, %edx
movl %r8d, %ebp
movl %r15d, %r9d
pushq %r12
movq 0x80(%rsp), %r12
pushq 0x260(%r13)
pushq %r12
pushq 0x68(%rsp)
pushq %r11
pushq %r10
callq 0x41abd
addq $0x30, %rsp
movl 0x6c(%r13), %r9d
movl 0x70(%r13), %eax
imull %r9d, %r14d
addl %r15d, %r14d
imull %eax, %r14d
addl %ebp, %r14d
imull %eax, %r9d
movl %r14d, %eax
movq 0x38(%rsp), %r14
cltd
idivl %r9d
movl %edx, %ecx
movl 0x58(%r13), %r10d
imull %r14d, %r10d
movl 0xfc(%r13), %r8d
leal 0x5(%r8), %edx
imull %eax, %edx
leal 0x4(%rdx), %eax
imull %r9d, %eax
addl %ecx, %r10d
addl %r10d, %eax
movq 0x268(%r13), %rdi
cltq
movss (%rdi,%rax,4), %xmm1
movss 0x13906(%rip), %xmm2    # 0x55008
subss %xmm1, %xmm2
movq 0x260(%r13), %rsi
movss %xmm2, (%rsi,%rax,4)
movl 0x68(%r13), %eax
imull %r14d, %eax
addl %eax, %ebx
movslq %ebx, %rax
movq 0x28(%rsp), %rcx
cvttss2si 0x10(%rcx,%rax,4), %ecx
movq 0x1b8(%r13), %rax
testq %rax, %rax
movaps %xmm0, 0x60(%rsp)
je 0x41741
movslq %ecx, %rcx
movl (%rax,%rcx,4), %ecx
movss 0x20(%rsp), %xmm0
movq 0x18(%rsp), %r15
addss %xmm1, %xmm0
movss %xmm0, 0x20(%rsp)
addl $0x5, %edx
imull %r9d, %edx
addl %r10d, %edx
subq $0x8, %rsp
leaq 0x3c(%rsp), %rax
pushq %rax
callq 0x41c88
addq $0x10, %rsp
incl 0x24(%rsp)
incl 0x30(%rsp)
movaps 0x60(%rsp), %xmm3
movaps %xmm3, %xmm0
shufps $0x0, %xmm3, %xmm0      # xmm0 = xmm0[0,0],xmm3[0,0]
movsd 0x18781(%rip), %xmm1    # 0x59f10
cmpltps %xmm0, %xmm1
movaps %xmm1, %xmm0
movaps 0x40(%rsp), %xmm2
andnps %xmm2, %xmm0
addps 0x13b4b(%rip), %xmm2    # 0x552f0
andps %xmm1, %xmm2
orps %xmm0, %xmm2
movaps %xmm2, %xmm0
movss 0x8(%rsp), %xmm2
addss %xmm3, %xmm2
jmp 0x417ca
movaps 0x40(%rsp), %xmm0
movss 0x8(%rsp), %xmm2
movq 0x38(%rsp), %r14
incq %r15
movslq 0x88(%r13), %rax
cmpq %rax, %r15
jl 0x414ef
jmp 0x417ea
movaps 0x40(%rsp), %xmm0
movss 0x8(%rsp), %xmm2
incl %r14d
movl 0x48(%r13), %esi
cmpl %esi, %r14d
movss 0x14(%rsp), %xmm1
jl 0x41182
xorps %xmm3, %xmm3
cvtsi2ssl 0x30(%rsp), %xmm3
movss %xmm3, 0x18(%rsp)
movaps %xmm0, 0x40(%rsp)
movss %xmm2, 0x8(%rsp)
jmp 0x41847
movl $0x0, 0x24(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0x18(%rsp)
movss %xmm0, 0x14(%rsp)
movss %xmm0, 0x20(%rsp)
movss %xmm0, 0x8(%rsp)
movq 0x260(%r13), %rdi
imull 0x58(%r13), %esi
callq 0xf92d
mulss %xmm0, %xmm0
movq 0x1d8(%r13), %rax
movss %xmm0, (%rax)
leaq 0x580(%rsp), %rax
movl 0xfc(%rax), %esi
movl 0x24(%rsp), %edx
cvtsi2ss %edx, %xmm5
movss 0x8(%rsp), %xmm0
divss %xmm5, %xmm0
cvtss2sd %xmm0, %xmm0
movss 0x34(%rsp), %xmm1
divss 0x18(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
movss 0x20(%rsp), %xmm2
divss %xmm5, %xmm2
movl 0x6c(%r13), %eax
imull 0x70(%r13), %eax
imull 0x84(%r13), %eax
imull 0x48(%r13), %eax
cvtss2sd %xmm2, %xmm2
xorps %xmm3, %xmm3
cvtsi2ss %eax, %xmm3
movss 0x14(%rsp), %xmm4
divss %xmm3, %xmm4
xorps %xmm3, %xmm3
cvtss2sd %xmm4, %xmm3
movaps 0x40(%rsp), %xmm6
movaps %xmm6, %xmm4
shufps $0x55, %xmm6, %xmm4     # xmm4 = xmm4[1,1],xmm6[1,1]
divss %xmm5, %xmm4
cvtss2sd %xmm4, %xmm4
divss %xmm5, %xmm6
xorps %xmm5, %xmm5
cvtss2sd %xmm6, %xmm5
leaq 0x18629(%rip), %rdi     # 0x59f26
movb $0x6, %al
callq 0x3160
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq