// File: /workspace/github_c_makefile_star2_O2/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 0xf7c6(%rip), %eax      # 0x18428
cmpl $0x2, %eax
je 0x8cb3
cmpl $0x1, %eax
jne 0x8fb5
movq 0x10(%rsp), %rax
movl (%rax), %r12d
movl %r12d, %eax
leaq 0xf7ce(%rip), %rsi      # 0x18450
subl 0x13664(%rip), %eax     # 0x1c2ec
jge 0x8d6e
movl 0x13654(%rip), %ecx     # 0x1c2e8
addl %ecx, %eax
addl $0x7d1, %eax            # imm = 0x7D1
movl $0x7d0, %ecx            # imm = 0x7D0
cltd
idivl %ecx
movslq %edx, %rax
leaq (%rsi,%rax,8), %rax
addq $0x18, %rax
jmp 0x8d7b
movl 0xf79b(%rip), %eax      # 0x18454
decl %eax
xorl %ecx, %ecx
testl %r10d, %r10d
cmovnsl %eax, %ecx
movq 0x18(%rsp), %rax
movl %ecx, (%rax)
js 0x8f42
je 0x8fb5
movq 0x10(%rsp), %rax
movslq (%rax), %rcx
movq %rcx, %rsi
shlq $0x3, %rsi
addq 0xf76c(%rip), %rsi      # 0x18458
movslq %r10d, %rdi
shlq $0x3, %rdi
leaq 0xf756(%rip), %r8       # 0x18450
movl $0x7d0, %r9d            # imm = 0x7D0
movl 0xf74a(%rip), %eax      # 0x18450
decl %eax
cmpl %eax, %ecx
jge 0x8fb5
movslq 0x135d5(%rip), %rax     # 0x1c2ec
cmpl %eax, %ecx
jge 0x8d3c
movl 0x135c7(%rip), %edx     # 0x1c2e8
subl %eax, %edx
leal (%rcx,%rdx), %eax
addl $0x7d1, %eax            # imm = 0x7D1
cltd
idivl %r9d
movslq %edx, %rax
leaq (%r8,%rax,8), %rdx
addq $0x18, %rdx
jmp 0x8d46
shlq $0x3, %rax
movq %rsi, %rdx
subq %rax, %rdx
movq (%rdx), %rax
movslq 0xf704(%rip), %rdx      # 0x18454
shlq $0x4, %rdx
testb $0x1, -0xb(%rax,%rdx)
je 0x8fb5
addl %r10d, %ecx
movq 0x10(%rsp), %rax
movl %ecx, (%rax)
addq %rdi, %rsi
jmp 0x8d00
cltq
shlq $0x3, %rax
addq 0xf6dd(%rip), %rax      # 0x18458
movq (%rax), %r13
movq 0x18(%rsp), %rax
movslq (%rax), %rbx
movq %rbx, %r15
shlq $0x4, %r15
movl (%r13,%r15), %esi
leaq 0xe807(%rip), %rax      # 0x175a0
movq (%rax), %rdi
testl %esi, %esi
movl %r10d, 0xc(%rsp)
movq %rdi, 0x30(%rsp)
je 0x8dc0
callq 0x6780
movl 0xc(%rsp), %r10d
xorl %r14d, %r14d
testq %rax, %rax
setne %r14b
jmp 0x8dc3
xorl %r14d, %r14d
addq %r15, %r13
movq %r13, 0x28(%rsp)
movq 0xf686(%rip), %rax      # 0x18458
movq %rax, 0x20(%rsp)
movl %ebx, %ebp
addl %r10d, %ebp
setns %al
movl 0xf66f(%rip), %ecx      # 0x18454
cmpl %ecx, %ebp
setl %dl
testb %dl, %al
je 0x8df6
movl %r12d, %r13d
jmp 0x8e76
addl %ecx, %ebp
movl %ebp, %eax
cltd
idivl %ecx
movl %r12d, %r13d
addl %r10d, %r13d
js 0x8fb5
cmpl 0xf640(%rip), %r13d     # 0x18450
jge 0x8fb5
movl %edx, %ebp
testl %r10d, %r10d
cmovlel %edx, %ebx
cmovlel %r13d, %r12d
subl 0x134c3(%rip), %r12d    # 0x1c2ec
jge 0x8e55
movl 0x134b7(%rip), %eax     # 0x1c2e8
addl %r12d, %eax
addl $0x7d1, %eax            # imm = 0x7D1
cltd
movl $0x7d0, %ecx            # imm = 0x7D0
idivl %ecx
movslq %edx, %rax
leaq 0xf605(%rip), %rcx      # 0x18450
leaq (%rcx,%rax,8), %rax
addq $0x18, %rax
jmp 0x8e61
movslq %r12d, %rax
movq 0x20(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movq (%rax), %rax
movslq %ebx, %rcx
shlq $0x4, %rcx
testb $0x1, 0x5(%rax,%rcx)
je 0x8fb5
movl %r13d, %edi
callq 0x7303
movl 0xc(%rsp), %r10d
cmpl %eax, %ebp
jge 0x8fb5
movl %r13d, %eax
subl 0x13458(%rip), %eax     # 0x1c2ec
jge 0x8ebf
movl 0x1344c(%rip), %ecx     # 0x1c2e8
addl %ecx, %eax
addl $0x7d1, %eax            # imm = 0x7D1
cltd
movl $0x7d0, %ecx            # imm = 0x7D0
idivl %ecx
movslq %edx, %rax
leaq 0xf59b(%rip), %rcx      # 0x18450
leaq (%rcx,%rax,8), %rax
addq $0x18, %rax
jmp 0x8eca
cltq
movq 0x20(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movq (%rax), %r15
movslq %ebp, %rbx
shlq $0x4, %rbx
movl (%r15,%rbx), %r12d
testl %r12d, %r12d
je 0x8ef7
movq 0x30(%rsp), %rdi
movl %r12d, %esi
callq 0x6780
movl 0xc(%rsp), %r10d
testq %rax, %rax
setne %al
jmp 0x8ef9
xorl %eax, %eax
addq %rbx, %r15
movzbl %al, %eax
testb $0x4, 0x5(%r15)
jne 0x8f21
cmpl %eax, %r14d
jne 0x8fb5
testl %eax, %eax
je 0x8f21
movq 0x28(%rsp), %rcx
cmpl (%rcx), %r12d
jne 0x8fb5
movq 0x18(%rsp), %rdx
movl %ebp, (%rdx)
movq 0x10(%rsp), %rcx
movl %r13d, (%rcx)
movl (%rdx), %ebx
movl %r13d, %r12d
movl %eax, %r14d
movq %r15, 0x28(%rsp)
jmp 0x8dd7
movq 0x10(%rsp), %rax
movl (%rax), %ecx
movq 0xf508(%rip), %rsi      # 0x18458
leaq 0xf4f9(%rip), %rdi      # 0x18450
movl $0x7d0, %r8d            # imm = 0x7D0
testl %ecx, %ecx
jle 0x8fb5
movl 0x13385(%rip), %edx     # 0x1c2ec
cmpl %edx, %ecx
jle 0x8f78
notl %edx
addl %ecx, %edx
movslq %edx, %rax
leaq (%rsi,%rax,8), %rax
jmp 0x8f94
movl 0x1336a(%rip), %eax     # 0x1c2e8
addl %ecx, %eax
subl %edx, %eax
addl %r8d, %eax
cltd
idivl %r8d
movslq %edx, %rax
leaq (%rdi,%rax,8), %rax
addq $0x18, %rax
movq (%rax), %rax
movslq 0xf4b6(%rip), %rdx      # 0x18454
shlq $0x4, %rdx
testb $0x1, -0xb(%rax,%rdx)
je 0x8fb5
addl %r10d, %ecx
movq 0x10(%rsp), %rax
movl %ecx, (%rax)
jmp 0x8f5d
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq