// File: /workspace/github_c_makefile_star2_O3/leiradel[P]zx48k-libretro/src/mem.h
// Function: mem_map
static void _mem_map(mem_t* m, int layer, uint16_t addr, uint32_t size, const uint8_t* read_ptr, uint8_t* write_ptr) {
    CHIPS_ASSERT(m);
    CHIPS_ASSERT((layer >= 0) && (layer < MEM_NUM_LAYERS));
    CHIPS_ASSERT((addr & MEM_PAGE_MASK) == 0);
    CHIPS_ASSERT((size & MEM_PAGE_MASK) == 0);
    CHIPS_ASSERT(size <= MEM_ADDR_RANGE);
    const int num = size>>MEM_PAGE_SHIFT;
    CHIPS_ASSERT(num <= MEM_NUM_PAGES);
    for (int i = 0; i < num; i++) {
        const uint16_t offset = i * MEM_PAGE_SIZE;
        /* the page_index will wrap-around */
        const uint16_t page_index = ((addr+offset) & MEM_ADDR_MASK) >> MEM_PAGE_SHIFT;
        CHIPS_ASSERT(page_index <= MEM_NUM_PAGES);
        mem_page_t* page = &m->layers[layer][page_index];
        page->read_ptr = read_ptr + offset;
        if (0 != write_ptr) {
            page->write_ptr = write_ptr + offset;
        }
        else {
            page->write_ptr = m->junk_page;
        }
        _mem_update_page_table(m, page_index);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r8, -0x38(%rbp)
testq %rdi, %rdi
je 0x42af
cmpl $0x4, %esi
jae 0x42ce
testl $0x3ff, %edx            # imm = 0x3FF
jne 0x42ed
testl $0x3ff, %ecx            # imm = 0x3FF
jne 0x430c
cmpl $0x10001, %ecx          # imm = 0x10001
jae 0x432b
testl %ecx, %ecx
je 0x42a0
shrl $0xa, %ecx
movl %esi, %eax
leaq 0x1800(%rdi), %rsi
leaq 0x1000(%rdi), %r10
leaq 0x1400(%rdi), %r8
movq %r8, -0x30(%rbp)
xorl %ebx, %ebx
movl $0xffff, %r8d           # imm = 0xFFFF
shlq $0xa, %rax
addq %rdi, %rax
movl %edx, %r15d
movl %ebx, %r13d
shll $0xa, %r13d
leal (%rdx,%r13), %r12d
andl %r8d, %r12d
shrl $0xa, %r12d
andl %r8d, %r13d
movl %r12d, %r14d
shll $0x4, %r14d
movq -0x38(%rbp), %r11
addq %r13, %r11
movq %r11, (%r14,%rax)
movl %r15d, %r11d
shrl $0x6, %r11d
andl $0x3f0, %r11d           # imm = 0x3F0
addq %rdi, %r11
addq %r9, %r13
testq %r9, %r9
cmoveq %rsi, %r13
movq %r13, 0x8(%r14,%rax)
xorl %r13d, %r13d
cmpq $0x0, (%r11,%r13)
jne 0x4281
addq $0x400, %r13            # imm = 0x400
cmpq $0x1000, %r13           # imm = 0x1000
jne 0x4257
shll $0x4, %r12d
movq -0x30(%rbp), %r11
movq %r11, (%r10,%r12)
movq %rsi, 0x8(%r10,%r12)
jmp 0x428f
shll $0x4, %r12d
movups (%r11,%r13), %xmm0
movups %xmm0, (%r10,%r12)
incl %ebx
addl $0x400, %r15d           # imm = 0x400
cmpl %ecx, %ebx
jne 0x420d
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xfc4b(%rip), %rdi      # 0x13f01
leaq 0xfc46(%rip), %rsi      # 0x13f03
leaq 0x10187(%rip), %rcx     # 0x1444b
movl $0x12a, %edx            # imm = 0x12A
callq 0x30b0
leaq 0xfaef(%rip), %rdi      # 0x13dc4
leaq 0xfc27(%rip), %rsi      # 0x13f03
leaq 0x10168(%rip), %rcx     # 0x1444b
movl $0x12b, %edx            # imm = 0x12B
callq 0x30b0
leaq 0x101a3(%rip), %rdi     # 0x14497
leaq 0xfc08(%rip), %rsi      # 0x13f03
leaq 0x10149(%rip), %rcx     # 0x1444b
movl $0x12c, %edx            # imm = 0x12C
callq 0x30b0
leaq 0x101a0(%rip), %rdi     # 0x144b3
leaq 0xfbe9(%rip), %rsi      # 0x13f03
leaq 0x1012a(%rip), %rcx     # 0x1444b
movl $0x12d, %edx            # imm = 0x12D
callq 0x30b0
leaq 0x1019d(%rip), %rdi     # 0x144cf
leaq 0xfbca(%rip), %rsi      # 0x13f03
leaq 0x1010b(%rip), %rcx     # 0x1444b
movl $0x12e, %edx            # imm = 0x12E
callq 0x30b0