// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
testq %rdi, %rdi
je 0x6002
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %r15
movb (%rdi), %al
cmpb $0x7e, %al
jne 0x5e52
cmpb $0x2f, 0x1(%r15)
jne 0x5e52
movq 0x137f4(%rip), %r13     # 0x19640
addq $0x2, %r15
jmp 0x5e61
cmpb $0x2f, %al
sete %al
testq %r13, %r13
setne %cl
orb %al, %cl
je 0x5e65
xorl %ebp, %ebp
jmp 0x5e7f
xorl %ebx, %ebx
xorl %edi, %edi
xorl %esi, %esi
callq 0x43a0
testq %rax, %rax
je 0x6004
movq %rax, %r13
movb $0x1, %bpl
movq %r15, %rdi
callq 0x5d70
movq %rax, %r12
testq %r13, %r13
je 0x5e9c
movq %r13, %rdi
callq 0x5d70
movq %rax, %rbx
jmp 0x5e9e
xorl %ebx, %ebx
movq %r14, %rax
testq %r14, %r14
jne 0x5ebc
leaq (%r12,%rbx), %rdi
addq $0x2, %rdi
callq 0x45c0
testq %rax, %rax
je 0x5ff0
cmpb $0x2f, (%r15)
movq %rax, 0x8(%rsp)
jne 0x5ed3
movq %r14, 0x10(%rsp)
movb $0x0, (%rax)
xorl %ebp, %ebp
jmp 0x5f0b
testq %r13, %r13
je 0x5fe6
incq %rbx
movq %rax, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x6016
movl %ebp, %ecx
movq %rax, %rbp
decq %rbp
testb %cl, %cl
movq %r14, 0x10(%rsp)
je 0x5f06
movq %r13, %rdi
callq 0x4090
movq 0x8(%rsp), %rax
movq %r12, %rdx
addq %rax, %rbp
addq %r15, %r12
xorl %r14d, %r14d
pushq $0x2f
popq %r13
cmpq %r12, %r14
je 0x5fb8
movq %r15, %rdi
movl %r13d, %esi
callq 0x4450
movq %rax, %r14
testq %rax, %rax
cmoveq %r12, %r14
movq %r14, %rbx
subq %r15, %rbx
cmpq $0x1, %rbx
je 0x5fa5
cmpq $0x2, %rbx
jne 0x5f77
cmpb $0x2e, (%r15)
jne 0x5f77
cmpb $0x2e, 0x1(%r15)
jne 0x5f77
movq %rbp, %rdx
subq 0x8(%rsp), %rdx
je 0x5f96
movq 0x8(%rsp), %rdi
movl %r13d, %esi
callq 0x46f0
movq %rax, %rbp
movb $0x0, (%rax)
jmp 0x5f96
cmpq %r15, %r14
je 0x5f96
leaq 0x1(%rbp), %rdi
movb $0x2f, (%rbp)
leaq 0x1(%rbx), %rdx
movq %r15, %rsi
callq 0x6016
addq %rbx, %rbp
incq %rbp
leaq 0x1(%r14), %r15
movq %r12, %rdx
subq %r15, %rdx
jmp 0x5f1b
cmpb $0x2e, (%r15)
sete %al
cmpq %r15, %r14
sete %cl
orb %al, %cl
je 0x5f7c
jmp 0x5f96
movq 0x8(%rsp), %rbx
cmpb $0x0, (%rbx)
jne 0x5fc7
movw $0x2f, (%rbx)
movq %rbx, %rdi
callq 0x5d70
cmpq $0x1000, %rax           # imm = 0x1000
movq 0x10(%rsp), %rax
jb 0x6004
testq %rax, %rax
je 0x5ffa
movb $0x0, (%rax)
jmp 0x6002
testq %r14, %r14
jne 0x6002
movq %rax, %rdi
jmp 0x5ffd
testb %bpl, %bpl
je 0x6002
movq %r13, %rdi
jmp 0x5ffd
movq %rbx, %rdi
callq 0x4090
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq