// File: /workspace/github_c_makefile_star2_O1/jarun[P]nnn/src/nnn.c
// Function: save_session
static void save_session(const char *sname, int *presel)
{
	int fd, i;
	session_header_t header = {0};
	bool status = FALSE;
	char ssnpath[PATH_MAX];
	char spath[PATH_MAX];

	header.ver = SESSIONS_VERSION;

	for (i = 0; i < CTX_MAX; ++i) {
		if (g_ctx[i].c_cfg.ctxactive) {
			if (cfg.curctx == i && ndents)
				/* Update current file name, arrows don't update it */
				xstrsncpy(g_ctx[i].c_name, pdents[cur].name, NAME_MAX + 1);
			header.pathln[i] = MIN(xstrlen(g_ctx[i].c_path), PATH_MAX) + 1;
			header.lastln[i] = MIN(xstrlen(g_ctx[i].c_last), PATH_MAX) + 1;
			header.nameln[i] = MIN(xstrlen(g_ctx[i].c_name), NAME_MAX) + 1;
			header.fltrln[i] = REGEX_MAX;
		}
	}

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

	fd = open(spath, O_CREAT | O_WRONLY | O_TRUNC, S_IWUSR | S_IRUSR);
	if (fd == -1) {
		printwait(messages[MSG_SEL_MISSING], presel);
		return;
	}

	if ((write(fd, &header, sizeof(header)) != (ssize_t)sizeof(header))
		|| (write(fd, &cfg, sizeof(cfg)) != (ssize_t)sizeof(cfg)))
		goto END;

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

	status = TRUE;

END:
	close(fd);

	if (!status)
		printwait(messages[MSG_FAILED], presel);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x20b8, %rsp           # imm = 0x20B8
movq %rsi, 0x8(%rsp)
movq %rdi, 0x10(%rsp)
leaq 0x28(%rsp), %r15
xorl %ebp, %ebp
movl $0x88, %edx
movq %r15, %rdi
xorl %esi, %esi
callq 0x4380
movq $0x1, (%r15)
movl 0xbc7d(%rip), %eax      # 0x1a4c8
shrl $0xd, %eax
andl $0x7, %eax
movl 0xd335(%rip), %ecx      # 0x1bb8c
movl %ecx, 0x4(%rsp)
movslq 0x15816(%rip), %rcx     # 0x24078
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
addq 0x157ff(%rip), %rcx     # 0x24070
movq %rcx, 0x18(%rsp)
leaq 0x90(%rsp), %r14
imulq $0x2138, %rax, %rax     # imm = 0x2138
movq %rax, 0x20(%rsp)
leaq 0xf2ff(%rip), %r15      # 0x1db90
movl $0x1001, %r13d          # imm = 0x1001
movq $-0x1000, %rbx          # imm = 0xF000
testb $0x2, 0x131(%r15)
je 0xe977
movq 0x20(%rsp), %rax
addq %rbp, %rax
jne 0xe8e1
cmpl $0x0, 0x4(%rsp)
je 0xe8e1
movq 0x18(%rsp), %rax
movq (%rax), %rsi
movl $0x100, %ecx            # imm = 0x100
movq %r15, %rdi
xorl %edx, %edx
callq 0x4860
testq %rax, %rax
jne 0xe8e1
movb $0x0, 0xff(%r15)
leaq -0x2000(%r15), %rdi
xorl %esi, %esi
callq 0x4490
movq %rbp, %r12
leaq 0xd297(%rip), %rcx      # 0x1bb90
subq %rcx, %r12
leaq (%r12,%rax), %rcx
movl $0x1000, %edx           # imm = 0x1000
cmpq %rdx, %rcx
leaq 0x1(%r12,%rax), %rax
cmovaeq %r13, %rax
movq %rax, -0x60(%r14)
leaq (%r15,%rbx), %rdi
xorl %esi, %esi
callq 0x4490
leaq (%r12,%rbx), %rcx
addq %rax, %rcx
movl $0x1000, %edx           # imm = 0x1000
cmpq %rdx, %rcx
leaq -0xfff(%r12,%rax), %rax
cmovaeq %r13, %rax
movq %rax, -0x40(%r14)
movq %r15, %rdi
xorl %esi, %esi
callq 0x4490
leaq (%r12,%rax), %rcx
addq $-0x2000, %rcx          # imm = 0xE000
cmpq $0xff, %rcx
leaq -0x1fff(%r12,%rax), %rax
movl $0x100, %ecx            # imm = 0x100
cmovaeq %rcx, %rax
movq %rax, -0x20(%r14)
movq $0x30, (%r14)
addq $-0x2138, %rbp          # imm = 0xDEC8
addq $0x8, %r14
addq $0x2138, %r15           # imm = 0x2138
cmpq $-0x84e0, %rbp          # imm = 0xFFFF7B20
jne 0xe89e
movq 0xcfcb(%rip), %rdi      # 0x1b968
leaq 0x7477(%rip), %rsi      # 0x15e1b
leaq 0x10b0(%rsp), %r15
movq %r15, %rdx
callq 0x6dd5
leaq 0xb0(%rsp), %r12
movq %r15, %rdi
movq 0x10(%rsp), %rsi
movq %r12, %rdx
callq 0x6dd5
movq %r12, %rdi
movl $0x241, %esi            # imm = 0x241
movl $0x180, %edx            # imm = 0x180
xorl %eax, %eax
callq 0x46b0
cmpl $-0x1, %eax
je 0xeb55
movl %eax, %ebp
leaq 0x28(%rsp), %rsi
movl $0x88, %edx
movl %eax, %edi
callq 0x4210
cmpq $0x88, %rax
jne 0xeb45
leaq 0xbab9(%rip), %rsi      # 0x1a4c8
movl $0x4, %edx
movl %ebp, %edi
callq 0x4210
cmpq $0x4, %rax
jne 0xeb45
leaq 0x90(%rsp), %r15
xorl %r12d, %r12d
leaq 0xd159(%rip), %rax      # 0x1bb90
leaq (%r12,%rax), %rsi
addq $0x2130, %rsi           # imm = 0x2130
movl $0x4, %edx
movl %ebp, %edi
callq 0x4210
cmpq $0x4, %rax
jne 0xeb45
leaq 0xd131(%rip), %rax      # 0x1bb90
leaq (%r12,%rax), %rsi
addq $0x2134, %rsi           # imm = 0x2134
movl $0x4, %edx
movl %ebp, %edi
callq 0x4210
cmpq $0x4, %rax
jne 0xeb45
movq -0x20(%r15), %r14
testq %r14, %r14
je 0xeaae
leaq 0xd100(%rip), %rax      # 0x1bb90
leaq (%r12,%rax), %rsi
addq $0x2000, %rsi           # imm = 0x2000
movl %ebp, %edi
movq %r14, %rdx
callq 0x4210
cmpq %r14, %rax
jne 0xeb45
movq -0x40(%r15), %r14
testq %r14, %r14
je 0xead8
leaq 0xd0d2(%rip), %rax      # 0x1bb90
leaq (%r12,%rax), %rsi
addq $0x1000, %rsi           # imm = 0x1000
movl %ebp, %edi
movq %r14, %rdx
callq 0x4210
cmpq %r14, %rax
jne 0xeb45
movq (%r15), %r14
testq %r14, %r14
je 0xeb01
leaq 0xd0a9(%rip), %rax      # 0x1bb90
leaq (%r12,%rax), %rsi
addq $0x2100, %rsi           # imm = 0x2100
movl %ebp, %edi
movq %r14, %rdx
callq 0x4210
cmpq %r14, %rax
jne 0xeb45
movq -0x60(%r15), %r14
testq %r14, %r14
je 0xeb24
leaq 0xd07f(%rip), %rax      # 0x1bb90
leaq (%rax,%r12), %rsi
movl %ebp, %edi
movq %r14, %rdx
callq 0x4210
cmpq %r14, %rax
jne 0xeb45
addq $0x8, %r15
addq $0x2138, %r12           # imm = 0x2138
cmpq $0x84e0, %r12           # imm = 0x84E0
jne 0xea30
movl %ebp, %edi
callq 0x43c0
jmp 0xeb66
movl %ebp, %edi
callq 0x43c0
leaq 0x738d(%rip), %rdi      # 0x15ee0
jmp 0xeb5c
leaq 0x74c8(%rip), %rdi      # 0x16024
movq 0x8(%rsp), %rsi
callq 0xefc8
addq $0x20b8, %rsp           # imm = 0x20B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq