// File: /workspace/github_c_makefile_star2_O2/jarun[P]nnn/src/nnn.c
// Function: endselection
static void endselection(bool endselmode)
{
	int fd;
	ssize_t count;
	char buf[sizeof(patterns[P_REPLACE]) + PATH_MAX + (TMP_LEN_MAX << 1)];

	if (endselmode && g_state.selmode)
		g_state.selmode = 0;

	/* The code below is only for listing mode */
	if (!listpath || !selbufpos)
		return;

	fd = create_tmp_file();
	if (fd == -1) {
		DPRINTF_S("couldn't create tmp file");
		return;
	}

	seltofile(fd, NULL, NEWLINE);
	if (close(fd)) {
		DPRINTF_S(strerror(errno));
		printwarn(NULL);
		return;
	}

	snprintf(buf, sizeof(buf), patterns[P_REPLACE], listpath, listroot, g_tmpfpath);
	spawn(utils[UTIL_SH_EXEC], buf, NULL, NULL, F_CLI);

	fd = open(g_tmpfpath, O_RDONLY);
	if (fd == -1) {
		DPRINTF_S(strerror(errno));
		printwarn(NULL);
		if (unlink(g_tmpfpath)) {
			DPRINTF_S(strerror(errno));
			printwarn(NULL);
		}
		return;
	}

	count = read(fd, pselbuf, selbuflen);
	if (count < 0) {
		DPRINTF_S(strerror(errno));
		printwarn(NULL);
		if (close(fd) || unlink(g_tmpfpath)) {
			DPRINTF_S(strerror(errno));
		}
		return;
	}

	if (close(fd) || unlink(g_tmpfpath)) {
		DPRINTF_S(strerror(errno));
		printwarn(NULL);
		return;
	}

	selbufpos = count;
	pselbuf[--count] = '\0';
	for (--count; count > 0; --count)
		if (pselbuf[count] == '\n' && pselbuf[count+1] == '/')
			pselbuf[count] = '\0';

	writesel(pselbuf, selbufpos - 1);
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
subq $0x1090, %rsp           # imm = 0x1090
testl %edi, %edi
je 0xdacd
movl 0xba68(%rip), %eax      # 0x19524
btl $0x15, %eax
jae 0xdacd
andl $0xffdfffff, %eax       # imm = 0xFFDFFFFF
movl %eax, 0xba57(%rip)      # 0x19524
cmpq $0x0, 0xd0ab(%rip)      # 0x1ab80
je 0xdc6e
cmpl $0x0, 0xcf8e(%rip)      # 0x1aa70
je 0xdc6e
callq 0xdc85
cmpl $-0x1, %eax
je 0xdc6e
movl %eax, %ebx
leaq 0x69d4(%rip), %rdx      # 0x144d3
movl %eax, %edi
xorl %esi, %esi
callq 0xd2a0
movl %ebx, %edi
callq 0x43c0
testl %eax, %eax
je 0xdb32
callq 0x40c0
movl (%rax), %edi
callq 0x4870
movq %rax, %rdi
addq $0x1090, %rsp           # imm = 0x1090
popq %rbx
popq %r14
popq %rbp
jmp 0xd5b9
movq 0xd047(%rip), %rcx      # 0x1ab80
movq 0xcf68(%rip), %r8       # 0x1aaa8
leaq 0x66f9(%rip), %rdx      # 0x14240
leaq 0xbbb2(%rip), %rbx      # 0x19700
movq %rsp, %r14
movl $0x1088, %esi           # imm = 0x1088
movq %r14, %rdi
movq %rbx, %r9
xorl %eax, %eax
callq 0x4350
leaq 0x62f0(%rip), %rdi      # 0x13e5a
pushq $0x9
popq %r8
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xdcb4
movq %rbx, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x46b0
cmpl $-0x1, %eax
je 0xdbcd
movl %eax, %ebp
movq 0xcee4(%rip), %rsi      # 0x1aa78
movl 0x15532(%rip), %edx     # 0x230cc
movl %eax, %edi
callq 0x4460
testq %rax, %rax
js 0xdc05
movq %rax, %rbx
movl %ebp, %edi
callq 0x43c0
testl %eax, %eax
jne 0xdbc4
leaq 0xbb45(%rip), %rdi      # 0x19700
callq 0x40d0
testl %eax, %eax
je 0xdc32
callq 0x40c0
movl (%rax), %edi
jmp 0xdbf6
callq 0x40c0
movq %rax, %rbx
movl (%rax), %edi
callq 0x4870
movq %rax, %rdi
callq 0xd5b9
leaq 0xbb15(%rip), %rdi      # 0x19700
callq 0x40d0
testl %eax, %eax
je 0xdc6e
movl (%rbx), %edi
callq 0x4870
movq %rax, %rdi
callq 0xd5b9
jmp 0xdc6e
callq 0x40c0
movl (%rax), %edi
callq 0x4870
movq %rax, %rdi
callq 0xd5b9
movl %ebp, %edi
callq 0x43c0
testl %eax, %eax
jne 0xdc6e
leaq 0xbad5(%rip), %rdi      # 0x19700
callq 0x40d0
jmp 0xdc6e
movl %ebx, 0xce38(%rip)      # 0x1aa70
movq 0xce39(%rip), %rdi      # 0x1aa78
movb $0x0, -0x1(%rdi,%rbx)
movq %rbx, %rax
addq $-0x2, %rax
testq %rax, %rax
jle 0xdc66
cmpb $0xa, (%rdi,%rax)
jne 0xdc61
cmpb $0x2f, 0x1(%rdi,%rax)
jne 0xdc61
movb $0x0, (%rdi,%rax)
decq %rax
jmp 0xdc4b
leal -0x1(%rbx), %esi
callq 0xe0b9
addq $0x1090, %rsp           # imm = 0x1090
popq %rbx
popq %r14
popq %rbp
retq