// File: /workspace/github_c_makefile_star2_O2/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, %ebp
movq %rcx, 0x20(%rsp)
movq %rdx, 0x18(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, %r15
testq %rdi, %rdi
setne %r12b
movl 0xb307(%rip), %r13d     # 0x19524
movq 0xc744(%rip), %rdi      # 0x1a968
leaq 0x5bee(%rip), %rsi      # 0x13e19
leaq 0x10b0(%rsp), %rdx
callq 0x6a9f
testl %ebp, %ebp
je 0xe25e
movl %ebp, 0xc(%rsp)
leaq 0x60c8(%rip), %rsi      # 0x1430f
leaq 0x10b0(%rsp), %rdi
leaq 0xb0(%rsp), %rdx
callq 0x6a9f
jmp 0xe2d6
testq %r15, %r15
je 0xe269
cmpb $0x0, (%r15)
jne 0xe28e
leaq 0x5c76(%rip), %rsi      # 0x13ee6
xorl %r14d, %r14d
xorl %edi, %edi
callq 0x102bd
cmpb $0x0, 0xb4cf(%rip)      # 0x19750
je 0xe48f
leaq 0xb4c2(%rip), %r15      # 0x19750
movl %r12d, %ebx
orb %bpl, %bl
leaq 0x10b0(%rsp), %rdi
leaq 0xb0(%rsp), %rdx
movq %r15, %rsi
callq 0x6a9f
cmpb $0x40, (%r15)
movl %ebp, 0xc(%rsp)
jne 0xe2c4
cmpb $0x0, 0x1(%r15)
sete %al
orb %al, %bl
je 0xe2c8
jmp 0xe2d6
testb %bl, %bl
jne 0xe2d6
leaq 0x6040(%rip), %rdi      # 0x1430f
xorl %esi, %esi
callq 0xcf5f
leaq 0xb0(%rsp), %rdi
xorl %esi, %esi
movl $0x180, %edx            # imm = 0x180
xorl %eax, %eax
callq 0x46b0
cmpl $-0x1, %eax
je 0xe469
movl %eax, %ebp
leaq 0x28(%rsp), %rsi
movl $0x88, %edx
movl %eax, %edi
callq 0x4460
cmpq $0x88, %rax
jne 0xe459
cmpq $0x1, 0x28(%rsp)
jne 0xe459
leaq 0xb1a1(%rip), %rsi      # 0x194c8
pushq $0x4
popq %rdx
movl %ebp, %edi
callq 0x4460
cmpq $0x4, %rax
jne 0xe459
movl 0xb187(%rip), %eax      # 0x194c8
shrl $0xd, %eax
andl $0x7, %eax
imulq $0x2138, %rax, %rax     # imm = 0x2138
leaq 0xc83b(%rip), %r13      # 0x1ab90
andw $0x0, 0x2100(%rax,%r13)
xorl %r14d, %r14d
movb %r14b, 0x1000(%rax,%r13)
movb %r14b, 0x2000(%rax,%r13)
leaq 0x90(%rsp), %rbx
cmpq $0x84e0, %r14           # imm = 0x84E0
je 0xe4a4
leaq (%r14,%r13), %rsi
addq $0x2130, %rsi           # imm = 0x2130
movl %ebp, %edi
pushq $0x4
popq %rdx
callq 0x4460
cmpq $0x4, %rax
jne 0xe459
leaq (%r14,%r13), %rsi
addq $0x2134, %rsi           # imm = 0x2134
movl %ebp, %edi
pushq $0x4
popq %rdx
callq 0x4460
cmpq $0x4, %rax
jne 0xe459
movq -0x20(%rbx), %r12
testq %r12, %r12
je 0xe3e8
leaq (%r14,%r13), %rsi
addq $0x2000, %rsi           # imm = 0x2000
movl %ebp, %edi
movq %r12, %rdx
callq 0x4460
cmpq %r12, %rax
jne 0xe459
movq -0x40(%rbx), %r12
testq %r12, %r12
je 0xe40b
leaq (%r14,%r13), %rsi
addq $0x1000, %rsi           # imm = 0x1000
movl %ebp, %edi
movq %r12, %rdx
callq 0x4460
cmpq %r12, %rax
jne 0xe459
movq (%rbx), %r12
testq %r12, %r12
je 0xe42d
leaq (%r14,%r13), %rsi
addq $0x2100, %rsi           # imm = 0x2100
movl %ebp, %edi
movq %r12, %rdx
callq 0x4460
cmpq %r12, %rax
jne 0xe459
movq -0x60(%rbx), %r12
testq %r12, %r12
je 0xe449
leaq (%r14,%r13), %rsi
movl %ebp, %edi
movq %r12, %rdx
callq 0x4460
cmpq %r12, %rax
jne 0xe459
addq $0x2138, %r14           # imm = 0x2138
addq $0x8, %rbx
jmp 0xe37a
movl %ebp, %edi
callq 0x43c0
leaq 0x5a77(%rip), %rdi      # 0x13ede
jmp 0xe47d
shrl $0xc, %r13d
andl $0x1, %r13d
andb %r13b, %r12b
jne 0xe48c
leaq 0x5ba5(%rip), %rdi      # 0x14022
callq 0xd5b9
movl $0x55730, %edi          # imm = 0x55730
callq 0xd617
xorl %r14d, %r14d
movl %r14d, %eax
addq $0x20b8, %rsp           # imm = 0x20B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0xb01e(%rip), %eax      # 0x194c8
shrl $0xd, %eax
andl $0x7, %eax
imulq $0x2138, %rax, %rax     # imm = 0x2138
leaq (%rax,%r13), %rcx
movq 0x10(%rsp), %rdx
movq %rcx, (%rdx)
leaq (%rax,%r13), %rcx
addq $0x1000, %rcx           # imm = 0x1000
movq 0x18(%rsp), %rdx
movq %rcx, (%rdx)
addq %r13, %rax
addq $0x2000, %rax           # imm = 0x2000
movq 0x20(%rsp), %rcx
movq %rax, (%rcx)
xorl %edi, %edi
callq 0x6b40
btl $0x19, 0xafd2(%rip)     # 0x194c8
leaq -0xfa8(%rip), %rax      # 0xd555
leaq -0x8784(%rip), %rcx     # 0x5d80
cmovaeq %rax, %rcx
movq %rcx, 0xaf89(%rip)      # 0x19498
testq %r15, %r15
leaq 0x5df6(%rip), %rsi      # 0x1430f
cmovneq %r15, %rsi
leaq 0xb00c(%rip), %rdi      # 0x19530
movl $0xff, %edx
callq 0x6016
movl %ebp, %edi
callq 0x43c0
movb $0x1, %r14b
cmpb $0x0, 0xc(%rsp)
je 0xe48f
leaq 0xb0(%rsp), %rdi
callq 0x40d0
jmp 0xe48f