// File: /workspace/github_c_makefile_star2_O3/neovim[P]libvterm/src/state.c
// Function: on_control
static int on_control(unsigned char control, void *user)
{
  VTermState *state = user;

  VTermPos oldpos = state->pos;

  switch(control) {
  case 0x07: // BEL - ECMA-48 8.3.3
    if(state->callbacks && state->callbacks->bell)
      (*state->callbacks->bell)(state->cbdata);
    break;

  case 0x08: // BS - ECMA-48 8.3.5
    if(state->pos.col > 0)
      state->pos.col--;
    break;

  case 0x09: // HT - ECMA-48 8.3.60
    tab(state, 1, +1);
    break;

  case 0x0a: // LF - ECMA-48 8.3.74
  case 0x0b: // VT
  case 0x0c: // FF
    linefeed(state);
    if(state->mode.newline)
      state->pos.col = 0;
    break;

  case 0x0d: // CR - ECMA-48 8.3.15
    state->pos.col = 0;
    break;

  case 0x0e: // LS1 - ECMA-48 8.3.76
    state->gl_set = 1;
    break;

  case 0x0f: // LS0 - ECMA-48 8.3.75
    state->gl_set = 0;
    break;

  case 0x84: // IND - DEPRECATED but implemented for completeness
    linefeed(state);
    break;

  case 0x85: // NEL - ECMA-48 8.3.86
    linefeed(state);
    state->pos.col = 0;
    break;

  case 0x88: // HTS - ECMA-48 8.3.62
    set_col_tabstop(state, state->pos.col);
    break;

  case 0x8d: // RI - ECMA-48 8.3.104
    if(state->pos.row == state->scrollregion_top) {
      VTermRect rect = {
        .start_row = state->scrollregion_top,
        .end_row   = SCROLLREGION_BOTTOM(state),
        .start_col = SCROLLREGION_LEFT(state),
        .end_col   = SCROLLREGION_RIGHT(state),
      };

      scroll(state, rect, -1, 0);
    }
    else if(state->pos.row > 0)
        state->pos.row--;
    break;

  case 0x8e: // SS2 - ECMA-48 8.3.141
    state->gsingle_set = 2;
    break;

  case 0x8f: // SS3 - ECMA-48 8.3.142
    state->gsingle_set = 3;
    break;

  default:
    if(state->fallbacks && state->fallbacks->control)
      if((*state->fallbacks->control)(control, state->fbdata))
        return 1;

    return 0;
  }

  updatecursor(state, &oldpos, 1);

#ifdef DEBUG
  if(state->pos.row < 0 || state->pos.row >= state->rows ||
     state->pos.col < 0 || state->pos.col >= state->cols) {
    fprintf(stderr, "Position out of bounds after Ctrl %02x: (%d,%d)\n",
        control, state->pos.row, state->pos.col);
    abort();
  }
#endif

  return 1;
}

// Assembly:
pushq %rbp
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq 0x38(%rsi), %rcx
movq %rcx, (%rsp)
movq %rcx, %rax
shrq $0x20, %rax
leal -0x7(%rdi), %edx
cmpl $0x8, %edx
ja 0x7ecc
leaq 0x48bf(%rip), %rcx      # 0xc768
movslq (%rcx,%rdx,4), %rdx
addq %rcx, %rdx
jmpq *%rdx
movq %rbx, %rdi
callq 0xa15f
testb $0x10, 0xac(%rbx)
jne 0x7f6c
jmp 0x802b
leal -0x84(%rdi), %edx
cmpl $0xb, %edx
ja 0x7fd3
leaq 0x48aa(%rip), %rsi      # 0xc78c
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
movq %rbx, %rdi
callq 0xa15f
jmp 0x802b
movl $0x3, 0x130(%rbx)
jmp 0x802b
movl $0x2, 0x130(%rbx)
jmp 0x802b
movq 0x58(%rbx), %rdx
movl %eax, %ecx
andb $0x7, %cl
movl $0x1, %esi
shll %cl, %esi
sarl $0x3, %eax
cltq
orb %sil, (%rdx,%rax)
jmp 0x802b
cmpl %ecx, 0x44(%rbx)
jne 0x7ffb
movl %ecx, %eax
movl 0x48(%rbx), %esi
testl %esi, %esi
jns 0x7f49
movl 0x30(%rbx), %esi
shlq $0x20, %rsi
orq %rax, %rsi
testb $0x10, 0xad(%rbx)
jne 0x8007
xorl %eax, %eax
jmp 0x8011
movq %rbx, %rdi
callq 0xa15f
movl $0x0, 0x3c(%rbx)
jmp 0x802b
movl $0x1, 0x128(%rbx)
jmp 0x802b
testl %eax, %eax
jle 0x802b
decl %eax
movl %eax, 0x3c(%rbx)
jmp 0x802b
movq %rbx, %rdi
movl $0x1, %esi
movl $0x1, %edx
callq 0xa471
jmp 0x802b
movl $0x0, 0x128(%rbx)
jmp 0x802b
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x802b
movq 0x40(%rax), %rax
testq %rax, %rax
je 0x802b
movq 0x10(%rbx), %rdi
callq *%rax
jmp 0x802b
movq 0x20(%rbx), %rax
testq %rax, %rax
je 0x7ff7
movq (%rax), %rax
testq %rax, %rax
je 0x7ff7
movq 0x28(%rbx), %rsi
movzbl %dil, %edi
callq *%rax
movl $0x1, %ebp
testl %eax, %eax
jne 0x8040
xorl %ebp, %ebp
jmp 0x8040
testl %ecx, %ecx
jle 0x802b
leal -0x1(%rcx), %eax
movl %eax, 0x38(%rbx)
jmp 0x802b
movl 0x4c(%rbx), %eax
movl 0x50(%rbx), %edx
testl %edx, %edx
jns 0x8014
movl 0x34(%rbx), %edx
shlq $0x20, %rdx
orq %rax, %rdx
movq %rbx, %rdi
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
callq 0xa1c0
movq %rsp, %rsi
movl $0x1, %ebp
movq %rbx, %rdi
movl $0x1, %edx
callq 0xa42b
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %rbp
retq