// File: /workspace/github_cpp_makefile_star2_O2/hey-yahei[P]ConvNet/src/Eltwise.cpp
// Function: Eltwise::Run(float*, float*, bool, bool)
float* Eltwise::Run(float* in1_data, float* in2_data, bool keep_in1, bool keep_in2) {
	int elt_idx;

	if (!keep_in1) {
		out_data = in1_data;
		keep_in1 = true;
	} else if (!keep_in2) {   // keep_in1 == true
		out_data = in2_data;
		in2_data = in1_data;  // now, in2 -> in1
		keep_in2 = true;
	} else {
		out_data = (float*)malloc(sizeof(float) * out_chw * out_shape[0]);
		memcpy(out_data, in1_data, sizeof(float) * out_chw * out_shape[0]);
	}

	for (int idx_bs = 0; idx_bs < out_shape[0]; idx_bs++) {
		for (int idx_c = 0; idx_c < out_shape[1]; idx_c++) {
			for (int idx_h = 0; idx_h < out_shape[2]; idx_h++) {
				for (int idx_w = 0; idx_w < out_shape[3]; idx_w++) {
					elt_idx = idx_bs * out_chw + idx_c * out_hw + idx_h * out_w + idx_w;
					if (!strcmp(elt_t, "add")) {
						out_data[elt_idx] += in2_data[elt_idx];
						if (with_relu && out_data[elt_idx] < 0.)
							out_data[elt_idx] = 0.;
					} else if (!strcmp(elt_t, "max")){
						out_data[elt_idx] = MAX(out_data[elt_idx], in2_data[elt_idx]);
						if (with_relu && out_data[elt_idx] < 0.)
							out_data[elt_idx] = 0.;
					} else {
						cerr << "Eltwise: unknown type" << endl;
						exit(1);
					}
				}
			}
		}
	}

	if (!keep_in1)
		free(in1_data);
	if (!keep_in2)
		free(in2_data);

	return out_data;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, -0x30(%rbp)
movq %rsi, %r12
movq %rdi, %rbx
testl %ecx, %ecx
je 0x3bbb
testb %r8b, %r8b
je 0x3bc1
movslq 0x30(%rbx), %rax
movslq 0x18(%rbx), %r15
imulq %rax, %r15
shlq $0x2, %r15
movq %r15, %rdi
callq 0x2210
movq %rax, 0x38(%rbx)
movq %rax, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x2110
movb $0x1, %r8b
jmp 0x3bd0
movq %r12, 0x38(%rbx)
jmp 0x3bd0
movq -0x30(%rbp), %rax
movq %rax, 0x38(%rbx)
movb $0x1, %r8b
movq %r12, -0x30(%rbp)
xorl %ecx, %ecx
movl %r8d, -0x34(%rbp)
cmpl 0x18(%rbx), %ecx
jge 0x3cc3
xorl %edx, %edx
movl %ecx, -0x38(%rbp)
cmpl 0x1c(%rbx), %edx
jge 0x3cbc
xorl %esi, %esi
movl %edx, -0x3c(%rbp)
cmpl 0x20(%rbx), %esi
jge 0x3cb1
xorl %r13d, %r13d
cmpl 0x24(%rbx), %r13d
jge 0x3caa
movl 0x30(%rbx), %eax
imull %ecx, %eax
movl 0x2c(%rbx), %ecx
imull %edx, %ecx
addl %eax, %ecx
movl 0x28(%rbx), %r12d
movl %esi, %r14d
imull %esi, %r12d
addl %r13d, %r12d
addl %ecx, %r12d
movq (%rbx), %r15
movq %r15, %rdi
leaq 0x2414(%rip), %rsi      # 0x6048
callq 0x2230
movslq %r12d, %r12
testl %eax, %eax
je 0x3c6f
movq %r15, %rdi
leaq 0x2402(%rip), %rsi      # 0x604c
callq 0x2230
testl %eax, %eax
jne 0x3ce4
movq 0x38(%rbx), %rax
leaq (%rax,%r12,4), %rax
movss (%rax), %xmm0
movq -0x30(%rbp), %rcx
maxss (%rcx,%r12,4), %xmm0
jmp 0x3c85
movq -0x30(%rbp), %rax
movss (%rax,%r12,4), %xmm0
movq 0x38(%rbx), %rax
leaq (%rax,%r12,4), %rax
addss (%rax), %xmm0
cmpb $0x0, 0x40(%rbx)
movl -0x38(%rbp), %ecx
movl -0x3c(%rbp), %edx
movl %r14d, %esi
je 0x3c9e
xorps %xmm1, %xmm1
maxss %xmm0, %xmm1
movaps %xmm1, %xmm0
movss %xmm0, (%rax)
incl %r13d
jmp 0x3bfe
incl %esi
jmp 0x3bf2
incl %edx
movl -0x34(%rbp), %r8d
jmp 0x3be4
incl %ecx
jmp 0x3bd6
testb %r8b, %r8b
jne 0x3cd1
movq -0x30(%rbp), %rdi
callq 0x21c0
movq 0x38(%rbx), %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x5305(%rip), %rdi      # 0x8ff0
leaq 0x263f(%rip), %rsi      # 0x6331
callq 0x2140
movq %rax, %rdi
callq 0x20c0
pushq $0x1
popq %rdi
callq 0x21d0
nop