// File: /workspace/github_c_makefile_star2_O0/krinkinmu[P]64bit/exec.c
// Function: map_elf_phdr
static int map_elf_phdr(struct mm *mm, const struct elf_phdr *phdr,
			struct fs_file *file)
{
	if (phdr->p_type != PT_LOAD)
		return 0;

	const virt_t begin = phdr->p_vaddr;
	const virt_t end = begin + phdr->p_memsz;
	const int perm = ((phdr->p_flags & PF_W) != 0) ? VMA_PERM_WRITE : 0;
	const unsigned long flags = (perm == VMA_PERM_WRITE) ? PTE_WRITE : 0;

	int rc = __mmap(mm, begin, end, perm);

	if (rc)
		return rc;

	if (!phdr->p_filesz)
		return 0;

	rc = vfs_seek(file, (int)phdr->p_offset, FSS_SET);
	if (rc < 0)
		return rc;

	struct page *page = 0;
	virt_t addr = ALIGN_DOWN(begin, PAGE_SIZE);
	size_t offset = begin & PAGE_MASK;
	size_t remain = phdr->p_filesz;

	while (remain) {
		page = alloc_pages(0);

		if (!page) {
			__munmap(mm, begin, end);
			return -ENOMEM;
		}

		const size_t size = MINU(remain, PAGE_SIZE - offset);
		char *buffer = page_addr(page);

		memset(buffer, 0, PAGE_SIZE);

		const int rc = read_buf(file, buffer + offset, size);

		if (rc) {
			free_pages(page, 0);
			__munmap(mm, begin, end);
			return rc;
		}

		page->u.refcount = 0;
		__mmap_pages(mm, addr, &page, 1, flags | PTE_USER);

		addr += PAGE_SIZE;
		remain -= size;
		offset = 0;
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x1, (%rax)
je 0xffffffff801118fc
movl $0x0, -0x4(%rbp)
jmp 0xffffffff80111aed
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0x18(%rbp), %rcx
addq 0x28(%rcx), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movl 0x4(%rax), %edx
andl $0x2, %edx
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %edx
xorl %eax, %eax
movl $0x2, %ecx
cmpl $0x1, %edx
cmoveq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
movl -0x34(%rbp), %ecx
callq 0xffffffff8010fc50
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0xffffffff8011196f
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xffffffff80111aed
movq -0x18(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0xffffffff80111986
movl $0x0, -0x4(%rbp)
jmp 0xffffffff80111aed
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %esi
xorl %edx, %edx
callq 0xffffffff80109d10
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
jge 0xffffffff801119af
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xffffffff80111aed
movq $0x0, -0x50(%rbp)
movq -0x28(%rbp), %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0xffffffff80111b00
movq %rax, -0x58(%rbp)
movq -0x28(%rbp), %rax
andq $0xfff, %rax            # imm = 0xFFF
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
je 0xffffffff80111ae6
xorl %edi, %edi
callq 0xffffffff801043b0
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0xffffffff80111a1d
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0xffffffff80110040
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xffffffff80111aed
movq -0x68(%rbp), %rdi
movl $0x1000, %esi           # imm = 0x1000
subq -0x60(%rbp), %rsi
callq 0xffffffff80111b30
movq %rax, -0x70(%rbp)
movq -0x50(%rbp), %rdi
callq 0xffffffff80111b60
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rdi
xorl %esi, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0xffffffff801017e0
movq -0x20(%rbp), %rdi
movq -0x78(%rbp), %rsi
addq -0x60(%rbp), %rsi
movq -0x70(%rbp), %rdx
callq 0xffffffff801117e0
movl %eax, -0x7c(%rbp)
cmpl $0x0, -0x7c(%rbp)
je 0xffffffff80111a92
movq -0x50(%rbp), %rdi
xorl %esi, %esi
callq 0xffffffff801043d0
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0xffffffff80110040
movl -0x7c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xffffffff80111aed
movq -0x50(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x10(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x40(%rbp), %r8
orq $0x4, %r8
leaq -0x50(%rbp), %rdx
movl $0x1, %ecx
callq 0xffffffff80110870
movq -0x58(%rbp), %rax
addq $0x1000, %rax           # imm = 0x1000
movq %rax, -0x58(%rbp)
movq -0x70(%rbp), %rcx
movq -0x68(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x68(%rbp)
movq $0x0, -0x60(%rbp)
jmp 0xffffffff801119e3
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)