// File: /workspace/github_c_makefile_star2_O2/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, 0x10(%rsp)
movq %rdi, 0x18(%rsp)
xorps %xmm0, %xmm0
leaq 0x90(%rsp), %rbx
movups %xmm0, 0x10(%rbx)
movups %xmm0, (%rbx)
movups %xmm0, -0x10(%rbx)
movups %xmm0, -0x20(%rbx)
movups %xmm0, -0x30(%rbx)
movups %xmm0, -0x40(%rbx)
movups %xmm0, -0x50(%rbx)
movups %xmm0, -0x60(%rbx)
movq $0x1, -0x68(%rbx)
movl 0xc516(%rip), %eax      # 0x194c8
shrl $0xd, %eax
andl $0x7, %eax
movslq 0x160b9(%rip), %rcx     # 0x23078
imulq $0x30, %rcx, %rcx
addq 0x160a6(%rip), %rcx     # 0x23070
movq %rcx, 0x20(%rsp)
movl 0xdbb7(%rip), %ecx      # 0x1ab8c
movl %ecx, 0xc(%rsp)
imulq $0x2138, %rax, %r14     # imm = 0x2138
xorl %r13d, %r13d
leaq 0xdba6(%rip), %r15      # 0x1ab90
movl $0x1000, %r12d          # imm = 0x1000
movl $0x1001, %ebp           # imm = 0x1001
cmpq $0x84e0, %r13           # imm = 0x84E0
je 0xd0bb
testb $0x2, 0x2131(%r13,%r15)
je 0xd0ab
cmpq %r13, %r14
jne 0xd03a
cmpl $0x0, 0xc(%rsp)
je 0xd03a
leaq (%r15,%r13), %rdi
addq $0x2000, %rdi           # imm = 0x2000
movq 0x20(%rsp), %rax
movq (%rax), %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x6016
leaq 0xdb4f(%rip), %rax      # 0x1ab90
leaq (%rax,%r13), %r15
movq %r15, %rdi
callq 0x5d70
leaq 0x1(%rax), %rcx
cmpq %r12, %rax
cmovaeq %rbp, %rcx
movq %rcx, -0x60(%rbx)
addq %r12, %r15
movq %r15, %rdi
leaq 0xdb27(%rip), %r15      # 0x1ab90
callq 0x5d70
leaq 0x1(%rax), %rcx
cmpq %r12, %rax
cmovaeq %rbp, %rcx
movq %rcx, -0x40(%rbx)
leaq (%r15,%r13), %rdi
addq $0x2000, %rdi           # imm = 0x2000
callq 0x5d70
leaq 0x1(%rax), %rcx
cmpq $0xff, %rax
movl $0x100, %eax            # imm = 0x100
cmovaeq %rax, %rcx
movq %rcx, -0x20(%rbx)
movq $0x30, (%rbx)
addq $0x2138, %r13           # imm = 0x2138
addq $0x8, %rbx
jmp 0xcff5
movq 0xd8a6(%rip), %rdi      # 0x1a968
leaq 0x6d50(%rip), %rsi      # 0x13e19
leaq 0x10b0(%rsp), %r15
movq %r15, %rdx
callq 0x6a9f
leaq 0xb0(%rsp), %r12
movq %r15, %rdi
movq 0x18(%rsp), %rsi
movq %r12, %rdx
callq 0x6a9f
movq %r12, %rdi
movl $0x241, %esi            # imm = 0x241
movl $0x180, %edx            # imm = 0x180
xorl %eax, %eax
callq 0x46b0
cmpl $-0x1, %eax
je 0xd274
movl %eax, %ebp
leaq 0x28(%rsp), %rsi
movl $0x88, %edx
movl %eax, %edi
callq 0x4210
cmpq $0x88, %rax
leaq 0x90(%rsp), %rbx
jne 0xd264
leaq 0xc38c(%rip), %rsi      # 0x194c8
pushq $0x4
popq %rdx
movl %ebp, %edi
callq 0x4210
cmpq $0x4, %rax
jne 0xd264
xorl %r12d, %r12d
pushq $0x4
popq %r14
cmpq $0x84e0, %r12           # imm = 0x84E0
leaq 0xda2b(%rip), %rax      # 0x1ab90
je 0xd297
leaq (%r12,%rax), %rsi
addq $0x2130, %rsi           # imm = 0x2130
movl %ebp, %edi
movq %r14, %rdx
callq 0x4210
cmpq $0x4, %rax
jne 0xd264
leaq 0xd9ff(%rip), %rax      # 0x1ab90
leaq (%r12,%rax), %rsi
addq $0x2134, %rsi           # imm = 0x2134
movl %ebp, %edi
movq %r14, %rdx
callq 0x4210
cmpq $0x4, %rax
jne 0xd264
movq -0x20(%rbx), %r15
testq %r15, %r15
je 0xd1de
leaq 0xd9d0(%rip), %rax      # 0x1ab90
leaq (%r12,%rax), %rsi
addq $0x2000, %rsi           # imm = 0x2000
movl %ebp, %edi
movq %r15, %rdx
callq 0x4210
cmpq %r15, %rax
jne 0xd264
movq -0x40(%rbx), %r15
testq %r15, %r15
je 0xd208
leaq 0xd9a2(%rip), %rax      # 0x1ab90
leaq (%r12,%rax), %rsi
addq $0x1000, %rsi           # imm = 0x1000
movl %ebp, %edi
movq %r15, %rdx
callq 0x4210
cmpq %r15, %rax
jne 0xd264
movq (%rbx), %r15
testq %r15, %r15
je 0xd231
leaq 0xd979(%rip), %rax      # 0x1ab90
leaq (%r12,%rax), %rsi
addq $0x2100, %rsi           # imm = 0x2100
movl %ebp, %edi
movq %r15, %rdx
callq 0x4210
cmpq %r15, %rax
jne 0xd264
movq -0x60(%rbx), %r15
testq %r15, %r15
je 0xd254
leaq 0xd94f(%rip), %rax      # 0x1ab90
leaq (%rax,%r12), %rsi
movl %ebp, %edi
movq %r15, %rdx
callq 0x4210
cmpq %r15, %rax
jne 0xd264
addq $0x2138, %r12           # imm = 0x2138
addq $0x8, %rbx
jmp 0xd157
movl %ebp, %edi
callq 0x43c0
leaq 0x6c6c(%rip), %rdi      # 0x13ede
jmp 0xd27b
leaq 0x6da7(%rip), %rdi      # 0x14022
movq 0x10(%rsp), %rsi
callq 0xd6e9
addq $0x20b8, %rsp           # imm = 0x20B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ebp, %edi
callq 0x43c0
jmp 0xd285