// File: /workspace/github_cpp_makefile_star2_O2/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 $0xd8, %rsp
movl %edx, -0x54(%rbp)
movq %rsi, -0xb0(%rbp)
movq %rdi, %r14
movslq 0x10(%rdi), %rbx
movslq 0x3c(%rdi), %rcx
movq %rbx, %rdi
movq %rcx, -0xc0(%rbp)
imulq %rcx, %rdi
shlq $0x2, %rdi
callq 0x2210
movq %rax, -0x50(%rbp)
movq %rax, 0x48(%r14)
movl 0x24(%r14), %eax
movl 0x28(%r14), %ecx
movl 0x2c(%r14), %edx
movl (%r14), %esi
cvtsi2ss %esi, %xmm0
movss %xmm0, -0x9c(%rbp)
xorl %edi, %edi
testl %esi, %esi
movl $0x0, %r8d
movq %rsi, -0xe0(%rbp)
cmovgl %esi, %r8d
movl %r8d, -0x94(%rbp)
movl 0x4(%r14), %esi
testl %edx, %edx
cmovlel %edi, %edx
movq %rdx, -0xd0(%rbp)
movl 0x8(%r14), %edx
testl %ecx, %ecx
cmovlel %edi, %ecx
movl %ecx, -0x7c(%rbp)
movl 0x38(%r14), %ecx
testl %eax, %eax
cmovlel %edi, %eax
movl %eax, -0x64(%rbp)
testl %ebx, %ebx
movl $0x0, %eax
cmovgl %ebx, %eax
movl %edx, %r8d
negl %r8d
movl %r8d, -0x74(%rbp)
movl %ecx, %r8d
notl %r8d
movl %edx, -0x78(%rbp)
imull %edx, %r8d
movl %r8d, -0x38(%rbp)
movl %esi, -0x3c(%rbp)
movl %ecx, -0x88(%rbp)
imull %ecx, %esi
movl %esi, -0x68(%rbp)
movl 0x18(%r14), %ecx
movl %ecx, -0xa4(%rbp)
movl 0x1c(%r14), %ecx
movl %ecx, -0xa0(%rbp)
movl 0x30(%r14), %ecx
movl %ecx, -0x58(%rbp)
movl 0x34(%r14), %ecx
movl %ecx, -0x5c(%rbp)
movq 0x50(%r14), %r13
movl 0x40(%r14), %ecx
movl %ecx, -0x60(%rbp)
movq %r14, -0xb8(%rbp)
movl 0x44(%r14), %ecx
movl %ecx, -0x70(%rbp)
movl %eax, -0x6c(%rbp)
cmpl %eax, %edi
je 0x42ef
movl %edi, %ecx
imull -0xc0(%rbp), %ecx
movq %rcx, -0xc8(%rbp)
xorl %edx, %edx
movl -0x38(%rbp), %ecx
movl %ecx, -0x30(%rbp)
movq %rdi, -0xd8(%rbp)
cmpl -0x64(%rbp), %edx
je 0x42bf
movl -0x60(%rbp), %ecx
imull %edx, %ecx
movl %ecx, -0x80(%rbp)
xorl %esi, %esi
movl -0x30(%rbp), %ecx
movl %ecx, -0x34(%rbp)
movl %edx, -0x8c(%rbp)
cmpl -0x7c(%rbp), %esi
je 0x42af
movl -0x3c(%rbp), %eax
imull %esi, %eax
subl -0x78(%rbp), %eax
movq %rax, -0xe8(%rbp)
movl -0x70(%rbp), %eax
imull %esi, %eax
movl %eax, -0x84(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x40(%rbp)
movl -0x74(%rbp), %eax
movl %eax, -0x44(%rbp)
xorl %r8d, %r8d
movl %esi, -0x90(%rbp)
cmpq -0xd0(%rbp), %r8
je 0x429c
xorl %ecx, %ecx
xorps %xmm0, %xmm0
movss %xmm0, -0x2c(%rbp)
movl -0x40(%rbp), %r15d
movq %r8, -0xf0(%rbp)
cmpl -0x94(%rbp), %ecx
je 0x4268
movq -0xe8(%rbp), %rax
movq %rcx, -0xf8(%rbp)
leal (%rcx,%rax), %r14d
movl %r15d, -0x98(%rbp)
movl -0x44(%rbp), %ebx
movq -0xe0(%rbp), %r12
subq $0x1, %r12
jb 0x4232
xorps %xmm0, %xmm0
testl %r14d, %r14d
js 0x41c9
testl %ebx, %ebx
js 0x41c9
cmpl -0xa4(%rbp), %r14d
jge 0x41c9
cmpl -0xa0(%rbp), %ebx
jge 0x41c9
movslq %r15d, %rax
movq -0xb0(%rbp), %rcx
movss (%rcx,%rax,4), %xmm0
movss %xmm0, -0x48(%rbp)
movq %r13, %rdi
leaq 0x1e74(%rip), %rsi      # 0x604c
callq 0x2230
testl %eax, %eax
je 0x4218
movq %r13, %rdi
leaq 0x215c(%rip), %rsi      # 0x6347
callq 0x2230
testl %eax, %eax
jne 0x42cc
movss -0x9c(%rbp), %xmm0
movss -0x48(%rbp), %xmm1
divss %xmm0, %xmm1
divss %xmm0, %xmm1
movss -0x2c(%rbp), %xmm0
addss %xmm1, %xmm0
jmp 0x4223
movss -0x48(%rbp), %xmm0
ucomiss -0x2c(%rbp), %xmm0
jbe 0x4228
movss %xmm0, -0x2c(%rbp)
incl %ebx
incl %r15d
jmp 0x4193
movq -0xf8(%rbp), %rcx
incl %ecx
movl -0x98(%rbp), %r15d
addl -0x88(%rbp), %r15d
movq -0xd8(%rbp), %rdi
movl -0x8c(%rbp), %edx
movl -0x90(%rbp), %esi
movq -0xf0(%rbp), %r8
jmp 0x4164
movq -0xc8(%rbp), %rax
addl %r8d, %eax
addl -0x80(%rbp), %eax
addl -0x84(%rbp), %eax
cltq
movq -0x50(%rbp), %rcx
movss -0x2c(%rbp), %xmm0
movss %xmm0, (%rcx,%rax,4)
incq %r8
movl -0x3c(%rbp), %eax
addl %eax, -0x44(%rbp)
addl %eax, -0x40(%rbp)
jmp 0x4142
incl %esi
movl -0x34(%rbp), %eax
addl -0x68(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x6c(%rbp), %eax
jmp 0x4108
incl %edx
movl -0x30(%rbp), %ecx
addl -0x5c(%rbp), %ecx
movl %ecx, -0x30(%rbp)
jmp 0x40e8
incl %edi
movl -0x58(%rbp), %ecx
addl %ecx, -0x38(%rbp)
jmp 0x40c1
movq 0x4d1d(%rip), %rdi      # 0x8ff0
leaq 0x2071(%rip), %rsi      # 0x634b
callq 0x2140
movq %rax, %rdi
callq 0x20c0
pushq $0x1
popq %rdi
callq 0x21d0
cmpb $0x0, -0x54(%rbp)
jne 0x4310
movq -0xb0(%rbp), %rdi
callq 0x21c0
movq -0xb8(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop