// File: /workspace/github_c_makefile_star2_O3/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, %rbx
movq (%rdi), %rax
movl 0x8(%rax), %ecx
movslq 0xc(%rax), %r14
movl %esi, 0x8(%rax)
movl %edx, 0xc(%rax)
testb $0x40, 0x4a(%rdi)
jne 0x5df2
movl %esi, 0x38(%rbx)
testl %esi, %esi
jne 0x5e02
movl $0x1, 0x8(%rax)
movl $0x1, %esi
testl %edx, %edx
jne 0x5e12
movl $0x1, 0xc(%rax)
movl $0x1, %edx
xorl %esi, %ecx
movl %r14d, %edi
xorl %edx, %edi
orl %ecx, %edi
je 0x5f00
cmpl %esi, (%rax)
jl 0x5e2a
leal -0x1(%rsi), %ecx
movl %ecx, (%rax)
cmpl %edx, 0x4(%rax)
jle 0x5e32
movl %edx, 0x4(%rax)
movq %rbx, %rdi
callq 0x5f0f
movq (%rbx), %r15
movl 0x20(%r15), %r12d
testl %r12d, %r12d
jle 0x5ea4
xorl %r13d, %r13d
movq %r14, %rax
shlq $0x5, %rax
movq %rax, (%rsp)
xorl %ebp, %ebp
movq 0x30(%r15), %rax
movq (%rax,%r13), %rdi
movslq 0xc(%r15), %rsi
shlq $0x5, %rsi
callq 0x3570
movq (%rbx), %r15
movq 0x30(%r15), %rcx
movq %rax, (%rcx,%r13)
movl 0xc(%r15), %ecx
subl %r14d, %ecx
jle 0x5e94
addq (%rsp), %rax
movslq %ecx, %rdx
shlq $0x5, %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x32c0
incq %rbp
movslq 0x20(%r15), %r12
addq $0x10, %r13
cmpq %r12, %rbp
jl 0x5e56
movl 0x8(%r15), %ebp
movl %r12d, %eax
subl %ebp, %eax
jge 0x5eee
movl %eax, (%rsp)
movslq %ebp, %r13
movslq 0xc(%r15), %rbx
movslq %r12d, %r14
subq %r14, %r13
shlq $0x4, %r14
addq 0x30(%r15), %r14
movl $0x20, %esi
movq %rbx, %rdi
callq 0x33b0
movq %rax, (%r14)
addq $0x10, %r14
decq %r13
jne 0x5ec7
cmpl %ebp, %r12d
movl (%rsp), %eax
jge 0x5eee
movl %ebp, 0x20(%r15)
xorl %eax, %eax
movl %eax, 0x28(%r15)
movl $0x0, 0x18(%r15)
decl %ebp
movl %ebp, 0x1c(%r15)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq