// File: /workspace/github_c_makefile_star2_O2/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 0x3b68
leaq 0xa3c9(%rip), %rcx      # 0xdc40
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x70(%rbp), %rsi
leaq 0xa68e(%rip), %rdx      # 0xdf19
movq %rbx, %rdi
callq 0x4536
movq -0x68(%rbp), %r14
movq %r14, %rdi
callq 0x81cb
testl %eax, %eax
je 0x3b38
leaq 0xa734(%rip), %rdx      # 0xdfe2
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3832
jmp 0x399d
leaq -0x40(%rbp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4417
movl 0x18(%r14), %ebx
callq 0xaadc
movq -0x10(%r14), %rdi
movl 0x10(%r14), %r14d
movl %r14d, %esi
movl %ebx, %edx
callq 0xa215
jmp 0x3afa
movq -0x50(%rbp), %rdi
addq $0x100, %rsp            # imm = 0x100
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x9801
movq %rbx, %rdi
callq 0x12a8
movl %eax, %r15d
movq %rbx, %rdi
callq 0x12a8
movl %eax, %r14d
movq -0x70(%rbp), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3b8f
leaq 0xa49c(%rip), %rsi      # 0xddcd
movq %r12, %rdi
callq 0x81de
movl %eax, %edi
movl %r15d, %esi
xorl %edx, %edx
callq 0x9a17
movq -0x68(%rbp), %r12
movq %r12, %rdi
callq 0x81cb
testl %eax, %eax
je 0x3b44
leaq 0xa4bc(%rip), %rdx      # 0xde1c
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3832
leaq 0xa4da(%rip), %rsi      # 0xde4c
movl %r14d, %edi
callq 0x99c9
movl %r15d, %edi
callq 0x9b55
movq -0x60(%rbp), %rsi
leaq 0xa4af(%rip), %rdx      # 0xde3c
movq %rbx, %rdi
callq 0x3832
movl %r14d, %edi
callq 0x9b55
addq $0x100, %rsp            # imm = 0x100
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x9cd2
leaq -0x40(%rbp), %r15
movq %r15, %rdi
callq 0x44ba
movl %eax, %r14d
testb $0x1, %r14b
jne 0x3b50
shll $0x3, %r14d
callq 0xaadc
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4417
movq -0x50(%rbp), %rsi
pushq $0x4
popq %rdi
movl %r14d, %edx
callq 0x9855
callq 0x9caa
movl -0x30(%rbp), %edi
jmp 0x3afd
movq -0x70(%rbp), %r14
leaq 0xa458(%rip), %r15      # 0xde5d
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x37f0
movq -0x68(%rbp), %rsi
leaq 0xa45e(%rip), %rdx      # 0xde7c
movq %rbx, %rdi
callq 0x3832
movl -0x28(%rbp), %edi
callq 0xa3cd
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 0x37f0
cmpl $0x7, -0x78(%rbp)
jne 0x3b5c
movq -0x70(%rbp), %rsi
movq %rbx, %rdi
addq $0x100, %rsp            # imm = 0x100
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x3b8f
leaq -0x40(%rbp), %r14
movq -0x30(%r14), %rsi
movq %rbx, %rdi
callq 0x3b8f
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4417
leaq 0xa40a(%rip), %rbx      # 0xde9c
movq %rbx, %rdi
callq 0xa0f4
callq 0xaadc
pushq $0x4
popq %rdi
movq %rbx, %rsi
callq 0x9904
callq 0x9caa
movl 0x10(%r14), %edi
jmp 0x3afd
leaq -0x40(%rbp), %r14
movq -0x30(%r14), %rsi
movq %rbx, %rdi
callq 0x3b8f
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4417
leaq 0xa3c5(%rip), %rbx      # 0xde9c
movq %rbx, %rdi
callq 0xa0f4
movl 0x18(%r14), %r15d
callq 0xaadc
movl 0x10(%r14), %r14d
movq %rbx, %rdi
movl %r14d, %esi
movl %r15d, %edx
callq 0xa307
movl %r14d, %edi
addq $0x100, %rsp            # imm = 0x100
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x9726
movq 0x124e0(%rip), %rax     # 0x15ff8
movq (%rax), %rdi
leaq 0xac21(%rip), %rsi      # 0xe743
movq %r14, %rdx
xorl %eax, %eax
callq 0x10f0
leaq 0xa3b5(%rip), %rdi      # 0xdee8
callq 0x814d
leaq 0xa3ff(%rip), %rdi      # 0xdf3e
callq 0x814d
leaq 0xa2a6(%rip), %rdi      # 0xddf1
callq 0x814d
leaq 0xa349(%rip), %rdi      # 0xdea0
callq 0x8186
leaq 0xa34e(%rip), %rdi      # 0xdeb1
callq 0x814d
movq 0x12489(%rip), %rax     # 0x15ff8
movq (%rax), %rdi
leaq 0xabca(%rip), %rsi      # 0xe743
movq %r14, %rdx
xorl %eax, %eax
callq 0x10f0
leaq 0xa484(%rip), %rdi      # 0xe00e
callq 0x814d