// File: /workspace/github_c_makefile_star2_O3/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 $0x28, %rsp
testq %rdi, %rdi
je 0x6276
movq %rdx, %rbx
movq %rsi, %r12
movq %rdi, %r14
movb (%rdi), %al
cmpb $0x7e, %al
jne 0x603d
cmpb $0x2f, 0x1(%r14)
jne 0x603d
movq 0x14609(%rip), %r12     # 0x1a640
addq $0x2, %r14
jmp 0x604c
cmpb $0x2f, %al
sete %al
testq %r12, %r12
setne %cl
orb %al, %cl
je 0x606c
movq %r14, %rdi
xorl %esi, %esi
callq 0x4490
movq %rax, %r13
subq %r14, %r13
testq %r12, %r12
je 0x60b8
movq $0x0, 0x18(%rsp)
jmp 0x609b
xorl %r15d, %r15d
xorl %edi, %edi
xorl %esi, %esi
callq 0x43a0
testq %rax, %rax
je 0x6279
movq %rax, %r12
movq %r14, %rdi
xorl %esi, %esi
callq 0x4490
movq %rax, %r13
subq %r14, %r13
movb $0x1, %al
movq %rax, 0x18(%rsp)
movl $0x0, 0x14(%rsp)
movq %r12, %rdi
xorl %esi, %esi
callq 0x4490
movq %rax, %r15
subq %r12, %r15
movq %r14, %rbp
jmp 0x60d0
movq $0x0, 0x18(%rsp)
movb $0x1, %al
movl %eax, 0x14(%rsp)
xorl %r12d, %r12d
movq %r14, %rbp
xorl %r15d, %r15d
movq %rbx, 0x8(%rsp)
movq %rbx, 0x20(%rsp)
testq %rbx, %rbx
jne 0x60fa
leaq (%r15,%r13), %rdi
addq $0x2, %rdi
callq 0x45c0
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x6262
cmpb $0x2f, (%rbp)
jne 0x610d
movq 0x8(%rsp), %rax
movb $0x0, (%rax)
xorl %r15d, %r15d
jmp 0x6175
cmpb $0x0, 0x14(%rsp)
je 0x612a
cmpq $0x0, 0x20(%rsp)
jne 0x6276
movq 0x8(%rsp), %rdi
jmp 0x6271
incq %r15
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
xorl %edx, %edx
movq %r15, %rcx
callq 0x4860
testq %rax, %rax
jne 0x6158
testq %r15, %r15
je 0x6152
movb $0x0, -0x1(%rbx,%r15)
addq %rbx, %r15
movq %r15, %rax
movq %rbx, %r15
notq %r15
addq %rax, %r15
cmpb $0x0, 0x18(%rsp)
je 0x6170
movq %r12, %rdi
callq 0x4090
movq 0x8(%rsp), %rax
movq %rax, 0x8(%rsp)
addq %rax, %r15
movq %rbp, %rbx
addq %r13, %rbx
addq %r13, %r14
movq %rbp, %rdi
movl $0x2f, %esi
movq %r13, %rdx
callq 0x4450
movq %rax, %r12
testq %rax, %rax
cmoveq %rbx, %r12
movq %r12, %r13
subq %rbp, %r13
cmpq $0x1, %r13
je 0x61e0
cmpq $0x2, %r13
jne 0x61e6
cmpb $0x2e, (%rbp)
jne 0x61e6
cmpb $0x2e, 0x1(%rbp)
jne 0x61e6
movq 0x8(%rsp), %rdi
cmpq %rdi, %r15
je 0x6219
subq %rdi, %r15
movl $0x2f, %esi
movq %r15, %rdx
callq 0x46f0
movq %rax, %r15
movb $0x0, (%rax)
jmp 0x6219
cmpb $0x2e, (%rbp)
je 0x6219
cmpq %rbp, %r12
je 0x6219
movb $0x2f, (%r15)
incq %r15
leaq 0x1(%r13), %rcx
movq %r15, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x4860
movq %r13, %rcx
incq %rcx
je 0x6216
testq %rax, %rax
jne 0x6216
movb $0x0, -0x1(%r15,%rcx)
addq %r13, %r15
leaq 0x1(%r12), %rbp
movq %r14, %r13
subq %rbp, %r13
cmpq %rbx, %r12
jne 0x6186
movq 0x8(%rsp), %r15
cmpb $0x0, (%r15)
jne 0x623e
movw $0x2f, (%r15)
movq %r15, %rdi
xorl %esi, %esi
callq 0x4490
subq %r15, %rax
cmpq $0x1000, %rax           # imm = 0x1000
movq 0x20(%rsp), %rax
jb 0x6279
testq %rax, %rax
je 0x626e
movb $0x0, (%rax)
jmp 0x6276
cmpb $0x0, 0x18(%rsp)
je 0x6276
movq %r12, %rdi
jmp 0x6271
movq %r15, %rdi
callq 0x4090
xorl %r15d, %r15d
movq %r15, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq