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

  switch (sel.snap) {
  case SNAP_WORD:
    /*
     * Snap around if the word wraps around at the end or
     * beginning of a line.
     */
    // >>>>>>>>>>>>>>>>>>>> scrollback
    // ==================== scrollback
    prevgp = &term.line[*y][*x];
    // ==================== scrollback
    // <<<<<<<<<<<<<<<<<<<< scrollback
    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;
        // >>>>>>>>>>>>>>>>>>>> scrollback
        // ==================== scrollback
        if (!(term.line[yt][xt].mode & ATTR_WRAP))
        // ==================== scrollback
        // <<<<<<<<<<<<<<<<<<<< scrollback
          break;
      }

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

      // >>>>>>>>>>>>>>>>>>>> scrollback
      // ==================== scrollback
      gp = &term.line[newy][newx];
      // ==================== scrollback
      // <<<<<<<<<<<<<<<<<<<< scrollback
      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) {
        // >>>>>>>>>>>>>>>>>>>> scrollback
        // ==================== scrollback
        if (!(term.line[*y - 1][term.col - 1].mode & ATTR_WRAP)) {
        // ==================== scrollback
        // <<<<<<<<<<<<<<<<<<<< scrollback
          break;
        }
      }
    } else if (direction > 0) {
      for (; *y < term.row - 1; *y += direction) {
        // >>>>>>>>>>>>>>>>>>>> scrollback
        // ==================== scrollback
        if (!(term.line[*y][term.col - 1].mode & ATTR_WRAP)) {
        // ==================== scrollback
        // <<<<<<<<<<<<<<<<<<<< scrollback
          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 0xd369(%rip), %eax      # 0x14788
cmpl $0x2, %eax
je 0x7482
cmpl $0x1, %eax
jne 0x7637
movl %edx, 0x8(%rsp)
movq 0xd380(%rip), %rax      # 0x147b8
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 0xd051(%rip), %rax      # 0x144b0
movq (%rax), %rdi
testl %esi, %esi
movq %rdi, 0x30(%rsp)
je 0x74f0
callq 0x5660
xorl %ecx, %ecx
testq %rax, %rax
setne %cl
movl %ecx, 0x4(%rsp)
jmp 0x74f8
movl 0xd32c(%rip), %eax      # 0x147b4
decl %eax
xorl %ecx, %ecx
testl %edx, %edx
cmovnsl %eax, %ecx
movq 0x10(%rsp), %rax
movl %ecx, (%rax)
js 0x7609
je 0x7637
movq 0xd30d(%rip), %rax      # 0x147b8
movslq (%rbp), %rcx
movl 0xd2fa(%rip), %r10d     # 0x147b0
decl %r10d
movslq 0xd2f4(%rip), %rsi      # 0x147b4
movslq %edx, %rdi
movl %edx, %r8d
shlq $0x4, %rsi
cmpl %r10d, %ecx
jge 0x7637
movq (%rax,%rcx,8), %r9
testb $0x1, -0xb(%r9,%rsi)
je 0x7637
leal (%r8,%rcx), %r9d
movl %r9d, (%rbp)
addq %rdi, %rcx
jmp 0x74ca
movl $0x0, 0x4(%rsp)
addq %rbx, %r14
movq %r14, 0x20(%rsp)
movl 0xd2ae(%rip), %eax      # 0x147b4
movl %eax, 0xc(%rsp)
movl 0xd2a0(%rip), %eax      # 0x147b0
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 0x757d
movl %r15d, %ecx
addl %esi, %ecx
js 0x7637
cmpl 0x1c(%rsp), %ecx
jge 0x7637
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 0x7637
movl %r15d, %edi
callq 0x6082
cmpl %eax, %r13d
jge 0x7637
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 0x75c2
movq 0x30(%rsp), %rdi
movl %r12d, %esi
callq 0x5660
testq %rax, %rax
setne %al
jmp 0x75c4
xorl %eax, %eax
addq %rbp, %r14
movzbl %al, %eax
testb $0x4, 0x5(%r14)
movq %rbx, %rbp
movl 0x8(%rsp), %esi
jne 0x75ec
cmpl %eax, 0x4(%rsp)
jne 0x7637
testl %eax, %eax
je 0x75ec
movq 0x20(%rsp), %rcx
cmpl (%rcx), %r12d
jne 0x7637
movq 0x10(%rsp), %rcx
movl %r13d, (%rcx)
movl %r15d, (%rbp)
movl (%rcx), %r12d
movl %eax, 0x4(%rsp)
movq %r14, 0x20(%rsp)
jmp 0x7518
movl (%rbp), %eax
movq 0xd1a5(%rip), %rcx      # 0x147b8
movslq 0xd19a(%rip), %rdi      # 0x147b4
shlq $0x4, %rdi
testl %eax, %eax
jle 0x7637
movl %eax, %esi
movq -0x8(%rcx,%rsi,8), %rsi
testb $0x1, -0xb(%rsi,%rdi)
je 0x7637
addl %edx, %eax
movl %eax, (%rbp)
jmp 0x761e
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq