// File: /workspace/github_c_makefile_star2_O3/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 $0x48, %rsp
movl %edx, %r15d
movq %rsi, 0x10(%rsp)
movq %rdi, 0x20(%rsp)
movl 0xda92(%rip), %eax      # 0x15788
cmpl $0x2, %eax
je 0x7d5c
cmpl $0x1, %eax
jne 0x7f2b
movq 0xdaad(%rip), %rcx      # 0x157b8
movq 0x10(%rsp), %rax
movslq (%rax), %r12
movq %rcx, 0x38(%rsp)
movq (%rcx,%r12,8), %r14
movq 0x20(%rsp), %rax
movslq (%rax), %r13
movq %r13, %rbx
shlq $0x4, %rbx
movl (%r14,%rbx), %esi
leaq 0xd77a(%rip), %rax      # 0x154b0
movq (%rax), %rdi
testl %esi, %esi
movq %rdi, 0x40(%rsp)
je 0x7de2
callq 0x5660
xorl %ecx, %ecx
testq %rax, %rax
setne %cl
movl %ecx, 0xc(%rsp)
jmp 0x7dea
movl 0xda52(%rip), %eax      # 0x157b4
decl %eax
xorl %ecx, %ecx
testl %r15d, %r15d
cmovnsl %eax, %ecx
movq 0x20(%rsp), %rax
movl %ecx, (%rax)
js 0x7ef2
je 0x7f2b
movq 0x10(%rsp), %rax
movslq (%rax), %rax
movl 0xda23(%rip), %ecx      # 0x157b0
decl %ecx
cmpl %ecx, %eax
jge 0x7f2b
movq 0xda1a(%rip), %rcx      # 0x157b8
movl %r15d, %edx
movslq 0xda0c(%rip), %rsi      # 0x157b4
movslq 0xda01(%rip), %rdi      # 0x157b0
decq %rdi
leal (%rax,%r15), %r8d
shlq $0x4, %rsi
movq (%rcx,%rax,8), %r9
testb $0x1, -0xb(%r9,%rsi)
je 0x7f2b
addq %rdx, %rax
movq 0x10(%rsp), %r9
movl %r8d, (%r9)
addl %r15d, %r8d
cmpq %rdi, %rax
jl 0x7dba
jmp 0x7f2b
movl $0x0, 0xc(%rsp)
addq %rbx, %r14
movq %r14, 0x30(%rsp)
movl 0xd9bc(%rip), %eax      # 0x157b4
movl %eax, 0x1c(%rsp)
movl 0xd9ae(%rip), %eax      # 0x157b0
movl %eax, 0x2c(%rsp)
movl %r13d, %ebp
addl %r15d, %ebp
setns %al
cmpl 0x1c(%rsp), %ebp
setl %cl
testb %cl, %al
jne 0x7e69
movl %r12d, %ecx
addl %r15d, %ecx
js 0x7f2b
cmpl 0x2c(%rsp), %ecx
jge 0x7f2b
movl 0x1c(%rsp), %esi
addl %esi, %ebp
movl %ebp, %eax
cltd
idivl %esi
testl %r15d, %r15d
movl %edx, %ebp
cmovlel %edx, %r13d
cmovlel %ecx, %r12d
movslq %r12d, %rax
movq 0x38(%rsp), %rdx
movq (%rdx,%rax,8), %rax
movslq %r13d, %rdx
shlq $0x4, %rdx
testb $0x1, 0x5(%rax,%rdx)
movl %ecx, %r12d
je 0x7f2b
movl %r12d, %edi
callq 0x65ed
cmpl %eax, %ebp
jge 0x7f2b
movq %r15, %r14
movslq %r12d, %rax
movq 0x38(%rsp), %rcx
movq (%rcx,%rax,8), %rbx
movslq %ebp, %r15
shlq $0x4, %r15
movl (%rbx,%r15), %r13d
testl %r13d, %r13d
je 0x7ead
movq 0x40(%rsp), %rdi
movl %r13d, %esi
callq 0x5660
testq %rax, %rax
setne %al
jmp 0x7eaf
xorl %eax, %eax
addq %r15, %rbx
movzbl %al, %eax
testb $0x4, 0x5(%rbx)
movq %r14, %r15
jne 0x7ed2
cmpl %eax, 0xc(%rsp)
jne 0x7f2b
testl %eax, %eax
je 0x7ed2
movq 0x30(%rsp), %rcx
cmpl (%rcx), %r13d
jne 0x7f2b
movq 0x20(%rsp), %rdx
movl %ebp, (%rdx)
movq 0x10(%rsp), %rcx
movl %r12d, (%rcx)
movl (%rdx), %r13d
movl %eax, 0xc(%rsp)
movq %rbx, 0x30(%rsp)
jmp 0x7e06
movq 0x10(%rsp), %rax
movl (%rax), %eax
testl %eax, %eax
jle 0x7f2b
movq 0xd8b4(%rip), %rcx      # 0x157b8
movslq 0xd8a9(%rip), %rdx      # 0x157b4
shlq $0x4, %rdx
movl %eax, %esi
movq -0x8(%rcx,%rsi,8), %rsi
testb $0x1, -0xb(%rsi,%rdx)
je 0x7f2b
addl %r15d, %eax
movq 0x10(%rsp), %rsi
movl %eax, (%rsi)
testl %eax, %eax
jg 0x7f0f
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq