// File: /workspace/github_c_makefile_star2_O2/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 %r14
pushq %rbx
pushq %rax
movq %rsi, %r14
movq 0x38(%rsi), %rcx
movq %rcx, (%rsp)
movq %rcx, %rax
shrq $0x20, %rax
leal -0x7(%rdi), %edx
pushq $0x1
popq %rbx
cmpl $0x8, %edx
ja 0x6b7b
leaq 0x3b71(%rip), %rcx      # 0xa6c8
movslq (%rcx,%rdx,4), %rdx
addq %rcx, %rdx
jmpq *%rdx
movq %r14, %rdi
callq 0x8c3b
testb $0x10, 0xac(%r14)
jne 0x6c1e
jmp 0x6cd3
leal -0x84(%rdi), %edx
cmpl $0xb, %edx
ja 0x6c7e
leaq 0x3b5b(%rip), %rsi      # 0xa6ec
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
movq %r14, %rdi
callq 0x8c3b
jmp 0x6cd3
movl $0x3, 0x130(%r14)
jmp 0x6cd3
movl $0x2, 0x130(%r14)
jmp 0x6cd3
movl %eax, %ecx
andb $0x7, %cl
pushq $0x1
popq %rdx
shll %cl, %edx
movq 0x58(%r14), %rcx
sarl $0x3, %eax
cltq
orb %dl, (%rcx,%rax)
jmp 0x6cd3
cmpl %ecx, 0x44(%r14)
jne 0x6ca1
movl %ecx, %eax
movl 0x48(%r14), %esi
testl %esi, %esi
jns 0x6bfa
movl 0x30(%r14), %esi
shlq $0x20, %rsi
orq %rax, %rsi
testb $0x10, 0xad(%r14)
jne 0x6cae
xorl %eax, %eax
jmp 0x6cba
movq %r14, %rdi
callq 0x8c3b
andl $0x0, 0x3c(%r14)
jmp 0x6cd3
movl $0x1, 0x128(%r14)
jmp 0x6cd3
testl %eax, %eax
jle 0x6cd3
decl %eax
movl %eax, 0x3c(%r14)
jmp 0x6cd3
pushq $0x1
popq %rdx
movq %r14, %rdi
movl %edx, %esi
callq 0x8f43
jmp 0x6cd3
andl $0x0, 0x128(%r14)
jmp 0x6cd3
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x6cd3
movq 0x40(%rax), %rax
testq %rax, %rax
je 0x6cd3
movq 0x10(%r14), %rdi
callq *%rax
jmp 0x6cd3
movq 0x20(%r14), %rax
testq %rax, %rax
je 0x6c9d
movq (%rax), %rax
testq %rax, %rax
je 0x6c9d
movq 0x28(%r14), %rsi
movzbl %dil, %edi
callq *%rax
testl %eax, %eax
jne 0x6ce0
xorl %ebx, %ebx
jmp 0x6ce0
testl %ecx, %ecx
jle 0x6cd3
leal -0x1(%rcx), %eax
movl %eax, 0x38(%r14)
jmp 0x6cd3
movl 0x4c(%r14), %eax
movl 0x50(%r14), %edx
testl %edx, %edx
jns 0x6cbe
movl 0x34(%r14), %edx
shlq $0x20, %rdx
orq %rax, %rdx
pushq $-0x1
popq %rcx
movq %r14, %rdi
xorl %r8d, %r8d
callq 0x8c9a
movq %rsp, %rsi
movq %r14, %rdi
movl %ebx, %edx
callq 0x8f00
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
retq