// File: /workspace/github_c_makefile_star2_O1/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
testq %r15, %r15
jne 0x6f67
movl 0x8(%rax), %ecx
decl %ecx
cmpl %ecx, 0x1c(%rax)
jne 0x6f67
cmpq $0x0, 0x8(%rbx)
je 0x7068
movl 0x1c(%rax), %r14d
movl %r14d, %eax
subl %r15d, %eax
movl %eax, %ecx
subl %r12d, %ecx
jge 0x6fbd
testl %eax, %eax
js 0x7055
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 0x6f83
jmp 0x7055
movl %ecx, 0x8(%rsp)
testl %r12d, %r12d
jle 0x6fee
movq %r15, %r13
movl %r12d, %ebp
movq (%rbx), %rax
movq 0x30(%rax), %rcx
movslq 0x28(%rax), %rax
addq %r13, %rax
shlq $0x4, %rax
movq (%rcx,%rax), %rdi
callq 0x3050
incq %r13
decl %ebp
jne 0x6fcc
movq (%rbx), %rax
movslq 0x28(%rax), %rdi
addq %r15, %rdi
shlq $0x4, %rdi
addq 0x30(%rax), %rdi
movslq %r12d, %rsi
shlq $0x4, %rsi
addq %rdi, %rsi
movl 0x8(%rsp), %eax
incl %eax
movslq %eax, %rdx
shlq $0x4, %rdx
callq 0x3590
testl %r12d, %r12d
jle 0x7055
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 0x7021
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, %r15
imulq $0x66666667, %r15, %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 0x714e
movl %ecx, 0x14(%rsp)
addl %ecx, %r14d
movq %rbx, %rdi
callq 0x5f35
testl %r14d, %r14d
jle 0x70d3
movl %r14d, %ebp
shlq $0x4, %rbp
xorl %r13d, %r13d
movq (%rbx), %rax
movq 0x30(%rax), %rax
movq (%rax,%r13), %rdi
callq 0x3050
addq $0x10, %r13
cmpq %r13, %rbp
jne 0x70ba
movq (%rbx), %rax
movq 0x30(%rax), %rdi
movslq %r14d, %rsi
shlq $0x4, %rsi
addq %rdi, %rsi
movq 0x8(%rsp), %rax
subl 0x14(%rsp), %eax
movslq %eax, %rdx
shlq $0x4, %rdx
callq 0x3590
testl %r12d, %r12d
jle 0x7138
movq 0x8(%rsp), %rax
subl %r12d, %eax
movq (%rbx), %rcx
movslq 0xc(%rcx), %r14
movslq %eax, %r13
movq %r13, %rbp
shlq $0x4, %rbp
addq 0x30(%rcx), %rbp
movl $0x20, %esi
movq %r14, %rdi
callq 0x33b0
movq %rax, (%rbp)
incq %r13
addq $0x10, %rbp
cmpq %r15, %r13
jl 0x711b
movq (%rbx), %rax
movq 0x8(%rsp), %rcx
movl %ecx, 0x20(%rax)
subl 0x8(%rax), %ecx
movl %ecx, 0x28(%rax)
jmp 0x7055
movq %rbx, %rdi
movl %eax, %esi
callq 0x5f35
testl %r12d, %r12d
jle 0x719e
movq (%rbx), %rax
movslq 0x8(%rax), %rcx
movslq 0x28(%rax), %r15
movslq 0xc(%rax), %r14
movl %r12d, %r13d
shlq $0x4, %rcx
shlq $0x4, %r15
addq %rcx, %r15
addq 0x30(%rax), %r15
shlq $0x4, %r13
xorl %ebp, %ebp
movl $0x20, %esi
movq %r14, %rdi
callq 0x33b0
movq %rax, (%r15,%rbp)
addq $0x10, %rbp
cmpq %rbp, %r13
jne 0x7184
movq (%rbx), %rax
addl %r12d, 0x20(%rax)
addl %r12d, 0x28(%rax)
jmp 0x7055