// File: /workspace/github_c_makefile_star2_O1/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 0x6270
movq %rdx, %rbx
movq %rsi, %r13
movq %rdi, %r15
movb (%rdi), %al
cmpb $0x7e, %al
jne 0x6075
cmpb $0x2f, 0x1(%r15)
jne 0x6075
movq %rbx, %r14
movq 0x145d1(%rip), %r13     # 0x1a640
addq $0x2, %r15
jmp 0x6087
cmpb $0x2f, %al
sete %al
testq %r13, %r13
setne %cl
orb %al, %cl
je 0x6091
movq %rbx, %r14
movl $0x0, 0x8(%rsp)
jmp 0x60b2
xorl %r14d, %r14d
xorl %edi, %edi
xorl %esi, %esi
callq 0x43a0
testq %rax, %rax
je 0x6273
movq %rax, %r13
movq %rbx, %r14
movb $0x1, %al
movl %eax, 0x8(%rsp)
xorl %ebp, %ebp
movq %r15, %rdi
xorl %esi, %esi
callq 0x4490
movq %rax, %rbx
testq %r13, %r13
je 0x60d6
movq %r13, %rdi
xorl %esi, %esi
callq 0x4490
movq %rax, %rbp
subq %r13, %rbp
movq %rbx, %r12
subq %r15, %r12
movq %r14, 0x10(%rsp)
testq %r14, %r14
jne 0x60f6
leaq (%r12,%rbp), %rdi
addq $0x2, %rdi
callq 0x45c0
movq %rax, %r14
testq %r14, %r14
je 0x6109
cmpb $0x2f, (%r15)
jne 0x611c
movb $0x0, (%r14)
xorl %ebp, %ebp
jmp 0x6166
cmpb $0x0, 0x8(%rsp)
je 0x6270
movq %r13, %rdi
jmp 0x626b
testq %r13, %r13
je 0x6260
incq %rbp
movq %r14, %rdi
movq %r13, %rsi
xorl %edx, %edx
movq %rbp, %rcx
callq 0x4860
testq %rax, %rax
jne 0x614e
testq %rbp, %rbp
je 0x6148
movb $0x0, -0x1(%r14,%rbp)
addq %r14, %rbp
movq %rbp, %rax
movq %r14, %rbp
notq %rbp
addq %rax, %rbp
cmpb $0x0, 0x8(%rsp)
je 0x6166
movq %r13, %rdi
callq 0x4090
movq %r14, 0x8(%rsp)
addq %r14, %rbp
leaq (%r15,%r12), %r14
movq %r15, %rdi
movl $0x2f, %esi
movq %r12, %rdx
callq 0x4450
movq %rax, %r13
testq %rax, %rax
cmoveq %r14, %r13
movq %r13, %r12
subq %r15, %r12
cmpq $0x1, %r12
je 0x61d2
cmpq $0x2, %r12
jne 0x61e5
cmpb $0x2e, (%r15)
jne 0x61cc
cmpb $0x2e, 0x1(%r15)
jne 0x61cc
movq %rbp, %rdx
subq 0x8(%rsp), %rdx
je 0x6218
movq 0x8(%rsp), %rdi
movl $0x2f, %esi
callq 0x46f0
movq %rax, %rbp
movb $0x0, (%rax)
jmp 0x6218
cmpq $0x1, %r12
jne 0x61e5
cmpb $0x2e, (%r15)
sete %al
cmpq %r15, %r13
sete %cl
orb %al, %cl
je 0x61ea
jmp 0x6218
cmpq %r15, %r13
je 0x6218
movb $0x2f, (%rbp)
incq %rbp
leaq 0x1(%r12), %rcx
movq %rbp, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x4860
movq %r12, %rcx
incq %rcx
je 0x6215
testq %rax, %rax
jne 0x6215
movb $0x0, -0x1(%rbp,%rcx)
addq %r12, %rbp
leaq 0x1(%r13), %r15
movq %rbx, %r12
subq %r15, %r12
cmpq %r14, %r13
jne 0x6172
movq 0x8(%rsp), %r14
cmpb $0x0, (%r14)
jne 0x623c
movw $0x2f, (%r14)
movq %r14, %rdi
xorl %esi, %esi
callq 0x4490
subq %r14, %rax
cmpq $0x1000, %rax           # imm = 0x1000
movq 0x10(%rsp), %rax
jb 0x6273
testq %rax, %rax
je 0x6268
movb $0x0, (%rax)
jmp 0x6270
cmpq $0x0, 0x10(%rsp)
jne 0x6270
movq %r14, %rdi
callq 0x4090
xorl %r14d, %r14d
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq