// File: /workspace/github_c_makefile_star2_O3/hsjoihs[P]c-compiler/codegen_expression.c
// Function: print_address_of_lvalue_or_struct_or_union
void print_address_of_lvalue_or_struct_or_union(struct PrinterState *ptr_prs,
                                                const struct Expr *ref_expr,
                                                const char *msg)
{
	const struct Expr expr = *ref_expr;
	switch (expr.category) {
	case CONDITIONAL_EXPR: {
		int label1 = get_new_label_name(ptr_prs);
		int label2 = get_new_label_name(ptr_prs);

		print_expression(ptr_prs, expr.ptr1);
		gen_if_zero_jmp_nbyte(
		    size_of_basic(&expr.ptr1->details.type,
		                  "condition of conditional expression"),
		    label1, 0);

		if (!is_struct_or_union(&expr.ptr2->details.type)) {
			simple_error("the conditional operator is used as lvalue");
		}

		print_address_of_lvalue_or_struct_or_union(
		    ptr_prs, expr.ptr2, "true branch of ternary operator");
		gen_jump(label2, "ternary operator");
		gen_label(label1);
		print_address_of_lvalue_or_struct_or_union(
		    ptr_prs, expr.ptr3, "false branch of ternary operator");
		gen_label(label2);
		gen_discard2nd();
		return;
	}
	case STRUCT_OR_UNION_ASSIGNMENT_EXPR: {
		print_address_of_lvalue(
		    ptr_prs, expr.ptr1, "left hand of struct assignment");
		print_address_of_lvalue_or_struct_or_union(
		    ptr_prs, expr.ptr2, "right hand of struct assignment");
		int size = expr.size_info_for_struct_or_union_assign;
		gen_copy_1st_struct_or_union_to_2nd_and_discard(size);
		print_address_of_lvalue(
		    ptr_prs, expr.ptr1, "left hand of struct assignment");
		return;
	}
	case FPCALL_EXPR_RETURNING_INTEGER_CLASS: {
		print_expression(ptr_prs, expr.ptr1);
		pass_args(ptr_prs, &expr.args);
		gen_pop_to_reg_8byte("r11");

		int size = expr.size_info_for_struct_or_union_assign;
		gen_raw_call_partA();
		gen_call_reg_and_assign_integerclass_struct_or_union_or_union_to_local(
		    "r11", expr.local_var_offset, size);
		gen_push_address_of_local(expr.local_var_offset);
		return;
	}
	case FPCALL_EXPR_RETURNING_MEMORY_CLASS: {
		print_expression(ptr_prs, expr.ptr1);
		pass_args(ptr_prs, &expr.args);
		gen_pop_to_reg_8byte("r11");

		/* call a function that returns a void */
		gen_raw_call_partA();
		gen_call_reg_and_push_ret_of_nbyte(4, "r11");
		gen_discard();

		gen_push_address_of_local(expr.local_var_offset);
		return;
	}
	case FUNCCALL_EXPR_RETURNING_INTEGER_CLASS: {

		pass_args(ptr_prs, &expr.args);

		int size = expr.size_info_for_struct_or_union_assign;
		gen_raw_call_partA();
		gen_call_and_assign_integerclass_struct_or_union_to_local(
		    expr.global_var_name, expr.local_var_offset, size);
		gen_push_address_of_local(expr.local_var_offset);
		return;
	}
	case FUNCCALL_EXPR_RETURNING_MEMORY_CLASS: {

		int arg_stacksize = count_args(&expr.args) * 8;
		if (arg_stacksize % 16 != 0) {
			unsupported("misaligned stack");
		}
		gen_raw_call_partA();
		pass_args(ptr_prs, &expr.args);

		/* call a function that returns a void */
		gen_push_ret_of_nbyte(4, expr.global_var_name, arg_stacksize);
		gen_discard();

		gen_push_address_of_local(expr.local_var_offset);
		return;
	}
	case LOCAL_VAR_: {
		gen_push_address_of_local(expr.local_var_offset);

		return;
	}
	case GLOBAL_VAR_: {
		gen_push_address_of_global(expr.global_var_name);

		return;
	}
	case UNARY_OP_EXPR:
		if (expr.unary_operator == UNARY_OP_ASTERISK) {
			print_expression(ptr_prs, expr.ptr1);
			return;
		} else {
			simple_error("the only unary operator that can create "
			             "lvalue is `*`\n");
		}
	case VOID_EXPR:
		fprintf(stderr, "context: %s\n", msg);
		simple_error("doesn't seem like an lvalue; it is a void expr.\n");
	case COMMA_EXPR: {
		print_expression_or_addr_of_struct_or_union(
		    ptr_prs, expr.ptr1, "struct as the first operand of comma");

		if (!is_struct_or_union(&expr.ptr2->details.type)) {
			simple_error(
			    "either the result of comma expression is used as an "
			    "lvalue, or the comma expression is used as if it were a "
			    "struct/union though the second operand of comma isn't.\n");
		}
		print_address_of_lvalue_or_struct_or_union(
		    ptr_prs, expr.ptr2, "struct/union as the second operand of comma");

		gen_discard2nd();
		return;
	}
	default:
		fprintf(stderr, "context: %s\n", msg);
		simple_error("doesn't seem like an lvalue or a struct\n");
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x100, %rsp            # imm = 0x100
movq %rdx, %r14
movq %rdi, %rbx
leaq -0x120(%rbp), %r15
movl $0x100, %edx            # imm = 0x100
movq %r15, %rdi
callq 0x1100
movl 0xa0(%r15), %eax
decl %eax
cmpl $0x18, %eax
ja 0x4e87
leaq 0xa06b(%rip), %rcx      # 0xec1c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x70(%rbp), %rsi
leaq 0xa330(%rip), %rdx      # 0xeef5
movq %rbx, %rdi
callq 0x587e
movq -0x68(%rbp), %r14
movq %r14, %rdi
callq 0x95ca
testl %eax, %eax
je 0x4e57
leaq 0xa3d6(%rip), %rdx      # 0xefbe
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4b6c
jmp 0x4cd7
leaq -0x40(%rbp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5753
movl 0x18(%r14), %ebx
callq 0xbd82
movq -0x10(%r14), %rdi
movl 0x10(%r14), %r14d
movl %r14d, %esi
movl %ebx, %edx
callq 0xb336
jmp 0x4e38
movq -0x50(%rbp), %rdi
addq $0x100, %rsp            # imm = 0x100
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0xa8ff
movq %rbx, %rdi
callq 0x245c
movl %eax, %r15d
movq %rbx, %rdi
callq 0x245c
movl %eax, %r14d
movq -0x70(%rbp), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4e8f
leaq 0xa13e(%rip), %rsi      # 0xeda9
movq %r12, %rdi
callq 0x95dd
movl %eax, %edi
movl %r15d, %esi
xorl %edx, %edx
callq 0xab15
movq -0x68(%rbp), %r12
movq %r12, %rdi
callq 0x95ca
testl %eax, %eax
je 0x4e63
leaq 0xa15e(%rip), %rdx      # 0xedf8
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4b6c
leaq 0xa17c(%rip), %rsi      # 0xee28
movl %r14d, %edi
callq 0xaac7
movl %r15d, %edi
callq 0xac74
movq -0x60(%rbp), %rsi
leaq 0xa151(%rip), %rdx      # 0xee18
movq %rbx, %rdi
callq 0x4b6c
movl %r14d, %edi
callq 0xac74
addq $0x100, %rsp            # imm = 0x100
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0xadf1
leaq -0x40(%rbp), %r15
movq %r15, %rdi
callq 0x5804
movl %eax, %r14d
testb $0x1, %r14b
jne 0x4e6f
shll $0x3, %r14d
callq 0xbd82
movq %rbx, %rdi
movq %r15, %rsi
callq 0x5753
movq -0x50(%rbp), %rsi
movl $0x4, %edi
movl %r14d, %edx
callq 0xa953
callq 0xadc9
movl -0x30(%rbp), %edi
jmp 0x4e3b
movq -0x70(%rbp), %r14
leaq 0xa0f8(%rip), %r15      # 0xee39
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x4b44
movq -0x68(%rbp), %rsi
leaq 0xa0fe(%rip), %rdx      # 0xee58
movq %rbx, %rdi
callq 0x4b6c
movl -0x28(%rbp), %edi
callq 0xb563
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
addq $0x100, %rsp            # imm = 0x100
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x4b44
cmpl $0x7, -0x78(%rbp)
jne 0x4e7b
movq -0x70(%rbp), %rsi
movq %rbx, %rdi
addq $0x100, %rsp            # imm = 0x100
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x4e8f
leaq -0x40(%rbp), %r14
movq -0x30(%r14), %rsi
movq %rbx, %rdi
callq 0x4e8f
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5753
leaq 0xa0aa(%rip), %rbx      # 0xee78
movq %rbx, %rdi
callq 0xb215
callq 0xbd82
movl $0x4, %edi
movq %rbx, %rsi
callq 0xaa02
callq 0xadc9
movl 0x10(%r14), %edi
jmp 0x4e3b
leaq -0x40(%rbp), %r14
movq -0x30(%r14), %rsi
movq %rbx, %rdi
callq 0x4e8f
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5753
leaq 0xa063(%rip), %rbx      # 0xee78
movq %rbx, %rdi
callq 0xb215
movl 0x18(%r14), %r15d
callq 0xbd82
movl 0x10(%r14), %r14d
movq %rbx, %rdi
movl %r14d, %esi
movl %r15d, %edx
callq 0xb464
movl %r14d, %edi
addq $0x100, %rsp            # imm = 0x100
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0xa824
movq %r14, %rdi
callq 0x1544
leaq 0xa0bc(%rip), %rdi      # 0xef1a
callq 0x9546
leaq 0x9f63(%rip), %rdi      # 0xedcd
callq 0x9546
leaq 0xa006(%rip), %rdi      # 0xee7c
callq 0x9581
leaq 0xa00b(%rip), %rdi      # 0xee8d
callq 0x9546
movq %r14, %rdi
callq 0x156f