// File: /workspace/github_cpp_makefile_star2_O1/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, -0x64(%rbp)
movq %rsi, -0xa8(%rbp)
movq %rdi, %rbx
movslq 0x10(%rdi), %r14
movslq 0x3c(%rdi), %rcx
movq %r14, %rdi
movq %rcx, -0xd0(%rbp)
imulq %rcx, %rdi
shlq $0x2, %rdi
callq 0x2240
movq %rax, -0xf8(%rbp)
movq %rax, 0x48(%rbx)
movq %r14, -0xc8(%rbp)
testq %r14, %r14
jle 0x4442
movl 0x24(%rbx), %eax
movl 0x28(%rbx), %ecx
movl %ecx, -0x34(%rbp)
movl 0x2c(%rbx), %ecx
movq %rcx, -0xa0(%rbp)
movl (%rbx), %ecx
movl 0x4(%rbx), %edx
movl 0x8(%rbx), %esi
movl 0x18(%rbx), %edi
movl %edi, -0x88(%rbp)
movl 0x1c(%rbx), %edi
movl %edi, -0x84(%rbp)
movq %rcx, -0x60(%rbp)
cvtsi2ss %ecx, %xmm0
movss %xmm0, -0x80(%rbp)
movl 0x30(%rbx), %ecx
movq %rcx, -0xb8(%rbp)
movl 0x38(%rbx), %ecx
movl %esi, %edi
negl %edi
movq %rdi, -0xe0(%rbp)
movq %rdx, -0x98(%rbp)
imull %ecx, %edx
movq %rdx, -0xe8(%rbp)
movq %rcx, -0x110(%rbp)
notl %ecx
movl %esi, -0x74(%rbp)
imull %esi, %ecx
movq %rcx, -0x48(%rbp)
movl 0x34(%rbx), %ecx
movq %rcx, -0xd8(%rbp)
movq 0x50(%rbx), %rcx
movq %rcx, -0xb0(%rbp)
movl 0x40(%rbx), %ecx
movl %ecx, -0x68(%rbp)
movl 0x44(%rbx), %ecx
movl %ecx, -0x70(%rbp)
movq $0x0, -0x40(%rbp)
movq %rbx, -0xc0(%rbp)
movl %eax, -0x6c(%rbp)
testl %eax, %eax
jle 0x4416
movq -0x40(%rbp), %rcx
imull -0xd0(%rbp), %ecx
movq %rcx, -0xf0(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x48(%rbp), %rcx
movq %rcx, -0x50(%rbp)
cmpl $0x0, -0x34(%rbp)
jle 0x43f4
movl -0x68(%rbp), %eax
imull -0x2c(%rbp), %eax
movl %eax, -0x78(%rbp)
movl $0x0, -0x30(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpl $0x0, -0xa0(%rbp)
jle 0x43d4
movq -0x98(%rbp), %rax
movl %eax, %ecx
movl -0x30(%rbp), %eax
imull %eax, %ecx
subl -0x74(%rbp), %ecx
movq %rcx, -0x118(%rbp)
movl -0x70(%rbp), %ecx
imull %eax, %ecx
movl %ecx, -0x7c(%rbp)
movq -0x58(%rbp), %r14
movq -0xe0(%rbp), %r13
xorl %edx, %edx
movq %rdx, -0x100(%rbp)
cmpl $0x0, -0x60(%rbp)
movq %r14, -0x108(%rbp)
jle 0x4389
movq -0xb0(%rbp), %rdi
leaq 0x1d98(%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 0x4306
movl %r13d, %eax
addl %r12d, %eax
js 0x4306
cmpl -0x88(%rbp), %ebx
jge 0x4306
cmpl -0x84(%rbp), %eax
jge 0x4306
leal (%r14,%r12), %eax
cltq
movq -0xa8(%rbp), %rcx
movss (%rcx,%rax,4), %xmm1
testl %r15d, %r15d
je 0x4359
movss %xmm1, -0x8c(%rbp)
movss %xmm0, -0x90(%rbp)
movq -0xb0(%rbp), %rdi
leaq 0x201e(%rip), %rsi      # 0x6347
callq 0x2250
testl %eax, %eax
jne 0x446a
movss -0x80(%rbp), %xmm0
movss -0x8c(%rbp), %xmm1
divss %xmm0, %xmm1
divss %xmm0, %xmm1
movss -0x90(%rbp), %xmm0
addss %xmm1, %xmm0
jmp 0x4361
ucomiss %xmm0, %xmm1
jbe 0x4361
movaps %xmm1, %xmm0
incq %r12
cmpq %r12, -0x60(%rbp)
jne 0x42d5
movq -0x120(%rbp), %rcx
incl %ecx
addq -0x110(%rbp), %r14
cmpl -0x60(%rbp), %ecx
jne 0x42c1
jmp 0x438c
xorps %xmm0, %xmm0
movq -0xf0(%rbp), %rax
movq -0x100(%rbp), %rdx
addl %edx, %eax
addl -0x78(%rbp), %eax
addl -0x7c(%rbp), %eax
cltq
movq -0xf8(%rbp), %rcx
movss %xmm0, (%rcx,%rax,4)
incq %rdx
movq -0x98(%rbp), %rax
addq %rax, %r13
movq -0x108(%rbp), %r14
addq %rax, %r14
cmpq -0xa0(%rbp), %rdx
jne 0x428e
movl -0x30(%rbp), %eax
incl %eax
movq -0x58(%rbp), %rcx
addq -0xe8(%rbp), %rcx
movq %rcx, -0x58(%rbp)
movl %eax, -0x30(%rbp)
cmpl -0x34(%rbp), %eax
jne 0x4252
movl -0x2c(%rbp), %ecx
incl %ecx
movq -0x50(%rbp), %rax
addq -0xd8(%rbp), %rax
movq %rax, -0x50(%rbp)
movl -0x6c(%rbp), %eax
movl %ecx, -0x2c(%rbp)
cmpl %eax, %ecx
jne 0x422f
movq -0x40(%rbp), %rdx
incl %edx
movq -0x48(%rbp), %rcx
addq -0xb8(%rbp), %rcx
movq %rcx, -0x48(%rbp)
movq %rdx, -0x40(%rbp)
cmpl -0xc8(%rbp), %edx
movq -0xc0(%rbp), %rbx
jne 0x4206
cmpb $0x0, -0x64(%rbp)
jne 0x4454
movq -0xa8(%rbp), %rdi
callq 0x2200
movq 0x48(%rbx), %rax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4b7f(%rip), %rdi      # 0x8ff0
leaq 0x1ed3(%rip), %rsi      # 0x634b
callq 0x2180
movq %rax, %rdi
callq 0x20d0
movl $0x1, %edi
callq 0x2210
nop