// File: /workspace/github_c_makefile_star2_O3/odknt[P]st/st.c
// Function: selsnap
void
selsnap(int *x, int *y, int direction)
{
	int newx, newy, xt, yt;
	int delim, prevdelim;
	const Glyph *gp, *prevgp;

	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, 0, term.row - 1))
					break;

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

			if (newx >= tlinelen(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 > 0; *y += direction) {
				if (!(TLINE(*y-1)[term.col-1].mode
						& ATTR_WRAP)) {
					break;
				}
			}
		} else if (direction > 0) {
			for (; *y < term.row-1; *y += direction) {
				if (!(TLINE(*y)[term.col-1].mode
						& ATTR_WRAP)) {
					break;
				}
			}
		}
		break;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %edx, %r10d
movq %rsi, 0x10(%rsp)
movq %rdi, 0x18(%rsp)
movl 0xfbd2(%rip), %eax      # 0x19418
cmpl $0x2, %eax
je 0x98ac
cmpl $0x1, %eax
jne 0x9c24
movq 0x10(%rsp), %rax
movl (%rax), %r12d
movl %r12d, %eax
leaq 0xfbda(%rip), %rsi      # 0x19440
subl 0x13a70(%rip), %eax     # 0x1d2dc
jge 0x9996
movl 0x13a60(%rip), %ecx     # 0x1d2d8
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 (%rsi,%rax,8), %rax
addq $0x18, %rax
jmp 0x99a3
movl 0xfb92(%rip), %eax      # 0x19444
decl %eax
xorl %ecx, %ecx
testl %r10d, %r10d
cmovnsl %eax, %ecx
movq 0x18(%rsp), %rax
movl %ecx, (%rax)
js 0x9b92
je 0x9c24
movq 0x10(%rsp), %rax
movl (%rax), %eax
movl 0xfb64(%rip), %ecx      # 0x19440
decl %ecx
cmpl %ecx, %eax
jge 0x9c24
movslq %eax, %rcx
shlq $0x3, %rcx
addq 0xfb54(%rip), %rcx      # 0x19448
movslq %r10d, %rdx
shlq $0x3, %rdx
leaq 0xfb3e(%rip), %rsi      # 0x19440
movslq 0x139d3(%rip), %rdi     # 0x1d2dc
cmpl %edi, %eax
jge 0x9950
movl 0x139c4(%rip), %r8d     # 0x1d2d8
subl %edi, %r8d
leal (%rax,%r8), %edi
addl $0x7d1, %edi            # imm = 0x7D1
movslq %edi, %rdi
imulq $0x10624dd3, %rdi, %r8  # imm = 0x10624DD3
movq %r8, %r9
shrq $0x3f, %r9
sarq $0x27, %r8
addl %r9d, %r8d
imull $0x7d0, %r8d, %r8d      # imm = 0x7D0
subl %r8d, %edi
movslq %edi, %rdi
leaq (%rsi,%rdi,8), %r8
addq $0x18, %r8
jmp 0x995a
shlq $0x3, %rdi
movq %rcx, %r8
subq %rdi, %r8
movq (%r8), %rdi
movslq 0xfae0(%rip), %r8       # 0x19444
shlq $0x4, %r8
testb $0x1, -0xb(%rdi,%r8)
je 0x9c24
addl %r10d, %eax
movq 0x10(%rsp), %rdi
movl %eax, (%rdi)
movl 0xfabc(%rip), %edi      # 0x19440
decl %edi
addq %rdx, %rcx
cmpl %edi, %eax
jl 0x9902
jmp 0x9c24
cltq
shlq $0x3, %rax
addq 0xfaa5(%rip), %rax      # 0x19448
movq (%rax), %r13
movq 0x18(%rsp), %rax
movslq (%rax), %r14
movq %r14, %r15
shlq $0x4, %r15
movl (%r13,%r15), %esi
leaq 0xebcf(%rip), %rax      # 0x18590
movq (%rax), %rdi
testl %esi, %esi
movl %r10d, 0xc(%rsp)
movq %rdi, 0x30(%rsp)
je 0x99e6
callq 0x6780
movl 0xc(%rsp), %r10d
xorl %ebx, %ebx
testq %rax, %rax
setne %bl
jmp 0x99e8
xorl %ebx, %ebx
addq %r15, %r13
movq %r13, 0x28(%rsp)
movq 0xfa51(%rip), %rax      # 0x19448
movq %rax, 0x20(%rsp)
movl %r14d, %ebp
addl %r10d, %ebp
setns %al
movl 0xfa39(%rip), %ecx      # 0x19444
cmpl %ecx, %ebp
setl %dl
testb %dl, %al
je 0x9a1c
movl %r12d, %r13d
jmp 0x9ab2
addl %ecx, %ebp
movl %ebp, %eax
cltd
idivl %ecx
movl %r12d, %r13d
addl %r10d, %r13d
js 0x9c24
cmpl 0xfa0a(%rip), %r13d     # 0x19440
jge 0x9c24
movl %edx, %ebp
testl %r10d, %r10d
cmovlel %edx, %r14d
cmovlel %r13d, %r12d
subl 0x1388c(%rip), %r12d    # 0x1d2dc
jge 0x9a91
movl 0x13880(%rip), %eax     # 0x1d2d8
addl %r12d, %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 0xf9b9(%rip), %rcx      # 0x19440
leaq (%rcx,%rax,8), %rax
addq $0x18, %rax
jmp 0x9a9d
movslq %r12d, %rax
movq 0x20(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movq (%rax), %rax
movslq %r14d, %rcx
shlq $0x4, %rcx
testb $0x1, 0x5(%rax,%rcx)
je 0x9c24
movl %r13d, %edi
callq 0x793d
movl 0xc(%rsp), %r10d
cmpl %eax, %ebp
jge 0x9c24
movl %r13d, %eax
subl 0x1380c(%rip), %eax     # 0x1d2dc
jge 0x9b10
movl 0x13800(%rip), %ecx     # 0x1d2d8
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 0xf93a(%rip), %rcx      # 0x19440
leaq (%rcx,%rax,8), %rax
addq $0x18, %rax
jmp 0x9b1b
cltq
movq 0x20(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movq (%rax), %r15
movslq %ebp, %r14
shlq $0x4, %r14
movl (%r15,%r14), %r12d
testl %r12d, %r12d
je 0x9b48
movq 0x30(%rsp), %rdi
movl %r12d, %esi
callq 0x6780
movl 0xc(%rsp), %r10d
testq %rax, %rax
setne %al
jmp 0x9b4a
xorl %eax, %eax
addq %r14, %r15
movzbl %al, %eax
testb $0x4, 0x5(%r15)
jne 0x9b71
cmpl %eax, %ebx
jne 0x9c24
testl %eax, %eax
je 0x9b71
movq 0x28(%rsp), %rcx
cmpl (%rcx), %r12d
jne 0x9c24
movq 0x18(%rsp), %rdx
movl %ebp, (%rdx)
movq 0x10(%rsp), %rcx
movl %r13d, (%rcx)
movl (%rdx), %r14d
movl %r13d, %r12d
movl %eax, %ebx
movq %r15, 0x28(%rsp)
jmp 0x99fc
movq 0x10(%rsp), %rax
movl (%rax), %eax
testl %eax, %eax
jle 0x9c24
movq 0xf8a0(%rip), %rcx      # 0x19448
leaq 0xf891(%rip), %rdx      # 0x19440
movl 0x13727(%rip), %esi     # 0x1d2dc
cmpl %esi, %eax
jle 0x9bc6
notl %esi
addl %eax, %esi
movslq %esi, %rsi
leaq (%rcx,%rsi,8), %rsi
jmp 0x9c01
movl 0x1370c(%rip), %edi     # 0x1d2d8
addl %eax, %edi
subl %esi, %edi
addl $0x7d0, %edi            # imm = 0x7D0
movslq %edi, %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 $0x18, %rsi
movq (%rsi), %rsi
movslq 0xf839(%rip), %rdi      # 0x19444
shlq $0x4, %rdi
testb $0x1, -0xb(%rsi,%rdi)
je 0x9c24
addl %r10d, %eax
movq 0x10(%rsp), %rsi
movl %eax, (%rsi)
testl %eax, %eax
jg 0x9baf
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq