// File: /workspace/github_cpp_makefile_star2_O1/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 $0x28, %rsp
movq %rdx, -0x40(%rbp)
movq %rsi, %r14
movq %rdi, %rbx
testl %ecx, %ecx
movq %rsi, -0x50(%rbp)
je 0x3ced
movl %ecx, -0x44(%rbp)
testb %r8b, %r8b
je 0x3cfc
movl %r8d, -0x2c(%rbp)
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 %r14, %rsi
movq %r15, %rdx
callq 0x2130
jmp 0x3d0d
movl %r8d, -0x2c(%rbp)
movq %r14, 0x38(%rbx)
movb $0x1, %al
movl %eax, -0x44(%rbp)
jmp 0x3d0d
movq -0x40(%rbp), %rax
movq %rax, 0x38(%rbx)
movb $0x1, %al
movl %eax, -0x2c(%rbp)
movq %r14, -0x40(%rbp)
cmpl $0x0, 0x18(%rbx)
jle 0x3e1a
movl $0x0, -0x30(%rbp)
cmpl $0x0, 0x1c(%rbx)
jle 0x3e09
movl $0x0, -0x34(%rbp)
cmpl $0x0, 0x20(%rbx)
jle 0x3df8
xorl %r14d, %r14d
cmpl $0x0, 0x24(%rbx)
jle 0x3deb
xorl %r15d, %r15d
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 %r15d, %r12d
addl %ecx, %r12d
movq (%rbx), %r13
movq %r13, %rdi
leaq 0x22d4(%rip), %rsi      # 0x6048
callq 0x2250
testl %eax, %eax
je 0x3dae
movq %r13, %rdi
leaq 0x22c5(%rip), %rsi      # 0x604c
callq 0x2250
testl %eax, %eax
jne 0x3e4b
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 0x3dc6
movslq %r12d, %rax
movq -0x40(%rbp), %rcx
movss (%rcx,%rax,4), %xmm0
movq 0x38(%rbx), %rcx
leaq (%rcx,%rax,4), %rax
addss (%rax), %xmm0
movss %xmm0, (%rax)
cmpb $0x0, 0x40(%rbx)
je 0x3dda
xorps %xmm1, %xmm1
maxss %xmm0, %xmm1
movaps %xmm1, %xmm0
movss %xmm0, (%rax)
incl %r15d
cmpl 0x24(%rbx), %r15d
jl 0x3d49
incl %r14d
cmpl 0x20(%rbx), %r14d
jl 0x3d3c
movl -0x34(%rbp), %eax
incl %eax
movl %eax, -0x34(%rbp)
cmpl 0x1c(%rbx), %eax
jl 0x3d2f
movl -0x30(%rbp), %eax
incl %eax
movl %eax, -0x30(%rbp)
cmpl 0x18(%rbx), %eax
jl 0x3d1e
cmpb $0x0, -0x44(%rbp)
jne 0x3e29
movq -0x50(%rbp), %rdi
callq 0x2200
cmpb $0x0, -0x2c(%rbp)
jne 0x3e38
movq -0x40(%rbp), %rdi
callq 0x2200
movq 0x38(%rbx), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x519e(%rip), %rdi      # 0x8ff0
leaq 0x24d8(%rip), %rsi      # 0x6331
callq 0x2180
movq %rax, %rdi
callq 0x20d0
movl $0x1, %edi
callq 0x2210