// File: /workspace/github_c_makefile_star2_O3/Ihsoh[P]ilang/compiler/assembler/ins_enc.c
// Function: ins_enc_xchg_rAX_XXX
void ins_enc_xchg_rAX_XXX(
	Instruction *ins,
	InstructionEncoderData *data
) {
	_CHECK_PARAMS(2);

	ParserASTNode *ins_node = data->ins_node;

	ParserASTNode *target = data->ins_node->childs[0];

	ParserASTNode *source = data->ins_node->childs[1];

	ins_check_operand_type(data->ctx, ins, target, source, NULL);

	EncoderInstruction enc_ins;

	ins_init(data->ctx, ins, ins_node, &enc_ins);

	InsRegister *ins_reg_target = ASM_REG_AST_NODE_GET_REG(target);
	InsRegister *ins_reg_source = ASM_REG_AST_NODE_GET_REG(source);
	InsRegister *ins_reg_another = NULL;
	if (ins_reg_target->index == INS_REGISTER_INDEX_AX) {
		ins_reg_another = ins_reg_source;
	} else {
		ins_reg_another = ins_reg_target;
	}

	int arch = ASM_PARSER_CONTEXT_DATA_GET_ARCH(data->ctx);

	switch (arch) {
		case ASM_ARCH_BIT16: {
			if (!(ins_reg_another->arch & ASM_ARCH_BIT16)) {
				data->ctx->syntax_error_node_msg(
					data->ctx,
					ins_node,
					"instruction not supported in 16-bit mode."
				);
			}

			if (ins_reg_another->type == INS_REGISTER_GENERAL_2BYTE) {

			} else if (ins_reg_another->type == INS_REGISTER_GENERAL_4BYTE) {
				enc_ins.legacy_prefix.operand_size_override = true;
			} else {
				data->ctx->syntax_error_node_msg(
					data->ctx,
					ins_node,
					"instruction not supported in 16-bit mode."
				);
			}
			break;
		}
		case ASM_ARCH_BIT32: {
			if (!(ins_reg_another->arch & ASM_ARCH_BIT32)) {
				data->ctx->syntax_error_node_msg(
					data->ctx,
					ins_node,
					"instruction not supported in 32-bit mode."
				);
			}

			if (ins_reg_another->type == INS_REGISTER_GENERAL_2BYTE) {
				enc_ins.legacy_prefix.operand_size_override = true;
			} else if (ins_reg_another->type == INS_REGISTER_GENERAL_4BYTE) {

			} else {
				data->ctx->syntax_error_node_msg(
					data->ctx,
					ins_node,
					"instruction not supported in 32-bit mode."
				);
			}
			break;
		}
		case ASM_ARCH_BIT64: {
			if (!(ins_reg_another->arch & ASM_ARCH_BIT64)) {
				data->ctx->syntax_error_node_msg(
					data->ctx,
					ins_node,
					"instruction not supported in 64-bit mode."
				);
			}

			if (ins_reg_another->index & 0x08) {
				enc_ins.rex_prefix_used = true;
				enc_ins.rex_prefix.r = true;
			}

			if (ins_reg_another->type == INS_REGISTER_GENERAL_2BYTE) {
				enc_ins.legacy_prefix.operand_size_override = true;
			} else if (ins_reg_another->type == INS_REGISTER_GENERAL_4BYTE) {

			} else if (ins_reg_another->type == INS_REGISTER_GENERAL_8BYTE) {
				
			} else {
				data->ctx->syntax_error_node_msg(
					data->ctx,
					ins_node,
					"instruction not supported in 64-bit mode."
				);
			}
			break;
		}
		default: {
			assert(0);
			break;
		}
	}

	_output(data, &enc_ins);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
testq %rdi, %rdi
je 0x1931b
movq %rsi, %rbx
testq %rsi, %rsi
je 0x19324
movq (%rbx), %r14
testq %r14, %r14
je 0x1932d
cmpl $0x2, 0x44(%r14)
jne 0x19336
movq %rdi, %r15
movq 0x48(%r14), %rax
movq (%rax), %r13
movq 0x8(%rax), %r12
movq 0x8(%rbx), %rdi
movq %r15, %rsi
movq %r13, %rdx
movq %r12, %rcx
xorl %r8d, %r8d
callq 0x16741
movq 0x8(%rbx), %rdi
leaq -0x88(%rbp), %rcx
movq %r15, %rsi
movq %r14, %rdx
callq 0x1524f
movq 0x60(%r13), %r15
cmpl $0x0, 0x10(%r15)
jne 0x19233
movq 0x60(%r12), %r15
movq 0x8(%rbx), %rdi
movl 0xb0(%rdi), %eax
cmpl $0x4, %eax
je 0x192b2
cmpl $0x2, %eax
je 0x19283
cmpl $0x1, %eax
jne 0x19355
testb $0x1, 0x14(%r15)
jne 0x19264
leaq 0x18bf2(%rip), %rdx     # 0x31e50
movq %r14, %rsi
callq *0x58(%rdi)
movl 0xc(%r15), %eax
cmpl $0x3, %eax
je 0x192fd
cmpl $0x4, %eax
je 0x192e6
movq 0x8(%rbx), %rdi
leaq 0x18bcf(%rip), %rdx     # 0x31e50
jmp 0x192f7
testb $0x2, 0x14(%r15)
jne 0x19297
leaq 0x18be9(%rip), %rdx     # 0x31e7a
movq %r14, %rsi
callq *0x58(%rdi)
movl 0xc(%r15), %eax
cmpl $0x4, %eax
je 0x192fd
cmpl $0x3, %eax
je 0x192e6
movq 0x8(%rbx), %rdi
leaq 0x18bca(%rip), %rdx     # 0x31e7a
jmp 0x192f7
testb $0x4, 0x14(%r15)
jne 0x192c6
leaq 0x18cc2(%rip), %rdx     # 0x31f82
movq %r14, %rsi
callq *0x58(%rdi)
testb $0x8, 0x10(%r15)
je 0x192d5
movb $0x1, %al
movb %al, -0x7b(%rbp)
movb %al, -0x79(%rbp)
movl 0xc(%r15), %eax
leal -0x4(%rax), %ecx
cmpl $0x2, %ecx
jb 0x192fd
cmpl $0x3, %eax
jne 0x192ec
movb $0x1, -0x7d(%rbp)
jmp 0x192fd
movq 0x8(%rbx), %rdi
leaq 0x18c8b(%rip), %rdx     # 0x31f82
movq %r14, %rsi
callq *0x58(%rdi)
leaq -0x88(%rbp), %rsi
movq %rbx, %rdi
callq 0x17e70
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x17408(%rip), %rdi     # 0x3072a
jmp 0x1933d
leaq 0x1d230(%rip), %rdi     # 0x3655b
jmp 0x1933d
leaq 0x1896d(%rip), %rdi     # 0x31ca1
jmp 0x1933d
leaq 0x18973(%rip), %rdi     # 0x31cb0
leaq 0x18900(%rip), %rsi     # 0x31c44
leaq 0x18ecd(%rip), %rcx     # 0x32218
movl $0x2ec, %edx            # imm = 0x2EC
callq 0xa0b0
leaq 0x15014(%rip), %rdi     # 0x2e370
leaq 0x188e1(%rip), %rsi     # 0x31c44
leaq 0x18eae(%rip), %rcx     # 0x32218
movl $0x350, %edx            # imm = 0x350
callq 0xa0b0