// File: /workspace/github_c_makefile_star2_O3/pajlada[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 = &term.line[*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 (!(term.line[yt][xt].mode & ATTR_WRAP))
					break;
			}

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

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %edx, %r15d
movq %rsi, 0x10(%rsp)
movq %rdi, 0x20(%rsp)
movl 0xd9ed(%rip), %eax      # 0x15728
cmpl $0x2, %eax
je 0x7da1
cmpl $0x1, %eax
jne 0x7f70
movq 0xda08(%rip), %rcx      # 0x15758
movq 0x10(%rsp), %rax
movslq (%rax), %r12
movq %rcx, 0x38(%rsp)
movq (%rcx,%r12,8), %r14
movq 0x20(%rsp), %rax
movslq (%rax), %r13
movq %r13, %rbx
shlq $0x4, %rbx
movl (%r14,%rbx), %esi
leaq 0xd745(%rip), %rax      # 0x154c0
movq (%rax), %rdi
testl %esi, %esi
movq %rdi, 0x40(%rsp)
je 0x7e27
callq 0x5670
xorl %ecx, %ecx
testq %rax, %rax
setne %cl
movl %ecx, 0xc(%rsp)
jmp 0x7e2f
movl 0xd9ad(%rip), %eax      # 0x15754
decl %eax
xorl %ecx, %ecx
testl %r15d, %r15d
cmovnsl %eax, %ecx
movq 0x20(%rsp), %rax
movl %ecx, (%rax)
js 0x7f37
je 0x7f70
movq 0x10(%rsp), %rax
movslq (%rax), %rax
movl 0xd97e(%rip), %ecx      # 0x15750
decl %ecx
cmpl %ecx, %eax
jge 0x7f70
movq 0xd975(%rip), %rcx      # 0x15758
movl %r15d, %edx
movslq 0xd967(%rip), %rsi      # 0x15754
movslq 0xd95c(%rip), %rdi      # 0x15750
decq %rdi
leal (%rax,%r15), %r8d
shlq $0x4, %rsi
movq (%rcx,%rax,8), %r9
testb $0x1, -0xb(%r9,%rsi)
je 0x7f70
addq %rdx, %rax
movq 0x10(%rsp), %r9
movl %r8d, (%r9)
addl %r15d, %r8d
cmpq %rdi, %rax
jl 0x7dff
jmp 0x7f70
movl $0x0, 0xc(%rsp)
addq %rbx, %r14
movq %r14, 0x30(%rsp)
movl 0xd917(%rip), %eax      # 0x15754
movl %eax, 0x1c(%rsp)
movl 0xd909(%rip), %eax      # 0x15750
movl %eax, 0x2c(%rsp)
movl %r13d, %ebp
addl %r15d, %ebp
setns %al
cmpl 0x1c(%rsp), %ebp
setl %cl
testb %cl, %al
jne 0x7eae
movl %r12d, %ecx
addl %r15d, %ecx
js 0x7f70
cmpl 0x2c(%rsp), %ecx
jge 0x7f70
movl 0x1c(%rsp), %esi
addl %esi, %ebp
movl %ebp, %eax
cltd
idivl %esi
testl %r15d, %r15d
movl %edx, %ebp
cmovlel %edx, %r13d
cmovlel %ecx, %r12d
movslq %r12d, %rax
movq 0x38(%rsp), %rdx
movq (%rdx,%rax,8), %rax
movslq %r13d, %rdx
shlq $0x4, %rdx
testb $0x1, 0x5(%rax,%rdx)
movl %ecx, %r12d
je 0x7f70
movl %r12d, %edi
callq 0x662d
cmpl %eax, %ebp
jge 0x7f70
movq %r15, %r14
movslq %r12d, %rax
movq 0x38(%rsp), %rcx
movq (%rcx,%rax,8), %rbx
movslq %ebp, %r15
shlq $0x4, %r15
movl (%rbx,%r15), %r13d
testl %r13d, %r13d
je 0x7ef2
movq 0x40(%rsp), %rdi
movl %r13d, %esi
callq 0x5670
testq %rax, %rax
setne %al
jmp 0x7ef4
xorl %eax, %eax
addq %r15, %rbx
movzbl %al, %eax
testb $0x4, 0x5(%rbx)
movq %r14, %r15
jne 0x7f17
cmpl %eax, 0xc(%rsp)
jne 0x7f70
testl %eax, %eax
je 0x7f17
movq 0x30(%rsp), %rcx
cmpl (%rcx), %r13d
jne 0x7f70
movq 0x20(%rsp), %rdx
movl %ebp, (%rdx)
movq 0x10(%rsp), %rcx
movl %r12d, (%rcx)
movl (%rdx), %r13d
movl %eax, 0xc(%rsp)
movq %rbx, 0x30(%rsp)
jmp 0x7e4b
movq 0x10(%rsp), %rax
movl (%rax), %eax
testl %eax, %eax
jle 0x7f70
movq 0xd80f(%rip), %rcx      # 0x15758
movslq 0xd804(%rip), %rdx      # 0x15754
shlq $0x4, %rdx
movl %eax, %esi
movq -0x8(%rcx,%rsi,8), %rsi
testb $0x1, -0xb(%rsi,%rdx)
je 0x7f70
addl %r15d, %eax
movq 0x10(%rsp), %rsi
movl %eax, (%rsi)
testl %eax, %eax
jg 0x7f54
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq