// File: /workspace/github_c_makefile_star2_O0/krinkinmu[P]64bit/mm.c
// Function: copy_vma
static int copy_vma(struct mm *dst, struct vma *vma)
{
	if (__mmap(dst, vma->begin, vma->end, vma->perm))
		return -ENOMEM;

	struct mm *old_mm = vma->mm;
	pte_t *pt = page_addr(old_mm->pt);
	struct pt_iter iter;

	for_each_slot_in_range(pt, vma->begin, vma->end, iter) {
		DBG_ASSERT(iter.level == 0);
		DBG_ASSERT(iter.pt[iter.level] != 0);

		const int level = iter.level;
		const int index = iter.idx[level];
		const pte_t pte = iter.pt[level][index];

		if (!pte_present(pte))
			continue;

		const phys_t phys = pte_phys(pte);
		struct page *page = pfn2page(phys >> PAGE_BITS);

		if ((pte & PTE_WRITE) != 0) {
			iter.pt[level][index] &= ~((pte_t)PTE_WRITE);
			flush_tlb_addr(iter.addr);
		}
		__mmap_pages(dst, iter.addr, &page, 1, PTE_USER);
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdx
movq -0x18(%rbp), %rax
movl 0x28(%rax), %ecx
callq 0xffffffff8010fc50
cmpl $0x0, %eax
je 0xffffffff801106b4
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xffffffff8011085d
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0xffffffff8010fdc0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdx
leaq -0x68(%rbp), %rdi
callq 0xffffffff80105050
movq %rax, -0x70(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x70(%rbp)
movb %al, -0x91(%rbp)
je 0xffffffff80110712
movq -0x70(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x18(%rbp), %rcx
cmpq 0x20(%rcx), %rax
setb %al
movb %al, -0x91(%rbp)
movb -0x91(%rbp), %al
testb $0x1, %al
jne 0xffffffff80110721
jmp 0xffffffff80110856
jmp 0xffffffff80110723
cmpl $0x0, -0x68(%rbp)
je 0xffffffff80110758
leaq 0x1a49(%rip), %rdi      # 0xffffffff80112179
leaq 0x2809(%rip), %rsi      # 0xffffffff80112f40
movl $0x14f, %edx            # imm = 0x14F
leaq 0x1a3a(%rip), %rcx      # 0xffffffff8011217d
leaq 0x2813(%rip), %r8       # 0xffffffff80112f5d
movb $0x0, %al
callq 0xffffffff80101e60
callq 0xffffffff80111cd0
jmp 0xffffffff80110756
jmp 0xffffffff8011075a
jmp 0xffffffff8011075c
movslq -0x68(%rbp), %rax
cmpq $0x0, -0x60(%rbp,%rax,8)
jne 0xffffffff80110797
leaq 0x1a0a(%rip), %rdi      # 0xffffffff80112179
leaq 0x27ca(%rip), %rsi      # 0xffffffff80112f40
movl $0x150, %edx            # imm = 0x150
leaq 0x19fb(%rip), %rcx      # 0xffffffff8011217d
leaq 0x27e4(%rip), %r8       # 0xffffffff80112f6d
movb $0x0, %al
callq 0xffffffff80101e60
callq 0xffffffff80111cd0
jmp 0xffffffff80110795
jmp 0xffffffff80110799
movl -0x68(%rbp), %eax
movl %eax, -0x74(%rbp)
movslq -0x74(%rbp), %rax
movl -0x40(%rbp,%rax,4), %eax
movl %eax, -0x78(%rbp)
movslq -0x74(%rbp), %rax
movq -0x60(%rbp,%rax,8), %rax
movslq -0x78(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rdi
callq 0xffffffff80110ab0
testb $0x1, %al
jne 0xffffffff801107ce
jmp 0xffffffff80110844
movq -0x80(%rbp), %rdi
callq 0xffffffff80110ad0
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
shrq $0xc, %rdi
callq 0xffffffff80103ce0
movq %rax, -0x90(%rbp)
movq -0x80(%rbp), %rax
andq $0x2, %rax
cmpq $0x0, %rax
je 0xffffffff80110825
movslq -0x74(%rbp), %rax
movq -0x60(%rbp,%rax,8), %rax
movslq -0x78(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
andq $-0x3, %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x30(%rbp), %rdi
callq 0xffffffff80110d10
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rsi
leaq -0x90(%rbp), %rdx
movl $0x1, %ecx
movl $0x4, %r8d
callq 0xffffffff80110870
movq -0x70(%rbp), %rdi
callq 0xffffffff80105320
movq %rax, -0x70(%rbp)
jmp 0xffffffff801106ea
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax)