// File: /workspace/github_c_makefile_star2_O3/anachronic[P]st/st.c
// Function: selsnap
void
selsnap(int *x, int *y, int direction)
{
	int newx, newy, xt, yt;
	int rtop = 0, rbot = term.row - 1;
	int delim, prevdelim;
	const Glyph *gp, *prevgp;

	if (!IS_SET(MODE_ALTSCREEN))
		rtop += -term.histf + term.scr, rbot += term.scr;

	switch (sel.snap) {
	case SNAP_WORD:
		/*
		 * Snap around if the word wraps around at the end or
		 * beginning of a line.
		 */
		prevgp = &TLINE(*y)[*x];
		prevdelim = ISDELIM(prevgp->u);
		for (;;) {
			newx = *x + direction;
			newy = *y;
			if (!BETWEEN(newx, 0, term.col - 1)) {
				newy += direction;
				newx = (newx + term.col) % term.col;
				if (!BETWEEN(newy, rtop, rbot))
					break;

				if (direction > 0)
					yt = *y, xt = *x;
				else
					yt = newy, xt = newx;
				if (!(TLINE(yt)[xt].mode & ATTR_WRAP))
					break;
			}

			if (newx >= tlinelen(TLINE(newy)))
				break;

			gp = &TLINE(newy)[newx];
			delim = ISDELIM(gp->u);
			if (!(gp->mode & ATTR_WDUMMY) && (delim != prevdelim ||
			    (delim && !(gp->u == ' ' && prevgp->u == ' '))))
				break;

			*x = newx;
			*y = newy;
			prevgp = gp;
			prevdelim = delim;
		}
		break;
	case SNAP_LINE:
		/*
		 * Snap around if the the previous line or the current one
		 * has set ATTR_WRAP at its end. Then the whole next or
		 * previous line will be selected.
		 */
		*x = (direction < 0) ? 0 : term.col - 1;
		if (direction < 0) {
			for (; *y > rtop; *y -= 1) {
				if (!tiswrapped(TLINE(*y-1)))
					break;
			}
		} else if (direction > 0) {
			for (; *y < rbot; *y += 1) {
				if (!tiswrapped(TLINE(*y)))
					break;
			}
		}
		break;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %edx, %r14d
movq %rsi, %r10
movl 0x13c96(%rip), %eax     # 0x1c778
movl %eax, %edx
subl 0x13c8a(%rip), %edx     # 0x1c774
xorl %ecx, %ecx
testb $0x4, 0x13cc9(%rip)     # 0x1c7bc
cmovnel %ecx, %edx
movl %edx, 0x4(%rsp)
movl $0x0, %edx
cmovel %eax, %edx
movl 0xfdd8(%rip), %esi      # 0x188e0
addl %esi, %edx
decl %edx
movl %edx, (%rsp)
movl 0xfda3(%rip), %edx      # 0x188b8
cmpl $0x2, %edx
je 0x8b72
cmpl $0x1, %edx
jne 0x8fb0
movl (%r10), %r13d
movl %r13d, %ecx
leaq 0xfdb0(%rip), %r9       # 0x188e0
subl %eax, %ecx
jge 0x8c51
movl 0x13c32(%rip), %eax     # 0x1c770
addl %ecx, %eax
addl $0x7d1, %eax            # imm = 0x7D1
cltq
imulq $0x10624dd3, %rax, %rcx # imm = 0x10624DD3
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x27, %rcx
addl %edx, %ecx
imull $0x7d0, %ecx, %ecx      # imm = 0x7D0
subl %ecx, %eax
cltq
leaq (%r9,%rax,8), %rax
addq $0x10, %rax
jmp 0x8c5f
movl 0xfd6c(%rip), %eax      # 0x188e4
decl %eax
testl %r14d, %r14d
cmovsl %ecx, %eax
movl %eax, (%rdi)
js 0x8ef1
je 0x8fb0
movl (%r10), %eax
cmpl (%rsp), %eax
jge 0x8fb0
movq 0xfd47(%rip), %rcx      # 0x188e8
leaq 0xfd38(%rip), %rdx      # 0x188e0
movl %eax, %esi
subl 0x13bc8(%rip), %esi     # 0x1c778
jge 0x8bed
movl 0x13bb8(%rip), %edi     # 0x1c770
addl %edi, %esi
addl $0x7d1, %esi            # imm = 0x7D1
movslq %esi, %rsi
imulq $0x10624dd3, %rsi, %rdi # imm = 0x10624DD3
movq %rdi, %r8
shrq $0x3f, %r8
sarq $0x27, %rdi
addl %r8d, %edi
imull $0x7d0, %edi, %edi      # imm = 0x7D0
subl %edi, %esi
movslq %esi, %rsi
leaq (%rdx,%rsi,8), %rsi
addq $0x10, %rsi
jmp 0x8bf4
movslq %esi, %rsi
leaq (%rcx,%rsi,8), %rsi
movq (%rsi), %rsi
movl 0xfce7(%rip), %edi      # 0x188e4
movq %rdi, %r8
shlq $0x4, %r8
addq %rsi, %r8
addq $-0xc, %r8
testl %edi, %edi
jle 0x8fb0
decq %rdi
movzwl (%r8), %r9d
addq $-0x10, %r8
testl $0x201, %r9d            # imm = 0x201
je 0x8c0b
incl %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
shlq $0x4, %rdi
testb $0x2, -0xb(%rsi,%rdi)
je 0x8fb0
incl %eax
movl %eax, (%r10)
cmpl (%rsp), %eax
jne 0x8ba8
jmp 0x8fb0
movslq %ecx, %rax
shlq $0x3, %rax
addq 0xfc89(%rip), %rax      # 0x188e8
movq (%rax), %rbp
movslq (%rdi), %r12
movq %r12, %rbx
shlq $0x4, %rbx
movl (%rbp,%rbx), %esi
leaq 0xf849(%rip), %rax      # 0x184c0
movq (%rax), %rax
testl %esi, %esi
movq %r10, 0x18(%rsp)
movq %rdi, 0x10(%rsp)
movq %rax, 0x30(%rsp)
je 0x8cb1
movq %rax, %rdi
movq %r9, %r15
callq 0x5670
movq %r15, %r9
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %r10
xorl %ecx, %ecx
testq %rax, %rax
setne %cl
movl %ecx, %eax
jmp 0x8cb3
xorl %eax, %eax
addq %rbx, %rbp
movq %rbp, 0x20(%rsp)
movq 0xfc26(%rip), %r11      # 0x188e8
movl %eax, %ebx
movq %r14, 0x28(%rsp)
movl %r12d, %ebp
addl %r14d, %ebp
setns %al
movl 0xfc0c(%rip), %ecx      # 0x188e4
cmpl %ecx, %ebp
setl %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x8cf2
movl 0x13a8e(%rip), %eax     # 0x1c778
movl %r13d, %r8d
jmp 0x8d88
leal (%r14,%r13), %r8d
addl %ecx, %ebp
movl %ebp, %eax
cltd
idivl %ecx
cmpl %r8d, 0x4(%rsp)
jg 0x8fb0
cmpl (%rsp), %r8d
jg 0x8fb0
movl %edx, %ebp
testl %r14d, %r14d
cmovlel %edx, %r12d
cmovlel %r8d, %r13d
movl 0x13a53(%rip), %eax     # 0x1c778
subl %eax, %r13d
jge 0x8d6c
movl 0x13a40(%rip), %edx     # 0x1c770
addl %r13d, %edx
addl $0x7d1, %edx            # imm = 0x7D1
movslq %edx, %rdx
imulq $0x10624dd3, %rdx, %rsi # imm = 0x10624DD3
movl %r8d, %r15d
movq %rsi, %r8
shrq $0x3f, %r8
sarq $0x27, %rsi
addl %r8d, %esi
movl %r15d, %r8d
imull $0x7d0, %esi, %esi      # imm = 0x7D0
subl %esi, %edx
movslq %edx, %rdx
leaq (%r9,%rdx,8), %rdx
addq $0x10, %rdx
jmp 0x8d73
movslq %r13d, %rdx
leaq (%r11,%rdx,8), %rdx
movq (%rdx), %rdx
movslq %r12d, %rsi
shlq $0x4, %rsi
testb $0x2, 0x5(%rdx,%rsi)
je 0x8fb0
movl %r8d, 0x8(%rsp)
movl %r8d, %edx
subl %eax, %edx
jge 0x8dcf
movl 0x139d6(%rip), %esi     # 0x1c770
addl %esi, %edx
addl $0x7d1, %edx            # imm = 0x7D1
movslq %edx, %rdx
imulq $0x10624dd3, %rdx, %rsi # imm = 0x10624DD3
movq %rsi, %r8
shrq $0x3f, %r8
sarq $0x27, %rsi
addl %r8d, %esi
imull $0x7d0, %esi, %esi      # imm = 0x7D0
subl %esi, %edx
movslq %edx, %rdx
leaq (%r9,%rdx,8), %rdx
addq $0x10, %rdx
jmp 0x8dd6
movslq %edx, %rdx
leaq (%r11,%rdx,8), %rdx
movl %ecx, %esi
movq (%rdx), %r8
movl %ecx, %edx
sarl $0x1f, %edx
andl %ecx, %edx
shlq $0x4, %rsi
addq %r8, %rsi
addq $-0xc, %rsi
testl %ecx, %ecx
jle 0x8e08
movzwl (%rsi), %r8d
addq $-0x10, %rsi
decl %ecx
testl $0x201, %r8d            # imm = 0x201
je 0x8ded
incl %ecx
movl %ecx, %edx
cmpl %edx, %ebp
movl 0x8(%rsp), %esi
jge 0x8fb0
movl %esi, %ecx
subl %eax, %ecx
jge 0x8e51
movl 0x13950(%rip), %eax     # 0x1c770
addl %ecx, %eax
addl $0x7d1, %eax            # imm = 0x7D1
cltq
imulq $0x10624dd3, %rax, %rcx # imm = 0x10624DD3
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x27, %rcx
addl %edx, %ecx
imull $0x7d0, %ecx, %ecx      # imm = 0x7D0
subl %ecx, %eax
cltq
leaq (%r9,%rax,8), %rax
addq $0x10, %rax
jmp 0x8e58
movslq %ecx, %rax
leaq (%r11,%rax,8), %rax
movq (%rax), %r15
movslq %ebp, %r12
shlq $0x4, %r12
movl (%r15,%r12), %r13d
testl %r13d, %r13d
je 0x8ea7
movq 0x30(%rsp), %rdi
movl %r13d, %esi
movq %r9, %r14
movl %ebx, 0xc(%rsp)
movq %r11, %rbx
callq 0x5670
movl 0x8(%rsp), %esi
movq %rbx, %r11
movl 0xc(%rsp), %ebx
movq %r14, %r9
movq 0x28(%rsp), %r14
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %r10
testq %rax, %rax
setne %al
jmp 0x8ea9
xorl %eax, %eax
addq %r12, %r15
movzbl %al, %eax
testb $0x8, 0x5(%r15)
jne 0x8eda
cmpl %eax, %ebx
jne 0x8fb0
testl %eax, %eax
je 0x8eda
cmpl $0x20, %r13d
jne 0x8fb0
movq 0x20(%rsp), %rcx
cmpl $0x20, (%rcx)
jne 0x8fb0
movl %ebp, (%rdi)
movl %esi, (%r10)
movl (%rdi), %r12d
movl %esi, %r13d
movl %eax, %ebx
movq %r15, 0x20(%rsp)
jmp 0x8cc9
movl (%r10), %eax
cmpl 0x4(%rsp), %eax
jle 0x8fb0
movq 0xf9e3(%rip), %rcx      # 0x188e8
leaq 0xf9d4(%rip), %rdx      # 0x188e0
movl %eax, %esi
leal -0x1(%rsi), %eax
movl 0x13861(%rip), %edi     # 0x1c778
subl %edi, %esi
jle 0x8f28
movl %eax, %esi
subl %edi, %esi
movslq %esi, %rsi
leaq (%rcx,%rsi,8), %rsi
jmp 0x8f61
movl 0x13842(%rip), %edi     # 0x1c770
addl %edi, %esi
addl $0x7d0, %esi            # imm = 0x7D0
movslq %esi, %rsi
imulq $0x10624dd3, %rsi, %rdi # imm = 0x10624DD3
movq %rdi, %r8
shrq $0x3f, %r8
sarq $0x27, %rdi
addl %r8d, %edi
imull $0x7d0, %edi, %edi      # imm = 0x7D0
subl %edi, %esi
movslq %esi, %rsi
leaq (%rdx,%rsi,8), %rsi
addq $0x10, %rsi
movq (%rsi), %rsi
movl 0xf97a(%rip), %edi      # 0x188e4
movq %rdi, %r8
shlq $0x4, %r8
addq %rsi, %r8
addq $-0xc, %r8
testl %edi, %edi
jle 0x8fb0
decq %rdi
movzwl (%r8), %r9d
addq $-0x10, %r8
testl $0x201, %r9d            # imm = 0x201
je 0x8f78
incl %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
shlq $0x4, %rdi
testb $0x2, -0xb(%rsi,%rdi)
je 0x8fb0
movl %eax, (%r10)
cmpl 0x4(%rsp), %eax
jg 0x8f0c
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq