// File: /workspace/github_c_makefile_star2_O2/doy[P]libvt100/src/screen.c
// Function: vt100_screen_set_window_size
void vt100_screen_set_window_size(VT100Screen *vt, int rows, int cols)
{
    struct vt100_loc old_size;
    int i;

    old_size.row = vt->grid->max.row;
    old_size.col = vt->grid->max.col;

    vt->grid->max.row = rows;
    vt->grid->max.col = cols;
    if (!vt->custom_scrollback_length)
        vt->scrollback_length = rows;

    if (vt->grid->max.row == 0) {
        vt->grid->max.row = 1;
    }
    if (vt->grid->max.col == 0) {
        vt->grid->max.col = 1;
    }

    if (vt->grid->max.row == old_size.row && vt->grid->max.col == old_size.col) {
        return;
    }

    if (vt->grid->cur.row >= vt->grid->max.row) {
        vt->grid->cur.row = vt->grid->max.row - 1;
    }
    if (vt->grid->cur.col > vt->grid->max.col) {
        vt->grid->cur.col = vt->grid->max.col;
    }

    vt100_screen_ensure_capacity(vt, vt->grid->max.row);

    for (i = 0; i < vt->grid->row_count; ++i) {
        vt->grid->rows[i].cells = realloc(
            vt->grid->rows[i].cells,
            vt->grid->max.col * sizeof(struct vt100_cell));
        if (old_size.col < vt->grid->max.col) {
            memset(
                &vt->grid->rows[i].cells[old_size.col], 0,
                (vt->grid->max.col - old_size.col) * sizeof(struct vt100_cell));
        }
    }

    for (i = vt->grid->row_count; i < vt->grid->max.row; ++i) {
        vt->grid->rows[i].cells = calloc(
            vt->grid->max.col, sizeof(struct vt100_cell));
    }

    if (vt->grid->row_count < vt->grid->max.row) {
        vt->grid->row_count = vt->grid->max.row;
        vt->grid->row_top = 0;
    }
    else {
        vt->grid->row_top = vt->grid->row_count - vt->grid->max.row;
    }

    vt->grid->scroll_top    = 0;
    vt->grid->scroll_bottom = vt->grid->max.row - 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %r13
movq (%rdi), %rax
movl 0x8(%rax), %ecx
movslq 0xc(%rax), %r14
movl %esi, 0x8(%rax)
movl %edx, 0xc(%rax)
testb $0x40, 0x4a(%rdi)
jne 0x5ad8
movl %esi, 0x38(%r13)
testl %esi, %esi
jne 0x5ae6
movl $0x1, 0x8(%rax)
pushq $0x1
popq %rsi
testl %edx, %edx
jne 0x5af4
pushq $0x1
popq %rdx
movl $0x1, 0xc(%rax)
xorl %esi, %ecx
movl %r14d, %edi
xorl %edx, %edi
orl %ecx, %edi
je 0x5bdd
cmpl %esi, (%rax)
jl 0x5b0c
leal -0x1(%rsi), %ecx
movl %ecx, (%rax)
cmpl %edx, 0x4(%rax)
jle 0x5b14
movl %edx, 0x4(%rax)
movq %r13, %rdi
callq 0x5bec
movq (%r13), %r15
xorl %r12d, %r12d
movq %r14, %rax
shlq $0x5, %rax
movq %rax, (%rsp)
xorl %ebp, %ebp
movslq 0x20(%r15), %rbx
cmpq %rbx, %rbp
jge 0x5b84
movq 0x30(%r15), %rax
movq (%rax,%r12), %rdi
movslq 0xc(%r15), %rsi
shlq $0x5, %rsi
callq 0x3580
movq (%r13), %r15
movq 0x30(%r15), %rcx
movq %rax, (%rcx,%r12)
movl 0xc(%r15), %ecx
cmpl %r14d, %ecx
jle 0x5b7b
addq (%rsp), %rax
subl %r14d, %ecx
movslq %ecx, %rdx
shlq $0x5, %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x32c0
incq %rbp
addq $0x10, %r12
jmp 0x5b30
movl 0x8(%r15), %eax
movl %eax, (%rsp)
movslq %eax, %r14
movq %rbx, %r12
shlq $0x4, %r12
pushq $0x20
popq %rbp
movq %rbx, %r13
cmpq %r14, %r13
jge 0x5bbd
movslq 0xc(%r15), %rdi
movq %rbp, %rsi
callq 0x33b0
movq 0x30(%r15), %rcx
movq %rax, (%rcx,%r12)
incq %r13
addq $0x10, %r12
jmp 0x5b9b
movl (%rsp), %eax
cmpl %eax, %ebx
jge 0x5bcc
movl %eax, 0x20(%r15)
xorl %ebx, %ebx
jmp 0x5bce
subl %eax, %ebx
movl %ebx, 0x28(%r15)
andl $0x0, 0x18(%r15)
decl %eax
movl %eax, 0x1c(%r15)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq