// File: /workspace/github_c_makefile_star2_O2/neovim[P]libvterm/src/screen.c
// Function: resize
static int resize(int new_rows, int new_cols, VTermStateFields *fields, void *user)
{
  VTermScreen *screen = user;

  int altscreen_active = (screen->buffers[BUFIDX_ALTSCREEN] && screen->buffer == screen->buffers[BUFIDX_ALTSCREEN]);

  int old_rows = screen->rows;
  int old_cols = screen->cols;

  if(new_cols > old_cols) {
    /* Ensure that ->sb_buffer is large enough for a new or and old row */
    if(screen->sb_buffer)
      vterm_allocator_free(screen->vt, screen->sb_buffer);

    screen->sb_buffer = vterm_allocator_malloc(screen->vt, sizeof(VTermScreenCell) * new_cols);
  }

  resize_buffer(screen, 0, new_rows, new_cols, !altscreen_active, fields);
  if(screen->buffers[BUFIDX_ALTSCREEN])
    resize_buffer(screen, 1, new_rows, new_cols, altscreen_active, fields);
  else if(new_rows != old_rows) {
    /* We don't need a full resize of the altscreen because it isn't enabled
     * but we should at least keep the lineinfo the right size */
    vterm_allocator_free(screen->vt, fields->lineinfos[BUFIDX_ALTSCREEN]);

    VTermLineInfo *new_lineinfo = vterm_allocator_malloc(screen->vt, sizeof(new_lineinfo[0]) * new_rows);
    for(int row = 0; row < new_rows; row++)
      new_lineinfo[row] = (VTermLineInfo){ 0 };

    fields->lineinfos[BUFIDX_ALTSCREEN] = new_lineinfo;
  }

  screen->buffer = altscreen_active ? screen->buffers[BUFIDX_ALTSCREEN] : screen->buffers[BUFIDX_PRIMARY];

  screen->rows = new_rows;
  screen->cols = new_cols;

  if(new_cols <= old_cols) {
    if(screen->sb_buffer)
      vterm_allocator_free(screen->vt, screen->sb_buffer);

    screen->sb_buffer = vterm_allocator_malloc(screen->vt, sizeof(VTermScreenCell) * new_cols);
  }

  /* TODO: Maaaaybe we can optimise this if there's no reflow happening */
  damagescreen(screen);

  if(screen->callbacks && screen->callbacks->resize)
    return (*screen->callbacks->resize)(new_rows, new_cols, screen->cbdata);

  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %r14
movq %rdx, %r15
movl %esi, %ebx
movl %edi, %ebp
movq 0x68(%rcx), %rax
testq %rax, %rax
je 0x4ec9
cmpq %rax, 0x70(%r14)
sete %r12b
jmp 0x4ecc
xorl %r12d, %r12d
movl 0x50(%r14), %eax
movl %eax, 0xc(%rsp)
movl 0x54(%r14), %r13d
movslq %ebx, %rax
movq %rax, 0x10(%rsp)
cmpl %ebx, %r13d
jge 0x4f08
movq 0x78(%r14), %rsi
testq %rsi, %rsi
je 0x4ef6
movq (%r14), %rdi
callq 0x9663
movq (%r14), %rdi
imulq $0x28, 0x10(%rsp), %rsi
callq 0x95f8
movq %rax, 0x78(%r14)
movl %r12d, %eax
xorb $0x1, %al
movzbl %al, %r8d
movq %r14, %rdi
xorl %esi, %esi
movl %ebp, %edx
movl %ebx, %ecx
movq %r15, %r9
callq 0x53e1
cmpq $0x0, 0x68(%r14)
movzbl %r12b, %r12d
je 0x4f44
pushq $0x1
popq %rsi
movq %r14, %rdi
movl %ebp, %edx
movl %ebx, %ecx
movl %r12d, %r8d
movq %r15, %r9
callq 0x53e1
jmp 0x4f83
cmpl %ebp, 0xc(%rsp)
je 0x4f83
movq (%r14), %rdi
movq 0x10(%r15), %rsi
callq 0x9663
movq (%r14), %rdi
movslq %ebp, %rsi
shlq $0x2, %rsi
callq 0x95f8
xorl %ecx, %ecx
testl %ebp, %ebp
movl $0x0, %edx
cmovgl %ebp, %edx
cmpq %rcx, %rdx
je 0x4f7f
movb $0x0, (%rax,%rcx,4)
incq %rcx
jmp 0x4f71
movq %rax, 0x10(%r15)
movq 0x60(%r14,%r12,8), %rax
movq %rax, 0x70(%r14)
movl %ebp, 0x50(%r14)
movl %ebx, 0x54(%r14)
cmpl %ebx, %r13d
jl 0x4fbc
movq 0x78(%r14), %rsi
testq %rsi, %rsi
je 0x4faa
movq (%r14), %rdi
callq 0x9663
movq (%r14), %rdi
imulq $0x28, 0x10(%rsp), %rsi
callq 0x95f8
movq %rax, 0x78(%r14)
movq %r14, %rdi
callq 0x53ce
movq 0x10(%r14), %rax
testq %rax, %rax
je 0x4fee
movq 0x28(%rax), %rax
testq %rax, %rax
je 0x4fee
movq 0x18(%r14), %rdx
movl %ebp, %edi
movl %ebx, %esi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *%rax
pushq $0x1
popq %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq