// File: /workspace/github_cpp_makefile_star2_O3/hey-yahei[P]ConvNet/src/Pooling.cpp
// Function: Pooling::Run(float*, bool)
float* Pooling::Run(float* in_data, bool keep_in) {
	int start_h, start_w;
	int idx_ih, idx_iw;
	float dat, result;

	out_data = (float*)malloc(sizeof(float) * in_shape[0] * out_chw);

	// Do convolution
	for (int idx_bs = 0; idx_bs < in_shape[0]; idx_bs++) {
		for (int idx_oc = 0; idx_oc < out_shape[1]; idx_oc++) {
			for (int idx_oh = 0; idx_oh < out_shape[2]; idx_oh++) {
				for (int idx_ow = 0; idx_ow < out_shape[3]; idx_ow++) {
					start_h = idx_oh * stride - pad;
					start_w = idx_ow * stride - pad;
					result = 0.;
					for (int idx_kh = 0; idx_kh < kernel; idx_kh++) {
						for (int idx_kw = 0; idx_kw < kernel; idx_kw++) {
							idx_ih = start_h + idx_kh;
							idx_iw = start_w + idx_kw;
							if ((idx_ih < 0) || (idx_iw < 0) || (idx_ih >= in_shape[2]) || (idx_iw >= in_shape[3]))
								dat = 0.;
							else
								dat = get_in_data(in_data, idx_bs, idx_oc, idx_ih, idx_iw);
							if (!strcmp(type, "max")) {
								if (result < dat)
									result = dat;
							} else if(!strcmp(type, "avg")) {
								result += dat / kernel / kernel;
							} else {
								cerr << "Pooling: unknown type" << endl;
								exit(1);
							}
						}
					}
					set_out_data(idx_bs, idx_oc, idx_oh, idx_ow, result);
				}
			}
		}
	}

	if (!keep_in)
		free(in_data);

	return out_data;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movl %edx, -0x6c(%rbp)
movq %rsi, -0xb0(%rbp)
movq %rdi, %rbx
movslq 0x10(%rdi), %r14
movslq 0x3c(%rdi), %rcx
movq %r14, %rdi
movq %rcx, -0xd8(%rbp)
imulq %rcx, %rdi
shlq $0x2, %rdi
callq 0x2240
movq %rax, -0x60(%rbp)
movq %rax, 0x48(%rbx)
movq %r14, -0xd0(%rbp)
testq %r14, %r14
jle 0x444c
movl 0x24(%rbx), %eax
movl 0x28(%rbx), %ecx
movl %ecx, -0x34(%rbp)
movl 0x2c(%rbx), %ecx
movq %rcx, -0xa8(%rbp)
movl (%rbx), %ecx
movl 0x4(%rbx), %edx
movl 0x8(%rbx), %esi
movl 0x18(%rbx), %edi
movl %edi, -0x90(%rbp)
movl 0x1c(%rbx), %edi
movl %edi, -0x8c(%rbp)
movq %rcx, -0x68(%rbp)
cvtsi2ss %ecx, %xmm0
movss %xmm0, -0x88(%rbp)
movl 0x30(%rbx), %ecx
movq %rcx, -0xc0(%rbp)
movl 0x38(%rbx), %ecx
movl %esi, %edi
negl %edi
movq %rdi, -0xe8(%rbp)
movq %rdx, -0xa0(%rbp)
imull %ecx, %edx
movq %rdx, -0xf0(%rbp)
movq %rcx, -0x110(%rbp)
notl %ecx
movl %esi, -0x7c(%rbp)
imull %esi, %ecx
movq %rcx, -0x48(%rbp)
movl 0x34(%rbx), %ecx
movq %rcx, -0xe0(%rbp)
movq 0x50(%rbx), %rcx
movq %rcx, -0xb8(%rbp)
movl 0x40(%rbx), %ecx
movl %ecx, -0x70(%rbp)
movl 0x44(%rbx), %ecx
movl %ecx, -0x78(%rbp)
movq $0x0, -0x40(%rbp)
movq %rbx, -0xc8(%rbp)
movl %eax, -0x74(%rbp)
testl %eax, %eax
jle 0x4420
movq -0x40(%rbp), %rcx
imull -0xd8(%rbp), %ecx
movq %rcx, -0xf8(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x48(%rbp), %rcx
movq %rcx, -0x50(%rbp)
cmpl $0x0, -0x34(%rbp)
jle 0x43fe
movl -0x70(%rbp), %eax
imull -0x2c(%rbp), %eax
movl %eax, -0x80(%rbp)
movl $0x0, -0x30(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpl $0x0, -0xa8(%rbp)
jle 0x43de
movq -0xa0(%rbp), %rax
movl %eax, %ecx
movl -0x30(%rbp), %eax
imull %eax, %ecx
subl -0x7c(%rbp), %ecx
movq %rcx, -0x118(%rbp)
movl -0x78(%rbp), %ecx
imull %eax, %ecx
movl %ecx, -0x84(%rbp)
movq -0x58(%rbp), %r14
movq -0xe8(%rbp), %r13
xorl %edx, %edx
movq %rdx, -0x100(%rbp)
cmpl $0x0, -0x68(%rbp)
movq %r14, -0x108(%rbp)
jle 0x4393
movq -0xb8(%rbp), %rdi
leaq 0x1d91(%rip), %rsi      # 0x604c
callq 0x2250
movl %eax, %r15d
xorps %xmm0, %xmm0
xorl %ecx, %ecx
movq -0x118(%rbp), %rax
movq %rcx, -0x120(%rbp)
leal (%rcx,%rax), %ebx
xorl %r12d, %r12d
xorps %xmm1, %xmm1
testl %ebx, %ebx
js 0x430d
movl %r13d, %eax
addl %r12d, %eax
js 0x430d
cmpl -0x90(%rbp), %ebx
jge 0x430d
cmpl -0x8c(%rbp), %eax
jge 0x430d
leal (%r14,%r12), %eax
cltq
movq -0xb0(%rbp), %rcx
movss (%rcx,%rax,4), %xmm1
testl %r15d, %r15d
je 0x4363
movss %xmm1, -0x94(%rbp)
movss %xmm0, -0x98(%rbp)
movq -0xb8(%rbp), %rdi
leaq 0x2017(%rip), %rsi      # 0x6347
callq 0x2250
testl %eax, %eax
jne 0x447c
movss -0x88(%rbp), %xmm0
movss -0x94(%rbp), %xmm1
divss %xmm0, %xmm1
divss %xmm0, %xmm1
movss -0x98(%rbp), %xmm0
addss %xmm1, %xmm0
jmp 0x436b
ucomiss %xmm0, %xmm1
jbe 0x436b
movaps %xmm1, %xmm0
incq %r12
cmpq %r12, -0x68(%rbp)
jne 0x42dc
movq -0x120(%rbp), %rcx
incl %ecx
addq -0x110(%rbp), %r14
cmpl -0x68(%rbp), %ecx
jne 0x42c8
jmp 0x4396
xorps %xmm0, %xmm0
movq -0xf8(%rbp), %rax
movq -0x100(%rbp), %rdx
addl %edx, %eax
addl -0x80(%rbp), %eax
addl -0x84(%rbp), %eax
cltq
movq -0x60(%rbp), %rcx
movss %xmm0, (%rcx,%rax,4)
incq %rdx
movq -0xa0(%rbp), %rax
addq %rax, %r13
movq -0x108(%rbp), %r14
addq %rax, %r14
cmpq -0xa8(%rbp), %rdx
jne 0x4295
movl -0x30(%rbp), %eax
incl %eax
movq -0x58(%rbp), %rcx
addq -0xf0(%rbp), %rcx
movq %rcx, -0x58(%rbp)
movl %eax, -0x30(%rbp)
cmpl -0x34(%rbp), %eax
jne 0x4256
movl -0x2c(%rbp), %ecx
incl %ecx
movq -0x50(%rbp), %rax
addq -0xe0(%rbp), %rax
movq %rax, -0x50(%rbp)
movl -0x74(%rbp), %eax
movl %ecx, -0x2c(%rbp)
cmpl %eax, %ecx
jne 0x4233
movq -0x40(%rbp), %rdx
incl %edx
movq -0x48(%rbp), %rcx
addq -0xc0(%rbp), %rcx
movq %rcx, -0x48(%rbp)
movq %rdx, -0x40(%rbp)
cmpl -0xd0(%rbp), %edx
movq -0xc8(%rbp), %rbx
jne 0x420a
cmpb $0x0, -0x6c(%rbp)
jne 0x4466
movq -0xb0(%rbp), %rdi
callq 0x2200
movq 0x48(%rbx), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4b6d(%rip), %rdi      # 0x8ff0
leaq 0x1ec1(%rip), %rsi      # 0x634b
callq 0x2180
movq %rax, %rdi
callq 0x20d0
movl $0x1, %edi
callq 0x2210
nopl (%rax)