// File: /workspace/github_c_makefile_star2_O3/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 %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %rbx
movl %esi, %r14d
movl %edi, %r15d
movq 0x38(%rdx), %rax
movq %rax, 0x8(%rsp)
cmpl %esi, 0x34(%rdx)
je 0x9f9a
movq (%rbx), %rdi
leal 0x7(%r14), %eax
leal 0xe(%r14), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
sarl $0x3, %ecx
movslq %ecx, %rsi
callq 0xacb9
movq %rax, %r12
xorl %eax, %eax
testl %r14d, %r14d
jle 0x9f59
cmpl $0x0, 0x34(%rbx)
jle 0x9f59
xorl %eax, %eax
movl %eax, %ecx
andb $0x7, %cl
movl $0x1, %edx
shll %cl, %edx
movq 0x58(%rbx), %rcx
movl %eax, %esi
shrl $0x3, %esi
movzbl (%rcx,%rsi), %ecx
movl %eax, %edi
andl $0x7, %edi
movb (%r12,%rsi), %r8b
movl %edx, %r9d
notb %r9b
andb %r8b, %r9b
orb %dl, %r8b
btl %edi, %ecx
movzbl %r8b, %ecx
movzbl %r9b, %edx
cmovbl %ecx, %edx
movb %dl, (%r12,%rsi)
incl %eax
cmpl %r14d, %eax
jge 0x9f59
cmpl 0x34(%rbx), %eax
jl 0x9f0d
cmpl %r14d, %eax
jge 0x9f8a
movl %eax, %ecx
andl $0x7, %ecx
je 0x9f79
movl $0x1, %edx
shll %cl, %edx
movl %eax, %ecx
shrl $0x3, %ecx
notb %dl
andb %dl, (%r12,%rcx)
jmp 0x9f83
movl %eax, %ecx
shrl $0x3, %ecx
orb $0x1, (%r12,%rcx)
incl %eax
cmpl %eax, %r14d
jne 0x9f5e
movq (%rbx), %rdi
movq 0x58(%rbx), %rsi
callq 0xad24
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 0x9fb8
movl %r15d, 0x48(%rbx)
movl 0x50(%rbx), %eax
testl %eax, %eax
sets %cl
cmpl %r14d, %eax
setle %al
orb %cl, %al
jne 0x9fce
movl %r14d, 0x50(%rbx)
movq 0x38(%rbx), %rax
movq %rax, 0x10(%rsp)
movups 0x60(%rbx), %xmm0
movups %xmm0, 0x18(%rsp)
movq 0x8(%rbx), %rcx
testq %rcx, %rcx
je 0xa019
movq 0x48(%rcx), %r8
testq %r8, %r8
je 0xa019
movq 0x10(%rbx), %rcx
leaq 0x10(%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 0xa050
cmpl %ecx, %edx
jle 0xa050
movl $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 0xa074
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 0xa08f
cmpl %r14d, %esi
cmovll %esi, %edx
movl %edx, 0x3c(%rbx)
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
movl $0x1, %edx
callq 0xa42b
movl $0x1, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq