// File: /workspace/github_c_makefile_star2_O3/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 0xf1c5
movl 0xb370(%rip), %eax      # 0x1a524
btl $0x15, %eax
jae 0xf1c5
andl $0xffdfffff, %eax       # imm = 0xFFDFFFFF
movl %eax, 0xb35f(%rip)      # 0x1a524
cmpq $0x0, 0xc9b3(%rip)      # 0x1bb80
je 0xf36d
cmpl $0x0, 0xc896(%rip)      # 0x1ba70
je 0xf36d
callq 0xf379
cmpl $-0x1, %eax
je 0xf36d
movl %eax, %ebx
leaq 0x72f8(%rip), %rdx      # 0x164ef
movl %eax, %edi
xorl %esi, %esi
callq 0xe969
movl %ebx, %edi
callq 0x43c0
testl %eax, %eax
je 0xf22a
callq 0x40c0
movl (%rax), %edi
callq 0x4870
movq %rax, %rdi
addq $0x1090, %rsp           # imm = 0x1090
popq %rbx
popq %r14
popq %rbp
jmp 0xec60
movq 0xc94f(%rip), %rcx      # 0x1bb80
movq 0xc870(%rip), %r8       # 0x1baa8
leaq 0x701d(%rip), %rdx      # 0x1625c
leaq 0xb4ba(%rip), %rbx      # 0x1a700
movq %rsp, %r14
movl $0x1088, %esi           # imm = 0x1088
movq %r14, %rdi
movq %rbx, %r9
xorl %eax, %eax
callq 0x4350
leaq 0x6c14(%rip), %rdi      # 0x15e76
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movl $0x9, %r8d
callq 0xf3d1
movq %rbx, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x46b0
cmpl $-0x1, %eax
je 0xf2c7
movl %eax, %ebp
movq 0xc7ea(%rip), %rsi      # 0x1ba78
movl 0x14e38(%rip), %edx     # 0x240cc
movl %eax, %edi
callq 0x4460
testq %rax, %rax
js 0xf2ff
movq %rax, %rbx
movl %ebp, %edi
callq 0x43c0
testl %eax, %eax
jne 0xf2be
leaq 0xb44b(%rip), %rdi      # 0x1a700
callq 0x40d0
testl %eax, %eax
je 0xf32c
callq 0x40c0
movl (%rax), %edi
jmp 0xf2f0
callq 0x40c0
movq %rax, %rbx
movl (%rax), %edi
callq 0x4870
movq %rax, %rdi
callq 0xec60
leaq 0xb41b(%rip), %rdi      # 0x1a700
callq 0x40d0
testl %eax, %eax
je 0xf36d
movl (%rbx), %edi
callq 0x4870
movq %rax, %rdi
callq 0xec60
jmp 0xf36d
callq 0x40c0
movl (%rax), %edi
callq 0x4870
movq %rax, %rdi
callq 0xec60
movl %ebp, %edi
callq 0x43c0
testl %eax, %eax
jne 0xf36d
leaq 0xb3db(%rip), %rdi      # 0x1a700
callq 0x40d0
jmp 0xf36d
movl %ebx, 0xc73e(%rip)      # 0x1ba70
movq 0xc73f(%rip), %rdi      # 0x1ba78
movb $0x0, -0x1(%rdi,%rbx)
cmpq $0x3, %rbx
jb 0xf365
movq %rbx, %rax
decq %rax
cmpb $0xa, -0x1(%rdi,%rax)
jne 0xf35c
cmpb $0x2f, (%rdi,%rax)
jne 0xf35c
movb $0x0, -0x1(%rdi,%rax)
decq %rax
cmpq $0x1, %rax
jg 0xf34a
leal -0x1(%rbx), %esi
callq 0xf7fd
addq $0x1090, %rsp           # imm = 0x1090
popq %rbx
popq %r14
popq %rbp
retq