// File: /workspace/github_c_makefile_star2_O1/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 0x7a0e
movl %r15d, %eax
leaq 0x81f6(%rip), %rcx      # 0xf4ec
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 0x79f8
cmpl $0x1, %ebx
jne 0x732c
cmpl $0x2, -0xf8(%rbp)
je 0x79f8
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 0x751c
leaq -0x248(%rbp), %rdi
movl $0x100, %edx            # imm = 0x100
movq -0x40(%rbp), %rsi
callq 0x1100
cmpl $0x1, %ebx
movq -0x30(%rbp), %r12
jne 0x73f5
leaq -0x248(%rbp), %r14
movq %r14, %rdi
callq 0x5fed
cmpl $0x9, 0xa0(%r14)
jne 0x73f5
cmpl $0x0, -0x180(%rbp)
jne 0x73f5
leaq -0x248(%rbp), %rdi
movl $0x12, 0xa0(%rdi)
leaq -0x148(%rbp), %rsi
movl $0xa0, %edx
callq 0x1100
leaq 0x874a(%rip), %rcx      # 0xfb46
leaq -0x248(%rbp), %r14
leaq -0x148(%rbp), %r15
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x5d65
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 0x9516
testl %eax, %eax
je 0x76d0
movl $0x14, 0xa0(%rbx)
leaq -0x148(%rbp), %rsi
movq %r12, %rdi
callq 0x2fe0
movl %eax, 0xf8(%rbx)
jmp 0x792b
leaq 0x88b0(%rip), %rsi      # 0xfd22
leaq -0x148(%rbp), %r14
movq %r14, %rdi
callq 0x5f80
leaq 0x88b6(%rip), %rsi      # 0xfd3e
leaq -0x398(%rbp), %r12
movq %r12, %rdi
callq 0x5f80
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
movl %r15d, %ecx
movq %r12, %r8
jmp 0x761f
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 0x75a4
cmpl $0x9, -0x3f8(%rbp)
jne 0x75dd
cmpl $0x0, -0x3d0(%rbp)
jne 0x75dd
leaq -0x498(%rbp), %rdi
movl $0x12, 0xa0(%rdi)
movl $0xa0, %edx
movq %r13, %rsi
jmp 0x75d8
leaq -0x148(%rbp), %rdi
callq 0x9516
testl %eax, %eax
movq -0x30(%rbp), %r12
jne 0x79fd
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 0x7629
leaq 0x864c(%rip), %rsi      # 0xfba5
leaq -0x398(%rbp), %r14
movq %r14, %rdi
callq 0x5f80
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 0x7a4b
movl %eax, 0xa4(%rbx)
movq %r13, 0xb8(%rbx)
jmp 0x76a4
cmpl $0x1, -0x398(%rbp)
jne 0x75dd
cmpl $0x9, -0x1a8(%rbp)
jne 0x75dd
cmpl $0x0, -0x180(%rbp)
jne 0x75dd
leaq -0x248(%rbp), %rdi
movl $0x12, 0xa0(%rdi)
movl $0xa0, %edx
movq %r14, %rsi
callq 0x1100
leaq 0x8777(%rip), %rcx      # 0xfd5b
leaq -0x248(%rbp), %r14
leaq -0x498(%rbp), %r12
movq -0x30(%rbp), %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x5d65
leaq -0x298(%rbp), %r13
movq %r13, %rdi
callq 0x94f8
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
movl %r15d, %ecx
movq %r13, %r8
callq 0x7a69
jmp 0x792b
cmpl $0x1, %eax
je 0x7a09
leaq 0x850d(%rip), %rcx      # 0xfb46
leaq -0x148(%rbp), %rsi
leaq -0x398(%rbp), %r14
movq %r12, %rdi
movq %r14, %rdx
callq 0x5d65
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 0x7a4b
movl %eax, 0xa4(%r14)
movq %r13, 0xb8(%r14)
cmpl $0x22, %ebx
movq %r14, %rbx
jne 0x792b
cmpl $0x1, -0x148(%rbp)
jne 0x792b
leaq -0x248(%rbp), %r14
leaq -0x148(%rbp), %rsi
movq %r14, %rdi
callq 0x9607
movq %r12, %rdi
movq %r14, %rsi
callq 0x2fe0
movl %eax, 0xf4(%rbx)
jmp 0x792b
movl $0xe, 0xa0(%rbx)
jmp 0x792b
leaq -0x148(%rbp), %rsi
leaq -0x398(%rbp), %rcx
movq %rbx, %rdi
movq %rcx, %rdx
callq 0x7cf0
jmp 0x792b
leaq 0x85f1(%rip), %r14      # 0xfcf5
leaq -0x148(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x5f65
leaq -0x398(%rbp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x5f65
leaq -0x248(%rbp), %r14
movq %r14, %rdi
callq 0x94f8
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
movl $0x10, %ecx
jmp 0x78ac
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 0x7a1b
leaq 0x7e18(%rip), %rcx      # 0xf59c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl -0x498(%rbp), %eax
cmpq $0x8, %rax
ja 0x7a33
leaq 0x7e1c(%rip), %rcx      # 0xf5c0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq -0x298(%rbp), %r14
movq %r14, %rdi
callq 0x94f8
leaq -0x148(%rbp), %rsi
leaq -0x398(%rbp), %rdx
movq %rbx, %rdi
movl $0x1, %ecx
jmp 0x785c
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 0x7a27
leaq 0x7dd3(%rip), %rcx      # 0xf5e4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl -0x498(%rbp), %eax
cmpq $0x8, %rax
ja 0x7a3f
leaq 0x7dd7(%rip), %rcx      # 0xf608
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq -0x298(%rbp), %r14
movq %r14, %rdi
callq 0x94f8
leaq -0x148(%rbp), %rsi
leaq -0x398(%rbp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
movq %r14, %r8
jmp 0x761f
leaq 0x84a1(%rip), %r14      # 0xfd0c
leaq -0x148(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x5f65
leaq -0x398(%rbp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x5f65
leaq -0x248(%rbp), %r14
movq %r14, %rdi
callq 0x94f8
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
movl $0xf, %ecx
movq %r14, %r8
callq 0x7c39
jmp 0x792b
movl -0x498(%rbp), %eax
cmpl $0x8, %eax
ja 0x7940
movl $0x111, %ecx            # imm = 0x111
btl %eax, %ecx
jae 0x7940
leaq -0x148(%rbp), %rdx
leaq -0x398(%rbp), %rcx
movq %rbx, %rdi
movq %r12, %rsi
movl $0x1, %r8d
jmp 0x7926
leaq 0x8348(%rip), %rsi      # 0xfc36
leaq -0x398(%rbp), %r14
movq %r14, %rdi
callq 0x5f80
leaq -0x148(%rbp), %rdx
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, %rcx
jmp 0x7923
leaq -0x398(%rbp), %rdx
leaq -0x148(%rbp), %rcx
movq %rbx, %rdi
movq %r12, %rsi
xorl %r8d, %r8d
callq 0x7b55
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 0x94f8
movq %r12, -0x30(%rbp)
leaq 0x50(%rbx), %r12
leaq -0x298(%rbp), %r13
movq %r13, %rdi
callq 0x94f8
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 0x9607
movq -0x30(%rbp), %rdi
jmp 0x76bd
callq 0x1c19
leaq -0x148(%rbp), %rdi
callq 0x1b9f
callq 0x1bf1
movl %r15d, %edi
callq 0x1c41
callq 0x1a2f
leaq -0x248(%rbp), %rdi
callq 0x1aa9
leaq -0x248(%rbp), %rdi
callq 0x1b4d
leaq -0x498(%rbp), %rdi
callq 0x1a57
leaq -0x498(%rbp), %rdi
callq 0x1afb