// File: /workspace/github_c_makefile_star2_O1/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
movl %edx, %r15d
movl 0xdd98(%rip), %eax      # 0x15798
cmpl $0x2, %eax
je 0x7a5b
cmpl $0x1, %eax
jne 0x7c2d
movq 0xddb3(%rip), %rcx      # 0x157c8
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 0x7ad2
leaq 0xda7b(%rip), %rax      # 0x154c0
movq (%rax), %rdi
callq 0x5670
xorl %ecx, %ecx
testq %rax, %rax
setne %cl
movl %ecx, 0x4(%rsp)
jmp 0x7ada
movl 0xdd63(%rip), %eax      # 0x157c4
decl %eax
xorl %ecx, %ecx
testl %r15d, %r15d
cmovnsl %eax, %ecx
movl %ecx, (%rdi)
js 0x7bfe
je 0x7c2d
movslq (%rsi), %rax
movl 0xdd3e(%rip), %ecx      # 0x157c0
decl %ecx
cmpl %ecx, %eax
jge 0x7c2d
movq 0xdd35(%rip), %rcx      # 0x157c8
movl %r15d, %edx
movslq 0xdd27(%rip), %r10      # 0x157c4
movslq 0xdd1c(%rip), %rdi      # 0x157c0
decq %rdi
leal (%rax,%r15), %r8d
shlq $0x4, %r10
movq (%rcx,%rax,8), %r9
testb $0x1, -0xb(%r9,%r10)
je 0x7c2d
addq %rdx, %rax
movl %r8d, (%rsi)
addl %r15d, %r8d
cmpq %rdi, %rax
jl 0x7aaf
jmp 0x7c2d
movl $0x0, 0x4(%rsp)
addq %rbx, %r14
movq %r14, 0x20(%rsp)
leaq 0xd9d7(%rip), %rax      # 0x154c0
movq (%rax), %rax
movq %rax, 0x30(%rsp)
movl 0xdccd(%rip), %eax      # 0x157c4
movl %eax, 0x8(%rsp)
movl 0xdcbf(%rip), %eax      # 0x157c0
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 0x7b2b
movl %esi, %r13d
jmp 0x7b77
movl %esi, %r13d
addl %r15d, %r13d
js 0x7c2d
cmpl 0xc(%rsp), %r13d
jge 0x7c2d
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 0x7c2d
movl %r13d, %edi
callq 0x6502
cmpl %eax, %ebp
jge 0x7c2d
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 0x7bbb
movq 0x30(%rsp), %rdi
movl %r12d, %esi
callq 0x5670
testq %rax, %rax
setne %al
jmp 0x7bbd
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 0x7beb
cmpl %eax, 0x4(%rsp)
jne 0x7c2d
testl %eax, %eax
je 0x7beb
movq 0x20(%rsp), %rsi
cmpl (%rsi), %r12d
jne 0x7c2d
movl %ebp, (%rcx)
movl %r13d, (%rdx)
movl %eax, 0x4(%rsp)
movq %r14, 0x20(%rsp)
jmp 0x7b0f
movl (%rsi), %eax
testl %eax, %eax
jle 0x7c2d
movq 0xdbbd(%rip), %rcx      # 0x157c8
movslq 0xdbb2(%rip), %rdx      # 0x157c4
shlq $0x4, %rdx
movl %eax, %edi
movq -0x8(%rcx,%rdi,8), %rdi
testb $0x1, -0xb(%rdi,%rdx)
je 0x7c2d
addl %r15d, %eax
movl %eax, (%rsi)
testl %eax, %eax
jg 0x7c16
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq