// File: /workspace/github_c_makefile_star2_O0/jarun[P]nnn/src/nnn.c
// Function: abspath
static char *abspath(const char *filepath, char *cwd, char *buf)
{
	const char *path = filepath;
	bool allocated = FALSE;

	if (!path)
		return NULL;

	/* when dealing with tilde, we need to be strict.
	 * otherwise a file named "~" can end up expanding to
	 * $HOME and causing disaster */
	if (tilde_is_home_strict(path)) {
		cwd = home;
		path += 2; /* advance 2 bytes past the "~/" */
	} else if ((path[0] != '/') && !cwd) {
		cwd = getcwd(NULL, 0); // NOLINT
		if (!cwd)
			return NULL;
		allocated = TRUE;
	}

	size_t dst_size = 0, src_size = xstrlen(path), cwd_size = cwd ? xstrlen(cwd) : 0;
	size_t len = src_size;
	const char *src;
	char *dst;
	/*
	 * We need to add 2 chars at the end as relative paths may start with:
	 * ./ (find .)
	 * no separator (fd .): this needs an additional char for '/'
	 */
	char *resolved_path = buf ? buf : malloc(src_size + cwd_size + 2);

	if (!resolved_path) {
		if (allocated)
			free(cwd);
		return NULL;
	}

	/* Turn relative paths into absolute */
	if (path[0] != '/') {
		if (!cwd) {
			if (!buf)
				free(resolved_path);
			return NULL;
		}
		dst_size = xstrsncpy(resolved_path, cwd, cwd_size + 1) - 1;
		if (allocated)
			free(cwd);
	} else
		resolved_path[0] = '\0';

	src = path;
	dst = resolved_path + dst_size;
	for (const char *next = NULL; next != path + src_size;) {
		next = memchr(src, '/', len);
		if (!next)
			next = path + src_size;

		if (next - src == 2 && src[0] == '.' && src[1] == '.') {
			if (dst - resolved_path) {
				dst = xmemrchr((uchar_t *)resolved_path, '/', dst - resolved_path);
				*dst = '\0';
			}
		} else if (next - src == 1 && src[0] == '.') {
			/* NOP */
		} else if (next - src) {
			*(dst++) = '/';
			xstrsncpy(dst, src, next - src + 1);
			dst += next - src;
		}

		src = next + 1;
		len = src_size - (src - path);
	}

	if (*resolved_path == '\0') {
		resolved_path[0] = '/';
		resolved_path[1] = '\0';
	}

	if (xstrlen(resolved_path) >= PATH_MAX) {
		if (!buf)
			free(resolved_path);
		else
			buf[0] = '\0';
		return NULL;
	}

	return resolved_path;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x28(%rbp)
movb $0x0, -0x29(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x5f37
movq $0x0, -0x8(%rbp)
jmp 0x6243
movq -0x28(%rbp), %rdi
callq 0xca90
testb $0x1, %al
jne 0x5f46
jmp 0x5f5f
movq 0x1a9f3(%rip), %rax     # 0x20940
movq %rax, -0x18(%rbp)
movq -0x28(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x28(%rbp)
jmp 0x5f9c
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2f, %eax
je 0x5f9a
cmpq $0x0, -0x18(%rbp)
jne 0x5f9a
xorl %eax, %eax
movl %eax, %esi
movq %rsi, %rdi
callq 0x43a0
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x5f96
movq $0x0, -0x8(%rbp)
jmp 0x6243
movb $0x1, -0x29(%rbp)
jmp 0x5f9c
movq $0x0, -0x38(%rbp)
movq -0x28(%rbp), %rdi
callq 0x5cc0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x5fc7
movq -0x18(%rbp), %rdi
callq 0x5cc0
movq %rax, -0x78(%rbp)
jmp 0x5fcf
xorl %eax, %eax
movq %rax, -0x78(%rbp)
jmp 0x5fcf
movq -0x78(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x5ff0
movq -0x20(%rbp), %rax
movq %rax, -0x80(%rbp)
jmp 0x6005
movq -0x40(%rbp), %rdi
addq -0x48(%rbp), %rdi
addq $0x2, %rdi
callq 0x45e0
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
jne 0x6030
testb $0x1, -0x29(%rbp)
je 0x6023
movq -0x18(%rbp), %rdi
callq 0x4090
movq $0x0, -0x8(%rbp)
jmp 0x6243
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2f, %eax
je 0x608e
cmpq $0x0, -0x18(%rbp)
jne 0x6060
cmpq $0x0, -0x20(%rbp)
jne 0x6053
movq -0x68(%rbp), %rdi
callq 0x4090
movq $0x0, -0x8(%rbp)
jmp 0x6243
movq -0x68(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x48(%rbp), %rdx
addq $0x1, %rdx
callq 0x6250
subq $0x1, %rax
movq %rax, -0x38(%rbp)
testb $0x1, -0x29(%rbp)
je 0x608c
movq -0x18(%rbp), %rdi
callq 0x4090
jmp 0x6095
movq -0x68(%rbp), %rax
movb $0x0, (%rax)
movq -0x28(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x68(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x60(%rbp)
movq $0x0, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq -0x28(%rbp), %rcx
addq -0x40(%rbp), %rcx
cmpq %rcx, %rax
je 0x61ec
movq -0x58(%rbp), %rdi
movq -0x50(%rbp), %rdx
movl $0x2f, %esi
callq 0x4450
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
jne 0x60ef
movq -0x28(%rbp), %rax
addq -0x40(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq -0x58(%rbp), %rcx
subq %rcx, %rax
cmpq $0x2, %rax
jne 0x6150
movq -0x58(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2e, %eax
jne 0x6150
movq -0x58(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x2e, %eax
jne 0x6150
movq -0x60(%rbp), %rax
movq -0x68(%rbp), %rcx
subq %rcx, %rax
cmpq $0x0, %rax
je 0x614e
movq -0x68(%rbp), %rdi
movq -0x60(%rbp), %rdx
movq -0x68(%rbp), %rax
subq %rax, %rdx
movl $0x2f, %esi
callq 0xcad0
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movb $0x0, (%rax)
jmp 0x61c5
movq -0x70(%rbp), %rax
movq -0x58(%rbp), %rcx
subq %rcx, %rax
cmpq $0x1, %rax
jne 0x616f
movq -0x58(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2e, %eax
jne 0x616f
jmp 0x61c3
movq -0x70(%rbp), %rax
movq -0x58(%rbp), %rcx
subq %rcx, %rax
cmpq $0x0, %rax
je 0x61c1
movq -0x60(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x60(%rbp)
movb $0x2f, (%rax)
movq -0x60(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq -0x58(%rbp), %rax
subq %rax, %rdx
addq $0x1, %rdx
callq 0x6250
movq -0x70(%rbp), %rax
movq -0x58(%rbp), %rcx
subq %rcx, %rax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x61c3
jmp 0x61c5
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
movq -0x40(%rbp), %rax
movq -0x58(%rbp), %rcx
movq -0x28(%rbp), %rdx
subq %rdx, %rcx
subq %rcx, %rax
movq %rax, -0x50(%rbp)
jmp 0x60b1
movq -0x68(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x6207
movq -0x68(%rbp), %rax
movb $0x2f, (%rax)
movq -0x68(%rbp), %rax
movb $0x0, 0x1(%rax)
movq -0x68(%rbp), %rdi
callq 0x5cc0
cmpq $0x1000, %rax           # imm = 0x1000
jb 0x623b
cmpq $0x0, -0x20(%rbp)
jne 0x622a
movq -0x68(%rbp), %rdi
callq 0x4090
jmp 0x6231
movq -0x20(%rbp), %rax
movb $0x0, (%rax)
movq $0x0, -0x8(%rbp)
jmp 0x6243
movq -0x68(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq