#include "backend/x86/bal_x86_assembler.h"
#include "bal_assert.h"
#include <stdbool.h>
#include <string.h>

#ifndef NDEBUG

// Strings MUST match the enum value order in bal_x86_macro_t.
static const char *const BAL_X86_CONDITION_CODES[] = {
    "o", "no", "b", "ae", "e", "ne", "be", "a", "s", "ns", "p", "np", "l", "ge", "le", "g",
};

static inline const char *
condition_code_to_string(const bal_x86_condition_t condition)
{
    const size_t num_names = sizeof(BAL_X86_CONDITION_CODES) / sizeof(BAL_X86_CONDITION_CODES[0]);

    if ((size_t)condition < num_names && BAL_X86_CONDITION_CODES[condition] != NULL)
    {
        return BAL_X86_CONDITION_CODES[condition];
    }

    return "???";
}

#endif // NDEBUG

BAL_HOT static bool is_valid_register(int reg);
BAL_HOT static bool can_emit(bal_x86_assembler_t *assembler, size_t size);
BAL_HOT static void emit8(uint8_t *buffer, size_t *offset, uint8_t value);
BAL_HOT static void emit32(uint8_t *buffer, size_t *offset, uint32_t value);
BAL_HOT static void emit64(uint8_t *buffer, size_t *offset, uint64_t value);

/// Emits the REX prefix.
/// w = 1 for 64-bit operands.
/// r = extension for the ModR/W `reg` field.
/// b = extension for the ModR/W `r/m` field or opcode register.
BAL_HOT static void emit_rex(uint8_t *buffer, size_t *offset, uint8_t w, uint8_t r, uint8_t b);

/// Emits the ModR/M byte for Register-to-Register operations.
BAL_HOT static void emit_modrm_register(uint8_t *buffer, size_t *offset, int reg, int rm);

/// Emits the ModR/M byte for Memory Addressing: [RBP + disp32].
BAL_HOT static void emit_modrm_memory_disp32_rbp(uint8_t *buffer, size_t *offset, int reg);

/// Emits `REX.W C1 /opcode_extension imm8` for 64-bit shifts and rotates.
BAL_HOT static void emit_shift_r64_imm8(bal_x86_assembler_t *assembler,
                                        bal_x86_register_t   destination,
                                        uint8_t              immediate,
                                        uint8_t              opcode_extension,
                                        const char          *mnemonic);

bal_error_t
bal_x86_assembler_init(bal_x86_assembler_t          *assembler,
                       const bal_executable_buffer_t executable_buffer,
                       const size_t                  size)
{
    const bal_error_t error = BAL_ERROR_INVALID_ARGUMENT;

    if (BAL_UNLIKELY(NULL == assembler))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: Assembler is NULL");
        return error;
    }

    if (BAL_UNLIKELY(NULL == executable_buffer.rx_pointer)
        || BAL_UNLIKELY(NULL == executable_buffer.rw_pointer))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: Buffer is NULL");
        return error;
    }

    if (BAL_UNLIKELY(0 == size))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: Size is 0");
        return error;
    }

    // WARNING: Assumes executable_buffer.rw_pointer represents a valid writable host memory
    // allocation of at least `size` bytes.
    assembler->buffer = (uint8_t *)executable_buffer.rw_pointer;

    // WARNING: Assumes executable_buffer.rx_pointer represents a valid executable host memory
    // allocation of at least `size` bytes.
    assembler->rx_buffer = (uint8_t *)executable_buffer.rx_pointer;
    assembler->capacity  = size;
    assembler->offset    = 0;
    assembler->status    = BAL_SUCCESS;

    BAL_LOG_INFO(&bal_thread_logger,
                 "x86 Assembler initialized. RW Buffer: %p, RX Buffer: %p, Capacity: %zu bytes",
                 executable_buffer.rw_pointer,
                 executable_buffer.rx_pointer,
                 size);
    return BAL_SUCCESS;
}

void
bal_x86_assembler_reset(bal_x86_assembler_t *assembler)
{
    if (BAL_UNLIKELY(NULL == assembler))
    {
        return;
    }

    assembler->offset = 0;
    assembler->status = BAL_SUCCESS;
    (void)memset(assembler->buffer, 0, assembler->capacity);
    (void)memset(assembler->rx_buffer, 0, assembler->capacity);
}

void
bal_x86_emit_add_mem64_rbp_offset_imm(bal_x86_assembler_t *assembler,
                                      const int32_t        offset,
                                      const int32_t        immediate)
{
    if (BAL_UNLIKELY(NULL == assembler))
    {
        return;
    }

    if (BAL_UNLIKELY(assembler->status != BAL_SUCCESS))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: assembler status != BAL_SUCCESS");
        return;
    }

    const bool   fit_in_8_bits          = (immediate >= -128) & (immediate <= 127);
    const size_t instruction_size_bytes = true == fit_in_8_bits ? 8 : 11;
    const bool   can_emit_status        = can_emit(assembler, instruction_size_bytes);

    if (BAL_UNLIKELY(false == can_emit_status))
    {
        return;
    }

    BAL_LOG_DEBUG(&bal_thread_logger,
                  "[0x%04zx] add qword ptr [rbp + 0x%X], 0x%X",
                  assembler->offset,
                  offset,
                  immediate);

    const uint8_t w          = 1;
    const uint8_t r          = 0;
    const uint8_t b          = 0;
    const uint8_t opcode     = fit_in_8_bits ? 0x83 : 0x81;
    const size_t  old_offset = assembler->offset;
    emit_rex(assembler->buffer, &assembler->offset, w, r, b);
    emit8(assembler->buffer, &assembler->offset, opcode);
    emit_modrm_memory_disp32_rbp(assembler->buffer, &assembler->offset, BAL_X86_RAX);

    // WARNING: Bit pattern of int32_t offset is preserved.
    emit32(assembler->buffer, &assembler->offset, (uint32_t)offset);

    if (true == fit_in_8_bits)
    {
        // WARNING: Cast is safe because it is only reached if fit_in_bits is true.
        emit8(assembler->buffer, &assembler->offset, (uint8_t)immediate);
    }
    else
    {
        // WARNING: Bit pattern of int32_t immediate is preserved.
        emit32(assembler->buffer, &assembler->offset, (uint32_t)immediate);
    }

    const size_t bytes_emitted = assembler->offset - old_offset;
    BAL_ASSERT_MSG(bytes_emitted == instruction_size_bytes,
                   "Bytes emitted %d does not match instruction size %d",
                   bytes_emitted,
                   instruction_size_bytes);
}

void
bal_x86_emit_and_r64_r64(bal_x86_assembler_t     *assembler,
                         const bal_x86_register_t destination,
                         const bal_x86_register_t source)
{
    if (BAL_UNLIKELY(NULL == assembler))
    {
        return;
    }

    if (BAL_UNLIKELY(assembler->status != BAL_SUCCESS))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: assembler status != BAL_SUCCESS");
        return;
    }

    bool is_valid_register_result = is_valid_register(destination);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Invalid destination register: %d", destination);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    is_valid_register_result = is_valid_register(source);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Invalid source register: %d", destination);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    const size_t instruction_size_bytes = 3;

    const bool can_emit_status = can_emit(assembler, instruction_size_bytes);

    if (BAL_UNLIKELY(false == can_emit_status))
    {
        return;
    }

    BAL_LOG_DEBUG(
        &bal_thread_logger, "[0x%04zx] and r%d, r%d", assembler->offset, destination, source);

    const uint8_t w = 1;

    // WARNING: Destination is verified by is_valid_register() to fall within the safe enum range
    // [0, 15].
    const uint8_t r = (uint8_t)destination >> 3;

    // WARNING: Source is verified by is_valid_register() to fall within the safe enum range
    // [0, 15].
    const uint8_t b = (uint8_t)source >> 3;

    const uint8_t opcode     = 0x23;
    const size_t  old_offset = assembler->offset;
    emit_rex(assembler->buffer, &assembler->offset, w, r, b);
    emit8(assembler->buffer, &assembler->offset, opcode);
    emit_modrm_register(assembler->buffer, &assembler->offset, destination, source);
    const size_t bytes_emitted = assembler->offset - old_offset;
    BAL_ASSERT_MSG(bytes_emitted == instruction_size_bytes,
                   "Bytes emitted %d does not match instruction size %d",
                   bytes_emitted,
                   instruction_size_bytes);
}

void
bal_x86_emit_call_r64(bal_x86_assembler_t *assembler, bal_x86_register_t reg)
{
    if (BAL_UNLIKELY(NULL == assembler))
    {
        return;
    }

    if (BAL_UNLIKELY(assembler->status != BAL_SUCCESS))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: assembler status != BAL_SUCCESS");
        return;
    }

    const bool is_valid_register_result = is_valid_register(reg);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Invalid register: %d", reg);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    size_t instruction_size_bytes = 2U;

    if (reg > 7)
    {
        instruction_size_bytes = 3U;
    }

    const bool can_emit_status = can_emit(assembler, instruction_size_bytes);

    if (BAL_UNLIKELY(false == can_emit_status))
    {
        return;
    }

    BAL_LOG_DEBUG(&bal_thread_logger, "[+0x%04zx] call r%d", assembler->offset, reg);
    const size_t old_offset = assembler->offset;

    if (reg > 7)
    {
        const uint8_t w = 0U;
        const uint8_t r = 0U;
        const uint8_t b = (uint8_t)reg >> 3;
        emit_rex(assembler->buffer, &assembler->offset, w, r, b);
    }

    emit8(assembler->buffer, &assembler->offset, 0xFFU);
    const uint8_t safe_reg = (uint8_t)reg & 7;
    const uint8_t modrm    = (uint8_t)(0xC0U | (2U << 3U) | safe_reg);
    emit8(assembler->buffer, &assembler->offset, modrm);
    const size_t bytes_emitted = assembler->offset - old_offset;
    BAL_ASSERT_MSG(bytes_emitted == instruction_size_bytes,
                   "Bytes emitted %d does not match instruction size %d",
                   bytes_emitted,
                   instruction_size_bytes);
}

void
bal_x86_emit_jmp_r64(bal_x86_assembler_t *assembler, const bal_x86_register_t reg)
{
    if (BAL_UNLIKELY(NULL == assembler))
    {
        return;
    }

    if (BAL_UNLIKELY(assembler->status != BAL_SUCCESS))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: assembler status != BAL_SUCCESS");
        return;
    }

    const bool is_valid_register_result = is_valid_register(reg);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Invalid register: %d", reg);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    size_t     instruction_size_bytes = 2U;

    if (reg > 7)
    {
        instruction_size_bytes = 3U;
    }

    const bool can_emit_status = can_emit(assembler, instruction_size_bytes);

    if (BAL_UNLIKELY(false == can_emit_status))
    {
        return;
    }

    BAL_LOG_DEBUG(&bal_thread_logger, "[+0x%04zx] jmp r%d", assembler->offset, reg);
    const size_t old_offset = assembler->offset;

    if (reg > 7)
    {
        const uint8_t w = 0;
        const uint8_t r = 0;

        // WARNING: reg is verified by is_valid_register() to fall within the safe enum range.
        const uint8_t b = (uint8_t)reg >> 3;
        emit_rex(assembler->buffer, &assembler->offset, w, r, b);
    }

    const uint8_t opcode = 0xFF;
    emit8(assembler->buffer, &assembler->offset, opcode);

    const uint8_t safe_reg = (uint8_t)reg & 7;
    const uint8_t modrm    = (uint8_t)(0xC0U | 4U << 3U | safe_reg);
    emit8(assembler->buffer, &assembler->offset, modrm);
    const size_t bytes_emitted = assembler->offset - old_offset;
    BAL_ASSERT_MSG(bytes_emitted == instruction_size_bytes,
                   "Bytes emitted %d does not match instruction size %d",
                   bytes_emitted,
                   instruction_size_bytes);
}

void
bal_x86_emit_jmp_rel32(bal_x86_assembler_t *assembler, const int32_t offset)
{
    if (BAL_UNLIKELY(NULL == assembler))
    {
        return;
    }

    if (BAL_UNLIKELY(assembler->status != BAL_SUCCESS))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: assembler status != BAL_SUCCESS");
        return;
    }

    const size_t instruction_size_bytes = 5;
    const bool   can_emit_status        = can_emit(assembler, instruction_size_bytes);

    if (BAL_UNLIKELY(false == can_emit_status))
    {
        return;
    }

    BAL_LOG_DEBUG(&bal_thread_logger, "[+0x%04zx] jmp 0x%X", assembler->offset, offset);
    const size_t  old_offset = assembler->offset;
    const uint8_t opcode     = 0xE9;
    emit8(assembler->buffer, &assembler->offset, opcode);

    // WARNING: Bit pattern of int32_t offset is preserved in uint32_t.
    emit32(assembler->buffer, &assembler->offset, (uint32_t)offset);

    const size_t bytes_emitted = assembler->offset - old_offset;
    BAL_ASSERT_MSG(bytes_emitted == instruction_size_bytes,
                   "Bytes emitted %d does not match instruction size %d",
                   bytes_emitted,
                   instruction_size_bytes);
}

void
bal_x86_emit_load_r64_rbp_offset(bal_x86_assembler_t     *assembler,
                                 const bal_x86_register_t destination,
                                 const int32_t            offset)
{
    if (BAL_UNLIKELY(NULL == assembler))
    {
        return;
    }

    if (BAL_UNLIKELY(assembler->status != BAL_SUCCESS))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: assembler status != BAL_SUCCESS");
        return;
    }

    const bool is_valid_register_result = is_valid_register(destination);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Invalid destination register: %d", destination);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    const size_t instruction_size_bytes = 7;
    const bool   can_emit_status        = can_emit(assembler, instruction_size_bytes);

    if (BAL_UNLIKELY(false == can_emit_status))
    {
        return;
    }

    BAL_LOG_DEBUG(&bal_thread_logger,
                  "[0x%04zx] mov r%d, [rbp + 0x%X]",
                  assembler->offset,
                  destination,
                  offset);

    const uint8_t w = 1;

    // WARNING: Destination is verified by is_valid_register() to fall within the safe enum range
    // [0, 15].
    const uint8_t r = (uint8_t)destination >> 3;

    const uint8_t b          = 0;
    const uint8_t opcode     = 0X8B;
    const size_t  old_offset = assembler->offset;
    emit_rex(assembler->buffer, &assembler->offset, w, r, b);
    emit8(assembler->buffer, &assembler->offset, opcode);
    emit_modrm_memory_disp32_rbp(assembler->buffer, &assembler->offset, destination);

    // WARNING: Bit pattern of int32_t offset is preserved in uint32_t.
    emit32(assembler->buffer, &assembler->offset, (uint32_t)offset);

    const size_t bytes_emitted = assembler->offset - old_offset;
    BAL_ASSERT_MSG(bytes_emitted == instruction_size_bytes,
                   "Bytes emitted %d does not match instruction size %d",
                   bytes_emitted,
                   instruction_size_bytes);
}

void
bal_x86_emit_load_r64_from_r64(bal_x86_assembler_t     *assembler,
                               const bal_x86_register_t destination,
                               const bal_x86_register_t base)
{
    if (BAL_UNLIKELY(NULL == assembler))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: assembler is NULL.");
        return;
    }

    if (BAL_UNLIKELY(assembler->status != BAL_SUCCESS))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: assembler status != BAL_SUCCESS");
        return;
    }

    bool is_valid_register_result = is_valid_register(destination);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(
            &bal_thread_logger, "Aborting function: invalid destination register: %d", destination);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    is_valid_register_result = is_valid_register(base);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: invalid base register: %d", base);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    // WARNING: Verified by is_valid_register() to fall within [0, 15].
    const uint8_t base_low = (uint8_t)base & 7U;

    size_t instruction_size_bytes = 3;
    bool   needs_sib              = false;
    bool   needs_disp8            = false;

    if (4U == base_low)
    {
        needs_sib              = true;
        instruction_size_bytes = 4;
    }
    else if (5U == base_low)
    {
        needs_disp8            = true;
        instruction_size_bytes = 4;
    }

    const bool can_emit_result = can_emit(assembler, instruction_size_bytes);

    if (BAL_UNLIKELY(false == can_emit_result))
    {
        return;
    }

    BAL_LOG_DEBUG(
        &bal_thread_logger, "[0x%04zx] mov r%d, [r%d]", assembler->offset, destination, base);

    const uint8_t w          = 1;
    const uint8_t r          = (uint8_t)destination >> 3;
    const uint8_t b          = (uint8_t)base >> 3;
    const uint8_t opcode     = 0x8BU;
    const size_t  old_offset = assembler->offset;

    emit_rex(assembler->buffer, &assembler->offset, w, r, b);
    emit8(assembler->buffer, &assembler->offset, opcode);
    const uint8_t safe_destination = (uint8_t)destination & 7U;

    if (true == needs_sib)
    {
        const uint8_t modrm = (uint8_t)((safe_destination << 3U) | 4);
        emit8(assembler->buffer, &assembler->offset, modrm);
        const uint8_t sib = 0x24U;
        emit8(assembler->buffer, &assembler->offset, sib);
    }
    else if (true == needs_disp8)
    {
        const uint8_t modrm = (uint8_t)(0x40U | (uint8_t)(safe_destination << 3U) | 5U);
        emit8(assembler->buffer, &assembler->offset, modrm);

        const uint8_t disp8 = 0x00U;
        emit8(assembler->buffer, &assembler->offset, disp8);
    }
    else
    {
        const uint8_t modrm = (uint8_t)((safe_destination << 3U) | base_low);
        emit8(assembler->buffer, &assembler->offset, modrm);
    }

    const size_t bytes_emitted = assembler->offset - old_offset;
    BAL_ASSERT_MSG(bytes_emitted == instruction_size_bytes,
                   "Bytes emitted %zu does not match instruction size %zu",
                   bytes_emitted,
                   instruction_size_bytes);
}

void
bal_x86_emit_store_r64_rbp_offset(bal_x86_assembler_t     *assembler,
                                  const bal_x86_register_t source,
                                  const int32_t            offset)
{
    if (BAL_UNLIKELY(NULL == assembler))
    {
        return;
    }

    if (BAL_UNLIKELY(assembler->status != BAL_SUCCESS))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: assembler status != BAL_SUCCESS");
        return;
    }

    const bool is_valid_register_result = is_valid_register(source);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Invalid source register: %d", source);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    const size_t instruction_size_bytes = 7;
    const bool   can_emit_status        = can_emit(assembler, instruction_size_bytes);

    if (BAL_UNLIKELY(false == can_emit_status))
    {
        return;
    }

    BAL_LOG_DEBUG(
        &bal_thread_logger, "[0x%04zx] mov[rbp + 0x%X], r%d", assembler->offset, offset, source);
    const uint8_t w = 1;

    // WARNING: Source is verified by is_valid_register() to fall within the safe enum range
    // [0, 15].
    const uint8_t r = (uint8_t)source >> 3;

    const uint8_t b          = 0;
    const uint8_t opcode     = 0X89;
    const size_t  old_offset = assembler->offset;
    emit_rex(assembler->buffer, &assembler->offset, w, r, b);
    emit8(assembler->buffer, &assembler->offset, opcode);
    emit_modrm_memory_disp32_rbp(assembler->buffer, &assembler->offset, source);

    // WARNING: Bit pattern of int32_t offset is preserved in uint32_t.
    emit32(assembler->buffer, &assembler->offset, (uint32_t)offset);

    const size_t bytes_emitted = assembler->offset - old_offset;
    BAL_ASSERT_MSG(bytes_emitted == instruction_size_bytes,
                   "Bytes emitted %d does not match instruction size %d",
                   bytes_emitted,
                   instruction_size_bytes);
}

void
bal_x86_emit_mov_r64_r64(bal_x86_assembler_t     *assembler,
                         const bal_x86_register_t destination,
                         const bal_x86_register_t source)
{
    if (BAL_UNLIKELY(NULL == assembler))
    {
        return;
    }

    if (BAL_UNLIKELY(assembler->status != BAL_SUCCESS))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Assembler status != BAL_SUCCESS, aborting function");
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    bool is_valid_register_result = is_valid_register(source);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Invalid source register: %d", source);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    is_valid_register_result = is_valid_register(destination);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Invalid destination register: %d", destination);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    const size_t instruction_size_bytes = 3;
    const bool   can_emit_status        = can_emit(assembler, instruction_size_bytes);

    if (BAL_UNLIKELY(false == can_emit_status))
    {
        return;
    }

    BAL_LOG_DEBUG(
        &bal_thread_logger, "[+0x%04zx] mov r%d, r%d", assembler->offset, destination, source);
    const uint8_t w = 1;

    // WARNING: Destination is verified by is_valid_register() to fall within the safe enum range
    // [0, 15].
    const uint8_t r = (uint8_t)destination >> 3;

    // WARNING: Source is verified by is_valid_register() to fall within the safe enum range
    // [0, 15].
    const uint8_t b = (uint8_t)source >> 3;

    const uint8_t opcode     = 0x8BU;
    const size_t  old_offset = assembler->offset;
    emit_rex(assembler->buffer, &assembler->offset, w, r, b);
    emit8(assembler->buffer, &assembler->offset, opcode);
    emit_modrm_register(assembler->buffer, &assembler->offset, destination, source);
    const size_t bytes_emitted = assembler->offset - old_offset;
    BAL_ASSERT_MSG(bytes_emitted == instruction_size_bytes,
                   "Bytes emitted %d does not match instruction size %d",
                   bytes_emitted,
                   instruction_size_bytes);
}

void
bal_x86_emit_mov_r32_r32(bal_x86_assembler_t *BAL_RESTRICT assembler,
                         const bal_x86_register_t          destination,
                         const bal_x86_register_t          source)
{
    if (BAL_UNLIKELY(NULL == assembler))
    {
        return;
    }

    if (BAL_UNLIKELY(assembler->status != BAL_SUCCESS))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: status != BAL_SUCCESS");
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    bool is_valid_register_result = is_valid_register(source);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(
            &bal_thread_logger, "Aborting function: invalid source register (%d).", source);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    is_valid_register_result = is_valid_register(destination);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(&bal_thread_logger,
                      "Aborting function: invalid destination register: (%d)",
                      destination);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    const size_t instruction_size_bytes = 3U;
    const bool   can_emit_status        = can_emit(assembler, instruction_size_bytes);

    if (BAL_UNLIKELY(false == can_emit_status))
    {
        return;
    }

    BAL_LOG_DEBUG(
        &bal_thread_logger, "[+0x%04zx] mov r%dd, r%dd", assembler->offset, destination, source);

    const uint8_t w          = 0U;
    const uint8_t r          = (uint8_t)destination >> 3;
    const uint8_t b          = (uint8_t)source >> 3;
    const uint8_t opcode     = 0x8BU;
    const size_t  old_offset = assembler->offset;
    emit_rex(assembler->buffer, &assembler->offset, w, r, b);
    emit8(assembler->buffer, &assembler->offset, opcode);
    emit_modrm_register(assembler->buffer, &assembler->offset, destination, source);
    const size_t bytes_emitted = assembler->offset - old_offset;

    BAL_ASSERT_MSG(bytes_emitted == instruction_size_bytes,
                   "Bytes emitted %d does not match instruction size %d",
                   bytes_emitted,
                   instruction_size_bytes);
}

void
bal_x86_emit_mov_r64_imm64(bal_x86_assembler_t     *assembler,
                           const bal_x86_register_t destination,
                           const uint64_t           immediate)
{
    if (BAL_UNLIKELY(NULL == assembler))
    {
        return;
    }

    if (BAL_UNLIKELY(assembler->status != BAL_SUCCESS))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Assembler status != BAL_SUCCESS, aborting function");
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    const bool is_valid_register_result = is_valid_register(destination);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Invalid destination register: %d", destination);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    const bool fits_in_32_bits = immediate <= 0xFFFFFFFFULL;

    // WARNING: Destination is verified by is_valid_register() to fall within the safe enum range
    // [0, 15].
    const uint8_t b = (uint8_t)destination >> 3;
    size_t        instruction_size_bytes;

    if (true == fits_in_32_bits)
    {
        // 1 byte opcode + 4 bytes imm32 = 5 bytes. +1 byte REX if destination is R8-R15.
        instruction_size_bytes = b > 0 ? 6 : 5;
    }
    else
    {
        // 1 byte REX + 1 byte opcode + 8 bytes imm64 = 10 bytes.
        instruction_size_bytes = 10;
    }

    const bool can_emit_status = can_emit(assembler, instruction_size_bytes);

    if (BAL_UNLIKELY(false == can_emit_status))
    {
        return;
    }

    BAL_LOG_DEBUG(&bal_thread_logger,
                  "[+0x%04zx] mov r%d, 0x%llX",
                  assembler->offset,
                  destination,
                  (unsigned long long)immediate);
    const size_t old_offset = assembler->offset;

    if (true == fits_in_32_bits)
    {
        if (b > 0)
        {
            // We only need REX.B if the register is R8-R15. No REX.W needed.
            //
            const uint8_t w = 0;
            const uint8_t r = 0;
            emit_rex(assembler->buffer, &assembler->offset, w, r, b);
        }

        emit8(assembler->buffer, &assembler->offset, 0xB8 + (destination & 7));

        // WARNING: Cast from uint64_t to uint32_t is safe because it is only reached if
        // `fits_in_32_bits` is true.
        emit32(assembler->buffer, &assembler->offset, (uint32_t)immediate);
    }
    else
    {
        const uint8_t w = 1;
        const uint8_t r = 0;
        emit_rex(assembler->buffer, &assembler->offset, w, r, b);
        emit8(assembler->buffer, &assembler->offset, 0xB8 + (destination & 7));
        emit64(assembler->buffer, &assembler->offset, immediate);
    }
    const size_t bytes_emitted = assembler->offset - old_offset;
    BAL_ASSERT_MSG(bytes_emitted == instruction_size_bytes,
                   "Bytes emitted %d does not match instruction size %d",
                   bytes_emitted,
                   instruction_size_bytes);
}

void
bal_x86_emit_ret(bal_x86_assembler_t *assembler)
{
    if (BAL_UNLIKELY(NULL == assembler))
    {
        return;
    }

    if (BAL_UNLIKELY(assembler->status != BAL_SUCCESS))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Assembler status != BAL_SUCCESS, aborting function");
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    const size_t instruction_size_bytes = 1;
    const bool   can_emit_status        = can_emit(assembler, instruction_size_bytes);

    if (BAL_UNLIKELY(false == can_emit_status))
    {
        return;
    }

    BAL_LOG_DEBUG(&bal_thread_logger, "[+0x%04zx] ret", assembler->offset);
    emit8(assembler->buffer, &assembler->offset, 0XC3);
}

void
bal_x86_emit_or_r64_r64(bal_x86_assembler_t     *assembler,
                        const bal_x86_register_t destination,
                        const bal_x86_register_t source)
{
    if (BAL_UNLIKELY(NULL == assembler))
    {
        return;
    }

    if (BAL_UNLIKELY(assembler->status != BAL_SUCCESS))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Assembler status != BAL_SUCCESS, aborting function");
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    bool is_valid_register_result = is_valid_register(source);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Invalid source register: %d", source);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    is_valid_register_result = is_valid_register(destination);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Invalid destination register: %d", destination);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    const size_t instruction_size_bytes = 3;
    const bool   can_emit_status        = can_emit(assembler, instruction_size_bytes);

    if (BAL_UNLIKELY(false == can_emit_status))
    {
        return;
    }

    BAL_LOG_DEBUG(
        &bal_thread_logger, "[+0x%04zx] or r%d, r%d", assembler->offset, destination, source);
    const uint8_t w = 1;

    // WARNING: Destination is verified by is_valid_register() to fall within the safe enum range
    // [0, 15].
    const uint8_t r = (uint8_t)destination >> 3;

    // WARNING: Source is verified by is_valid_register() to fall within the safe enum range
    // [0, 15].
    const uint8_t b = (uint8_t)source >> 3;

    const uint8_t opcode     = 0x0B;
    const size_t  old_offset = assembler->offset;
    emit_rex(assembler->buffer, &assembler->offset, w, r, b);
    emit8(assembler->buffer, &assembler->offset, opcode);
    emit_modrm_register(assembler->buffer, &assembler->offset, destination, source);
    const size_t bytes_emitted = assembler->offset - old_offset;
    BAL_ASSERT_MSG(bytes_emitted == instruction_size_bytes,
                   "Bytes emitted %d does not match instruction size %d",
                   bytes_emitted,
                   instruction_size_bytes);
}

void
bal_x86_emit_ror_r64_imm8(bal_x86_assembler_t     *assembler,
                          const bal_x86_register_t destination,
                          const uint8_t            immediate)
{
    const uint8_t opcode_extension = 1;
    emit_shift_r64_imm8(assembler, destination, immediate, opcode_extension, "ror");
}

void
bal_x86_emit_sar_r64_imm8(bal_x86_assembler_t     *assembler,
                          const bal_x86_register_t destination,
                          const uint8_t            immediate)
{
    const uint8_t opcode_extension = 7;
    emit_shift_r64_imm8(assembler, destination, immediate, opcode_extension, "sar");
}

void
bal_x86_emit_shl_r64_imm8(bal_x86_assembler_t     *assembler,
                          const bal_x86_register_t destination,
                          const uint8_t            immediate)
{
    const uint8_t opcode_extension = 4;
    emit_shift_r64_imm8(assembler, destination, immediate, opcode_extension, "shl");
}

void
bal_x86_emit_shr_r64_imm8(bal_x86_assembler_t     *assembler,
                          const bal_x86_register_t destination,
                          const uint8_t            immediate)
{
    const uint8_t opcode_extension = 5;
    emit_shift_r64_imm8(assembler, destination, immediate, opcode_extension, "shr");
}

void
bal_x86_emit_push_r64(bal_x86_assembler_t *assembler, const bal_x86_register_t reg)
{
    if (BAL_UNLIKELY(NULL == assembler))
    {
        return;
    }

    if (BAL_UNLIKELY(assembler->status != BAL_SUCCESS))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Assembler status != BAL_SUCCESS, aborting function");
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    const bool is_valid_register_result = is_valid_register(reg);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Invalid register: %d", reg);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    size_t instruction_size_bytes = 1U;

    if (reg > 7)
    {
        instruction_size_bytes = 2U;
    }

    const bool can_emit_status = can_emit(assembler, instruction_size_bytes);

    if (BAL_UNLIKELY(false == can_emit_status))
    {
        return;
    }

    BAL_LOG_DEBUG(&bal_thread_logger, "[+0x%04zx] push r%d", assembler->offset, reg);
    const size_t old_offset = assembler->offset;

    if (reg > 7)
    {
        const uint8_t w = 0;
        const uint8_t r = 0;

        // WARNING: reg is verified by is_valid_register() to fall within the safe enum range
        // [0, 15].
        const uint8_t b = (uint8_t)reg >> 3;

        emit_rex(assembler->buffer, &assembler->offset, w, r, b);
    }

    const uint8_t opcode = 0x50;
    emit8(assembler->buffer, &assembler->offset, opcode + (reg & 7));
    const size_t bytes_emitted = assembler->offset - old_offset;
    BAL_ASSERT_MSG(bytes_emitted <= instruction_size_bytes,
                   "Bytes emitted %d does not match instruction size %d",
                   bytes_emitted,
                   instruction_size_bytes);
}

void
bal_x86_emit_pop_r64(bal_x86_assembler_t *assembler, const bal_x86_register_t reg)
{
    if (BAL_UNLIKELY(NULL == assembler))
    {
        return;
    }

    if (BAL_UNLIKELY(assembler->status != BAL_SUCCESS))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Assembler status != BAL_SUCCESS, aborting function");
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    const bool is_valid_register_result = is_valid_register(reg);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Invalid register: %d", reg);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    size_t instruction_size_bytes = 1U;

    if (reg > 7)
    {
        instruction_size_bytes = 2U;
    }

    const bool can_emit_status = can_emit(assembler, instruction_size_bytes);

    if (BAL_UNLIKELY(false == can_emit_status))
    {
        return;
    }

    BAL_LOG_DEBUG(&bal_thread_logger, "[+0x%04zx] pop r%d", assembler->offset, reg);
    const size_t old_offset = assembler->offset;

    if (reg > 7)
    {
        const uint8_t w = 0;
        const uint8_t r = 0;

        // WARNING: reg is verified by is_valid_register() to fall within the safe enum range
        // [0, 15].
        const uint8_t b = (uint8_t)reg >> 3;

        emit_rex(assembler->buffer, &assembler->offset, w, r, b);
    }

    const uint8_t opcode = 0x58;
    emit8(assembler->buffer, &assembler->offset, opcode + (reg & 7));
    const size_t bytes_emitted = assembler->offset - old_offset;
    BAL_ASSERT_MSG(bytes_emitted == instruction_size_bytes,
                   "Bytes emitted %d does not match instruction size %d",
                   bytes_emitted,
                   instruction_size_bytes);
}

void
bal_x86_emit_setcc_mem8_rbp_offset(bal_x86_assembler_t      *assembler,
                                   const bal_x86_condition_t condition,
                                   const int32_t             offset)
{
    if (BAL_UNLIKELY(NULL == assembler))
    {
        return;
    }

    if (BAL_UNLIKELY(assembler->status != BAL_SUCCESS))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Assembler status != BAL_SUCCESS, aborting function");
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    const size_t instruction_size_bytes = 7;
    const bool   can_emit_status        = can_emit(assembler, instruction_size_bytes);

    if (BAL_UNLIKELY(false == can_emit_status))
    {
        return;
    }

    BAL_LOG_DEBUG(&bal_thread_logger,
                  "[+0x%04zx] set%s byte ptr [rbp + 0x%X]",
                  assembler->offset,
                  condition_code_to_string(condition),
                  offset);
    const size_t old_offset = assembler->offset;

    // SETcc is 0x0F 0x90+cc.
    emit8(assembler->buffer, &assembler->offset, 0x0F);

    // WARNING: Cast to uint8_t is safe because bal_x86_condition_t enum values fall within 0x0 to
    // 0xF.
    emit8(assembler->buffer, &assembler->offset, 0x90 + (uint8_t)condition);

    // Opcode extension is 0.
    emit_modrm_memory_disp32_rbp(assembler->buffer, &assembler->offset, BAL_X86_RAX);

    // WARNING: Bit pattern of int32_t offset is preserved.
    emit32(assembler->buffer, &assembler->offset, (uint32_t)offset);

    const size_t bytes_emitted = assembler->offset - old_offset;
    BAL_ASSERT_MSG(bytes_emitted == instruction_size_bytes,
                   "Bytes emitted %d does not match instruction size %d",
                   bytes_emitted,
                   instruction_size_bytes);
}

void
bal_x86_emit_jcc_rel32(bal_x86_assembler_t      *assembler,
                       const bal_x86_condition_t condition,
                       const int32_t             offset)
{
    if (BAL_UNLIKELY(NULL == assembler))
    {
        return;
    }

    if (BAL_UNLIKELY(assembler->status != BAL_SUCCESS))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Assembler status != BAL_SUCCESS, aborting function");
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    const size_t instruction_size_bytes = 6;
    const bool   can_emit_status        = can_emit(assembler, instruction_size_bytes);

    if (BAL_UNLIKELY(false == can_emit_status))
    {
        return;
    }

    BAL_LOG_DEBUG(&bal_thread_logger,
                  "[0x%04zx] j%s 0x%X",
                  assembler->offset,
                  condition_code_to_string(condition),
                  offset);
    const size_t old_offset = assembler->offset;
    emit8(assembler->buffer, &assembler->offset, 0x0F);

    // WARNING: Cast to uint8_t is safe because bal_x86_condition_t enum values fall within 0x0 to
    // 0xF.
    emit8(assembler->buffer, &assembler->offset, 0x80 + (uint8_t)condition);

    // WARNING: Bit pattern of int32_t offset is preserved in uint32_t.
    emit32(assembler->buffer, &assembler->offset, (uint32_t)offset);

    const size_t bytes_emitted = assembler->offset - old_offset;
    BAL_ASSERT_MSG(bytes_emitted == instruction_size_bytes,
                   "Bytes emitted %d does not match instruction size %d",
                   bytes_emitted,
                   instruction_size_bytes);
}

void
bal_x86_emit_add_r64_r64(bal_x86_assembler_t     *assembler,
                         const bal_x86_register_t destination,
                         const bal_x86_register_t source)
{

    if (BAL_UNLIKELY(NULL == assembler))
    {
        return;
    }

    if (BAL_UNLIKELY(assembler->status != BAL_SUCCESS))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: assembler->status != BAL_SUCCESS");
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    bool is_valid_register_result = is_valid_register(source);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: invalid source register: %d", source);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    is_valid_register_result = is_valid_register(destination);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(
            &bal_thread_logger, "Aborting function: invalid destination register: %d", destination);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    const size_t instruction_size_bytes = 3;
    const bool   can_emit_status        = can_emit(assembler, instruction_size_bytes);

    if (BAL_UNLIKELY(false == can_emit_status))
    {
        return;
    }

    BAL_LOG_DEBUG(
        &bal_thread_logger, "[+0x%04zx] add r%d, r%d", assembler->offset, destination, source);
    const uint8_t w = 1;

    // WARNING: Destination is verified by is_valid_register().
    const uint8_t r = (uint8_t)destination >> 3;

    // WARNING: Source is verified by is_valid_register().
    const uint8_t b = (uint8_t)source >> 3;

    const uint8_t opcode     = 0x03;
    const size_t  old_offset = assembler->offset;
    emit_rex(assembler->buffer, &assembler->offset, w, r, b);
    emit8(assembler->buffer, &assembler->offset, opcode);
    emit_modrm_register(assembler->buffer, &assembler->offset, destination, source);
    const size_t bytes_emitted = assembler->offset - old_offset;
    BAL_ASSERT_MSG(bytes_emitted == instruction_size_bytes,
                   "Bytes emitted %d does not match instruction size %d",
                   bytes_emitted,
                   instruction_size_bytes);
}

void
bal_x86_emit_add_r32_r32(bal_x86_assembler_t *assembler,
                         bal_x86_register_t   destination,
                         bal_x86_register_t   source)
{
    if (BAL_UNLIKELY(NULL == assembler))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: assembler is NULL.");
        return;
    }

    if (BAL_UNLIKELY(assembler->status != BAL_SUCCESS))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: assembler->status != BAL_SUCCESS.");
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
    }

    bool is_valid_register_result = is_valid_register(source);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(
            &bal_thread_logger, "Aborting function: invalid source register: %d.", source);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    is_valid_register_result = is_valid_register(destination);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(
            &bal_thread_logger, "Aborting function: invalid destination register: %d", destination);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    const size_t instruction_size_bytes = 3U;
    const bool   can_emit_status        = can_emit(assembler, instruction_size_bytes);

    if (BAL_UNLIKELY(false == can_emit_status))
    {
        return;
    }

    BAL_LOG_DEBUG(
        &bal_thread_logger, "[+0x%04zx] add r%dd, r%dd", assembler->offset, destination, source);

    const uint8_t w          = 0U;
    const uint8_t r          = (uint8_t)destination >> 3U;
    const uint8_t b          = (uint8_t)source >> 3U;
    const uint8_t opcode     = 0x03U;
    const size_t  old_offset = assembler->offset;
    emit_rex(assembler->buffer, &assembler->offset, w, r, b);
    emit8(assembler->buffer, &assembler->offset, opcode);
    emit_modrm_register(assembler->buffer, &assembler->offset, destination, source);
    const size_t bytes_emitted = assembler->offset - old_offset;
    BAL_ASSERT_MSG(bytes_emitted == instruction_size_bytes,
                   "Bytes emitted %d does not match instruction size %d",
                   bytes_emitted,
                   instruction_size_bytes);
}

void
bal_x86_emit_sub_r64_r64(bal_x86_assembler_t     *assembler,
                         const bal_x86_register_t destination,
                         const bal_x86_register_t source)
{

    if (BAL_UNLIKELY(NULL == assembler))
    {
        return;
    }

    if (BAL_UNLIKELY(assembler->status != BAL_SUCCESS))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: assembler->status != BAL_SUCCESS");
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    bool is_valid_register_result = is_valid_register(source);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: invalid source register: %d", source);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    is_valid_register_result = is_valid_register(destination);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(
            &bal_thread_logger, "Aborting function: invalid destination register: %d", destination);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    const size_t instruction_size_bytes = 3;
    const bool   can_emit_status        = can_emit(assembler, instruction_size_bytes);

    if (BAL_UNLIKELY(false == can_emit_status))
    {
        return;
    }

    BAL_LOG_DEBUG(
        &bal_thread_logger, "[+0x%04zx] sub r%d, r%d", assembler->offset, destination, source);
    const uint8_t w = 1;

    // WARNING: Destination is verified by is_valid_register().
    const uint8_t r = (uint8_t)destination >> 3;

    // WARNING: Source is verified by is_valid_register().
    const uint8_t b = (uint8_t)source >> 3;

    const uint8_t opcode     = 0x2B;
    const size_t  old_offset = assembler->offset;
    emit_rex(assembler->buffer, &assembler->offset, w, r, b);
    emit8(assembler->buffer, &assembler->offset, opcode);
    emit_modrm_register(assembler->buffer, &assembler->offset, destination, source);
    const size_t bytes_emitted = assembler->offset - old_offset;
    BAL_ASSERT_MSG(bytes_emitted == instruction_size_bytes,
                   "Bytes emitted %d does not match instruction size %d",
                   bytes_emitted,
                   instruction_size_bytes);
}

void
bal_x86_emit_sub_r32_r32(bal_x86_assembler_t *assembler,
                         bal_x86_register_t   destination,
                         bal_x86_register_t   source)
{
    if (BAL_UNLIKELY(NULL == assembler))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: assembler is NULL.");
        return;
    }

    if (BAL_UNLIKELY(assembler->status != BAL_SUCCESS))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: assembler->status != BAL_SUCCESS.");
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    bool is_valid_register_result = is_valid_register(source);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(
            &bal_thread_logger, "Aborting function: invalid source register: %d.", source);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    is_valid_register_result = is_valid_register(destination);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(
            &bal_thread_logger, "Aborting function: invalid destination register: %d", destination);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    const size_t instruction_size_bytes = 3U;
    const bool   can_emit_status        = can_emit(assembler, instruction_size_bytes);

    if (BAL_UNLIKELY(false == can_emit_status))
    {
        return;
    }

    BAL_LOG_DEBUG(
        &bal_thread_logger, "[+0x%04zx] sub r%d, r%d", assembler->offset, destination, source);

    const uint8_t w          = 0U;
    const uint8_t r          = (uint8_t)destination >> 3U;
    const uint8_t b          = (uint8_t)source >> 3U;
    const uint8_t opcode     = 0x2BU;
    const size_t  old_offset = assembler->offset;
    emit_rex(assembler->buffer, &assembler->offset, w, r, b);
    emit8(assembler->buffer, &assembler->offset, opcode);
    emit_modrm_register(assembler->buffer, &assembler->offset, destination, source);
    const size_t bytes_emitted = assembler->offset - old_offset;
    BAL_ASSERT_MSG(bytes_emitted == instruction_size_bytes,
                   "Bytes emitted %d does not match instruction size %d",
                   bytes_emitted,
                   instruction_size_bytes);
}

void
bal_x86_emit_add_r64_imm32(bal_x86_assembler_t     *assembler,
                           const bal_x86_register_t destination,
                           const int32_t            immediate)
{
    if (BAL_UNLIKELY(NULL == assembler))
    {
        return;
    }

    if (BAL_UNLIKELY(assembler->status != BAL_SUCCESS))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: assembler->status != BAL_SUCCESS");
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    const bool is_valid_register_result = is_valid_register(destination);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(
            &bal_thread_logger, "Aborting function: invalid destination register: %d", destination);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    const bool fit_in_8_bits = immediate >= -128 && immediate < 127;

    // WARNING: Destination is verified by is_valid_register().
    const uint8_t b = (uint8_t)destination >> 3;

    const size_t instruction_size_bytes = true == fit_in_8_bits ? 4 : 7;
    const bool   can_emit_status        = can_emit(assembler, instruction_size_bytes);

    if (BAL_UNLIKELY(false == can_emit_status))
    {
        return;
    }

    BAL_LOG_DEBUG(
        &bal_thread_logger, "[+0x%04zx] add r%d, 0x%X", assembler->offset, destination, immediate);
    const size_t  old_offset = assembler->offset;
    const uint8_t w          = 1;
    const uint8_t r          = 0;
    emit_rex(assembler->buffer, &assembler->offset, w, r, b);
    const uint8_t opcode = true == fit_in_8_bits ? 0x83 : 0x81;
    emit8(assembler->buffer, &assembler->offset, opcode);
    emit_modrm_register(assembler->buffer, &assembler->offset, BAL_X86_RAX, destination);

    if (true == fit_in_8_bits)
    {
        // WARNING: Cast is safe if fit_in_8_bits is true.
        emit8(assembler->buffer, &assembler->offset, (uint8_t)immediate);
    }
    else
    {
        // WARNING: Bit pattern of int32_t is preserved.
        emit32(assembler->buffer, &assembler->offset, (uint32_t)immediate);
    }

    const size_t bytes_emitted = assembler->offset - old_offset;
    BAL_ASSERT_MSG(bytes_emitted == instruction_size_bytes,
                   "Bytes emitted %d does not match instruction size %d",
                   bytes_emitted,
                   instruction_size_bytes);
}

void
bal_x86_emit_add_r32_imm32(bal_x86_assembler_t *assembler,
                           bal_x86_register_t   destination,
                           int32_t              immediate)
{
    if (BAL_UNLIKELY(NULL == assembler))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: assembler is NULL.");
        return;
    }

    if (BAL_UNLIKELY(assembler->status != BAL_SUCCESS))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: assembler->status != BAL_SUCCESS.");
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    const bool is_valid_register_result = is_valid_register(destination);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(&bal_thread_logger,
                      "Aborting function: invalid destination register: %d.",
                      destination);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    const bool   fit_in_8_bits          = immediate >= -128 && immediate <= 127;
    const size_t instruction_size_bytes = true == fit_in_8_bits ? 4 : 7;
    const bool   can_emit_status        = can_emit(assembler, instruction_size_bytes);

    if (BAL_UNLIKELY(false == can_emit_status))
    {
        return;
    }

    BAL_LOG_DEBUG(
        &bal_thread_logger, "[+0x%04zx] add r%dd, 0x%X", assembler->offset, destination, immediate);

    const size_t  old_offset = assembler->offset;
    const uint8_t w          = 0U;
    const uint8_t r          = 0U;
    const uint8_t b          = (uint8_t)destination >> 3U;
    emit_rex(assembler->buffer, &assembler->offset, w, r, b);
    const uint8_t opcode = true == fit_in_8_bits ? 0x83 : 0x81;
    emit8(assembler->buffer, &assembler->offset, opcode);
    emit_modrm_register(assembler->buffer, &assembler->offset, BAL_X86_RAX, destination);

    if (true == fit_in_8_bits)
    {
        emit8(assembler->buffer, &assembler->offset, (uint8_t)immediate);
    }
    else
    {
        emit32(assembler->buffer, &assembler->offset, (uint32_t)immediate);
    }

    const size_t bytes_emitted = assembler->offset - old_offset;
    BAL_ASSERT_MSG(bytes_emitted == instruction_size_bytes,
                   "Bytes emitted %d does not match instruction size %d",
                   bytes_emitted,
                   instruction_size_bytes);
}

void
bal_x86_emit_test_r64_r64(bal_x86_assembler_t     *assembler,
                          const bal_x86_register_t destination,
                          const bal_x86_register_t source)
{
    if (BAL_UNLIKELY(NULL == assembler))
    {
        return;
    }
    if (BAL_UNLIKELY(assembler->status != BAL_SUCCESS))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: assembler status != BAL_SUCCESS");
        return;
    }

    bool is_valid_register_result = is_valid_register(destination);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Invalid destination register: %d", destination);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    is_valid_register_result = is_valid_register(source);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Invalid source register: %d", source);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    const size_t instruction_size_bytes = 3;
    const bool   can_emit_status        = can_emit(assembler, instruction_size_bytes);

    if (BAL_UNLIKELY(false == can_emit_status))
    {
        return;
    }

    BAL_LOG_DEBUG(
        &bal_thread_logger, "[+0x%04zx] test r%d, r%d", assembler->offset, destination, source);

    const uint8_t w = 1;
    const uint8_t r = (uint8_t)source >> 3;
    const uint8_t b = (uint8_t)destination >> 3;

    const uint8_t opcode     = 0x85;
    const size_t  old_offset = assembler->offset;
    emit_rex(assembler->buffer, &assembler->offset, w, r, b);
    emit8(assembler->buffer, &assembler->offset, opcode);
    emit_modrm_register(assembler->buffer, &assembler->offset, source, destination);

    const size_t bytes_emitted = assembler->offset - old_offset;
    BAL_ASSERT_MSG(bytes_emitted == instruction_size_bytes,
                   "Bytes emitted %d does not match instruction size %d",
                   bytes_emitted,
                   instruction_size_bytes);
}

void
bal_x86_emit_xor_r64_r64(bal_x86_assembler_t *assembler,
                         bal_x86_register_t   destination,
                         bal_x86_register_t   source)
{
    if (BAL_UNLIKELY(NULL == assembler))
    {
        return;
    }

    if (BAL_UNLIKELY(assembler->status != BAL_SUCCESS))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: assembler status != BAL_SUCCESS");
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    bool is_valid_register_result = is_valid_register(source);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(
            &bal_thread_logger, "Aborting function: invalid destination register %d", source);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    is_valid_register_result = is_valid_register(destination);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(
            &bal_thread_logger, "Aborting function: invalid destination register %d", destination);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    const size_t instruction_size_bytes = 3;
    const bool   can_emit_status        = can_emit(assembler, instruction_size_bytes);

    if (BAL_UNLIKELY(false == can_emit_status))
    {
        return;
    }

    BAL_LOG_DEBUG(
        &bal_thread_logger, "[+0x%04zx] xor r%d, r%d", assembler->offset, destination, source);

    const uint8_t w = 1;

    // WARNING: Destination is verified by is_valid_register() to fall within the safe enum range
    // [0, 15].
    const uint8_t r = (uint8_t)destination >> 3;

    // WARNING: Source is verified by is_valid_register() to fall within the safe enum range
    // [0, 15].
    const uint8_t b = (uint8_t)source >> 3;

    const uint8_t opcode     = 0x33;
    const size_t  old_offset = assembler->offset;
    emit_rex(assembler->buffer, &assembler->offset, w, r, b);
    emit8(assembler->buffer, &assembler->offset, opcode);
    emit_modrm_register(assembler->buffer, &assembler->offset, destination, source);
    const size_t bytes_emitted = assembler->offset - old_offset;
    BAL_ASSERT_MSG(bytes_emitted == instruction_size_bytes,
                   "Bytes emitted %d does not match instruction size %d",
                   bytes_emitted,
                   instruction_size_bytes);
}

void
bal_x86_emit_sub_r64_imm32(bal_x86_assembler_t     *assembler,
                           const bal_x86_register_t destination,
                           const int32_t            immediate)
{
    if (BAL_UNLIKELY(NULL == assembler))
    {
        return;
    }
    if (BAL_UNLIKELY(assembler->status != BAL_SUCCESS))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: assembler status != BAL_SUCCESS");
        return;
    }

    bool is_valid_register_result = is_valid_register(destination);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Invalid destination register: %d", destination);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    const bool   fit_in_8_bits          = immediate >= -128 && immediate < 127;
    const size_t instruction_size_bytes = true == fit_in_8_bits ? 4 : 7;
    const bool   can_emit_status        = can_emit(assembler, instruction_size_bytes);

    if (BAL_UNLIKELY(false == can_emit_status))
    {
        return;
    }

    BAL_LOG_DEBUG(
        &bal_thread_logger, "[+0x%04zx] sub r%d, 0x%X", assembler->offset, destination, immediate);

    const size_t  old_offset = assembler->offset;
    const uint8_t b          = (uint8_t)destination >> 3;
    const uint8_t w          = 1;
    const uint8_t r          = 0;
    emit_rex(assembler->buffer, &assembler->offset, w, r, b);
    const uint8_t opcode = true == fit_in_8_bits ? 0x83 : 0x81;
    emit8(assembler->buffer, &assembler->offset, opcode);
    emit_modrm_register(assembler->buffer, &assembler->offset, BAL_X86_RBP, destination);

    if (true == fit_in_8_bits)
    {
        emit8(assembler->buffer, &assembler->offset, (uint8_t)immediate);
    }
    else
    {
        emit32(assembler->buffer, &assembler->offset, (uint32_t)immediate);
    }

    const size_t bytes_emitted = assembler->offset - old_offset;
    BAL_ASSERT_MSG(bytes_emitted == instruction_size_bytes,
                   "Bytes emitted %d does not match instruction size %d",
                   bytes_emitted,
                   instruction_size_bytes);
}

void
bal_x86_emit_sub_r32_imm32(bal_x86_assembler_t *assembler,
                           bal_x86_register_t   destination,
                           int32_t              immediate)
{
    if (BAL_UNLIKELY(NULL == assembler))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: assembler is NULL.");
        return;
    }

    if (BAL_UNLIKELY(assembler->status != BAL_SUCCESS))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: assembler->status != BAL_SUCCESS.");
        return;
    }

    const bool is_valid_register_result = is_valid_register(destination);

    if (BAL_UNLIKELY(false == is_valid_register_result))
    {
        BAL_LOG_ERROR(
            &bal_thread_logger, "Aborting function: Invalid destination register: %d", destination);
    }

    const bool   fit_in_8_bits          = immediate >= -128 && immediate <= 127;
    const size_t instruction_size_bytes = true == fit_in_8_bits ? 4 : 7;
    const bool   can_emit_status        = can_emit(assembler, instruction_size_bytes);

    if (BAL_UNLIKELY(false == can_emit_status))
    {
        return;
    }

    BAL_LOG_DEBUG(
        &bal_thread_logger, "[+0x%04zx] sub r%dd, 0x%X", assembler->offset, destination, immediate);

    const size_t  old_offset = assembler->offset;
    const uint8_t w          = 0U;
    const uint8_t r          = 0U;
    const uint8_t b          = (uint8_t)destination >> 3U;

    emit_rex(assembler->buffer, &assembler->offset, w, r, b);
    const uint8_t opcode = true == fit_in_8_bits ? 0x83 : 0x81;
    emit8(assembler->buffer, &assembler->offset, opcode);
    emit_modrm_register(assembler->buffer, &assembler->offset, BAL_X86_RBP, destination);

    if (true == fit_in_8_bits)
    {
        emit8(assembler->buffer, &assembler->offset, (uint8_t)immediate);
    }
    else
    {
        emit32(assembler->buffer, &assembler->offset, (uint32_t)immediate);
    }

    const size_t bytes_emitted = assembler->offset - old_offset;
    BAL_ASSERT_MSG(bytes_emitted == instruction_size_bytes,
                   "Bytes emitted %d does not match instruction size %d",
                   bytes_emitted,
                   instruction_size_bytes);
}

void
bal_x86_emit_cmp_mem8_rbp_offset_imm(bal_x86_assembler_t *assembler,
                                     const int32_t        offset,
                                     const int8_t         immediate)
{
    if (BAL_UNLIKELY(NULL == assembler))
    {
        return;
    }

    if (BAL_UNLIKELY(assembler->status != BAL_SUCCESS))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: assembler->status != BAL_SUCCESS ");
        return;
    }

    const size_t instruction_size_bytes = 8;
    const bool   can_emit_status        = can_emit(assembler, instruction_size_bytes);

    if (BAL_UNLIKELY(false == can_emit_status))
    {
        return;
    }

    BAL_LOG_DEBUG(&bal_thread_logger,
                  "[+0x%04zx] cmp byte ptr [rbp + 0x%X], 0x%X",
                  assembler->offset,
                  offset,
                  immediate);

    const size_t  old_offset = assembler->offset;
    const uint8_t w          = 0;
    const uint8_t r          = 0;
    const uint8_t b          = 0;
    const uint8_t opcode     = 0x80;
    emit_rex(assembler->buffer, &assembler->offset, w, r, b);
    emit8(assembler->buffer, &assembler->offset, opcode);
    emit_modrm_memory_disp32_rbp(assembler->buffer, &assembler->offset, BAL_X86_RDI);
    emit32(assembler->buffer, &assembler->offset, (uint32_t)offset);
    emit8(assembler->buffer, &assembler->offset, (uint8_t)immediate);

    const size_t bytes_emitted = assembler->offset - old_offset;
    BAL_ASSERT_MSG(bytes_emitted == instruction_size_bytes,
                   "Bytes emitted %d does not match instruction size %d",
                   bytes_emitted,
                   instruction_size_bytes);
}

void
bal_x86_emit_ud2(bal_x86_assembler_t *BAL_RESTRICT assembler)
{
    if (BAL_UNLIKELY(NULL == assembler))
    {
        return;
    }

    if (BAL_UNLIKELY(assembler->status != BAL_SUCCESS))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: assembler status != BAL_SUCCESS.");
        return;
    }

    const size_t instruction_size_bytes = 2;
    const bool   can_emit_status        = can_emit(assembler, instruction_size_bytes);

    if (BAL_UNLIKELY(false == can_emit_status))
    {
        return;
    }

    BAL_LOG_DEBUG(&bal_thread_logger, "[+0x%04zx] ud2", assembler->offset);

    const size_t old_offset = assembler->offset;
    emit8(assembler->buffer, &assembler->offset, 0x0F);
    emit8(assembler->buffer, &assembler->offset, 0x0B);

    const size_t bytes_emitted = assembler->offset - old_offset;
    BAL_ASSERT_MSG(bytes_emitted == instruction_size_bytes,
                   "Bytes emitted %d does not match instruction size %d.",
                   bytes_emitted,
                   instruction_size_bytes);
}

bool
is_valid_register(const int reg)
{
    return reg >= BAL_X86_RAX && reg <= BAL_X86_R15;
}

bool
can_emit(bal_x86_assembler_t *assembler, const size_t size)
{
    if (BAL_UNLIKELY(NULL == assembler))
    {
        return false;
    }

    if (BAL_UNLIKELY(assembler->status != BAL_SUCCESS))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: Assembler status != BAL_SUCCESS");
        return false;
    }

    if (BAL_UNLIKELY(SIZE_MAX - assembler->offset < size))
    {
        BAL_LOG_ERROR(&bal_thread_logger,
                      "x86 Assembler Integer Overflow. Current offset: %zu, Requested size: %zu",
                      assembler->offset,
                      size);
        assembler->status = BAL_ERROR_INSTRUCTION_OVERFLOW;
        return false;
    }

    const size_t assembler_size = assembler->offset + size;

    if (BAL_UNLIKELY(assembler_size > assembler->capacity))
    {
        BAL_LOG_ERROR(&bal_thread_logger,
                      "x86 Assembler Overflow. Capacity %zu reached",
                      assembler->capacity);
        assembler->status = BAL_ERROR_INSTRUCTION_OVERFLOW;
        return false;
    }

    return true;
}

void
emit8(uint8_t *BAL_RESTRICT buffer, size_t *BAL_RESTRICT offset, uint8_t const value)
{
    if (BAL_UNLIKELY(NULL == buffer))
    {
        return;
    }

    buffer += *offset;
    *buffer = value;
    *offset += 1;
}

void
emit32(uint8_t *BAL_RESTRICT buffer, size_t *BAL_RESTRICT offset, const uint32_t value)
{
    if (BAL_UNLIKELY(NULL == buffer))
    {
        return;
    }

    buffer += *offset;
    memcpy(buffer, &value, sizeof(uint32_t));
    *offset += sizeof(uint32_t);
}

void
emit64(uint8_t *BAL_RESTRICT buffer, size_t *BAL_RESTRICT offset, uint64_t const value)
{
    if (BAL_UNLIKELY(NULL == buffer))
    {
        return;
    }

    buffer += *offset;
    memcpy(buffer, &value, sizeof(uint64_t));
    *offset += sizeof(uint64_t);
}

void
emit_rex(uint8_t *BAL_RESTRICT buffer,
         size_t *BAL_RESTRICT  offset,
         const uint8_t         w,
         const uint8_t         r,
         const uint8_t         b)
{
    const uint8_t safe_w = w & 1U;
    const uint8_t safe_r = r & 1U;
    const uint8_t safe_b = b & 1U;

    // WARNING: Casting safe_* to unsigned prevents undefined behavior during bitwise shifts.
    // WARNING: Cast to uint8_t is safe as the result of the combined bitwise-OR  fits within
    // uint8_t [0x40, 0x4F].
    const uint8_t rex = (uint8_t)(0x40U | (unsigned)safe_w << 3U | (unsigned)safe_r << 2U | safe_b);

    emit8(buffer, offset, rex);
}

void
emit_shift_r64_imm8(bal_x86_assembler_t *BAL_RESTRICT assembler,
                    const bal_x86_register_t          destination,
                    const uint8_t                     immediate,
                    const uint8_t                     opcode_extension,
                    const char *BAL_RESTRICT          mnemonic)
{
    if (BAL_UNLIKELY(NULL == assembler))
    {
        return;
    }

    if (BAL_UNLIKELY(assembler->status != BAL_SUCCESS))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Aborting function: assembler status != BAL_SUCCESS");
        return;
    }

    if (BAL_UNLIKELY(false == is_valid_register(destination)))
    {
        BAL_LOG_ERROR(&bal_thread_logger, "Invalid destination register: %d", destination);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    if (BAL_UNLIKELY(immediate > 63U))
    {
        BAL_LOG_ERROR(
            &bal_thread_logger, "Invalid %s immediate: %u (must be 0-63)", mnemonic, immediate);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    if (BAL_UNLIKELY(opcode_extension > 7U))
    {
        BAL_LOG_ERROR(&bal_thread_logger,
                      "Invalid %s opcode extension: %u (must be 0-7)",
                      mnemonic,
                      opcode_extension);
        assembler->status = BAL_ERROR_INVALID_ARGUMENT;
        return;
    }

    const size_t instruction_size_bytes = 4;
    const bool   can_emit_status        = can_emit(assembler, instruction_size_bytes);

    if (BAL_UNLIKELY(false == can_emit_status))
    {
        return;
    }

    BAL_LOG_DEBUG(&bal_thread_logger,
                  "[+0x%04zx] %s r%d, %u",
                  assembler->offset,
                  mnemonic,
                  destination,
                  immediate);

    const uint8_t w          = 1;
    const uint8_t r          = 0;
    const uint8_t b          = (uint8_t)destination >> 3;
    const size_t  old_offset = assembler->offset;
    emit_rex(assembler->buffer, &assembler->offset, w, r, b);
    emit8(assembler->buffer, &assembler->offset, 0xC1);
    emit_modrm_register(assembler->buffer, &assembler->offset, (int)opcode_extension, destination);
    emit8(assembler->buffer, &assembler->offset, immediate);

    const size_t bytes_emitted = assembler->offset - old_offset;
    BAL_ASSERT_MSG(bytes_emitted == instruction_size_bytes,
                   "Bytes emitted %d does not match instruction size %d",
                   bytes_emitted,
                   instruction_size_bytes);
}

void
emit_modrm_register(uint8_t *BAL_RESTRICT buffer, size_t *BAL_RESTRICT offset, int reg, int rm)
{
    // WARNING: Lossless conversion of reg (0 - 15) before applying mask.
    const uint8_t safe_reg = (uint8_t)reg & 7;

    // WARNING: Lossless conversion of rm (0 - 15) before applying mask.
    const uint8_t safe_rm = (uint8_t)rm & 7U;

    // WARNING: Cast safe_reg to unsigned prevents undefined behavior during bitwise shifts.
    // WARNING: Cast to uint8_t is safe as the resulting byte fits within the uint8_t range
    // [0xC0, 0xFF].
    const uint8_t modrm = (uint8_t)(0xC0U | (unsigned)safe_reg << 3U | safe_rm);

    emit8(buffer, offset, modrm);
}

void
emit_modrm_memory_disp32_rbp(uint8_t *BAL_RESTRICT buffer, size_t *BAL_RESTRICT offset, int reg)
{
    // WARNING: Lossless conversion of reg (0 - 15) before applying mask.
    const uint8_t safe_reg = (uint8_t)reg & 7;

    // WARNING: Cast safe_reg to unsigned prevents undefined behavior during bitwise shifts.
    // WARNING: Cast to uint8_t is safe as the resulting byte fits within the uint8_t range
    // [0xC0, 0xFF].
    uint8_t const modrm = (uint8_t)(0x80U | (unsigned)safe_reg << 3U | 0x05U);

    emit8(buffer, offset, modrm);
}

/*** end of file ***/