// File: /workspace/github_c_makefile_star2_O2/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, %r8d
movq %rdi, %rbx
movq (%rdi), %rcx
movslq 0x18(%rcx), %r14
movl 0x1c(%rcx), %r15d
testq %r14, %r14
jne 0x6b23
movl 0x8(%rcx), %eax
decl %eax
cmpl %eax, %r15d
jne 0x6b23
cmpq $0x0, 0x8(%rbx)
je 0x6c1c
movl %r15d, %r12d
subl %r14d, %r12d
movl %r12d, %eax
subl %r8d, %eax
jge 0x6b71
xorl %r15d, %r15d
cmpl %r12d, %r15d
jg 0x6d56
movq (%rbx), %rax
movq 0x30(%rax), %r13
leal (%r14,%r15), %ecx
addl 0x28(%rax), %ecx
movslq %ecx, %rbp
shlq $0x4, %rbp
movq (%r13,%rbp), %rdi
movslq 0xc(%rax), %rdx
shlq $0x5, %rdx
xorl %esi, %esi
callq 0x32c0
andb $-0x2, 0x8(%r13,%rbp)
incl %r15d
jmp 0x6b34
movl %eax, 0x10(%rsp)
xorl %r13d, %r13d
testl %r8d, %r8d
movl %r8d, 0xc(%rsp)
cmovgl %r8d, %r13d
movl %r14d, %ebp
movl %r13d, %r12d
subl $0x1, %r12d
jb 0x6baf
movq (%rbx), %rax
movq 0x30(%rax), %rcx
movl 0x28(%rax), %eax
addl %ebp, %eax
cltq
shlq $0x4, %rax
movq (%rcx,%rax), %rdi
callq 0x3050
incl %ebp
jmp 0x6b8a
movq (%rbx), %rax
movslq 0x28(%rax), %rdi
addq %r14, %rdi
shlq $0x4, %rdi
addq 0x30(%rax), %rdi
movslq 0xc(%rsp), %rsi
shlq $0x4, %rsi
addq %rdi, %rsi
movl 0x10(%rsp), %eax
incl %eax
movslq %eax, %rdx
shlq $0x4, %rdx
callq 0x35a0
pushq $0x20
popq %r14
subl $0x1, %r13d
jb 0x6d56
movq (%rbx), %rax
movq 0x30(%rax), %r12
movl 0x28(%rax), %ecx
addl %r15d, %ecx
movslq %ecx, %rbp
shlq $0x4, %rbp
movslq 0xc(%rax), %rdi
movq %r14, %rsi
callq 0x33b0
movq %rax, (%r12,%rbp)
andb $-0x2, 0x8(%r12,%rbp)
decl %r15d
jmp 0x6be3
movl 0x38(%rbx), %edi
pushq $0xa
popq %rsi
movl %edi, %eax
cltd
idivl %esi
movl %eax, %r15d
leal (%r15,%rdi), %esi
movl 0x20(%rcx), %eax
addl %r8d, %eax
movl %eax, %r13d
subl %esi, %r13d
movl %r8d, 0xc(%rsp)
jle 0x6cf4
movq %rdi, 0x10(%rsp)
movslq %edi, %r12
addl %r13d, %r15d
movq %rbx, %rdi
callq 0x5bec
xorl %r14d, %r14d
testl %r15d, %r15d
movl $0x0, %ebp
cmovgl %r15d, %ebp
shlq $0x4, %rbp
movq (%rbx), %rax
movq 0x30(%rax), %rdi
cmpq %r14, %rbp
je 0x6c86
movq (%rdi,%r14), %rdi
callq 0x3050
addq $0x10, %r14
jmp 0x6c6b
movslq %r15d, %rsi
shlq $0x4, %rsi
addq %rdi, %rsi
movq 0x10(%rsp), %r14
movl %r14d, %eax
subl %r13d, %eax
movslq %eax, %rdx
shlq $0x4, %rdx
callq 0x35a0
movl %r14d, %eax
subl 0xc(%rsp), %eax
movslq %eax, %r14
movq (%rbx), %r13
movq %r14, %rbp
shlq $0x4, %rbp
pushq $0x20
popq %r15
cmpq %r12, %r14
jge 0x6ce1
movslq 0xc(%r13), %rdi
movq %r15, %rsi
callq 0x33b0
movq 0x30(%r13), %rcx
movq %rax, (%rcx,%rbp)
incq %r14
addq $0x10, %rbp
jmp 0x6cbf
movq 0x10(%rsp), %rax
movl %eax, 0x20(%r13)
subl 0x8(%r13), %eax
movl %eax, 0x28(%r13)
jmp 0x6d56
movl %r8d, %ebp
movq %rbx, %rdi
movl %eax, %esi
callq 0x5bec
xorl %r15d, %r15d
testl %ebp, %ebp
movl $0x0, %r13d
cmovgl %ebp, %r13d
movq (%rbx), %r12
cmpl %r15d, %r13d
je 0x6d48
movl 0x8(%r12), %eax
movq 0x30(%r12), %rbp
addl 0x28(%r12), %eax
addl %r15d, %eax
movslq %eax, %r14
shlq $0x4, %r14
movslq 0xc(%r12), %rdi
pushq $0x20
popq %rsi
callq 0x33b0
movq %rax, (%rbp,%r14)
incl %r15d
jmp 0x6d13
movl 0xc(%rsp), %eax
addl %eax, 0x20(%r12)
addl %eax, 0x28(%r12)
orb $0x20, 0x4a(%rbx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq