// File: /workspace/github_c_makefile_star2_O2/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
movq %rsi, %rbp
movq %rdi, 0x10(%rsp)
movl 0xd2f4(%rip), %eax      # 0x14728
cmpl $0x2, %eax
je 0x7497
cmpl $0x1, %eax
jne 0x764c
movl %edx, 0x8(%rsp)
movq 0xd30b(%rip), %rax      # 0x14758
movslq (%rbp), %r15
movq %rax, 0x28(%rsp)
movq (%rax,%r15,8), %r14
movq 0x10(%rsp), %rax
movslq (%rax), %r12
movq %r12, %rbx
shlq $0x4, %rbx
movl (%r14,%rbx), %esi
leaq 0xd04c(%rip), %rax      # 0x144c0
movq (%rax), %rdi
testl %esi, %esi
movq %rdi, 0x30(%rsp)
je 0x7505
callq 0x5670
xorl %ecx, %ecx
testq %rax, %rax
setne %cl
movl %ecx, 0x4(%rsp)
jmp 0x750d
movl 0xd2b7(%rip), %eax      # 0x14754
decl %eax
xorl %ecx, %ecx
testl %edx, %edx
cmovnsl %eax, %ecx
movq 0x10(%rsp), %rax
movl %ecx, (%rax)
js 0x761e
je 0x764c
movq 0xd298(%rip), %rax      # 0x14758
movslq (%rbp), %rcx
movl 0xd285(%rip), %r10d     # 0x14750
decl %r10d
movslq 0xd27f(%rip), %rsi      # 0x14754
movslq %edx, %rdi
movl %edx, %r8d
shlq $0x4, %rsi
cmpl %r10d, %ecx
jge 0x764c
movq (%rax,%rcx,8), %r9
testb $0x1, -0xb(%r9,%rsi)
je 0x764c
leal (%r8,%rcx), %r9d
movl %r9d, (%rbp)
addq %rdi, %rcx
jmp 0x74df
movl $0x0, 0x4(%rsp)
addq %rbx, %r14
movq %r14, 0x20(%rsp)
movl 0xd239(%rip), %eax      # 0x14754
movl %eax, 0xc(%rsp)
movl 0xd22b(%rip), %eax      # 0x14750
movl %eax, 0x1c(%rsp)
movl 0x8(%rsp), %esi
movl %r12d, %r13d
addl %esi, %r13d
setns %al
cmpl 0xc(%rsp), %r13d
setl %cl
testb %cl, %al
jne 0x7592
movl %r15d, %ecx
addl %esi, %ecx
js 0x764c
cmpl 0x1c(%rsp), %ecx
jge 0x764c
movl 0xc(%rsp), %edi
addl %edi, %r13d
movl %r13d, %eax
cltd
idivl %edi
testl %esi, %esi
movl %edx, %r13d
cmovlel %edx, %r12d
cmovlel %ecx, %r15d
movslq %r15d, %rax
movq 0x28(%rsp), %rdx
movq (%rdx,%rax,8), %rax
movslq %r12d, %rdx
shlq $0x4, %rdx
testb $0x1, 0x5(%rax,%rdx)
movl %ecx, %r15d
je 0x764c
movl %r15d, %edi
callq 0x6092
cmpl %eax, %r13d
jge 0x764c
movq %rbp, %rbx
movslq %r15d, %rax
movq 0x28(%rsp), %rcx
movq (%rcx,%rax,8), %r14
movslq %r13d, %rbp
shlq $0x4, %rbp
movl (%r14,%rbp), %r12d
testl %r12d, %r12d
je 0x75d7
movq 0x30(%rsp), %rdi
movl %r12d, %esi
callq 0x5670
testq %rax, %rax
setne %al
jmp 0x75d9
xorl %eax, %eax
addq %rbp, %r14
movzbl %al, %eax
testb $0x4, 0x5(%r14)
movq %rbx, %rbp
movl 0x8(%rsp), %esi
jne 0x7601
cmpl %eax, 0x4(%rsp)
jne 0x764c
testl %eax, %eax
je 0x7601
movq 0x20(%rsp), %rcx
cmpl (%rcx), %r12d
jne 0x764c
movq 0x10(%rsp), %rcx
movl %r13d, (%rcx)
movl %r15d, (%rbp)
movl (%rcx), %r12d
movl %eax, 0x4(%rsp)
movq %r14, 0x20(%rsp)
jmp 0x752d
movl (%rbp), %eax
movq 0xd130(%rip), %rcx      # 0x14758
movslq 0xd125(%rip), %rdi      # 0x14754
shlq $0x4, %rdi
testl %eax, %eax
jle 0x764c
movl %eax, %esi
movq -0x8(%rcx,%rsi,8), %rsi
testb $0x1, -0xb(%rsi,%rdi)
je 0x764c
addl %edx, %eax
movl %eax, (%rbp)
jmp 0x7633
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq