// File: /workspace/github_c_makefile_star2_O1/jarun[P]nnn/src/nnn.c
// Function: load_session
static bool load_session(const char *sname, char **path, char **lastdir, char **lastname, bool restore)
{
	int fd, i = 0;
	session_header_t header;
	bool has_loaded_dynamically = !(sname || restore);
	bool status = (sname && g_state.picker); /* Picker mode with session program option */
	char ssnpath[PATH_MAX];
	char spath[PATH_MAX];

	mkpath(cfgpath, toks[TOK_SSN], ssnpath);

	if (!restore) {
		if (!sname || !sname[0])
			sname = xreadline(NULL, messages[MSG_SSN_NAME]);
		if (!sname[0])
			return FALSE;

		mkpath(ssnpath, sname, spath);

		/* If user is explicitly loading the "last session", skip auto-save */
		if ((sname[0] == '@') && !sname[1])
			has_loaded_dynamically = FALSE;
	} else
		mkpath(ssnpath, "@", spath);

	if (has_loaded_dynamically)
		save_session("@", NULL);

	fd = open(spath, O_RDONLY, S_IWUSR | S_IRUSR);
	if (fd == -1) {
		if (!status) {
			printmsg(messages[MSG_SEL_MISSING]);
			xdelay(XDELAY_INTERVAL_MS);
		}
		return FALSE;
	}

	status = FALSE;

	if ((read(fd, &header, sizeof(header)) != (ssize_t)sizeof(header))
		|| (header.ver != SESSIONS_VERSION)
		|| (read(fd, &cfg, sizeof(cfg)) != (ssize_t)sizeof(cfg)))
		goto END;

	g_ctx[cfg.curctx].c_name[0] = g_ctx[cfg.curctx].c_last[0]
		= g_ctx[cfg.curctx].c_fltr[0] = g_ctx[cfg.curctx].c_fltr[1] = '\0';

	for (; i < CTX_MAX; ++i)
		if ((read(fd, &g_ctx[i].c_cfg, sizeof(settings)) != (ssize_t)sizeof(settings))
			|| (read(fd, &g_ctx[i].color, sizeof(uint_t)) != (ssize_t)sizeof(uint_t))
			|| (header.nameln[i] > 0
			    && read(fd, g_ctx[i].c_name, header.nameln[i]) != (ssize_t)header.nameln[i])
			|| (header.lastln[i] > 0
			    && read(fd, g_ctx[i].c_last, header.lastln[i]) != (ssize_t)header.lastln[i])
			|| (header.fltrln[i] > 0
			    && read(fd, g_ctx[i].c_fltr, header.fltrln[i]) != (ssize_t)header.fltrln[i])
			|| (header.pathln[i] > 0
			    && read(fd, g_ctx[i].c_path, header.pathln[i]) != (ssize_t)header.pathln[i]))
			goto END;

	*path = g_ctx[cfg.curctx].c_path;
	*lastdir = g_ctx[cfg.curctx].c_last;
	*lastname = g_ctx[cfg.curctx].c_name;
	/* Set correct sort and filter options */
	set_sort_flags('\0');
	filterfn = cfg.regex ? &visible_re : &visible_str;
	xstrsncpy(curssn, sname ? sname : "@", NAME_MAX);
	status = TRUE;

END:
	close(fd);

	if (!status) {
		printmsg(messages[MSG_FAILED]);
		xdelay(XDELAY_INTERVAL_MS);
	} else if (restore)
		unlink(spath);

	return status;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x20b8, %rsp           # imm = 0x20B8
movl %r8d, %r13d
movq %rcx, %rbp
movq %rdx, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, %r14
testq %rdi, %rdi
setne %r15b
movl %r15d, %ebx
orb %r13b, %bl
xorb $0x1, %bl
movl 0xa97a(%rip), %r12d     # 0x1a524
movq 0xbdb7(%rip), %rdi      # 0x1b968
leaq 0x6263(%rip), %rsi      # 0x15e1b
leaq 0x10b0(%rsp), %rdx
callq 0x6dd5
testl %r13d, %r13d
je 0xfbe8
leaq 0x6740(%rip), %rsi      # 0x16311
leaq 0x10b0(%rsp), %rdi
leaq 0xb0(%rsp), %rdx
callq 0x6dd5
jmp 0xfc3a
testq %r14, %r14
je 0xfbf3
cmpb $0x0, (%r14)
jne 0xfc08
leaq 0x62ee(%rip), %rsi      # 0x15ee8
xorl %edi, %edi
callq 0x122b4
leaq 0xab48(%rip), %r14      # 0x1a750
cmpb $0x0, (%r14)
je 0xfec2
leaq 0x10b0(%rsp), %rdi
leaq 0xb0(%rsp), %rdx
movq %r14, %rsi
callq 0x6dd5
cmpb $0x40, (%r14)
jne 0xfc3a
cmpb $0x0, 0x1(%r14)
setne %al
andb %al, %bl
movq %rbp, 0x10(%rsp)
testb %bl, %bl
je 0xfc51
leaq 0x66c7(%rip), %rdi      # 0x16311
xorl %esi, %esi
callq 0xe80d
leaq 0xb0(%rsp), %rdi
xorl %esi, %esi
movl $0x180, %edx            # imm = 0x180
xorl %eax, %eax
callq 0x46b0
cmpl $-0x1, %eax
je 0xfe90
movl %eax, %ebp
leaq 0x28(%rsp), %rsi
movl $0x88, %edx
movl %eax, %edi
callq 0x4460
xorl %r15d, %r15d
cmpq $0x88, %rax
jne 0xfed4
cmpq $0x1, 0x28(%rsp)
jne 0xfed4
leaq 0xa823(%rip), %rsi      # 0x1a4c8
movl $0x4, %edx
movl %ebp, %edi
callq 0x4460
cmpq $0x4, %rax
jne 0xfec7
movl %r13d, 0xc(%rsp)
movl 0xa802(%rip), %eax      # 0x1a4c8
shrl $0xd, %eax
andl $0x7, %eax
imulq $0x2138, %rax, %rax     # imm = 0x2138
leaq 0xbeb6(%rip), %r12      # 0x1bb90
movw $0x0, 0x2100(%rax,%r12)
xorl %r13d, %r13d
movb %r13b, 0x1000(%rax,%r12)
movb %r13b, 0x2000(%rax,%r12)
leaq 0x90(%rsp), %rbx
leaq (%r12,%r13), %rsi
addq $0x2130, %rsi           # imm = 0x2130
movl $0x4, %edx
movl %ebp, %edi
callq 0x4460
cmpq $0x4, %rax
jne 0xfecc
leaq (%r12,%r13), %rsi
addq $0x2134, %rsi           # imm = 0x2134
movl $0x4, %edx
movl %ebp, %edi
callq 0x4460
cmpq $0x4, %rax
jne 0xfecc
movq -0x20(%rbx), %r15
testq %r15, %r15
je 0xfd69
leaq (%r12,%r13), %rsi
addq $0x2000, %rsi           # imm = 0x2000
movl %ebp, %edi
movq %r15, %rdx
callq 0x4460
cmpq %r15, %rax
jne 0xfecc
movq -0x40(%rbx), %r15
testq %r15, %r15
je 0xfd90
leaq (%r12,%r13), %rsi
addq $0x1000, %rsi           # imm = 0x1000
movl %ebp, %edi
movq %r15, %rdx
callq 0x4460
cmpq %r15, %rax
jne 0xfecc
movq (%rbx), %r15
testq %r15, %r15
je 0xfdb6
leaq (%r12,%r13), %rsi
addq $0x2100, %rsi           # imm = 0x2100
movl %ebp, %edi
movq %r15, %rdx
callq 0x4460
cmpq %r15, %rax
jne 0xfecc
movq -0x60(%rbx), %r15
testq %r15, %r15
je 0xfdd6
leaq (%r12,%r13), %rsi
movl %ebp, %edi
movq %r15, %rdx
callq 0x4460
cmpq %r15, %rax
jne 0xfecc
addq $0x8, %rbx
addq $0x2138, %r13           # imm = 0x2138
cmpq $0x84e0, %r13           # imm = 0x84E0
jne 0xfd00
movl 0xa6d4(%rip), %eax      # 0x1a4c8
shrl $0xd, %eax
andl $0x7, %eax
imulq $0x2138, %rax, %rax     # imm = 0x2138
leaq (%r12,%rax), %rcx
movq 0x18(%rsp), %rdx
movq %rcx, (%rdx)
leaq (%rax,%r12), %rcx
addq $0x1000, %rcx           # imm = 0x1000
movq 0x20(%rsp), %rdx
movq %rcx, (%rdx)
addq %r12, %rax
addq $0x2000, %rax           # imm = 0x2000
movq 0x10(%rsp), %rcx
movq %rax, (%rcx)
xorl %edi, %edi
callq 0x7237
btl $0x19, 0xa688(%rip)     # 0x1a4c8
leaq -0xff5(%rip), %rax      # 0xee52
leaq -0x9ec1(%rip), %rcx     # 0x5f8d
cmovaeq %rax, %rcx
movq %rcx, 0xa63f(%rip)      # 0x1a498
testq %r14, %r14
leaq 0x64ae(%rip), %rsi      # 0x16311
cmovneq %r14, %rsi
leaq 0xa6c2(%rip), %rdi      # 0x1a530
movl $0xff, %ecx
xorl %edx, %edx
callq 0x4860
movb $0x1, %r15b
testq %rax, %rax
movl 0xc(%rsp), %r13d
jne 0xfed4
movb $0x0, 0xa7a0(%rip)      # 0x1a62e
jmp 0xfed4
shrl $0xc, %r12d
andl $0x1, %r12d
andb %r12b, %r15b
jne 0xfec2
leaq 0x6180(%rip), %rdi      # 0x16024
callq 0xee6f
movq 0xa0e0(%rip), %rax      # 0x19f90
movq (%rax), %rdi
callq 0x4440
movl $0x55730, %edi          # imm = 0x55730
callq 0x48f0
xorl %r15d, %r15d
jmp 0xff19
xorl %r15d, %r15d
jmp 0xfed4
xorl %r15d, %r15d
movl 0xc(%rsp), %r13d
movl %ebp, %edi
callq 0x43c0
testb %r15b, %r15b
je 0xfef4
testb %r13b, %r13b
je 0xff19
leaq 0xb0(%rsp), %rdi
callq 0x40d0
jmp 0xff19
leaq 0x5fe5(%rip), %rdi      # 0x15ee0
callq 0xee6f
movq 0xa089(%rip), %rax      # 0x19f90
movq (%rax), %rdi
callq 0x4440
movl $0x55730, %edi          # imm = 0x55730
callq 0x48f0
movl %r15d, %eax
addq $0x20b8, %rsp           # imm = 0x20B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq