// File: /workspace/github_c_makefile_star2_O3/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, %rbx
movq %rsi, 0x20(%rsp)
movq %rdi, %r12
testq %rdi, %rdi
setne %r14b
movl 0xabbd(%rip), %r15d     # 0x1a524
movq 0xbffa(%rip), %rdi      # 0x1b968
leaq 0x64c0(%rip), %rsi      # 0x15e35
leaq 0x10b0(%rsp), %rdx
callq 0x6d5c
testl %r13d, %r13d
je 0xf9a5
movq %rbp, 0x10(%rsp)
leaq 0x6998(%rip), %rsi      # 0x1632b
leaq 0x10b0(%rsp), %rdi
leaq 0xb0(%rsp), %rdx
jmp 0xf9c9
testq %r12, %r12
je 0xf9d0
cmpb $0x0, (%r12)
je 0xf9d0
movq %rbp, 0x10(%rsp)
leaq 0x10b0(%rsp), %rdi
leaq 0xb0(%rsp), %rdx
movq %r12, %rsi
callq 0x6d5c
jmp 0xfa4d
leaq 0x652b(%rip), %rsi      # 0x15f02
xorl %r12d, %r12d
xorl %edi, %edi
callq 0x11f73
cmpb $0x0, 0xad68(%rip)      # 0x1a750
je 0xfce9
movq %rbx, 0x8(%rsp)
movl %r14d, %ebx
orb %r13b, %bl
leaq 0xad50(%rip), %r12      # 0x1a750
leaq 0x10b0(%rsp), %rdi
leaq 0xb0(%rsp), %rdx
movq %r12, %rsi
callq 0x6d5c
cmpb $0x40, 0xad31(%rip)     # 0x1a750
movq %rbp, 0x10(%rsp)
jne 0xfa36
cmpb $0x0, 0xad24(%rip)      # 0x1a751
sete %al
orb %al, %bl
je 0xfa3a
jmp 0xfa48
testb %bl, %bl
jne 0xfa48
leaq 0x68ea(%rip), %rdi      # 0x1632b
xorl %esi, %esi
callq 0xe5e7
movq 0x8(%rsp), %rbx
leaq 0xb0(%rsp), %rdi
xorl %esi, %esi
movl $0x180, %edx            # imm = 0x180
xorl %eax, %eax
callq 0x46b0
cmpl $-0x1, %eax
je 0xfcb4
movl %eax, %ebp
movq %rbx, 0x8(%rsp)
movl %r13d, 0x1c(%rsp)
leaq 0x28(%rsp), %rsi
movl $0x88, %edx
movl %eax, %edi
callq 0x4460
cmpq $0x88, %rax
jne 0xfca4
cmpq $0x1, 0x28(%rsp)
jne 0xfca4
leaq 0xaa20(%rip), %rsi      # 0x1a4c8
movl $0x4, %edx
movl %ebp, %edi
callq 0x4460
cmpq $0x4, %rax
jne 0xfca4
movl 0xaa04(%rip), %eax      # 0x1a4c8
shrl $0xd, %eax
andl $0x7, %eax
imulq $0x2138, %rax, %rax     # imm = 0x2138
leaq 0xc0b8(%rip), %r15      # 0x1bb90
movw $0x0, 0x2100(%rax,%r15)
xorl %r13d, %r13d
movb %r13b, 0x1000(%rax,%r15)
movb %r13b, 0x2000(%rax,%r15)
leaq 0x90(%rsp), %rbx
leaq (%r15,%r13), %rsi
addq $0x2130, %rsi           # imm = 0x2130
movl $0x4, %edx
movl %ebp, %edi
callq 0x4460
cmpq $0x4, %rax
jne 0xfca4
leaq (%r15,%r13), %rsi
addq $0x2134, %rsi           # imm = 0x2134
movl $0x4, %edx
movl %ebp, %edi
callq 0x4460
cmpq $0x4, %rax
jne 0xfca4
movq -0x20(%rbx), %r14
testq %r14, %r14
je 0xfb67
leaq (%r15,%r13), %rsi
addq $0x2000, %rsi           # imm = 0x2000
movl %ebp, %edi
movq %r14, %rdx
callq 0x4460
cmpq %r14, %rax
jne 0xfca4
movq -0x40(%rbx), %r14
testq %r14, %r14
je 0xfb8e
leaq (%r15,%r13), %rsi
addq $0x1000, %rsi           # imm = 0x1000
movl %ebp, %edi
movq %r14, %rdx
callq 0x4460
cmpq %r14, %rax
jne 0xfca4
movq (%rbx), %r14
testq %r14, %r14
je 0xfbb4
leaq (%r15,%r13), %rsi
addq $0x2100, %rsi           # imm = 0x2100
movl %ebp, %edi
movq %r14, %rdx
callq 0x4460
cmpq %r14, %rax
jne 0xfca4
movq -0x60(%rbx), %r14
testq %r14, %r14
je 0xfbd4
leaq (%r15,%r13), %rsi
movl %ebp, %edi
movq %r14, %rdx
callq 0x4460
cmpq %r14, %rax
jne 0xfca4
addq $0x8, %rbx
addq $0x2138, %r13           # imm = 0x2138
cmpq $0x84e0, %r13           # imm = 0x84E0
jne 0xfafe
movl 0xa8d6(%rip), %eax      # 0x1a4c8
shrl $0xd, %eax
andl $0x7, %eax
imulq $0x2138, %rax, %rax     # imm = 0x2138
leaq (%r15,%rax), %rcx
movq 0x20(%rsp), %rdx
movq %rcx, (%rdx)
leaq (%rax,%r15), %rcx
addq $0x1000, %rcx           # imm = 0x1000
movq 0x8(%rsp), %rdx
movq %rcx, (%rdx)
addq %r15, %rax
addq $0x2000, %rax           # imm = 0x2000
movq 0x10(%rsp), %rcx
movq %rax, (%rcx)
xorl %edi, %edi
callq 0x71ac
btl $0x19, 0xa88a(%rip)     # 0x1a4c8
leaq -0x1002(%rip), %rax     # 0xec43
leaq -0x9cf4(%rip), %rcx     # 0x5f58
cmovaeq %rax, %rcx
movq %rcx, 0xa841(%rip)      # 0x1a498
testq %r12, %r12
leaq 0x66ca(%rip), %rsi      # 0x1632b
cmovneq %r12, %rsi
leaq 0xa8c4(%rip), %rdi      # 0x1a530
movl $0xff, %ecx
xorl %edx, %edx
callq 0x4860
testq %rax, %rax
jne 0xfc84
movb $0x0, 0xa9aa(%rip)      # 0x1a62e
movl %ebp, %edi
callq 0x43c0
movb $0x1, %r12b
cmpb $0x0, 0x1c(%rsp)
je 0xfce9
leaq 0xb0(%rsp), %rdi
callq 0x40d0
jmp 0xfce9
movl %ebp, %edi
callq 0x43c0
leaq 0x6248(%rip), %rdi      # 0x15efa
jmp 0xfcc8
shrl $0xc, %r15d
andl $0x1, %r15d
andb %r15b, %r14b
jne 0xfce6
leaq 0x6376(%rip), %rdi      # 0x1603e
callq 0xec60
movq 0xa2bc(%rip), %rax      # 0x19f90
movq (%rax), %rdi
callq 0x4440
movl $0x55730, %edi          # imm = 0x55730
callq 0x48f0
xorl %r12d, %r12d
movl %r12d, %eax
addq $0x20b8, %rsp           # imm = 0x20B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq