// File: /workspace/github_c_makefile_star2_O3/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)
xorps %xmm0, %xmm0
leaq 0x90(%rsp), %r14
movups %xmm0, 0x10(%r14)
movups %xmm0, (%r14)
movups %xmm0, -0x10(%r14)
movups %xmm0, -0x20(%r14)
movups %xmm0, -0x30(%r14)
movups %xmm0, -0x40(%r14)
movups %xmm0, -0x50(%r14)
movups %xmm0, -0x60(%r14)
movq $0x1, -0x68(%r14)
movl 0xbe86(%rip), %eax      # 0x1a4c8
shrl $0xd, %eax
andl $0x7, %eax
movl 0xd53e(%rip), %ecx      # 0x1bb8c
movl %ecx, 0x4(%rsp)
movslq 0x15a1f(%rip), %rcx     # 0x24078
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
addq 0x15a08(%rip), %rcx     # 0x24070
movq %rcx, 0x18(%rsp)
imulq $0x2138, %rax, %rax     # imm = 0x2138
movq %rax, 0x20(%rsp)
leaq 0xf510(%rip), %r15      # 0x1db90
xorl %r13d, %r13d
movl $0x1000, %ebp           # imm = 0x1000
movq $-0x1000, %r12          # imm = 0xF000
testb $0x2, 0x131(%r15)
je 0xe768
movq 0x20(%rsp), %rax
addq %r13, %rax
jne 0xe6d2
cmpl $0x0, 0x4(%rsp)
je 0xe6d2
movq 0x18(%rsp), %rax
movq (%rax), %rsi
movl $0x100, %ecx            # imm = 0x100
movq %r15, %rdi
xorl %edx, %edx
callq 0x4860
testq %rax, %rax
jne 0xe6d2
movb $0x0, 0xff(%r15)
leaq -0x2000(%r15), %rdi
xorl %esi, %esi
callq 0x4490
movq %r13, %rbx
leaq 0xd4a6(%rip), %rcx      # 0x1bb90
subq %rcx, %rbx
leaq (%rbx,%rax), %rcx
cmpq %rbp, %rcx
leaq 0x1(%rbx,%rax), %rax
movl $0x1001, %ecx           # imm = 0x1001
cmovaeq %rcx, %rax
movq %rax, -0x60(%r14)
leaq (%r15,%r12), %rdi
xorl %esi, %esi
callq 0x4490
leaq (%rbx,%r12), %rcx
addq %rax, %rcx
cmpq %rbp, %rcx
leaq -0xfff(%rbx,%rax), %rax
movl $0x1001, %ecx           # imm = 0x1001
cmovaeq %rcx, %rax
movq %rax, -0x40(%r14)
movq %r15, %rdi
xorl %esi, %esi
callq 0x4490
leaq (%rbx,%rax), %rcx
addq $-0x2000, %rcx          # imm = 0xE000
cmpq $0xff, %rcx
leaq -0x1fff(%rbx,%rax), %rax
movl $0x100, %ecx            # imm = 0x100
cmovaeq %rcx, %rax
movq %rax, -0x20(%r14)
movq $0x30, (%r14)
addq $-0x2138, %r13          # imm = 0xDEC8
addq $0x8, %r14
addq $0x2138, %r15           # imm = 0x2138
cmpq $-0x84e0, %r13          # imm = 0xFFFF7B20
jne 0xe68f
movq 0xd1da(%rip), %rdi      # 0x1b968
leaq 0x76a0(%rip), %rsi      # 0x15e35
leaq 0x10b0(%rsp), %r15
movq %r15, %rdx
callq 0x6d5c
leaq 0xb0(%rsp), %r12
movq %r15, %rdi
movq 0x10(%rsp), %rsi
movq %r12, %rdx
callq 0x6d5c
movq %r12, %rdi
movl $0x241, %esi            # imm = 0x241
movl $0x180, %edx            # imm = 0x180
xorl %eax, %eax
callq 0x46b0
cmpl $-0x1, %eax
je 0xe946
movl %eax, %ebp
leaq 0x28(%rsp), %rsi
movl $0x88, %edx
movl %eax, %edi
callq 0x4210
cmpq $0x88, %rax
leaq 0x90(%rsp), %rbx
jne 0xe936
leaq 0xbcc0(%rip), %rsi      # 0x1a4c8
movl $0x4, %edx
movl %ebp, %edi
callq 0x4210
cmpq $0x4, %rax
jne 0xe936
xorl %r15d, %r15d
leaq 0xd368(%rip), %rax      # 0x1bb90
leaq (%r15,%rax), %rsi
addq $0x2130, %rsi           # imm = 0x2130
movl $0x4, %edx
movl %ebp, %edi
callq 0x4210
cmpq $0x4, %rax
jne 0xe936
leaq 0xd340(%rip), %rax      # 0x1bb90
leaq (%r15,%rax), %rsi
addq $0x2134, %rsi           # imm = 0x2134
movl $0x4, %edx
movl %ebp, %edi
callq 0x4210
cmpq $0x4, %rax
jne 0xe936
movq -0x20(%rbx), %r14
testq %r14, %r14
je 0xe89f
leaq 0xd30f(%rip), %rax      # 0x1bb90
leaq (%r15,%rax), %rsi
addq $0x2000, %rsi           # imm = 0x2000
movl %ebp, %edi
movq %r14, %rdx
callq 0x4210
cmpq %r14, %rax
jne 0xe936
movq -0x40(%rbx), %r14
testq %r14, %r14
je 0xe8c9
leaq 0xd2e1(%rip), %rax      # 0x1bb90
leaq (%r15,%rax), %rsi
addq $0x1000, %rsi           # imm = 0x1000
movl %ebp, %edi
movq %r14, %rdx
callq 0x4210
cmpq %r14, %rax
jne 0xe936
movq (%rbx), %r14
testq %r14, %r14
je 0xe8f2
leaq 0xd2b8(%rip), %rax      # 0x1bb90
leaq (%r15,%rax), %rsi
addq $0x2100, %rsi           # imm = 0x2100
movl %ebp, %edi
movq %r14, %rdx
callq 0x4210
cmpq %r14, %rax
jne 0xe936
movq -0x60(%rbx), %r14
testq %r14, %r14
je 0xe915
leaq 0xd28e(%rip), %rax      # 0x1bb90
leaq (%rax,%r15), %rsi
movl %ebp, %edi
movq %r14, %rdx
callq 0x4210
cmpq %r14, %rax
jne 0xe936
addq $0x8, %rbx
addq $0x2138, %r15           # imm = 0x2138
cmpq $0x84e0, %r15           # imm = 0x84E0
jne 0xe821
movl %ebp, %edi
callq 0x43c0
jmp 0xe957
movl %ebp, %edi
callq 0x43c0
leaq 0x75b6(%rip), %rdi      # 0x15efa
jmp 0xe94d
leaq 0x76f1(%rip), %rdi      # 0x1603e
movq 0x8(%rsp), %rsi
callq 0xedba
addq $0x20b8, %rsp           # imm = 0x20B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq