// File: /workspace/github_c_makefile_star2_O0/krinkinmu[P]64bit/exec.c
// Function: copy_args
static int copy_args(struct mm *mm, int argc, const char **argv)
{
	const size_t ptrsz = sizeof(char *);

	struct vma *stack = mm->stack;
	size_t bytes = 0;

	for (int i = 0; i != argc; ++i)
		bytes += strlen(argv[i]) + 1;

	bytes = ALIGN(bytes, ALIGN_OF(void *)); // force alignment

	const size_t size = ALIGN(bytes + ptrsz * (argc + 1), PAGE_SIZE);
	const size_t count = size / PAGE_SIZE;
	struct page **pages = kmem_alloc(count * sizeof(struct page *));

	if (!pages)
		return -ENOMEM;

	memset(pages, 0, sizeof(*pages) * count);

	int rc = -ENOMEM;
	for (size_t i = 0; i != count; ++i) {
		pages[i] = alloc_pages(0);
		if (!pages[i])
			goto out;
		pages[i]->u.refcount = 0;
	}

	void *buffer = kmap(pages, count);

	if (!buffer)
		goto out;

	char *data = (char *)buffer + (size - bytes);
	uintptr_t usrdata = stack->end - bytes;
	uintptr_t *array = (uintptr_t *)(data - (argc + 1) * ptrsz);
	uintptr_t usrarray = usrdata - (argc + 1) * ptrsz;

	memset(buffer, 0, size);

	for (int i = 0; i != argc; ++i) {
		const size_t len = strlen(argv[i]) + 1;

		array[i] = usrdata;
		usrdata += len;
		memcpy(data, argv[i], len);
		data += len;
	}

	__mmap_pages(mm, stack->end - size, pages, count,
				PTE_WRITE | PTE_USER);

	mm->stack_pointer = usrarray;
	mm->argv_addr = usrarray;
	mm->argc = argc;

	kunmap(buffer);
	kmem_free(pages);

	return 0;	

out:
	for (size_t i = 0; i != count; ++i)
		if (pages[i])
			free_pages(pages[i], 0);

	kmem_free(pages);
	return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq $0x8, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x14(%rbp), %eax
je 0xffffffff801113f9
movq -0x20(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0xffffffff801018d0
addq $0x1, %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0xffffffff801113c9
movq -0x38(%rbp), %rdi
movl $0x8, %esi
callq 0xffffffff80111870
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
movl -0x14(%rbp), %eax
addl $0x1, %eax
cltq
shlq $0x3, %rax
addq %rax, %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0xffffffff80111870
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
shrq $0xc, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
shlq $0x3, %rdi
callq 0xffffffff80107510
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0xffffffff8011145c
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xffffffff801116c8
movq -0x58(%rbp), %rdi
movq -0x50(%rbp), %rdx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0xffffffff801017e0
movl $0xffffffff, -0x5c(%rbp) # imm = 0xFFFFFFFF
movq $0x0, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x50(%rbp), %rax
je 0xffffffff801114d4
xorl %edi, %edi
callq 0xffffffff801043b0
movq %rax, %rdx
movq -0x58(%rbp), %rax
movq -0x68(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x58(%rbp), %rax
movq -0x68(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0xffffffff801114b2
jmp 0xffffffff80111663
movq -0x58(%rbp), %rax
movq -0x68(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq $0x0, 0x18(%rax)
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0xffffffff8011147e
movq -0x58(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0xffffffff80105b10
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
jne 0xffffffff801114f1
jmp 0xffffffff80111663
movq -0x70(%rbp), %rax
movq -0x48(%rbp), %rcx
subq -0x38(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rax
subq -0x38(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x78(%rbp), %rax
movl -0x14(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rdx
shlq $0x3, %rdx
xorl %ecx, %ecx
subq %rdx, %rcx
addq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x80(%rbp), %rax
movl -0x14(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x3, %rcx
subq %rcx, %rax
movq %rax, -0x90(%rbp)
movq -0x70(%rbp), %rdi
movq -0x48(%rbp), %rdx
xorl %esi, %esi
callq 0xffffffff801017e0
movl $0x0, -0x94(%rbp)
movl -0x94(%rbp), %eax
cmpl -0x14(%rbp), %eax
je 0xffffffff801115fd
movq -0x20(%rbp), %rax
movslq -0x94(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0xffffffff801018d0
addq $0x1, %rax
movq %rax, -0xa0(%rbp)
movq -0x80(%rbp), %rdx
movq -0x88(%rbp), %rax
movslq -0x94(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0xa0(%rbp), %rax
addq -0x80(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x78(%rbp), %rdi
movq -0x20(%rbp), %rax
movslq -0x94(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movq -0xa0(%rbp), %rdx
callq 0xffffffff801016a0
movq -0xa0(%rbp), %rax
addq -0x78(%rbp), %rax
movq %rax, -0x78(%rbp)
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0xffffffff80111568
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rsi
subq -0x48(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x50(%rbp), %rcx
movl $0x6, %r8d
callq 0xffffffff80110870
movq -0x90(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x90(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movl -0x14(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x28(%rax)
movq -0x70(%rbp), %rdi
callq 0xffffffff80105de0
movq -0x58(%rbp), %rdi
callq 0xffffffff801075b0
movl $0x0, -0x4(%rbp)
jmp 0xffffffff801116c8
movq $0x0, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
cmpq -0x50(%rbp), %rax
je 0xffffffff801116b9
movq -0x58(%rbp), %rax
movq -0xa8(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0xffffffff801116a3
movq -0x58(%rbp), %rax
movq -0xa8(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
xorl %esi, %esi
callq 0xffffffff801043d0
jmp 0xffffffff801116a5
movq -0xa8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa8(%rbp)
jmp 0xffffffff8011166e
movq -0x58(%rbp), %rdi
callq 0xffffffff801075b0
movl -0x5c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)