// File: /workspace/github_c_makefile_star2_O2/neovim[P]libvterm/src/state.c
// Function: on_resize
static int on_resize(int rows, int cols, void *user)
{
  VTermState *state = user;
  VTermPos oldpos = state->pos;

  if(cols != state->cols) {
    unsigned char *newtabstops = vterm_allocator_malloc(state->vt, (cols + 7) / 8);

    /* TODO: This can all be done much more efficiently bytewise */
    int col;
    for(col = 0; col < state->cols && col < cols; col++) {
      unsigned char mask = 1 << (col & 7);
      if(state->tabstops[col >> 3] & mask)
        newtabstops[col >> 3] |= mask;
      else
        newtabstops[col >> 3] &= ~mask;
      }

    for( ; col < cols; col++) {
      unsigned char mask = 1 << (col & 7);
      if(col % 8 == 0)
        newtabstops[col >> 3] |= mask;
      else
        newtabstops[col >> 3] &= ~mask;
    }

    vterm_allocator_free(state->vt, state->tabstops);
    state->tabstops = newtabstops;
  }

  state->rows = rows;
  state->cols = cols;

  if(state->scrollregion_bottom > -1)
    UBOUND(state->scrollregion_bottom, state->rows);
  if(state->scrollregion_right > -1)
    UBOUND(state->scrollregion_right, state->cols);

  VTermStateFields fields = {
    .pos       = state->pos,
    .lineinfos = { [0] = state->lineinfos[0], [1] = state->lineinfos[1] },
  };

  if(state->callbacks && state->callbacks->resize) {
    (*state->callbacks->resize)(rows, cols, &fields, state->cbdata);
    state->pos = fields.pos;

    state->lineinfos[0] = fields.lineinfos[0];
    state->lineinfos[1] = fields.lineinfos[1];
  }
  else {
    if(rows != state->rows) {
      for(int bufidx = BUFIDX_PRIMARY; bufidx <= BUFIDX_ALTSCREEN; bufidx++) {
        VTermLineInfo *oldlineinfo = state->lineinfos[bufidx];
        if(!oldlineinfo)
          continue;

        VTermLineInfo *newlineinfo = vterm_allocator_malloc(state->vt, rows * sizeof(VTermLineInfo));

        int row;
        for(row = 0; row < state->rows && row < rows; row++) {
          newlineinfo[row] = oldlineinfo[row];
        }

        for( ; row < rows; row++) {
          newlineinfo[row] = (VTermLineInfo){
            .doublewidth = 0,
          };
        }

        vterm_allocator_free(state->vt, state->lineinfos[bufidx]);
        state->lineinfos[bufidx] = newlineinfo;
      }
    }
  }

  state->lineinfo = state->lineinfos[state->mode.alt_screen ? BUFIDX_ALTSCREEN : BUFIDX_PRIMARY];

  if(state->at_phantom && state->pos.col < cols-1) {
    state->at_phantom = 0;
    state->pos.col++;
  }

  if(state->pos.row < 0)
    state->pos.row = 0;
  if(state->pos.row >= rows)
    state->pos.row = rows - 1;
  if(state->pos.col < 0)
    state->pos.col = 0;
  if(state->pos.col >= cols)
    state->pos.col = cols - 1;

  updatecursor(state, &oldpos, 1);

  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x20, %rsp
movq %rdx, %rbx
movl %esi, %r14d
movl %edi, %r15d
movq 0x38(%rdx), %rax
movq %rax, (%rsp)
cmpl %esi, 0x34(%rdx)
je 0x8a7d
movq (%rbx), %rdi
leal 0x7(%r14), %eax
pushq $0x8
popq %rcx
cltd
idivl %ecx
movslq %eax, %rsi
callq 0x95f8
movq %rax, %r12
xorl %eax, %eax
pushq $0x1
popq %rdx
cmpl %r14d, %eax
jge 0x8a39
cmpl 0x34(%rbx), %eax
jge 0x8a39
movl %eax, %ecx
andb $0x7, %cl
movl %edx, %esi
shll %cl, %esi
movq 0x58(%rbx), %rcx
movl %eax, %edi
shrl $0x3, %edi
movzbl (%rcx,%rdi), %ecx
movl %eax, %r8d
andl $0x7, %r8d
movb (%r12,%rdi), %r9b
movl %esi, %r10d
notb %r10b
andb %r9b, %r10b
orb %sil, %r9b
btl %r8d, %ecx
movzbl %r9b, %ecx
movzbl %r10b, %esi
cmovbl %ecx, %esi
movb %sil, (%r12,%rdi)
incl %eax
jmp 0x89eb
cmpl %eax, %r14d
movl %eax, %esi
cmovgl %r14d, %esi
cmpl %eax, %esi
je 0x8a6d
movl %eax, %ecx
andl $0x7, %ecx
je 0x8a5f
movl %edx, %edi
shll %cl, %edi
movl %eax, %ecx
shrl $0x3, %ecx
notb %dil
andb %dil, (%r12,%rcx)
jmp 0x8a69
movl %eax, %ecx
shrl $0x3, %ecx
orb $0x1, (%r12,%rcx)
incl %eax
jmp 0x8a42
movq (%rbx), %rdi
movq 0x58(%rbx), %rsi
callq 0x9663
movq %r12, 0x58(%rbx)
movl %r15d, 0x30(%rbx)
movl %r14d, 0x34(%rbx)
movl 0x48(%rbx), %eax
testl %eax, %eax
sets %cl
cmpl %r15d, %eax
setle %al
orb %cl, %al
jne 0x8a9b
movl %r15d, 0x48(%rbx)
movl 0x50(%rbx), %eax
testl %eax, %eax
sets %cl
cmpl %r14d, %eax
setle %al
orb %cl, %al
jne 0x8ab1
movl %r14d, 0x50(%rbx)
movq 0x38(%rbx), %rax
movq %rax, 0x8(%rsp)
movups 0x60(%rbx), %xmm0
movups %xmm0, 0x10(%rsp)
movq 0x8(%rbx), %rcx
testq %rcx, %rcx
je 0x8afc
movq 0x48(%rcx), %r8
testq %r8, %r8
je 0x8afc
movq 0x10(%rbx), %rcx
leaq 0x8(%rsp), %r12
movl %r15d, %edi
movl %r14d, %esi
movq %r12, %rdx
callq *%r8
movq (%r12), %rax
movq %rax, 0x38(%rbx)
movups 0x8(%r12), %xmm0
movups %xmm0, 0x60(%rbx)
movq %rax, %rcx
shrq $0x20, %rcx
movzwl 0xac(%rbx), %edx
shrl $0x6, %edx
andl $0x8, %edx
movq 0x60(%rbx,%rdx), %rdx
movq %rdx, 0x70(%rbx)
leal -0x1(%r14), %edx
cmpl $0x0, 0x40(%rbx)
je 0x8b30
cmpl %ecx, %edx
jle 0x8b30
andl $0x0, 0x40(%rbx)
incl %ecx
movl %ecx, 0x3c(%rbx)
xorl %esi, %esi
testl %eax, %eax
cmovlel %esi, %eax
setns %dil
cmpl %r15d, %eax
setl %r8b
testb %r8b, %dil
jne 0x8b54
leal -0x1(%r15), %edi
cmpl %r15d, %eax
cmovll %eax, %edi
movl %edi, 0x38(%rbx)
testl %ecx, %ecx
cmovgl %ecx, %esi
setns %al
cmpl %r14d, %esi
setl %cl
testb %cl, %al
jne 0x8b6f
cmpl %r14d, %esi
cmovll %esi, %edx
movl %edx, 0x3c(%rbx)
movq %rsp, %rsi
pushq $0x1
popq %rbp
movq %rbx, %rdi
movl %ebp, %edx
callq 0x8f00
movl %ebp, %eax
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq