// File: /workspace/github_c_makefile_star2_O1/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, %r9d
movq %rsi, %r8
movl 0x13d9c(%rip), %eax     # 0x1c788
movl %eax, %edx
subl 0x13d90(%rip), %edx     # 0x1c784
xorl %ecx, %ecx
testb $0x4, 0x13dcf(%rip)     # 0x1c7cc
cmovnel %ecx, %edx
movl %edx, (%rsp)
movq %rdi, %r15
movl $0x0, %edx
cmovel %eax, %edx
movl 0xfedc(%rip), %esi      # 0x188f0
addl %esi, %edx
decl %edx
movl %edx, 0x4(%rsp)
movl 0xfea6(%rip), %edx      # 0x188c8
cmpl $0x2, %edx
movq %r8, 0x8(%rsp)
je 0x8a81
cmpl $0x1, %edx
jne 0x8e47
movl (%r8), %ecx
leaq 0xfeb1(%rip), %r10      # 0x188f0
subl %eax, %ecx
jge 0x8b2a
movl 0x13d33(%rip), %eax     # 0x1c780
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 (%r10,%rax,8), %rax
addq $0x10, %rax
jmp 0x8b38
movl 0xfe6d(%rip), %eax      # 0x188f4
decl %eax
testl %r9d, %r9d
cmovsl %ecx, %eax
movl %eax, (%r15)
js 0x8dab
je 0x8e47
movl (%r8), %ebx
cmpl 0x4(%rsp), %ebx
jge 0x8e47
movq 0xfe46(%rip), %r14      # 0x188f8
leaq 0xfe37(%rip), %r15      # 0x188f0
movl 0x13cc9(%rip), %eax     # 0x1c788
movl %ebx, %ecx
subl %eax, %ecx
jge 0x8aff
movl 0x13cb5(%rip), %ecx     # 0x1c780
subl %eax, %ecx
leal (%rbx,%rcx), %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 (%r15,%rax,8), %rax
addq $0x10, %rax
jmp 0x8b06
movslq %ecx, %rax
leaq (%r14,%rax,8), %rax
movq (%rax), %rdi
callq 0x8e56
testl %eax, %eax
movq 0x8(%rsp), %rax
je 0x8e47
incl %ebx
movl %ebx, (%rax)
cmpl %ebx, 0x4(%rsp)
jne 0x8ab9
jmp 0x8e47
movslq %ecx, %rax
shlq $0x3, %rax
addq 0xfdc0(%rip), %rax      # 0x188f8
movq (%rax), %r12
movslq (%r15), %rbx
shlq $0x4, %rbx
movl (%r12,%rbx), %esi
testl %esi, %esi
movq %r9, 0x18(%rsp)
je 0x8b78
leaq 0xf97a(%rip), %rax      # 0x184d0
movq (%rax), %rdi
movq %r10, %r14
callq 0x5680
movq %r14, %r10
movq 0x18(%rsp), %r9
movq 0x8(%rsp), %r8
xorl %ecx, %ecx
testq %rax, %rax
setne %cl
jmp 0x8b7a
xorl %ecx, %ecx
addq %rbx, %r12
movq 0xfd74(%rip), %r11      # 0x188f8
leaq 0xf945(%rip), %rax      # 0x184d0
movq (%rax), %rax
movq %rax, 0x20(%rsp)
movl %ecx, %ebx
movq %r15, 0x28(%rsp)
movq %r12, 0x30(%rsp)
movl (%r15), %esi
movl %esi, %ebp
addl %r9d, %ebp
movl (%r8), %edi
setns %al
movl 0xfd41(%rip), %ecx      # 0x188f4
cmpl %ecx, %ebp
setl %dl
testb %dl, %al
je 0x8bc4
movl %edi, %r14d
jmp 0x8c4b
leal (%rdi,%r9), %r14d
addl %ecx, %ebp
movl %ebp, %eax
cltd
idivl %ecx
cmpl %r14d, (%rsp)
jg 0x8e47
cmpl 0x4(%rsp), %r14d
jg 0x8e47
movl %edx, %ebp
testl %r9d, %r9d
cmovlel %edx, %esi
cmovlel %r14d, %edi
subl 0x13b92(%rip), %edi     # 0x1c788
jge 0x8c2f
movl 0x13b82(%rip), %eax     # 0x1c780
addl %edi, %eax
addl $0x7d1, %eax            # imm = 0x7D1
cltq
imulq $0x10624dd3, %rax, %rdx # imm = 0x10624DD3
movq %rdx, %rdi
shrq $0x3f, %rdi
sarq $0x27, %rdx
addl %edi, %edx
imull $0x7d0, %edx, %edx      # imm = 0x7D0
subl %edx, %eax
cltq
leaq (%r10,%rax,8), %rax
addq $0x10, %rax
jmp 0x8c36
movslq %edi, %rax
leaq (%r11,%rax,8), %rax
movq (%rax), %rax
movslq %esi, %rdx
shlq $0x4, %rdx
testb $0x2, 0x5(%rax,%rdx)
je 0x8e47
movl 0x13b37(%rip), %eax     # 0x1c788
movl %r14d, %edx
subl %eax, %edx
jge 0x8c92
movl 0x13b22(%rip), %esi     # 0x1c780
addl %esi, %edx
addl $0x7d1, %edx            # imm = 0x7D1
movslq %edx, %rdx
imulq $0x10624dd3, %rdx, %rsi # imm = 0x10624DD3
movq %rsi, %rdi
shrq $0x3f, %rdi
sarq $0x27, %rsi
addl %edi, %esi
imull $0x7d0, %esi, %esi      # imm = 0x7D0
subl %esi, %edx
movslq %edx, %rdx
leaq (%r10,%rdx,8), %rdx
addq $0x10, %rdx
jmp 0x8c99
movslq %edx, %rdx
leaq (%r11,%rdx,8), %rdx
movl %ecx, %esi
movq (%rdx), %rdi
movl %ecx, %edx
sarl $0x1f, %edx
andl %ecx, %edx
shlq $0x4, %rsi
addq %rdi, %rsi
addq $-0xc, %rsi
testl %ecx, %ecx
jle 0x8cc9
movzwl (%rsi), %edi
addq $-0x10, %rsi
decl %ecx
testl $0x201, %edi            # imm = 0x201
je 0x8cb0
incl %ecx
movl %ecx, %edx
cmpl %edx, %ebp
jge 0x8e47
movl %r14d, %ecx
subl %eax, %ecx
movl %r14d, 0x14(%rsp)
jge 0x8d14
movl 0x13a9d(%rip), %eax     # 0x1c780
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 (%r10,%rax,8), %rax
addq $0x10, %rax
jmp 0x8d1b
movslq %ecx, %rax
leaq (%r11,%rax,8), %rax
movq (%rax), %r12
movslq %ebp, %r14
shlq $0x4, %r14
movl (%r12,%r14), %r13d
testl %r13d, %r13d
je 0x8d66
movq 0x20(%rsp), %rdi
movl %r13d, %esi
movq %r10, %r15
movl %ebx, 0x10(%rsp)
movq %r11, %rbx
callq 0x5680
movq %rbx, %r11
movl 0x10(%rsp), %ebx
movq %r15, %r10
movq 0x28(%rsp), %r15
movq 0x18(%rsp), %r9
movq 0x8(%rsp), %r8
testq %rax, %rax
setne %al
jmp 0x8d68
xorl %eax, %eax
addq %r14, %r12
movzbl %al, %eax
testb $0x8, 0x5(%r12)
movl 0x14(%rsp), %edx
jne 0x8d9e
cmpl %eax, %ebx
jne 0x8e47
testl %eax, %eax
je 0x8d9e
cmpl $0x20, %r13d
jne 0x8e47
movq 0x30(%rsp), %rcx
cmpl $0x20, (%rcx)
jne 0x8e47
movl %ebp, (%r15)
movl %edx, (%r8)
movl %eax, %ebx
jmp 0x8b9a
movslq (%r8), %rax
cmpl (%rsp), %eax
jle 0x8e47
movq 0xfb3a(%rip), %rcx      # 0x188f8
leal 0x7d0(%rax), %ebx
leaq (%rcx,%rax,8), %r14
addq $-0x8, %r14
leaq 0xfb1d(%rip), %r15      # 0x188f0
leal -0x7d0(%rbx), %ecx
movslq 0x139a8(%rip), %rax     # 0x1c788
cmpl %eax, %ecx
jle 0x8df0
shlq $0x3, %rax
movq %r14, %rcx
subq %rax, %rcx
jmp 0x8e23
movl 0x1398a(%rip), %ecx     # 0x1c780
subl %eax, %ecx
addl %ebx, %ecx
movslq %ecx, %rax
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 (%r15,%rax,8), %rcx
addq $0x10, %rcx
movq (%rcx), %rdi
callq 0x8e56
testl %eax, %eax
movq 0x8(%rsp), %rcx
je 0x8e47
leal -0x7d1(%rbx), %eax
movl %eax, (%rcx)
decl %ebx
addq $-0x8, %r14
cmpl (%rsp), %eax
jg 0x8dd3
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq