// File: /workspace/github_c_makefile_star2_O1/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 0x5e1e
movl %esi, 0x38(%rbx)
testl %esi, %esi
jne 0x5e29
movl $0x1, 0x8(%rax)
testl %edx, %edx
jne 0x5e34
movl $0x1, 0xc(%rax)
movl 0x8(%rax), %esi
cmpl %ecx, %esi
jne 0x5e45
cmpl %r14d, 0xc(%rax)
je 0x5f26
cmpl %esi, (%rax)
jl 0x5e4e
leal -0x1(%rsi), %ecx
movl %ecx, (%rax)
movl 0xc(%rax), %ecx
cmpl %ecx, 0x4(%rax)
jle 0x5e59
movl %ecx, 0x4(%rax)
movq %rbx, %rdi
callq 0x5f35
movq (%rbx), %r15
movl 0x20(%r15), %r12d
testl %r12d, %r12d
jle 0x5ed0
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), %rcx
movq 0x30(%rcx), %rdx
movq %rax, (%rdx,%r13)
movl 0xc(%rcx), %ecx
cmpl %ecx, %r14d
jge 0x5ebd
addq (%rsp), %rax
subl %r14d, %ecx
movslq %ecx, %rdx
shlq $0x5, %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x32c0
incq %rbp
movq (%rbx), %r15
movslq 0x20(%r15), %r12
addq $0x10, %r13
cmpq %r12, %rbp
jl 0x5e7d
movl 0x8(%r15), %ebp
cmpl %ebp, %r12d
jge 0x5f08
movslq %ebp, %r14
movslq 0xc(%r15), %rbx
movslq %r12d, %r13
subq %r13, %r14
shlq $0x4, %r13
addq 0x30(%r15), %r13
movl $0x20, %esi
movq %rbx, %rdi
callq 0x33b0
movq %rax, (%r13)
addq $0x10, %r13
decq %r14
jne 0x5eee
subl %ebp, %r12d
jge 0x5f14
movl %ebp, 0x20(%r15)
xorl %r12d, %r12d
movl %r12d, 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