// File: /workspace/github_c_makefile_star2_O0/jarun[P]nnn/src/nnn.c
// Function: invertselbuf
static void invertselbuf(const int pathlen)
{
	size_t len, endpos, shrinklen = 0, alloclen = 0;
	char * const pbuf = g_sel + pathlen;
	char *found;
	int i, nmarked = 0, prev = 0;
	struct entry *dentp;
	bool scan = FALSE;
	selmark *marked = malloc(nselected * sizeof(selmark));

	if (!marked) {
		printwarn(NULL);
		return;
	}

	/* First pass: inversion */
	for (i = 0; i < ndents; ++i) {
		dentp = &pdents[i];

		if (dentp->flags & FILE_SCANNED) {
			if (dentp->flags & FILE_SELECTED) {
				dentp->flags ^= FILE_SELECTED; /* Clear selection status */
				scan = TRUE;
			} else {
				dentp->flags |= FILE_SELECTED;
				alloclen += pathlen + dentp->nlen;
			}
		} else {
			dentp->flags |= FILE_SCANNED;
			scan = TRUE;
		}

		if (scan) {
			len = pathlen + xstrsncpy(pbuf, dentp->name, NAME_MAX);
			found = findinsel(findselpos, len);
			if (found) {
				if (findselpos == found)
					findselpos += len;

				if (nmarked && (found
				    == (marked[nmarked - 1].startpos + marked[nmarked - 1].len)))
					marked[nmarked - 1].len += len;
				else {
					marked[nmarked].startpos = found;
					marked[nmarked].len = len;
					++nmarked;
				}

				--nselected;
				shrinklen += len; /* buffer size adjustment */
			} else {
				dentp->flags |= FILE_SELECTED;
				alloclen += pathlen + dentp->nlen;
			}
			scan = FALSE;
		}
	}

	/*
	 * Files marked for deselection could be found in arbitrary order.
	 * Sort by appearance in selection buffer.
	 * With entries sorted we can merge adjacent ones allowing us to
	 * move them in a single go.
	 */
	qsort(marked, nmarked, sizeof(selmark), &markcmp);

	/* Some files might be adjacent. Merge them into a single entry */
	for (i = 1; i < nmarked; ++i) {
		if (marked[i].startpos == marked[prev].startpos + marked[prev].len)
			marked[prev].len += marked[i].len;
		else {
			++prev;
			marked[prev].startpos = marked[i].startpos;
			marked[prev].len = marked[i].len;
		}
	}

	/*
	 * Number of entries is increased by encountering a non-adjacent entry
	 * After we finish the loop we should increment it once more.
	 */

	if (nmarked) /* Make sure there is something to deselect */
		nmarked = prev + 1;

	/* Using merged entries remove unselected chunks from selection buffer */
	for (i = 0; i < nmarked; ++i) {
		/*
		 * found: points to where the current block starts
		 *        variable is recycled from previous for readability
		 * endpos: points to where the the next block starts
		 *         area between the end of current block (found + len)
		 *         and endpos is selected entries. This is what we are
		 *         moving back.
		 */
		found = marked[i].startpos;
		endpos = (i + 1 == nmarked ? selbufpos : marked[i + 1].startpos - pselbuf);
		len = marked[i].len;

		/* Move back only selected entries. No selected memory is moved twice */
		memmove(found, found + len, endpos - (found + len - pselbuf));
	}

	free(marked);

	/* Buffer size adjustment */
	selbufpos -= shrinklen;

	selbufrealloc(alloclen);

	/* Second pass: append newly selected to buffer */
	for (i = 0; i < ndents; ++i) {
		if (pdents[i].flags & FILE_SELECTED) {
			len = pathlen + xstrsncpy(pbuf, pdents[i].name, NAME_MAX);
			appendfpath(g_sel, len);
			++nselected;
		}
	}

	nselected ? writesel(pselbuf, selbufpos - 1) : clearselection();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movl %edi, -0x4(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movslq -0x4(%rbp), %rcx
leaq 0x171e7(%rip), %rax     # 0x2a5c0
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movb $0x0, -0x51(%rbp)
movslq 0x171b3(%rip), %rdi     # 0x2a5ac
shlq $0x4, %rdi
callq 0x45e0
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jne 0x1342a
callq 0x40c0
movl (%rax), %edi
callq 0x4870
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xd210
jmp 0x13992
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl 0xea52(%rip), %eax      # 0x21e8c
jge 0x136e2
movq 0x16f29(%rip), %rax     # 0x2a370
movslq -0x3c(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rax
shrq $0x38, %rax
andl $0x20, %eax
cmpl $0x0, %eax
je 0x13516
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rax
shrq $0x38, %rax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x134bd
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rcx
shrq $0x38, %rcx
xorl $0x10, %ecx
movslq %ecx, %rdx
movq 0x20(%rax), %rcx
andq $0xff, %rdx
shlq $0x38, %rdx
movabsq $0xffffffffffffff, %rsi # imm = 0xFFFFFFFFFFFFFF
andq %rsi, %rcx
orq %rdx, %rcx
movq %rcx, 0x20(%rax)
movb $0x1, -0x51(%rbp)
jmp 0x13514
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rcx
shrq $0x38, %rcx
orl $0x10, %ecx
movslq %ecx, %rdx
movq 0x20(%rax), %rcx
andq $0xff, %rdx
shlq $0x38, %rdx
movabsq $0xffffffffffffff, %rsi # imm = 0xFFFFFFFFFFFFFF
andq %rsi, %rcx
orq %rdx, %rcx
movq %rcx, 0x20(%rax)
movl -0x4(%rbp), %eax
movq -0x50(%rbp), %rcx
movq 0x20(%rcx), %rcx
shrq $0x28, %rcx
andq $0xffff, %rcx           # imm = 0xFFFF
addl %ecx, %eax
cltq
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0x1354f
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rcx
shrq $0x38, %rcx
orl $0x20, %ecx
movslq %ecx, %rdx
movq 0x20(%rax), %rcx
andq $0xff, %rdx
shlq $0x38, %rdx
movabsq $0xffffffffffffff, %rsi # imm = 0xFFFFFFFFFFFFFF
andq %rsi, %rcx
orq %rdx, %rcx
movq %rcx, 0x20(%rax)
movb $0x1, -0x51(%rbp)
testb $0x1, -0x51(%rbp)
je 0x136d2
movslq -0x4(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x30(%rbp), %rdi
movq -0x50(%rbp), %rax
movq (%rax), %rsi
movl $0xff, %edx
callq 0x6250
movq %rax, %rcx
movq -0x68(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x10(%rbp)
movq 0x18035(%rip), %rdi     # 0x2b5c0
movq -0x10(%rbp), %rax
movl %eax, %esi
callq 0x17c90
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x13677
movq 0x18014(%rip), %rax     # 0x2b5c0
cmpq -0x38(%rbp), %rax
jne 0x135c4
movq -0x10(%rbp), %rax
addq 0x18003(%rip), %rax     # 0x2b5c0
movq %rax, 0x17ffc(%rip)     # 0x2b5c0
cmpl $0x0, -0x40(%rbp)
je 0x13624
movq -0x38(%rbp), %rax
movq -0x60(%rbp), %rcx
movl -0x40(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
movq -0x60(%rbp), %rdx
movl -0x40(%rbp), %esi
subl $0x1, %esi
movslq %esi, %rsi
shlq $0x4, %rsi
addq %rsi, %rdx
addq 0x8(%rdx), %rcx
cmpq %rcx, %rax
jne 0x13624
movq -0x10(%rbp), %rcx
movq -0x60(%rbp), %rax
movl -0x40(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
shlq $0x4, %rdx
addq %rdx, %rax
addq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
jmp 0x1365a
movq -0x38(%rbp), %rcx
movq -0x60(%rbp), %rax
movslq -0x40(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rcx
movq -0x60(%rbp), %rax
movslq -0x40(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
movl 0x16f4c(%rip), %eax     # 0x2a5ac
addl $-0x1, %eax
movl %eax, 0x16f43(%rip)     # 0x2a5ac
movq -0x10(%rbp), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
jmp 0x136ce
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rcx
shrq $0x38, %rcx
orl $0x10, %ecx
movslq %ecx, %rdx
movq 0x20(%rax), %rcx
andq $0xff, %rdx
shlq $0x38, %rdx
movabsq $0xffffffffffffff, %rsi # imm = 0xFFFFFFFFFFFFFF
andq %rsi, %rcx
orq %rdx, %rcx
movq %rcx, 0x20(%rax)
movl -0x4(%rbp), %eax
movq -0x50(%rbp), %rcx
movq 0x20(%rcx), %rcx
shrq $0x28, %rcx
andq $0xffff, %rcx           # imm = 0xFFFF
addl %ecx, %eax
cltq
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movb $0x0, -0x51(%rbp)
jmp 0x136d4
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x13431
movq -0x60(%rbp), %rdi
movslq -0x40(%rbp), %rsi
movl $0x10, %edx
leaq 0x624a(%rip), %rcx      # 0x19940
callq 0x4150
movl $0x1, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x40(%rbp), %eax
jge 0x137d9
movq -0x60(%rbp), %rax
movslq -0x3c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq -0x60(%rbp), %rcx
movslq -0x44(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
movq -0x60(%rbp), %rdx
movslq -0x44(%rbp), %rsi
shlq $0x4, %rsi
addq %rsi, %rdx
addq 0x8(%rdx), %rcx
cmpq %rcx, %rax
jne 0x13776
movq -0x60(%rbp), %rax
movslq -0x3c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rcx
movq -0x60(%rbp), %rax
movslq -0x44(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
addq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
jmp 0x137c9
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
movq -0x60(%rbp), %rax
movslq -0x3c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq -0x60(%rbp), %rax
movslq -0x44(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq -0x60(%rbp), %rax
movslq -0x3c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rcx
movq -0x60(%rbp), %rax
movslq -0x44(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
jmp 0x137cb
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x13702
cmpl $0x0, -0x40(%rbp)
je 0x137e8
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x40(%rbp), %eax
jge 0x138a4
movq -0x60(%rbp), %rax
movslq -0x3c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
cmpl -0x40(%rbp), %eax
jne 0x13828
movl 0xe54e(%rip), %eax      # 0x21d70
movq %rax, -0x70(%rbp)
jmp 0x1384d
movq -0x60(%rbp), %rax
movl -0x3c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq 0xe532(%rip), %rcx      # 0x21d78
subq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x60(%rbp), %rax
movslq -0x3c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x10(%rbp)
movq -0x38(%rbp), %rdi
movq -0x38(%rbp), %rsi
addq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x38(%rbp), %rax
addq -0x10(%rbp), %rax
movq 0xe4ed(%rip), %rcx      # 0x21d78
subq %rcx, %rax
subq %rax, %rdx
callq 0x46e0
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x137ef
movq -0x60(%rbp), %rdi
callq 0x4090
movq -0x20(%rbp), %rcx
movl 0xe4b9(%rip), %eax      # 0x21d70
subq %rcx, %rax
movl %eax, 0xe4b0(%rip)      # 0x21d70
movq -0x28(%rbp), %rdi
callq 0x19970
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl 0xe5b3(%rip), %eax      # 0x21e8c
jge 0x13969
movq 0x16a8a(%rip), %rax     # 0x2a370
movslq -0x3c(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq 0x20(%rax), %rax
shrq $0x38, %rax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x13959
movslq -0x4(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x30(%rbp), %rdi
movq 0x16a5c(%rip), %rax     # 0x2a370
movslq -0x3c(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rsi
movl $0xff, %edx
callq 0x6250
movq %rax, %rcx
movq -0x78(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rsi
leaq 0x16c7b(%rip), %rdi     # 0x2a5c0
callq 0x10af0
movl 0x16c5c(%rip), %eax     # 0x2a5ac
addl $0x1, %eax
movl %eax, 0x16c53(%rip)     # 0x2a5ac
jmp 0x1395b
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x138d0
cmpl $0x0, 0x16c3c(%rip)     # 0x2a5ac
je 0x1398d
movq 0xe3ff(%rip), %rdi      # 0x21d78
movl 0xe3f1(%rip), %eax      # 0x21d70
subl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
callq 0xe130
jmp 0x13992
callq 0x12c80
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax,%rax)