// File: /workspace/github_cpp_makefile_star2_O3/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, -0x40(%rbp)
movq %rsi, %r12
movq %rdi, %rbx
testl %ecx, %ecx
je 0x3cf7
testb %r8b, %r8b
je 0x3d01
movslq 0x30(%rbx), %rax
movslq 0x18(%rbx), %r15
imulq %rax, %r15
shlq $0x2, %r15
movq %r15, %rdi
callq 0x2240
movq %rax, 0x38(%rbx)
movq %rax, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x2130
movb $0x1, %al
movl %eax, -0x2c(%rbp)
jmp 0x3d12
movl %r8d, -0x2c(%rbp)
movq %r12, 0x38(%rbx)
jmp 0x3d12
movq -0x40(%rbp), %rax
movq %rax, 0x38(%rbx)
movb $0x1, %al
movl %eax, -0x2c(%rbp)
movq %r12, -0x40(%rbp)
movl 0x18(%rbx), %ecx
testl %ecx, %ecx
jle 0x3e27
movl 0x1c(%rbx), %eax
movl $0x0, -0x30(%rbp)
testl %eax, %eax
jle 0x3e17
movl 0x20(%rbx), %ecx
movl $0x0, -0x34(%rbp)
testl %ecx, %ecx
jle 0x3e04
movl 0x24(%rbx), %eax
xorl %r14d, %r14d
testl %eax, %eax
jle 0x3df5
xorl %r13d, %r13d
movl 0x30(%rbx), %eax
imull -0x30(%rbp), %eax
movl 0x2c(%rbx), %ecx
imull -0x34(%rbp), %ecx
addl %eax, %ecx
movl 0x28(%rbx), %r12d
imull %r14d, %r12d
addl %r13d, %r12d
addl %ecx, %r12d
movq (%rbx), %r15
movq %r15, %rdi
leaq 0x22cb(%rip), %rsi      # 0x6048
callq 0x2250
testl %eax, %eax
je 0x3db7
movq %r15, %rdi
leaq 0x22bc(%rip), %rsi      # 0x604c
callq 0x2250
testl %eax, %eax
jne 0x3e49
movq 0x38(%rbx), %rax
movslq %r12d, %rcx
leaq (%rax,%rcx,4), %rax
movss (%rax), %xmm0
movq -0x40(%rbp), %rdx
maxss (%rdx,%rcx,4), %xmm0
jmp 0x3dcf
movslq %r12d, %rax
movq -0x40(%rbp), %rcx
movss (%rcx,%rax,4), %xmm0
movq 0x38(%rbx), %rcx
leaq (%rcx,%rax,4), %rax
addss (%rax), %xmm0
cmpb $0x0, 0x40(%rbx)
je 0x3ddf
xorps %xmm1, %xmm1
maxss %xmm0, %xmm1
movaps %xmm1, %xmm0
movss %xmm0, (%rax)
incl %r13d
movl 0x24(%rbx), %eax
cmpl %eax, %r13d
jl 0x3d52
movl 0x20(%rbx), %ecx
incl %r14d
cmpl %ecx, %r14d
jl 0x3d47
movl 0x1c(%rbx), %eax
movl -0x34(%rbp), %edx
incl %edx
movl %edx, -0x34(%rbp)
cmpl %eax, %edx
jl 0x3d39
movl 0x18(%rbx), %ecx
movl -0x30(%rbp), %edx
incl %edx
movl %edx, -0x30(%rbp)
cmpl %ecx, %edx
jl 0x3d27
cmpb $0x0, -0x2c(%rbp)
jne 0x3e36
movq -0x40(%rbp), %rdi
callq 0x2200
movq 0x38(%rbx), %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x51a0(%rip), %rdi      # 0x8ff0
leaq 0x24da(%rip), %rsi      # 0x6331
callq 0x2180
movq %rax, %rdi
callq 0x20d0
movl $0x1, %edi
callq 0x2210
nop