// File: /workspace/github_c_makefile_star2_O3/doy[P]libvt100/src/screen.c
// Function: vt100_screen_scroll_up
void vt100_screen_scroll_up(VT100Screen *vt, int count)
{
    struct vt100_row *row;
    int i;

    if (vt100_screen_scroll_region_is_active(vt) || vt->alternate) {
        int bottom = vt->grid->scroll_bottom, top = vt->grid->scroll_top;

        if (bottom - top + 1 > count) {
            for (i = 0; i < count; ++i) {
                row = vt100_screen_row_at(vt, top + i);
                free(row->cells);
            }
            row = vt100_screen_row_at(vt, top);
            memmove(
                row, row + count,
                (bottom - top + 1 - count) * sizeof(struct vt100_row));
            for (i = 0; i < count; ++i) {
                row = vt100_screen_row_at(vt, bottom - i);
                row->cells = calloc(
                    vt->grid->max.col, sizeof(struct vt100_cell));
                row->wrapped = 0;
            }
        }
        else {
            for (i = 0; i < bottom - top + 1; ++i) {
                row = vt100_screen_row_at(vt, top + i);
                memset(
                    row->cells, 0,
                    vt->grid->max.col * sizeof(struct vt100_cell));
                row->wrapped = 0;
            }
        }
    }
    else {
        int scrollback = vt->scrollback_length;
        int slack = scrollback / 10;
        int max_row_buffer_size = scrollback + slack;
        int overflow = vt->grid->row_count + count - max_row_buffer_size;

        if (overflow > 0) {
            int shift = overflow + slack;

            vt100_screen_ensure_capacity(vt, max_row_buffer_size);
            for (i = 0; i < shift; ++i) {
                free(vt->grid->rows[i].cells);
            }
            memmove(
                &vt->grid->rows[0], &vt->grid->rows[shift],
                (max_row_buffer_size - shift) * sizeof(struct vt100_row));
            for (i = scrollback - count; i < scrollback; ++i) {
                vt->grid->rows[i].cells = calloc(
                    vt->grid->max.col, sizeof(struct vt100_cell));
            }
            vt->grid->row_count = scrollback;
            vt->grid->row_top = scrollback - vt->grid->max.row;
        }
        else {
            vt100_screen_ensure_capacity(vt, vt->grid->row_count + count);
            for (i = 0; i < count; ++i) {
                row = vt100_screen_row_at(vt, i + vt->grid->max.row);
                row->cells = calloc(
                    vt->grid->max.col, sizeof(struct vt100_cell));
            }
            vt->grid->row_count += count;
            vt->grid->row_top += count;
        }
    }

    vt->dirty = 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %esi, %r12d
movq %rdi, %rbx
movq (%rdi), %rax
movslq 0x18(%rax), %r15
movl 0x1c(%rax), %r14d
testq %r15, %r15
jne 0x6fb1
movl 0x8(%rax), %ecx
decl %ecx
cmpl %ecx, %r14d
jne 0x6fb1
cmpq $0x0, 0x8(%rbx)
je 0x70ae
movl %r14d, %ecx
subl %r15d, %ecx
movl %ecx, %edx
subl %r12d, %edx
jge 0x7003
testl %ecx, %ecx
js 0x709b
incl %r14d
movq (%rbx), %rax
movq 0x30(%rax), %r12
movl 0x28(%rax), %ecx
addl %r15d, %ecx
movslq %ecx, %r13
shlq $0x4, %r13
movq (%r12,%r13), %rdi
movslq 0xc(%rax), %rdx
shlq $0x5, %rdx
xorl %esi, %esi
callq 0x32c0
andb $-0x2, 0x8(%r12,%r13)
incl %r15d
cmpl %r15d, %r14d
jne 0x6fc9
jmp 0x709b
testl %r12d, %r12d
jle 0x703b
movl %edx, 0x8(%rsp)
movq %r15, %r13
movl %r12d, %ebp
movq (%rbx), %rax
movslq 0x28(%rax), %rcx
movq 0x30(%rax), %rax
addq %r13, %rcx
shlq $0x4, %rcx
movq (%rax,%rcx), %rdi
callq 0x3050
incq %r13
decl %ebp
jne 0x7012
movq (%rbx), %rax
movl 0x8(%rsp), %edx
movslq 0x28(%rax), %rdi
addq %r15, %rdi
shlq $0x4, %rdi
addq 0x30(%rax), %rdi
movslq %r12d, %rsi
shlq $0x4, %rsi
addq %rdi, %rsi
incl %edx
movslq %edx, %rdx
shlq $0x4, %rdx
callq 0x3590
testl %r12d, %r12d
jle 0x709b
movq (%rbx), %rax
movq 0x30(%rax), %r15
movl 0x28(%rax), %ecx
addl %r14d, %ecx
movslq %ecx, %r13
shlq $0x4, %r13
movslq 0xc(%rax), %rdi
movl $0x20, %esi
callq 0x33b0
movq %rax, (%r15,%r13)
andb $-0x2, 0x8(%r15,%r13)
decl %r14d
decl %r12d
jne 0x7067
orb $0x20, 0x4a(%rbx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x38(%rbx), %edx
movslq %edx, %r13
imulq $0x66666667, %r13, %r14 # imm = 0x66666667
movq %r14, %rcx
shrq $0x3f, %rcx
sarq $0x22, %r14
addl %ecx, %r14d
movq %rdx, 0x8(%rsp)
leal (%r14,%rdx), %esi
movl 0x20(%rax), %eax
addl %r12d, %eax
movl %eax, %ecx
subl %esi, %ecx
jle 0x7199
movl %ecx, 0x10(%rsp)
addl %ecx, %r14d
movq %rbx, %rdi
callq 0x5f0f
testl %r14d, %r14d
jle 0x7118
movl %r14d, %r15d
shlq $0x4, %r15
xorl %ebp, %ebp
movq (%rbx), %rax
movq 0x30(%rax), %rax
movq (%rax,%rbp), %rdi
callq 0x3050
addq $0x10, %rbp
cmpq %rbp, %r15
jne 0x70ff
movq (%rbx), %rax
movq 0x30(%rax), %rdi
movslq %r14d, %rsi
shlq $0x4, %rsi
addq %rdi, %rsi
movq 0x8(%rsp), %rax
subl 0x10(%rsp), %eax
movslq %eax, %rdx
shlq $0x4, %rdx
callq 0x3590
movq (%rbx), %rax
movq %rax, 0x10(%rsp)
testl %r12d, %r12d
jle 0x7187
movq 0x8(%rsp), %rax
subl %r12d, %eax
movq 0x10(%rsp), %rcx
movslq 0xc(%rcx), %r14
movslq %eax, %r15
movq %r15, %rbp
shlq $0x4, %rbp
addq 0x30(%rcx), %rbp
movl $0x20, %esi
movq %r14, %rdi
callq 0x33b0
movq %rax, (%rbp)
incq %r15
addq $0x10, %rbp
cmpq %r13, %r15
jl 0x716a
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
movl %eax, 0x20(%rcx)
subl 0x8(%rcx), %eax
jmp 0x720b
movq %rbx, %rdi
movl %eax, %esi
callq 0x5f0f
movq (%rbx), %rcx
testl %r12d, %r12d
jle 0x7201
movslq 0x8(%rcx), %rax
movslq 0x28(%rcx), %rbp
movslq 0xc(%rcx), %r14
movl %r12d, %r13d
shlq $0x4, %rax
movq %rbp, 0x10(%rsp)
shlq $0x4, %rbp
addq %rax, %rbp
movq %rcx, 0x8(%rsp)
addq 0x30(%rcx), %rbp
shlq $0x4, %r13
xorl %r15d, %r15d
movl $0x20, %esi
movq %r14, %rdi
callq 0x33b0
movq %rax, (%rbp,%r15)
addq $0x10, %r15
cmpq %r15, %r13
jne 0x71da
movq 0x8(%rsp), %rcx
movq 0x10(%rsp), %rax
jmp 0x7204
movl 0x28(%rcx), %eax
addl %r12d, 0x20(%rcx)
addl %r12d, %eax
movl %eax, 0x28(%rcx)
jmp 0x709b