// File: /workspace/github_c_makefile_star2_O1/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 0xf3f0
movl 0xb145(%rip), %eax      # 0x1a524
btl $0x15, %eax
jae 0xf3f0
andl $0xffdfffff, %eax       # imm = 0xFFDFFFFF
movl %eax, 0xb134(%rip)      # 0x1a524
cmpq $0x0, 0xc788(%rip)      # 0x1bb80
je 0xf598
cmpl $0x0, 0xc66b(%rip)      # 0x1ba70
je 0xf598
callq 0xf5a4
cmpl $-0x1, %eax
je 0xf598
movl %eax, %ebx
leaq 0x70b3(%rip), %rdx      # 0x164d5
movl %eax, %edi
xorl %esi, %esi
callq 0xeb78
movl %ebx, %edi
callq 0x43c0
testl %eax, %eax
je 0xf455
callq 0x40c0
movl (%rax), %edi
callq 0x4870
movq %rax, %rdi
addq $0x1090, %rsp           # imm = 0x1090
popq %rbx
popq %r14
popq %rbp
jmp 0xee6f
movq 0xc724(%rip), %rcx      # 0x1bb80
movq 0xc645(%rip), %r8       # 0x1baa8
leaq 0x6dd8(%rip), %rdx      # 0x16242
leaq 0xb28f(%rip), %rbx      # 0x1a700
movq %rsp, %r14
movl $0x1088, %esi           # imm = 0x1088
movq %r14, %rdi
movq %rbx, %r9
xorl %eax, %eax
callq 0x4350
leaq 0x69cf(%rip), %rdi      # 0x15e5c
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movl $0x9, %r8d
callq 0xf5fc
movq %rbx, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x46b0
cmpl $-0x1, %eax
je 0xf4f2
movl %eax, %ebp
movq 0xc5bf(%rip), %rsi      # 0x1ba78
movl 0x14c0d(%rip), %edx     # 0x240cc
movl %eax, %edi
callq 0x4460
testq %rax, %rax
js 0xf52a
movq %rax, %rbx
movl %ebp, %edi
callq 0x43c0
testl %eax, %eax
jne 0xf4e9
leaq 0xb220(%rip), %rdi      # 0x1a700
callq 0x40d0
testl %eax, %eax
je 0xf557
callq 0x40c0
movl (%rax), %edi
jmp 0xf51b
callq 0x40c0
movq %rax, %rbx
movl (%rax), %edi
callq 0x4870
movq %rax, %rdi
callq 0xee6f
leaq 0xb1f0(%rip), %rdi      # 0x1a700
callq 0x40d0
testl %eax, %eax
je 0xf598
movl (%rbx), %edi
callq 0x4870
movq %rax, %rdi
callq 0xee6f
jmp 0xf598
callq 0x40c0
movl (%rax), %edi
callq 0x4870
movq %rax, %rdi
callq 0xee6f
movl %ebp, %edi
callq 0x43c0
testl %eax, %eax
jne 0xf598
leaq 0xb1b0(%rip), %rdi      # 0x1a700
callq 0x40d0
jmp 0xf598
movl %ebx, 0xc513(%rip)      # 0x1ba70
movq 0xc514(%rip), %rdi      # 0x1ba78
movb $0x0, -0x1(%rdi,%rbx)
cmpq $0x3, %rbx
jl 0xf590
movq %rbx, %rax
decq %rax
cmpb $0xa, -0x1(%rdi,%rax)
jne 0xf587
cmpb $0x2f, (%rdi,%rax)
jne 0xf587
movb $0x0, -0x1(%rdi,%rax)
decq %rax
cmpq $0x1, %rax
jg 0xf575
leal -0x1(%rbx), %esi
callq 0xfa35
addq $0x1090, %rsp           # imm = 0x1090
popq %rbx
popq %r14
popq %rbp
retq