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

	for (int i = pml3_i(from); vaddr != to; ++i) {
		struct page *pt;
		phys_t paddr;
		const virt_t bytes =
			MINU(PML2_SIZE - (vaddr & PML2_MASK), to - vaddr);
		const pfn_t pages = bytes >> PAGE_BITS;

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

			if (!pt) {
				pt_release_pml3(pml3, from, vaddr);
				return -ENOMEM;
			}

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

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

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

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

			if (pt->u.refcount == 0) {
				pml3[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 0xffffffff80106320
movl %eax, -0x34(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x20(%rbp), %rax
je 0xffffffff8010659d
movq -0x30(%rbp), %rax
andq $0x3fffffff, %rax       # imm = 0x3FFFFFFF
movl $0x40000000, %edi       # imm = 0x40000000
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 0xffffffff801064cb
movq -0x28(%rbp), %rdi
callq 0xffffffff80106180
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0xffffffff8010649f
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0xffffffff801065d0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xffffffff801065a4
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 0xffffffff801064f9
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 0xffffffff801066f0
movl %eax, -0x64(%rbp)
cmpl $0x0, -0x64(%rbp)
je 0xffffffff80106583
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0xffffffff801065d0
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 0xffffffff8010657b
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 0xffffffff801065a4
movq -0x50(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xffffffff8010641c
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)