// File: /workspace/github_c_makefile_star2_O3/Ihsoh[P]ilang/compiler/frontend/irgen_llvmir.c
// Function: ir_expr_logic
static bool _ir_expr_logic(
	IRGeneratorLlvmirContext *ctx,
	ResizableString *rstr,
	_ExprResult *expr_result,
	ParserASTNode *node
) {
	assert(ctx);
	assert(rstr);
	assert(expr_result);
	assert(node);

	switch (node->type) {
		case FE_NODE_EXPR_AND:
		case FE_NODE_EXPR_OR: {
			assert(node->nchilds == 2);

			ParserASTNode *node_type = FE_EXPR_AST_NODE_GET_TYPE_NODE(node);
			
			ResizableString rstr_type;
			rstr_init(&rstr_type);
			_ir_type(ctx, &rstr_type, node_type);

			_TMP_LABEL_DEF(ctx, lhs)
			_TMP_LABEL_DEF(ctx, rhs)
			_TMP_LABEL_DEF(ctx, rhs1)
			_TMP_LABEL_DEF(ctx, end)
			_TMP_LABEL_DEF(ctx, result)
			_TMP_LABEL_DEF(ctx, result_casted)

			ParserASTNode *node_lhs = node->childs[0];
			ParserASTNode *node_rhs = node->childs[1];

			_ExprResult result_lhs;
			_expr_result_init(&result_lhs);

			_ExprResult result_rhs;
			_expr_result_init(&result_rhs);

			_ir_expr(ctx, rstr, &result_lhs, node_lhs);
			rstr_append_with_rstr(rstr, &(result_lhs.rstr_for_result_ptr));
			rstr_append_with_rstr(rstr, &(result_lhs.rstr_for_result));

			ResizableString rstr_lhs_cond;
			rstr_init(&rstr_lhs_cond);
			_ir_cond_expr_with_rstr(ctx, rstr, &rstr_lhs_cond, &result_lhs, node_lhs);

			// 左表达式的Basic Block的标号。
			rstr_appendf(
				rstr,
				"br label %s\n%s:\n",
				_TMP_LABEL_CSTR(lhs),
				_TMP_LABEL_NAME_CSTR(lhs)
			);

			if (node->type == FE_NODE_EXPR_AND) {
				rstr_appendf(
					rstr,
					"br i1 %s, label %s, label %s\n",
					RSTR_CSTR(&rstr_lhs_cond),
					_TMP_LABEL_CSTR(rhs),
					_TMP_LABEL_CSTR(end)
				);
			} else if (node->type == FE_NODE_EXPR_OR) {
				rstr_appendf(
					rstr,
					"br i1 %s, label %s, label %s\n",
					RSTR_CSTR(&rstr_lhs_cond),
					_TMP_LABEL_CSTR(end),
					_TMP_LABEL_CSTR(rhs)
				);
			} else {
				assert(0);
			}

			// 右表达式的Basic Block的标号。
			rstr_appendf(
				rstr,
				"br label %s\n%s:\n",
				_TMP_LABEL_CSTR(rhs),
				_TMP_LABEL_NAME_CSTR(rhs)
			);

			_ir_expr(ctx, rstr, &result_rhs, node_rhs);
			rstr_append_with_rstr(rstr, &(result_rhs.rstr_for_result_ptr));
			rstr_append_with_rstr(rstr, &(result_rhs.rstr_for_result));

			ResizableString rstr_rhs_cond;
			rstr_init(&rstr_rhs_cond);
			_ir_cond_expr_with_rstr(ctx, rstr, &rstr_rhs_cond, &result_rhs, node_rhs);

			rstr_appendf(
				rstr,
				"br label %s\n%s:\n",
				_TMP_LABEL_CSTR(rhs1),
				_TMP_LABEL_NAME_CSTR(rhs1)
			);

			// 结束的Basic Block的标号。
			rstr_appendf(
				rstr,
				"br label %s\n%s:\n",
				_TMP_LABEL_CSTR(end),
				_TMP_LABEL_NAME_CSTR(end)
			);

			if (node->type == FE_NODE_EXPR_AND) {
				rstr_appendf(
					&(expr_result->rstr_for_result),
					"%s = phi i1 [ false, %s ], [ %s, %s ]\n",
					_TMP_LABEL_CSTR(result),
					_TMP_LABEL_CSTR(lhs),
					RSTR_CSTR(&rstr_rhs_cond),
					_TMP_LABEL_CSTR(rhs1)
				);
			} else if (node->type == FE_NODE_EXPR_OR) {
				rstr_appendf(
					&(expr_result->rstr_for_result),
					"%s = phi i1 [ true, %s ], [ %s, %s ]\n",
					_TMP_LABEL_CSTR(result),
					_TMP_LABEL_CSTR(lhs),
					RSTR_CSTR(&rstr_rhs_cond),
					_TMP_LABEL_CSTR(rhs1)
				);
			} else {
				assert(0);
			}

			rstr_appendf(
				&(expr_result->rstr_for_result),
				"%s = zext i1 %s to %s\n",
				_TMP_LABEL_CSTR(result_casted),
				_TMP_LABEL_CSTR(result),
				RSTR_CSTR(&rstr_type)
			);
			_expr_result_set_result(
				expr_result,
				RSTR_CSTR(&rstr_type),
				_TMP_LABEL_CSTR(result_casted)
			);

			rstr_free(&rstr_type);

			_TMP_LABEL_FREE(lhs)
			_TMP_LABEL_FREE(rhs)
			_TMP_LABEL_FREE(rhs1)
			_TMP_LABEL_FREE(end)
			_TMP_LABEL_FREE(result)
			_TMP_LABEL_FREE(result_casted)

			_expr_result_free(&result_lhs);
			_expr_result_free(&result_rhs);

			rstr_free(&rstr_lhs_cond);

			return true;
		}
		default: {
			return false;
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1a8, %rsp            # imm = 0x1A8
movl (%rcx), %eax
orl $0x10, %eax
cmpl $0xe30, %eax            # imm = 0xE30
jne 0x1d19d
movq %rdx, -0x60(%rbp)
cmpl $0x2, 0x44(%rcx)
jne 0x1d3e6
movq %rsi, %r15
movq %rdi, %r12
movq 0x68(%rcx), %rbx
leaq -0xd0(%rbp), %r13
movq %r13, %rdi
movq %rcx, %r14
movq %rcx, -0x30(%rbp)
callq 0x7354
movq %r13, %rdi
movq %rbx, %rsi
callq 0x19540
leaq -0x80(%rbp), %rbx
movq %rbx, %rdi
callq 0x7354
movq 0x40(%r12), %rdi
movq %rbx, %rsi
callq 0x1df8d
leaq -0x50(%rbp), %rbx
movq %rbx, %rdi
callq 0x7354
movq 0x40(%r12), %rdi
movq %rbx, %rsi
callq 0x1df8d
leaq -0x70(%rbp), %rbx
movq %rbx, %rdi
callq 0x7354
movq 0x40(%r12), %rdi
movq %rbx, %rsi
callq 0x1df8d
leaq -0x40(%rbp), %rbx
movq %rbx, %rdi
callq 0x7354
movq 0x40(%r12), %rdi
movq %rbx, %rsi
callq 0x1df8d
leaq -0xc0(%rbp), %rbx
movq %rbx, %rdi
callq 0x7354
movq 0x40(%r12), %rdi
movq %rbx, %rsi
callq 0x1df8d
leaq -0xb0(%rbp), %rbx
movq %rbx, %rdi
callq 0x7354
movq 0x40(%r12), %rdi
movq %rbx, %rsi
callq 0x1df8d
movq 0x48(%r14), %rax
movq (%rax), %rbx
movq 0x8(%rax), %rax
movq %rax, -0x58(%rbp)
leaq -0x1d0(%rbp), %r13
movq %r13, %rdi
callq 0x19b1c
leaq -0x150(%rbp), %rdi
callq 0x19b1c
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x1a9a8
leaq -0x1c0(%rbp), %rsi
movq %r15, %rdi
callq 0x769e
movq %r15, %rdi
movq %r13, %rsi
callq 0x769e
leaq -0x90(%rbp), %r14
movq %r14, %rdi
callq 0x7354
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %r13, %rcx
movq %rbx, %r8
callq 0x1fdad
leaq -0x80(%rbp), %rax
movq 0x8(%rax), %rdx
testq %rdx, %rdx
leaq 0x25dbf(%rip), %rbx     # 0x42f06
cmoveq %rbx, %rdx
leaq 0x1(%rdx), %rcx
leaq 0x28c3d(%rip), %rsi     # 0x45d93
movq %r15, %rdi
xorl %eax, %eax
callq 0x7945
movq -0x30(%rbp), %rax
movl (%rax), %ecx
cmpl $0xe30, %ecx            # imm = 0xE30
je 0x1d1a4
leaq -0x38(%rbp), %rax
cmpl $0xe20, %ecx            # imm = 0xE20
leaq -0x48(%rbp), %rsi
je 0x1d1ac
leaq 0x221db(%rip), %rdi     # 0x3f360
leaq 0x277b8(%rip), %rsi     # 0x44944
leaq 0x28b9c(%rip), %rcx     # 0x45d2f
movl $0x10b1, %edx           # imm = 0x10B1
callq 0x30a0
xorl %eax, %eax
jmp 0x1d3d4
leaq -0x48(%rbp), %rax
leaq -0x38(%rbp), %rsi
movq -0x88(%rbp), %rdx
testq %rdx, %rdx
cmoveq %rbx, %rdx
movq (%rax), %rcx
testq %rcx, %rcx
cmoveq %rbx, %rcx
movq (%rsi), %r8
testq %r8, %r8
cmoveq %rbx, %r8
leaq 0x28bcf(%rip), %rsi     # 0x45da4
movq %r15, %rdi
xorl %eax, %eax
callq 0x7945
movq -0x48(%rbp), %rdx
testq %rdx, %rdx
cmoveq %rbx, %rdx
leaq 0x1(%rdx), %rcx
leaq 0x28b9e(%rip), %rsi     # 0x45d93
movq %r15, %rdi
xorl %eax, %eax
callq 0x7945
leaq -0x150(%rbp), %r14
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq -0x58(%rbp), %rbx
movq %rbx, %rcx
callq 0x1a9a8
leaq -0x140(%rbp), %rsi
movq %r15, %rdi
callq 0x769e
movq %r15, %rdi
movq %r14, %rsi
callq 0x769e
leaq -0xa0(%rbp), %r13
movq %r13, %rdi
callq 0x7354
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
movq %r14, %rcx
movq %rbx, %r8
leaq 0x25cac(%rip), %rbx     # 0x42f06
callq 0x1fdad
movq -0x68(%rbp), %rdx
testq %rdx, %rdx
cmoveq %rbx, %rdx
leaq 0x1(%rdx), %rcx
movq %r15, %rdi
leaq 0x28b1b(%rip), %r14     # 0x45d93
movq %r14, %rsi
xorl %eax, %eax
callq 0x7945
movq -0x38(%rbp), %rdx
testq %rdx, %rdx
cmoveq %rbx, %rdx
leaq 0x1(%rdx), %rcx
movq %r15, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x7945
movq -0x30(%rbp), %rax
movl (%rax), %eax
cmpl $0xe30, %eax            # imm = 0xE30
je 0x1d2bf
cmpl $0xe20, %eax            # imm = 0xE20
jne 0x1d405
leaq 0x28b2c(%rip), %rsi     # 0x45de9
jmp 0x1d2c6
leaq 0x28afc(%rip), %rsi     # 0x45dc2
leaq -0xc0(%rbp), %r14
movq 0x8(%r14), %rdx
testq %rdx, %rdx
cmoveq %rbx, %rdx
leaq -0x80(%rbp), %rax
movq 0x8(%rax), %rcx
testq %rcx, %rcx
cmoveq %rbx, %rcx
movq -0x98(%rbp), %r8
testq %r8, %r8
cmoveq %rbx, %r8
leaq -0x70(%rbp), %rax
movq 0x8(%rax), %r9
testq %r9, %r9
cmoveq %rbx, %r9
movq -0x60(%rbp), %r15
movq %r15, %rdi
xorl %eax, %eax
callq 0x7945
leaq -0xb0(%rbp), %r12
movq 0x8(%r12), %rdx
testq %rdx, %rdx
cmoveq %rbx, %rdx
movq 0x8(%r14), %rcx
testq %rcx, %rcx
cmoveq %rbx, %rcx
leaq -0xd0(%rbp), %r13
movq 0x8(%r13), %r8
testq %r8, %r8
cmoveq %rbx, %r8
leaq 0x2805a(%rip), %rsi     # 0x453a3
movq %r15, %rdi
xorl %eax, %eax
callq 0x7945
movq 0x8(%r13), %rsi
testq %rsi, %rsi
cmoveq %rbx, %rsi
movq 0x8(%r12), %rdx
testq %rdx, %rdx
cmoveq %rbx, %rdx
movq %r15, %rdi
callq 0x1def5
movq %r13, %rdi
callq 0x7852
leaq -0x80(%rbp), %rdi
callq 0x7852
leaq -0x50(%rbp), %rdi
callq 0x7852
leaq -0x70(%rbp), %rdi
callq 0x7852
leaq -0x40(%rbp), %rdi
callq 0x7852
movq %r14, %rdi
callq 0x7852
movq %r12, %rdi
callq 0x7852
leaq -0x1d0(%rbp), %rdi
callq 0x1a3fd
leaq -0x150(%rbp), %rdi
callq 0x1a3fd
leaq -0x90(%rbp), %rdi
callq 0x7852
movb $0x1, %al
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2d908(%rip), %rdi     # 0x4acf5
leaq 0x27550(%rip), %rsi     # 0x44944
leaq 0x28934(%rip), %rcx     # 0x45d2f
movl $0x1078, %edx           # imm = 0x1078
callq 0x30a0
leaq 0x21f54(%rip), %rdi     # 0x3f360
leaq 0x27531(%rip), %rsi     # 0x44944
leaq 0x28915(%rip), %rcx     # 0x45d2f
movl $0x10e6, %edx           # imm = 0x10E6
callq 0x30a0