// File: /workspace/github_c_makefile_star2_O0/krinkinmu[P]64bit/paging.c
// Function: pt_populate_pml4
static int pt_populate_pml4(pte_t *pml4, virt_t from, virt_t to, pte_t flags)
{
	virt_t vaddr = from;

	for (int i = pml4_i(from); vaddr < to; ++i) {
		struct page *pt;
		phys_t paddr;
		const virt_t bytes =
			MINU(PML3_SIZE - (vaddr & PML3_MASK), to - vaddr);
		const pfn_t pages = bytes >> PAGE_BITS;

		if (!pte_present(pml4[i])) {
			pt = alloc_page_table(flags);

			if (!pt) {
				pt_release_pml4(pml4, from, vaddr);
				return -ENOMEM;
			}

			paddr = page_paddr(pt);
			pml4[i] = paddr | (flags & ~PTE_LARGE);
		} else {
			const pte_t pte = pml4[i];

			paddr = pte_phys(pte);
			pt = pfn2page(paddr >> PAGE_BITS);
		}
		pt->u.refcount += pages;

		const int rc = pt_populate_pml3(va(paddr), vaddr, vaddr + bytes,
					flags);

		if (rc) {
			pt_release_pml4(pml4, from, vaddr);
			pt->u.refcount -= pages;

			if (pt->u.refcount == 0) {
				pml4[i] = 0;
				free_page_table(pt);
			}

			return rc;
		}

		vaddr += bytes;
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rdi
callq 0xffffffff801051a0
movl %eax, -0x34(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0xffffffff801058d6
movabsq $0x7fffffffff, %rax # imm = 0x7FFFFFFFFF
andq -0x30(%rbp), %rax
movabsq $0x8000000000, %rdi # imm = 0x8000000000
subq %rax, %rdi
movq -0x20(%rbp), %rsi
subq -0x30(%rbp), %rsi
callq 0xffffffff801063c0
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
shrq $0xc, %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0xffffffff801051c0
testb $0x1, %al
jne 0xffffffff80105804
movq -0x28(%rbp), %rdi
callq 0xffffffff80106180
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0xffffffff801057d8
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0xffffffff801059e0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xffffffff801058dd
movq -0x40(%rbp), %rdi
callq 0xffffffff80105da0
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdx
movq -0x28(%rbp), %rax
andq $-0x81, %rax
orq %rax, %rdx
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0xffffffff80105832
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rdi
callq 0xffffffff80105200
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
shrq $0xc, %rdi
callq 0xffffffff80103ce0
movq %rax, -0x40(%rbp)
movq -0x58(%rbp), %rcx
movq -0x40(%rbp), %rax
addq 0x18(%rax), %rcx
movq %rcx, 0x18(%rax)
movq -0x48(%rbp), %rdi
callq 0xffffffff80105220
movq %rax, %rdi
movq -0x30(%rbp), %rsi
movq -0x30(%rbp), %rdx
addq -0x50(%rbp), %rdx
movq -0x28(%rbp), %rcx
callq 0xffffffff801063f0
movl %eax, -0x64(%rbp)
cmpl $0x0, -0x64(%rbp)
je 0xffffffff801058bc
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0xffffffff801059e0
movq -0x58(%rbp), %rdx
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rcx
subq %rdx, %rcx
movq %rcx, 0x18(%rax)
movq -0x40(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0xffffffff801058b4
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
movq -0x40(%rbp), %rdi
callq 0xffffffff801065b0
movl -0x64(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xffffffff801058dd
movq -0x50(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xffffffff8010574c
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)