// File: /workspace/github_c_makefile_star2_O2/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, 0x8(%rsp)
movl 0x14613(%rip), %eax     # 0x1c788
movl %eax, %edx
subl 0x14607(%rip), %edx     # 0x1c784
xorl %ecx, %ecx
testb $0x4, 0x14646(%rip)     # 0x1c7cc
cmovnel %ecx, %edx
movl %edx, 0x10(%rsp)
movq %rdi, 0x20(%rsp)
movl $0x0, %edx
cmovel %eax, %edx
movl 0x10750(%rip), %esi     # 0x188f0
addl %esi, %edx
decl %edx
movl %edx, 0x14(%rsp)
movl 0x1071a(%rip), %edx     # 0x188c8
cmpl $0x2, %edx
je 0x81fb
cmpl $0x1, %edx
jne 0x852c
movq 0x8(%rsp), %rcx
movl (%rcx), %r13d
movl %r13d, %ecx
leaq 0x10722(%rip), %rsi     # 0x188f0
subl %eax, %ecx
jge 0x829c
movl 0x145a4(%rip), %eax     # 0x1c780
addl %ecx, %eax
addl $0x7d1, %eax            # imm = 0x7D1
movl $0x7d0, %ecx            # imm = 0x7D0
cltd
idivl %ecx
movslq %edx, %rax
leaq (%rsi,%rax,8), %rax
addq $0x10, %rax
jmp 0x82aa
movl 0x106f3(%rip), %eax     # 0x188f4
decl %eax
testl %r14d, %r14d
cmovsl %ecx, %eax
movq 0x20(%rsp), %rcx
movl %eax, (%rcx)
js 0x84b6
je 0x852c
movq 0x8(%rsp), %rax
movslq (%rax), %rbx
movq %rbx, %r14
shlq $0x3, %r14
addq 0x106c6(%rip), %r14     # 0x188f8
leaq 0x106b7(%rip), %r15     # 0x188f0
movl $0x7d0, %ebp            # imm = 0x7D0
cmpl 0x14(%rsp), %ebx
jge 0x852c
movslq 0x14539(%rip), %rax     # 0x1c788
cmpl %eax, %ebx
jge 0x8273
movl 0x14527(%rip), %ecx     # 0x1c780
subl %eax, %ecx
leal (%rbx,%rcx), %eax
addl $0x7d1, %eax            # imm = 0x7D1
cltd
idivl %ebp
movslq %edx, %rax
leaq (%r15,%rax,8), %rcx
addq $0x10, %rcx
jmp 0x827d
shlq $0x3, %rax
movq %r14, %rcx
subq %rax, %rcx
movq (%rcx), %rdi
callq 0x853b
testl %eax, %eax
je 0x852c
incl %ebx
movq 0x8(%rsp), %rax
movl %ebx, (%rax)
addq $0x8, %r14
jmp 0x823e
movslq %ecx, %rax
shlq $0x3, %rax
addq 0x1064e(%rip), %rax     # 0x188f8
movq (%rax), %r12
movq 0x20(%rsp), %rax
movslq (%rax), %rbx
movq %rbx, %r15
shlq $0x4, %r15
movl (%r12,%r15), %esi
leaq 0x101f9(%rip), %rax     # 0x184c0
movq (%rax), %rdi
testl %esi, %esi
movq %rdi, 0x30(%rsp)
je 0x82e6
callq 0x5670
xorl %ecx, %ecx
testq %rax, %rax
setne %cl
movl %ecx, 0x4(%rsp)
jmp 0x82ee
movl $0x0, 0x4(%rsp)
addq %r15, %r12
movq %r12, 0x28(%rsp)
movq 0x105fb(%rip), %rax     # 0x188f8
movq %rax, 0x18(%rsp)
movl %ebx, %ebp
addl %r14d, %ebp
setns %al
movl 0x105e4(%rip), %ecx     # 0x188f4
cmpl %ecx, %ebp
setl %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x832b
movl 0x14465(%rip), %r15d    # 0x1c788
movl %r13d, %r12d
jmp 0x83af
leal (%r14,%r13), %r12d
addl %ecx, %ebp
movl %ebp, %eax
cltd
idivl %ecx
cmpl %r12d, 0x10(%rsp)
jg 0x852c
cmpl 0x14(%rsp), %r12d
jg 0x852c
movl %edx, %ebp
testl %r14d, %r14d
cmovlel %edx, %ebx
cmovlel %r12d, %r13d
movl 0x14429(%rip), %r15d    # 0x1c788
subl %r15d, %r13d
jge 0x838e
movl 0x14416(%rip), %eax     # 0x1c780
addl %r13d, %eax
addl $0x7d1, %eax            # imm = 0x7D1
cltd
movl $0x7d0, %ecx            # imm = 0x7D0
idivl %ecx
movslq %edx, %rax
leaq 0x1056c(%rip), %rcx     # 0x188f0
leaq (%rcx,%rax,8), %rax
addq $0x10, %rax
jmp 0x839a
movslq %r13d, %rax
movq 0x18(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movq (%rax), %rax
movslq %ebx, %rcx
shlq $0x4, %rcx
testb $0x2, 0x5(%rax,%rcx)
je 0x852c
movl %r12d, %eax
subl %r15d, %eax
jge 0x83e0
movl 0x143c3(%rip), %ecx     # 0x1c780
addl %ecx, %eax
addl $0x7d1, %eax            # imm = 0x7D1
cltd
movl $0x7d0, %ecx            # imm = 0x7D0
idivl %ecx
movslq %edx, %rax
leaq 0x1051a(%rip), %rcx     # 0x188f0
leaq (%rcx,%rax,8), %rax
addq $0x10, %rax
jmp 0x83eb
cltq
movq 0x18(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movq (%rax), %rdi
callq 0x6214
cmpl %eax, %ebp
jge 0x852c
movl %r12d, %eax
subl %r15d, %eax
jge 0x842c
movl 0x14377(%rip), %ecx     # 0x1c780
addl %ecx, %eax
addl $0x7d1, %eax            # imm = 0x7D1
cltd
movl $0x7d0, %ecx            # imm = 0x7D0
idivl %ecx
movslq %edx, %rax
leaq 0x104ce(%rip), %rcx     # 0x188f0
leaq (%rcx,%rax,8), %rax
addq $0x10, %rax
jmp 0x8437
cltq
movq 0x18(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movq (%rax), %r15
movslq %ebp, %rbx
shlq $0x4, %rbx
movl (%r15,%rbx), %r13d
testl %r13d, %r13d
je 0x845f
movq 0x30(%rsp), %rdi
movl %r13d, %esi
callq 0x5670
testq %rax, %rax
setne %al
jmp 0x8461
xorl %eax, %eax
addq %rbx, %r15
movzbl %al, %eax
testb $0x8, 0x5(%r15)
jne 0x8494
cmpl %eax, 0x4(%rsp)
jne 0x852c
testl %eax, %eax
je 0x8494
cmpl $0x20, %r13d
jne 0x852c
movq 0x28(%rsp), %rcx
cmpl $0x20, (%rcx)
jne 0x852c
movq 0x20(%rsp), %rdx
movl %ebp, (%rdx)
movq 0x8(%rsp), %rcx
movl %r12d, (%rcx)
movl (%rdx), %ebx
movl %r12d, %r13d
movl %eax, 0x4(%rsp)
movq %r15, 0x28(%rsp)
jmp 0x8302
movq 0x1043b(%rip), %rbx     # 0x188f8
movq 0x8(%rsp), %rax
movl (%rax), %r14d
decl %r14d
leaq 0x10421(%rip), %r15     # 0x188f0
movl $0x7d0, %ebp            # imm = 0x7D0
leal 0x1(%r14), %eax
cmpl 0x10(%rsp), %eax
jle 0x852c
movl 0x142a4(%rip), %ecx     # 0x1c788
cmpl %ecx, %eax
jle 0x84f5
movl %r14d, %eax
subl %ecx, %eax
cltq
leaq (%rbx,%rax,8), %rax
jmp 0x8513
movl 0x14285(%rip), %eax     # 0x1c780
addl %r14d, %eax
subl %ecx, %eax
addl $0x7d1, %eax            # imm = 0x7D1
cltd
idivl %ebp
movslq %edx, %rax
leaq (%r15,%rax,8), %rax
addq $0x10, %rax
movq (%rax), %rdi
callq 0x853b
testl %eax, %eax
je 0x852c
movq 0x8(%rsp), %rax
movl %r14d, (%rax)
decl %r14d
jmp 0x84d4
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq