// File: /workspace/github_c_makefile_star2_O0/doy[P]libvt100/src/screen.c
// Function: vt100_screen_get_string
static void vt100_screen_get_string(
    VT100Screen *vt, struct vt100_loc *start, struct vt100_loc *end,
    char **strp, size_t *lenp, int formatted)
{
    int row, col;
    size_t capacity = 8;
    struct vt100_cell_attrs attrs;

    memset(&attrs, 0, sizeof(struct vt100_cell_attrs));

    *lenp = 0;

    if (end->row < start->row || (end->row == start->row && end->col <= start->col)) {
        *strp = NULL;
        return;
    }

    *strp = malloc(capacity);

    for (row = start->row; row <= end->row; ++row) {
        int start_col, end_col, max_col, was_wide = 0;
        struct vt100_row *grid_row = &vt->grid->rows[row];

        max_col = vt100_screen_row_max_col(vt, row);

        if (row == start->row) {
            if (start->col > max_col) {
                start_col = vt->grid->max.col;
            }
            else {
                start_col = start->col;
            }
        }
        else {
            start_col = 0;
        }

        if (row == end->row) {
            if (end->col > max_col) {
                end_col = vt->grid->max.col;
            }
            else {
                end_col = end->col;
            }
        }
        else {
            end_col = vt->grid->max.col;
        }

        if (end_col > max_col) {
            end_col = max_col;
        }

        for (col = start_col; col < end_col; ++col) {
            struct vt100_cell *cell = &grid_row->cells[col];
            char *contents = cell->contents;
            size_t len = cell->len;

            if (formatted
                && memcmp(&attrs, &cell->attrs,
                          sizeof(struct vt100_cell_attrs))) {
                int attr_codes[6] = { 0 };
                int first = 1;
                size_t i;

                if (attrs.fgcolor.id != cell->attrs.fgcolor.id) {
                    switch (cell->attrs.fgcolor.type) {
                    case VT100_COLOR_DEFAULT:
                        attr_codes[0] = 39;
                        break;
                    case VT100_COLOR_IDX:
                        attr_codes[0] = 30 + cell->attrs.fgcolor.idx;
                        break;
                    case VT100_COLOR_RGB:
                        // XXX
                        break;
                    }
                }
                if (attrs.bgcolor.id != cell->attrs.bgcolor.id) {
                    switch (cell->attrs.bgcolor.type) {
                    case VT100_COLOR_DEFAULT:
                        attr_codes[1] = 49;
                        break;
                    case VT100_COLOR_IDX:
                        attr_codes[1] = 40 + cell->attrs.bgcolor.idx;
                        break;
                    case VT100_COLOR_RGB:
                        // XXX
                        break;
                    }
                }
                if (attrs.bold != cell->attrs.bold) {
                    attr_codes[2] = cell->attrs.bold ? 1 : 21;
                }
                if (attrs.italic != cell->attrs.italic) {
                    attr_codes[3] = cell->attrs.italic ? 3 : 23;
                }
                if (attrs.underline != cell->attrs.underline) {
                    attr_codes[4] = cell->attrs.underline ? 4 : 24;
                }
                if (attrs.inverse != cell->attrs.inverse) {
                    attr_codes[5] = cell->attrs.inverse ? 7 : 27;
                }
                vt100_screen_push_string(strp, lenp, &capacity, "\033[", 2);
                for (i = 0; i < sizeof(attr_codes) / sizeof(int); ++i) {
                    char buf[3];

                    if (!attr_codes[i]) {
                        continue;
                    }

                    if (!first) {
                        vt100_screen_push_string(strp, lenp, &capacity, ";", 1);
                    }
                    sprintf(buf, "%d", attr_codes[i]);
                    vt100_screen_push_string(strp, lenp, &capacity, buf,
                                             strlen(buf));

                    first = 0;
                }
                vt100_screen_push_string(strp, lenp, &capacity, "m", 1);
                memcpy(&attrs, &cell->attrs, sizeof(struct vt100_cell_attrs));
            }

            if (!was_wide) {
                if (cell->len == 0) {
                    contents = " ";
                    len = 1;
                }

                vt100_screen_push_string(strp, lenp, &capacity, contents, len);
            }

            was_wide = cell->is_wide;
        }

        if ((row != end->row || end->col > max_col) && !grid_row->wrapped) {
            vt100_screen_push_string(strp, lenp, &capacity, "\n", 1);
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
movq $0x8, -0x40(%rbp)
leaq -0x4c(%rbp), %rdi
xorl %esi, %esi
movl $0xc, %edx
callq 0x32f0
movq -0x28(%rbp), %rax
movq $0x0, (%rax)
movq -0x18(%rbp), %rax
movl (%rax), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jl 0x8012
movq -0x18(%rbp), %rax
movl (%rax), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jne 0x8022
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jg 0x8022
movq -0x20(%rbp), %rax
movq $0x0, (%rax)
jmp 0x852c
movq -0x40(%rbp), %rdi
callq 0x3530
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl (%rcx), %eax
jg 0x852c
movl $0x0, -0x5c(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x30(%rax), %rax
movslq -0x30(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
movl -0x30(%rbp), %esi
callq 0x3480
movl %eax, -0x58(%rbp)
movl -0x30(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jne 0x80af
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
cmpl -0x58(%rbp), %eax
jle 0x80a3
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, -0x50(%rbp)
jmp 0x80ad
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x50(%rbp)
jmp 0x80b6
movl $0x0, -0x50(%rbp)
movl -0x30(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl (%rcx), %eax
jne 0x80e8
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
cmpl -0x58(%rbp), %eax
jle 0x80dc
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, -0x54(%rbp)
jmp 0x80e6
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x54(%rbp)
jmp 0x80f5
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x58(%rbp), %eax
jle 0x8103
movl -0x58(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0x50(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x54(%rbp), %eax
jge 0x84d6
movq -0x68(%rbp), %rax
movq (%rax), %rax
movslq -0x34(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x70(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x80(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x847c
movq -0x70(%rbp), %rsi
addq $0x10, %rsi
leaq -0x4c(%rbp), %rdi
movl $0xc, %edx
callq 0x3380
cmpl $0x0, %eax
je 0x847c
leaq -0xa0(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x32f0
movl $0x1, -0xa4(%rbp)
movl -0x4c(%rbp), %eax
movq -0x70(%rbp), %rcx
cmpl 0x10(%rcx), %eax
je 0x81e2
movq -0x70(%rbp), %rax
movzbl 0x13(%rax), %eax
movl %eax, -0xb8(%rbp)
testl %eax, %eax
je 0x81bf
jmp 0x81a5
movl -0xb8(%rbp), %eax
subl $0x1, %eax
je 0x81cb
jmp 0x81b2
movl -0xb8(%rbp), %eax
subl $0x2, %eax
je 0x81de
jmp 0x81e0
movl $0x27, -0xa0(%rbp)
jmp 0x81e0
movq -0x70(%rbp), %rax
movzbl 0x10(%rax), %eax
addl $0x1e, %eax
movl %eax, -0xa0(%rbp)
jmp 0x81e0
jmp 0x81e0
jmp 0x81e2
movl -0x48(%rbp), %eax
movq -0x70(%rbp), %rcx
cmpl 0x14(%rcx), %eax
je 0x823f
movq -0x70(%rbp), %rax
movzbl 0x17(%rax), %eax
movl %eax, -0xbc(%rbp)
testl %eax, %eax
je 0x821c
jmp 0x8202
movl -0xbc(%rbp), %eax
subl $0x1, %eax
je 0x8228
jmp 0x820f
movl -0xbc(%rbp), %eax
subl $0x2, %eax
je 0x823b
jmp 0x823d
movl $0x31, -0x9c(%rbp)
jmp 0x823d
movq -0x70(%rbp), %rax
movzbl 0x14(%rax), %eax
addl $0x28, %eax
movl %eax, -0x9c(%rbp)
jmp 0x823d
jmp 0x823d
jmp 0x823f
movb -0x44(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movq -0x70(%rbp), %rcx
movb 0x18(%rcx), %cl
andb $0x1, %cl
movzbl %cl, %ecx
cmpl %ecx, %eax
je 0x827a
movq -0x70(%rbp), %rax
movb 0x18(%rax), %al
andb $0x1, %al
movzbl %al, %edx
movl $0x15, %eax
movl $0x1, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x98(%rbp)
movb -0x44(%rbp), %al
shrb %al
andb $0x1, %al
movzbl %al, %eax
movq -0x70(%rbp), %rcx
movb 0x18(%rcx), %cl
shrb %cl
andb $0x1, %cl
movzbl %cl, %ecx
cmpl %ecx, %eax
je 0x82bb
movq -0x70(%rbp), %rax
movb 0x18(%rax), %al
shrb %al
andb $0x1, %al
movzbl %al, %edx
movl $0x17, %eax
movl $0x3, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x94(%rbp)
movb -0x44(%rbp), %al
shrb $0x2, %al
andb $0x1, %al
movzbl %al, %eax
movq -0x70(%rbp), %rcx
movb 0x18(%rcx), %cl
shrb $0x2, %cl
andb $0x1, %cl
movzbl %cl, %ecx
cmpl %ecx, %eax
je 0x82ff
movq -0x70(%rbp), %rax
movb 0x18(%rax), %al
shrb $0x2, %al
andb $0x1, %al
movzbl %al, %edx
movl $0x18, %eax
movl $0x4, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x90(%rbp)
movb -0x44(%rbp), %al
shrb $0x3, %al
andb $0x1, %al
movzbl %al, %eax
movq -0x70(%rbp), %rcx
movb 0x18(%rcx), %cl
shrb $0x3, %cl
andb $0x1, %cl
movzbl %cl, %ecx
cmpl %ecx, %eax
je 0x8343
movq -0x70(%rbp), %rax
movb 0x18(%rax), %al
shrb $0x3, %al
andb $0x1, %al
movzbl %al, %edx
movl $0x1b, %eax
movl $0x7, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x8c(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
leaq -0x40(%rbp), %rdx
leaq 0x45ff(%rip), %rcx      # 0xc955
movl $0x2, %r8d
callq 0xa960
movq $0x0, -0xb0(%rbp)
cmpq $0x6, -0xb0(%rbp)
jae 0x844c
movq -0xb0(%rbp), %rax
cmpl $0x0, -0xa0(%rbp,%rax,4)
jne 0x8390
jmp 0x8435
cmpl $0x0, -0xa4(%rbp)
jne 0x83b7
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
leaq -0x40(%rbp), %rdx
leaq 0x40dd(%rip), %rcx      # 0xc489
movl $0x1, %r8d
callq 0xa960
leaq -0xb3(%rbp), %rdi
movq -0xb0(%rbp), %rax
movl -0xa0(%rbp,%rax,4), %edx
leaq 0x4585(%rip), %rsi      # 0xc958
movb $0x0, %al
callq 0x3650
movq -0x20(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0xd0(%rbp)
leaq -0xb3(%rbp), %rax
movq %rax, -0xc8(%rbp)
leaq -0xb3(%rbp), %rdi
callq 0x3210
movq -0xd8(%rbp), %rdi
movq -0xd0(%rbp), %rsi
movq -0xc8(%rbp), %rcx
movq %rax, %r8
leaq -0x40(%rbp), %rdx
callq 0xa960
movl $0x0, -0xa4(%rbp)
movq -0xb0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xb0(%rbp)
jmp 0x836c
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
leaq -0x40(%rbp), %rdx
leaq 0x44fc(%rip), %rcx      # 0xc95b
movl $0x1, %r8d
callq 0xa960
movq -0x70(%rbp), %rax
movq 0x10(%rax), %rcx
movq %rcx, -0x4c(%rbp)
movl 0x18(%rax), %eax
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x5c(%rbp)
jne 0x84b9
movq -0x70(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x84a0
leaq 0x44c9(%rip), %rax      # 0xc95d
movq %rax, -0x78(%rbp)
movq $0x1, -0x80(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x78(%rbp), %rcx
movq -0x80(%rbp), %r8
leaq -0x40(%rbp), %rdx
callq 0xa960
movq -0x70(%rbp), %rax
movb 0x1c(%rax), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x5c(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x8109
movl -0x30(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl (%rcx), %eax
jne 0x84ed
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
cmpl -0x58(%rbp), %eax
jle 0x851c
movq -0x68(%rbp), %rax
movb 0x8(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x851c
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
leaq -0x40(%rbp), %rdx
leaq 0x432e(%rip), %rcx      # 0xc83f
movl $0x1, %r8d
callq 0xa960
jmp 0x851e
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x803e
addq $0xe0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)