// File: /workspace/github_c_makefile_star2_O3/hsjoihs[P]c-compiler/typecheck_expression.c
// Function: typecheck_binary_expression
struct Expr typecheck_binary_expression(const struct AnalyzerState *ptr_ps,
                                        const struct Expr *ref_expr,
                                        const struct Expr *ref_expr2,
                                        enum TokenKind op)
{
	const struct Expr expr = *ref_expr;
	const struct Expr expr2 = *ref_expr2;

	if (isAssign(op)) {
		if (expr.details.type.type_category == ARRAY ||
		    (expr.details.type.type_category == PTR_ &&
		     expr.details.true_type.type_category == ARRAY)) {
			fprintf(stderr, "array is not an lvalue\n");
			exit(EXIT_FAILURE);
		}

		struct Expr new_expr;
		new_expr.details = expr.details;
		struct Expr *ptr_expr1 = calloc(1, sizeof(struct Expr));
		struct Expr *ptr_expr2 = calloc(1, sizeof(struct Expr));
		*ptr_expr1 = expr;
		new_expr.ptr1 = ptr_expr1;
		new_expr.ptr3 = 0;

		if (op == OP_EQ) {
			struct Expr expr2_new = expr2;

			if (expr.details.type.type_category == PTR_) {
				/* automatically convert function name to function pointer */
				if_function_cast_to_pointer(&expr2_new);
				cast_to_null_pointer_if_possible(&expr2_new, &expr.details);
			}

			expect_type(ptr_ps, &expr2_new.details.type, &expr.details.type,
			            "mismatch in assignment operator");

			*ptr_expr2 = expr2_new;
			new_expr.ptr2 = ptr_expr2;

			if (is_struct_or_union(&expr.details.type)) {
				new_expr.category = STRUCT_OR_UNION_ASSIGNMENT_EXPR;
				new_expr.size_info_for_struct_or_union_assign =
				    size_of(ptr_ps, &expr.details.type);
			} else {
				new_expr.category = ASSIGNMENT_EXPR;
			}
			return new_expr;
		}

		/* op != OP_EQ */
		if (is_struct_or_union(&expr.details.type)) {
			fprintf(stderr, "invalid compound assignment operator used on a "
			                "value of type `");
			debug_print_type(&expr.details.type);
			fprintf(stderr, "`\n");
			exit(EXIT_FAILURE);
		}

		if (expr.details.type.type_category == PTR_ &&
		    (op == OP_PLUS_EQ || op == OP_MINUS_EQ)) {

			expect_integral(&expr2.details.type,
			                "right side of += or -= to a pointer");

			*ptr_expr2 = expr2;

			new_expr.category = COMPOUND_ASSIGNMENT_EXPR;
			new_expr.simple_binary_operator = op_before_assign(op);

			new_expr.ptr2 = ptr_expr2;

			const struct Type deref = deref_type(&expr.details.type);
			new_expr.size_info_for_pointer_arith = size_of(ptr_ps, &deref);

			return new_expr;
		}

		if (expr.details.type.type_category == PTR_) {
			fprintf(stderr, "invalid compound assignment operator "
			                "used on a pointer\n");
			exit(EXIT_FAILURE);
		}

		expect_type(ptr_ps, &expr.details.type, &expr2.details.type,
		            "mismatch in assignment operator");

		*ptr_expr2 = expr2;

		new_expr.category = COMPOUND_ASSIGNMENT_EXPR;
		new_expr.simple_binary_operator = op_before_assign(op);

		new_expr.ptr2 = ptr_expr2;

		if (expr.details.type.type_category == PTR_ &&
		    (op == OP_PLUS_EQ || op == OP_MINUS_EQ)) {
			const struct Type deref = deref_type(&expr.details.type);
			new_expr.size_info_for_pointer_arith = size_of(ptr_ps, &deref);
		}

		return new_expr;
	}

	switch (op) {
	case OP_PLUS: {
		const struct Type type1 = expr.details.type;
		const struct Type type2 = expr2.details.type;

		if (is_integral(&type1)) {
			if (is_integral(&type2)) {
				const struct Type t = INT_TYPE();
				return simple_binary_op(&expr, &expr2, OP_PLUS, &t);
			} else if (type2.type_category == PTR_) {
				/* swapped */
				return pointer_plusorminus_int(ptr_ps, &expr2, &expr, OP_PLUS);
			} else {
				fprintf(stderr, "invalid type `");
				debug_print_type(&type2);
				fprintf(stderr, "` as the right operand of a binary +\n");
				exit(EXIT_FAILURE);
			}
		} else if (type1.type_category == PTR_) {
			expect_integral(&expr2.details.type,
			                "cannot add a pointer/struct to a pointer");
			return pointer_plusorminus_int(ptr_ps, &expr, &expr2, OP_PLUS);
		} else {
			fprintf(stderr, "invalid type `");
			debug_print_type(&type1);
			fprintf(stderr, "`as the left operand of binary +\n");
			exit(EXIT_FAILURE);
		}
	}
	case OP_MINUS: {

		struct Type type1 = expr.details.type;
		struct Type type2 = expr2.details.type;

		if (is_integral(&type1)) {
			if (is_integral(&type2)) {
				const struct Type t = INT_TYPE();
				return simple_binary_op(&expr, &expr2, OP_MINUS, &t);
			} else if (type2.type_category == PTR_) {

				fprintf(stderr, "cannot subtract a pointer "
				                "from an integer.\n");
				exit(EXIT_FAILURE);
			} else {
				fprintf(stderr, "invalid type `");
				debug_print_type(&type2);
				fprintf(stderr, "` as the right operand of a binary -\n");
				exit(EXIT_FAILURE);
			}

		} else if (type1.type_category == PTR_) {
			if (is_integral(&type2)) {

				/* pointer minus int */
				return pointer_plusorminus_int(ptr_ps, &expr, &expr2, OP_MINUS);
			} else {
				/* pointer minus pointer */

				struct Expr *ptr_expr1 = calloc(1, sizeof(struct Expr));
				struct Expr *ptr_expr2 = calloc(1, sizeof(struct Expr));
				*ptr_expr1 = expr;
				*ptr_expr2 = expr2;

				struct Expr new_expr;
				new_expr.details.type = INT_TYPE();
				new_expr.details.true_type = INT_TYPE();
				new_expr.category = POINTER_MINUS_POINTER;
				new_expr.ptr1 = ptr_expr1;
				new_expr.ptr2 = ptr_expr2;
				new_expr.ptr3 = 0;
				const struct Type deref = deref_type(&expr.details.type);
				new_expr.size_info_for_pointer_arith = size_of(ptr_ps, &deref);

				return new_expr;
			}
		} else {
			fprintf(stderr, "invalid type `");
			debug_print_type(&type1);
			fprintf(stderr, "`as the left operand of binary -\n");
			exit(EXIT_FAILURE);
		}
	}
	case OP_AND_AND: {
		expect_scalar(&expr.details.type, "operand of logical AND");
		expect_scalar(&expr2.details.type, "operand of logical AND");
		const struct Type t = INT_TYPE();
		return binary_op(&expr, &expr2, LOGICAL_AND_EXPR, &t);
	}
	case OP_OR_OR: {
		expect_scalar(&expr.details.type, "operand of logical OR");
		expect_scalar(&expr2.details.type, "operand of logical OR");
		const struct Type t = INT_TYPE();
		return binary_op(&expr, &expr2, LOGICAL_OR_EXPR, &t);
	}
	case OP_OR:
	case OP_AND:
	case OP_LSHIFT:
	case OP_RSHIFT:
	case OP_HAT:
	case OP_ASTERISK:
	case OP_SLASH:
	case OP_PERCENT: {
		expect_integral(&expr.details.type, "left operand of an operator");
		expect_integral(&expr2.details.type, "right operand of an operator");
		return simple_binary_op(&expr, &expr2, op, &expr2.details.type);
	}
	case OP_GT:
	case OP_GT_EQ:
	case OP_LT:
	case OP_LT_EQ:
	case OP_NOT_EQ:
	case OP_EQ_EQ: {
		struct Expr expr_new = expr;
		struct Expr expr2_new = expr2;

		if (expr.details.type.type_category == PTR_) {
			cast_to_null_pointer_if_possible(&expr2_new, &expr.details);
		} else if (expr2.details.type.type_category == PTR_) {

			cast_to_null_pointer_if_possible(&expr_new, &expr2.details);
		}

		expect_type(ptr_ps, &expr_new.details.type, &expr2_new.details.type,
		            "mismatch in operands of an "
		            "equality/comparison operator");
		const struct Type t = INT_TYPE();
		return simple_binary_op(&expr_new, &expr2_new, op, &t);
	}
	case OP_COMMA: {
		return comma_op(&expr, &expr2, &expr2.details.type);
	}
	default: {
		fprintf(stderr, "FAILURE::::::: INVALID TOKEN %d in binary expr\n", op);
		exit(EXIT_FAILURE);
	}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x478, %rsp            # imm = 0x478
movl %r8d, %r15d
movq %rcx, %r14
movq %rdx, %r13
movq %rsi, %r12
movq %rdi, %rbx
leaq -0x148(%rbp), %rdi
movl $0x100, %edx            # imm = 0x100
movq %r13, %rsi
callq 0x1100
leaq -0x398(%rbp), %rdi
movl $0x100, %edx            # imm = 0x100
movq %r14, %rsi
callq 0x1100
cmpl $0x2b, %r15d
ja 0x7bcb
movl %r15d, %eax
leaq 0x800d(%rip), %rcx      # 0xf4c4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r14, -0x40(%rbp)
movq %r12, -0x30(%rbp)
movq %rbx, -0x38(%rbp)
movl -0x148(%rbp), %ebx
cmpl $0x2, %ebx
je 0x7bb5
cmpl $0x1, %ebx
jne 0x74ed
cmpl $0x2, -0xf8(%rbp)
je 0x7bb5
movl $0xa0, %edx
movq -0x38(%rbp), %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0x1100
movl $0x1, %edi
movl $0x100, %esi            # imm = 0x100
callq 0x10d0
movq %rax, %r14
movl $0x1, %edi
movl $0x100, %esi            # imm = 0x100
callq 0x10d0
movq %rax, -0x48(%rbp)
movl $0x100, %edx            # imm = 0x100
movq %r14, %rdi
movq %r13, %rsi
callq 0x1100
movq %r14, 0xb0(%r12)
movq $0x0, 0xc0(%r12)
cmpl $0x17, %r15d
jne 0x76d8
leaq -0x248(%rbp), %rdi
movl $0x100, %edx            # imm = 0x100
movq -0x40(%rbp), %rsi
callq 0x1100
cmpl $0x1, %ebx
movq -0x30(%rbp), %r12
jne 0x75b6
leaq -0x248(%rbp), %r14
movq %r14, %rdi
callq 0x61b8
cmpl $0x9, 0xa0(%r14)
jne 0x75b6
cmpl $0x0, -0x180(%rbp)
jne 0x75b6
leaq -0x248(%rbp), %rdi
movl $0x12, 0xa0(%rdi)
leaq -0x148(%rbp), %rsi
movl $0xa0, %edx
callq 0x1100
leaq 0x8561(%rip), %rcx      # 0xfb1e
leaq -0x248(%rbp), %r14
leaq -0x148(%rbp), %r15
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x5f2a
movl $0x100, %edx            # imm = 0x100
movq -0x48(%rbp), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x1100
movq -0x38(%rbp), %rbx
movq %r13, 0xb8(%rbx)
movq %r15, %rdi
callq 0x95ca
testl %eax, %eax
je 0x788c
leaq -0x148(%rbp), %rsi
movq %r12, %rdi
callq 0x2ffc
movl %eax, 0xf8(%rbx)
movl $0x14, %eax
jmp 0x7891
leaq 0x86cc(%rip), %rsi      # 0xfcfa
leaq -0x148(%rbp), %r14
movq %r14, %rdi
callq 0x614b
leaq 0x86d2(%rip), %rsi      # 0xfd16
leaq -0x398(%rbp), %r12
movq %r12, %rdi
callq 0x614b
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
movl %r15d, %ecx
movq %r12, %r8
jmp 0x77db
leaq -0x248(%rbp), %rdi
movl $0x100, %edx            # imm = 0x100
movq %r13, %rsi
callq 0x1100
leaq -0x498(%rbp), %rdi
movl $0x100, %edx            # imm = 0x100
movq %r14, %rsi
callq 0x1100
cmpl $0x1, -0x148(%rbp)
movq %r12, -0x30(%rbp)
jne 0x7760
cmpl $0x9, -0x3f8(%rbp)
jne 0x7799
cmpl $0x0, -0x3d0(%rbp)
jne 0x7799
leaq -0x498(%rbp), %rdi
movl $0x12, 0xa0(%rdi)
movl $0xa0, %edx
movq %r13, %rsi
jmp 0x7794
leaq -0x148(%rbp), %rdi
callq 0x95ca
testl %eax, %eax
movq -0x30(%rbp), %r12
jne 0x7bba
movl -0x148(%rbp), %eax
movl %eax, %ecx
xorl $0x1, %ecx
movl %r15d, %ebx
andl $-0x2, %ebx
movl %ebx, %edx
xorl $0x22, %edx
orl %ecx, %edx
jne 0x77e5
leaq 0x8468(%rip), %rsi      # 0xfb7d
leaq -0x398(%rbp), %r14
movq %r14, %rdi
callq 0x614b
movl $0x100, %edx            # imm = 0x100
movq -0x48(%rbp), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x1100
movq -0x38(%rbp), %rbx
movl $0xd, 0xa0(%rbx)
movl %r15d, %edi
callq 0x7c08
movl %eax, 0xa4(%rbx)
movq %r13, 0xb8(%rbx)
jmp 0x7860
cmpl $0x1, -0x398(%rbp)
jne 0x7799
cmpl $0x9, -0x1a8(%rbp)
jne 0x7799
cmpl $0x0, -0x180(%rbp)
jne 0x7799
leaq -0x248(%rbp), %rdi
movl $0x12, 0xa0(%rdi)
movl $0xa0, %edx
movq %r14, %rsi
callq 0x1100
leaq 0x8593(%rip), %rcx      # 0xfd33
leaq -0x248(%rbp), %r14
leaq -0x498(%rbp), %r12
movq -0x30(%rbp), %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x5f2a
leaq -0x298(%rbp), %r13
movq %r13, %rdi
callq 0x95ac
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
movl %r15d, %ecx
movq %r13, %r8
callq 0x7c26
jmp 0x7ae8
cmpl $0x1, %eax
je 0x7bc6
leaq 0x8329(%rip), %rcx      # 0xfb1e
leaq -0x148(%rbp), %rsi
leaq -0x398(%rbp), %r14
movq %r12, %rdi
movq %r14, %rdx
callq 0x5f2a
movl $0x100, %edx            # imm = 0x100
movq -0x48(%rbp), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x1100
movq -0x38(%rbp), %r14
movl $0xd, 0xa0(%r14)
movl %r15d, %edi
callq 0x7c08
movl %eax, 0xa4(%r14)
movq %r13, 0xb8(%r14)
cmpl $0x22, %ebx
movq %r14, %rbx
jne 0x7ae8
cmpl $0x1, -0x148(%rbp)
jne 0x7ae8
leaq -0x248(%rbp), %r14
leaq -0x148(%rbp), %rsi
movq %r14, %rdi
callq 0x96bb
movq %r12, %rdi
movq %r14, %rsi
callq 0x2ffc
movl %eax, 0xf4(%rbx)
jmp 0x7ae8
movl $0xe, %eax
movl %eax, 0xa0(%rbx)
jmp 0x7ae8
leaq -0x148(%rbp), %rsi
leaq -0x398(%rbp), %rcx
movq %rbx, %rdi
movq %rcx, %rdx
callq 0x7ea4
jmp 0x7ae8
leaq 0x840c(%rip), %r14      # 0xfccd
leaq -0x148(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x6130
leaq -0x398(%rbp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x6130
leaq -0x248(%rbp), %r14
movq %r14, %rdi
callq 0x95ac
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
movl $0x10, %ecx
jmp 0x7a69
leaq -0x248(%rbp), %rax
movl $0xa, %ecx
movq %rax, %rdi
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq -0x498(%rbp), %rdi
movl $0xa, %ecx
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
movl (%rax), %eax
cmpq $0x8, %rax
ja 0x7bd8
leaq 0x7c33(%rip), %rcx      # 0xf574
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl -0x498(%rbp), %eax
cmpq $0x8, %rax
ja 0x7bf0
leaq 0x7c37(%rip), %rcx      # 0xf598
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq -0x298(%rbp), %r14
movq %r14, %rdi
callq 0x95ac
leaq -0x148(%rbp), %rsi
leaq -0x398(%rbp), %rdx
movq %rbx, %rdi
movl $0x1, %ecx
jmp 0x7a19
leaq -0x248(%rbp), %rax
movl $0xa, %ecx
movq %rax, %rdi
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq -0x498(%rbp), %rdi
movl $0xa, %ecx
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
movl (%rax), %eax
cmpq $0x8, %rax
ja 0x7be4
leaq 0x7bee(%rip), %rcx      # 0xf5bc
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl -0x498(%rbp), %eax
cmpq $0x8, %rax
ja 0x7bfc
leaq 0x7bf2(%rip), %rcx      # 0xf5e0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq -0x298(%rbp), %r14
movq %r14, %rdi
callq 0x95ac
leaq -0x148(%rbp), %rsi
leaq -0x398(%rbp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
movq %r14, %r8
jmp 0x77db
leaq 0x82bc(%rip), %r14      # 0xfce4
leaq -0x148(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x6130
leaq -0x398(%rbp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x6130
leaq -0x248(%rbp), %r14
movq %r14, %rdi
callq 0x95ac
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
movl $0xf, %ecx
movq %r14, %r8
callq 0x7df0
jmp 0x7ae8
movl -0x498(%rbp), %eax
cmpl $0x8, %eax
ja 0x7afd
movl $0x111, %ecx            # imm = 0x111
btl %eax, %ecx
jae 0x7afd
leaq -0x148(%rbp), %rdx
leaq -0x398(%rbp), %rcx
movq %rbx, %rdi
movq %r12, %rsi
movl $0x1, %r8d
jmp 0x7ae3
leaq 0x8163(%rip), %rsi      # 0xfc0e
leaq -0x398(%rbp), %r14
movq %r14, %rdi
callq 0x614b
leaq -0x148(%rbp), %rdx
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, %rcx
jmp 0x7ae0
leaq -0x398(%rbp), %rdx
leaq -0x148(%rbp), %rcx
movq %rbx, %rdi
movq %r12, %rsi
xorl %r8d, %r8d
callq 0x7d0f
movq %rbx, %rax
addq $0x478, %rsp            # imm = 0x478
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %edi
movl $0x100, %esi            # imm = 0x100
callq 0x10d0
movq %r14, -0x40(%rbp)
movq %rax, %r14
movl $0x1, %edi
movl $0x100, %esi            # imm = 0x100
callq 0x10d0
movq %rax, %r15
movl $0x100, %edx            # imm = 0x100
movq %r14, %rdi
movq %r13, %rsi
callq 0x1100
movl $0x100, %edx            # imm = 0x100
movq %r15, %rdi
movq -0x40(%rbp), %rsi
callq 0x1100
movq %rbx, %rdi
callq 0x95ac
movq %r12, -0x30(%rbp)
leaq 0x50(%rbx), %r12
leaq -0x298(%rbp), %r13
movq %r13, %rdi
callq 0x95ac
movl $0xa, %ecx
movq %r12, %rdi
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
movl $0x4, 0xa0(%rbx)
movq %r14, 0xb0(%rbx)
movq %r15, 0xb8(%rbx)
movq $0x0, 0xc0(%rbx)
leaq -0x298(%rbp), %r14
leaq -0x148(%rbp), %rsi
movq %r14, %rdi
callq 0x96bb
movq -0x30(%rbp), %rdi
jmp 0x7879
callq 0x1c3d
leaq -0x148(%rbp), %rdi
callq 0x1bc3
callq 0x1c15
movl %r15d, %edi
callq 0x1c65
callq 0x1a53
leaq -0x248(%rbp), %rdi
callq 0x1acd
leaq -0x248(%rbp), %rdi
callq 0x1b71
leaq -0x498(%rbp), %rdi
callq 0x1a7b
leaq -0x498(%rbp), %rdi
callq 0x1b1f