// File: /workspace/github_c_makefile_star2_O1/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 $0x38, %rsp
movl %edx, %r15d
movl 0xdce3(%rip), %eax      # 0x15728
cmpl $0x2, %eax
je 0x7aa0
cmpl $0x1, %eax
jne 0x7c72
movq 0xdcfe(%rip), %rcx      # 0x15758
movq %rsi, 0x18(%rsp)
movslq (%rsi), %rax
movq %rcx, 0x28(%rsp)
movq (%rcx,%rax,8), %r14
movq %rdi, 0x10(%rsp)
movslq (%rdi), %rbx
shlq $0x4, %rbx
movl (%r14,%rbx), %esi
testl %esi, %esi
je 0x7b17
leaq 0xda36(%rip), %rax      # 0x154c0
movq (%rax), %rdi
callq 0x5680
xorl %ecx, %ecx
testq %rax, %rax
setne %cl
movl %ecx, 0x4(%rsp)
jmp 0x7b1f
movl 0xdcae(%rip), %eax      # 0x15754
decl %eax
xorl %ecx, %ecx
testl %r15d, %r15d
cmovnsl %eax, %ecx
movl %ecx, (%rdi)
js 0x7c43
je 0x7c72
movslq (%rsi), %rax
movl 0xdc89(%rip), %ecx      # 0x15750
decl %ecx
cmpl %ecx, %eax
jge 0x7c72
movq 0xdc80(%rip), %rcx      # 0x15758
movl %r15d, %edx
movslq 0xdc72(%rip), %r10      # 0x15754
movslq 0xdc67(%rip), %rdi      # 0x15750
decq %rdi
leal (%rax,%r15), %r8d
shlq $0x4, %r10
movq (%rcx,%rax,8), %r9
testb $0x1, -0xb(%r9,%r10)
je 0x7c72
addq %rdx, %rax
movl %r8d, (%rsi)
addl %r15d, %r8d
cmpq %rdi, %rax
jl 0x7af4
jmp 0x7c72
movl $0x0, 0x4(%rsp)
addq %rbx, %r14
movq %r14, 0x20(%rsp)
leaq 0xd992(%rip), %rax      # 0x154c0
movq (%rax), %rax
movq %rax, 0x30(%rsp)
movl 0xdc18(%rip), %eax      # 0x15754
movl %eax, 0x8(%rsp)
movl 0xdc0a(%rip), %eax      # 0x15750
movl %eax, 0xc(%rsp)
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %rcx
movl (%rcx), %ecx
movl %ecx, %ebp
addl %r15d, %ebp
movl (%rdx), %esi
setns %al
cmpl 0x8(%rsp), %ebp
setl %dl
testb %dl, %al
je 0x7b70
movl %esi, %r13d
jmp 0x7bbc
movl %esi, %r13d
addl %r15d, %r13d
js 0x7c72
cmpl 0xc(%rsp), %r13d
jge 0x7c72
movl 0x8(%rsp), %edi
addl %edi, %ebp
movl %ebp, %eax
cltd
idivl %edi
testl %r15d, %r15d
movl %edx, %ebp
cmovlel %edx, %ecx
cmovlel %r13d, %esi
movslq %esi, %rax
movq 0x28(%rsp), %rdx
movq (%rdx,%rax,8), %rax
movslq %ecx, %rcx
shlq $0x4, %rcx
testb $0x1, 0x5(%rax,%rcx)
je 0x7c72
movl %r13d, %edi
callq 0x6542
cmpl %eax, %ebp
jge 0x7c72
movq %r15, %rbx
movslq %r13d, %rax
movq 0x28(%rsp), %rcx
movq (%rcx,%rax,8), %r14
movslq %ebp, %r15
shlq $0x4, %r15
movl (%r14,%r15), %r12d
testl %r12d, %r12d
je 0x7c00
movq 0x30(%rsp), %rdi
movl %r12d, %esi
callq 0x5680
testq %rax, %rax
setne %al
jmp 0x7c02
xorl %eax, %eax
addq %r15, %r14
movzbl %al, %eax
testb $0x4, 0x5(%r14)
movq %rbx, %r15
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %rcx
jne 0x7c30
cmpl %eax, 0x4(%rsp)
jne 0x7c72
testl %eax, %eax
je 0x7c30
movq 0x20(%rsp), %rsi
cmpl (%rsi), %r12d
jne 0x7c72
movl %ebp, (%rcx)
movl %r13d, (%rdx)
movl %eax, 0x4(%rsp)
movq %r14, 0x20(%rsp)
jmp 0x7b54
movl (%rsi), %eax
testl %eax, %eax
jle 0x7c72
movq 0xdb08(%rip), %rcx      # 0x15758
movslq 0xdafd(%rip), %rdx      # 0x15754
shlq $0x4, %rdx
movl %eax, %edi
movq -0x8(%rcx,%rdi,8), %rdi
testb $0x1, -0xb(%rdi,%rdx)
je 0x7c72
addl %r15d, %eax
movl %eax, (%rsi)
testl %eax, %eax
jg 0x7c5b
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq