// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, 0x70(%rsp)
movq $0x8, 0x18(%rsp)
xorl %eax, %eax
movq %rax, 0x8(%rsp)
movl $0x0, 0x10(%rsp)
movq %rax, (%r8)
movl (%rdx), %eax
movq %rsi, 0x20(%rsp)
movslq (%rsi), %rbp
cmpl %ebp, %eax
jge 0x6050
movq $0x0, (%rcx)
jmp 0x6407
movl %r9d, %r13d
movq %r8, %r15
movq %rdx, %r12
cmpl %ebp, %eax
movq %rcx, (%rsp)
jne 0x6074
movl 0x4(%r12), %eax
movq 0x20(%rsp), %rcx
cmpl 0x4(%rcx), %eax
movq (%rsp), %rcx
jle 0x6044
movl $0x8, %edi
movq %rcx, %r14
callq 0x34f0
movq %rax, (%r14)
movq %r12, %r14
movl (%r12), %r12d
cmpl %r12d, %ebp
jg 0x6407
movl %r13d, 0x34(%rsp)
movq %r14, 0x68(%rsp)
movq 0x70(%rsp), %rdi
movq (%rdi), %rbx
movq %rbp, %rax
shlq $0x4, %rax
addq 0x30(%rbx), %rax
movq %rax, 0x58(%rsp)
movl %ebp, %esi
callq 0x3440
movl %eax, %edx
movl $0x0, %eax
movq 0x20(%rsp), %rcx
movq %rbp, 0x28(%rsp)
cmpl %ebp, (%rcx)
jne 0x60e1
movq 0x20(%rsp), %rax
movl 0x4(%rax), %eax
cmpl %edx, %eax
jle 0x60e1
movl 0xc(%rbx), %eax
cmpl 0x28(%rsp), %r12d
jne 0x60f0
movl 0x4(%r14), %ebp
cmpl %edx, %ebp
jle 0x60f3
movl 0xc(%rbx), %ebp
cmpl %edx, %ebp
movl %edx, 0x30(%rsp)
cmovgel %edx, %ebp
cmpl %ebp, %eax
movq 0x58(%rsp), %r14
jge 0x63b3
movslq %eax, %r12
xorl %edx, %edx
movl %ebp, 0x38(%rsp)
movq (%r14), %rax
movq %r12, %rcx
shlq $0x5, %rcx
leaq (%rax,%rcx), %rdi
movq 0x8(%rax,%rcx), %r8
testl %r13d, %r13d
je 0x6363
movq 0x8(%rsp), %rax
xorq 0x10(%rdi), %rax
movl 0x10(%rsp), %ecx
xorl 0x18(%rdi), %ecx
orq %rax, %rcx
je 0x6363
leaq 0x10(%rdi), %rcx
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
movl 0x8(%rsp), %eax
cmpl (%rcx), %eax
je 0x6185
movzbl 0x13(%rdi), %eax
cmpl $0x1, %eax
je 0x617b
testl %eax, %eax
jne 0x6185
movl $0x27, 0x40(%rsp)
jmp 0x6185
movzbl (%rcx), %eax
addl $0x1e, %eax
movl %eax, 0x40(%rsp)
movl 0xc(%rsp), %eax
cmpl 0x14(%rdi), %eax
je 0x61b0
movzbl 0x17(%rdi), %eax
cmpl $0x1, %eax
je 0x61a5
testl %eax, %eax
jne 0x61b0
movl $0x31, 0x44(%rsp)
jmp 0x61b0
movzbl 0x14(%rdi), %eax
addl $0x28, %eax
movl %eax, 0x44(%rsp)
movq %rcx, 0x78(%rsp)
movl %edx, 0x3c(%rsp)
movb 0x10(%rsp), %cl
movl %ecx, %esi
andb $0x1, %sil
movb 0x18(%rdi), %al
movl %eax, %edx
andb $0x1, %dl
cmpb %dl, %sil
je 0x61e3
testb %dl, %dl
movl $0x1, %edx
movl $0x15, %esi
cmovel %esi, %edx
movl %edx, 0x48(%rsp)
movl %ecx, %esi
shrb %sil
andb $0x1, %sil
movl %eax, %edx
shrb %dl
andb $0x1, %dl
cmpb %dl, %sil
je 0x620b
testb %dl, %dl
movl $0x3, %edx
movl $0x17, %esi
cmovel %esi, %edx
movl %edx, 0x4c(%rsp)
movl %ecx, %esi
shrb $0x2, %sil
andb $0x1, %sil
movl %eax, %edx
shrb $0x2, %dl
andb $0x1, %dl
cmpb %dl, %sil
je 0x6235
testb %dl, %dl
movl $0x4, %edx
movl $0x18, %esi
cmovel %esi, %edx
movl %edx, 0x50(%rsp)
movq %r8, 0x80(%rsp)
movq %rdi, 0x88(%rsp)
movq %r12, 0x90(%rsp)
shrb $0x3, %cl
andb $0x1, %cl
shrb $0x3, %al
andb $0x1, %al
cmpb %al, %cl
je 0x626f
testb %al, %al
movl $0x7, %eax
movl $0x1b, %ecx
cmovel %ecx, %eax
movl %eax, 0x54(%rsp)
movl $0x2, %r8d
movq (%rsp), %rdi
movq %r15, %rsi
leaq 0x18(%rsp), %rdx
leaq 0x36ed(%rip), %rcx      # 0x9975
callq 0x7788
movl $0x1, %eax
xorl %r13d, %r13d
movl 0x40(%rsp,%r13,4), %r12d
testl %r12d, %r12d
je 0x62fe
testl %eax, %eax
leaq 0x18(%rsp), %r14
movq (%rsp), %rbp
jne 0x62c7
movl $0x1, %r8d
movq %rbp, %rdi
movq %r15, %rsi
movq %r14, %rdx
leaq 0x31f7(%rip), %rcx      # 0x94b9
callq 0x7788
leaq 0x65(%rsp), %rbx
movq %rbx, %rdi
leaq 0x36a2(%rip), %rsi      # 0x9978
movl %r12d, %edx
xorl %eax, %eax
callq 0x35f0
movq %rbx, %rdi
callq 0x31e0
movq %rbp, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %rbx, %rcx
movq %rax, %r8
callq 0x7788
xorl %eax, %eax
incq %r13
cmpq $0x6, %r13
jne 0x6295
movl $0x1, %r8d
movq (%rsp), %rdi
movq %r15, %rsi
leaq 0x18(%rsp), %rdx
leaq 0x365b(%rip), %rcx      # 0x997b
callq 0x7788
movq 0x78(%rsp), %rcx
movl 0x8(%rcx), %eax
movl %eax, 0x10(%rsp)
movq (%rcx), %rax
movq %rax, 0x8(%rsp)
movl 0x34(%rsp), %r13d
movq 0x58(%rsp), %r14
movl 0x38(%rsp), %ebp
movq 0x90(%rsp), %r12
movl 0x3c(%rsp), %edx
movq 0x88(%rsp), %rdi
movq 0x80(%rsp), %r8
testl %edx, %edx
jne 0x63a0
cmpq $0x0, 0x8(%rdi)
movq %rdi, %rcx
leaq 0x3607(%rip), %rax      # 0x997d
cmoveq %rax, %rcx
movl $0x1, %eax
cmoveq %rax, %r8
movl %r13d, %ebx
movq %rdi, %r13
movq (%rsp), %rdi
movq %r15, %rsi
leaq 0x18(%rsp), %rdx
callq 0x7788
movq %r13, %rdi
movl %ebx, %r13d
movzbl 0x1c(%rdi), %edx
andl $0x1, %edx
incq %r12
cmpl %r12d, %ebp
jne 0x6112
movq 0x68(%rsp), %rbx
movq 0x28(%rsp), %rax
cmpl %eax, (%rbx)
jne 0x63ca
movl 0x30(%rsp), %ecx
cmpl %ecx, 0x4(%rbx)
jle 0x63f4
testb $0x1, 0x8(%r14)
jne 0x63f4
movl $0x1, %r8d
movq (%rsp), %rdi
movq %r15, %rsi
leaq 0x18(%rsp), %rdx
leaq 0x3485(%rip), %rcx      # 0x986f
callq 0x7788
movq 0x28(%rsp), %rax
leaq 0x1(%rax), %rbp
movq %rbx, %r14
movslq (%rbx), %r12
cmpq %r12, %rax
jl 0x609e
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq