// File: /workspace/github_c_makefile_star2_O1/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 %rbx
subq $0x28, %rsp
movq %rdx, %rbx
movl %esi, %r14d
movl %edi, %ebp
movq 0x38(%rdx), %rax
movq %rax, 0x8(%rsp)
cmpl %esi, 0x34(%rdx)
je 0x9643
movq (%rbx), %rdi
leal 0x7(%r14), %eax
leal 0xe(%r14), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
sarl $0x3, %ecx
movslq %ecx, %rsi
callq 0xa39f
movq %rax, %r15
xorl %eax, %eax
testl %r14d, %r14d
jle 0x9602
cmpl $0x0, 0x34(%rbx)
jle 0x9602
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 (%r15,%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, (%r15,%rsi)
incl %eax
cmpl %r14d, %eax
jge 0x9602
cmpl 0x34(%rbx), %eax
jl 0x95b6
cmpl %r14d, %eax
jge 0x9633
movl %eax, %ecx
andl $0x7, %ecx
je 0x9622
movl $0x1, %edx
shll %cl, %edx
movl %eax, %ecx
shrl $0x3, %ecx
notb %dl
andb %dl, (%r15,%rcx)
jmp 0x962c
movl %eax, %ecx
shrl $0x3, %ecx
orb $0x1, (%r15,%rcx)
incl %eax
cmpl %eax, %r14d
jne 0x9607
movq (%rbx), %rdi
movq 0x58(%rbx), %rsi
callq 0xa40a
movq %r15, 0x58(%rbx)
movl %ebp, 0x30(%rbx)
movl %r14d, 0x34(%rbx)
movl 0x48(%rbx), %eax
testl %eax, %eax
sets %cl
cmpl %ebp, %eax
setle %al
orb %cl, %al
jne 0x965e
movl %ebp, 0x48(%rbx)
movl 0x50(%rbx), %eax
testl %eax, %eax
sets %cl
cmpl %r14d, %eax
setle %al
orb %cl, %al
jne 0x9674
movl %r14d, 0x50(%rbx)
movq 0x38(%rbx), %rax
movq %rax, 0x10(%rsp)
movq 0x60(%rbx), %rax
movq %rax, 0x18(%rsp)
movq 0x68(%rbx), %rax
movq %rax, 0x20(%rsp)
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x96cb
movq 0x48(%rax), %rax
testq %rax, %rax
je 0x96cb
movq 0x10(%rbx), %rcx
leaq 0x10(%rsp), %r15
movl %ebp, %edi
movl %r14d, %esi
movq %r15, %rdx
callq *%rax
movq (%r15), %rax
movq %rax, 0x38(%rbx)
movq 0x8(%r15), %rax
movq %rax, 0x60(%rbx)
movq 0x10(%r15), %rax
movq %rax, 0x68(%rbx)
movzwl 0xac(%rbx), %eax
shrl $0x6, %eax
andl $0x8, %eax
movq 0x60(%rbx,%rax), %rax
movq %rax, 0x70(%rbx)
cmpl $0x0, 0x40(%rbx)
je 0x96fe
movl 0x3c(%rbx), %eax
leal -0x1(%r14), %ecx
cmpl %ecx, %eax
jge 0x96fe
movl $0x0, 0x40(%rbx)
incl %eax
movl %eax, 0x3c(%rbx)
cmpl $0x0, 0x38(%rbx)
jns 0x970b
movl $0x0, 0x38(%rbx)
cmpl %ebp, 0x38(%rbx)
jl 0x9715
decl %ebp
movl %ebp, 0x38(%rbx)
cmpl $0x0, 0x3c(%rbx)
jns 0x9722
movl $0x0, 0x3c(%rbx)
cmpl %r14d, 0x3c(%rbx)
jl 0x972f
decl %r14d
movl %r14d, 0x3c(%rbx)
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
movl $0x1, %edx
callq 0x9af5
movl $0x1, %eax
addq $0x28, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq