// File: /workspace/github_c_makefile_star2_O2/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 $0xa8, %rsp
movq %rcx, 0x10(%rsp)
andl $0x0, 0x20(%rsp)
andq $0x0, 0x18(%rsp)
movq $0x8, 0x30(%rsp)
andq $0x0, (%r8)
movq %rdi, 0x80(%rsp)
movq %rdx, 0x8(%rsp)
movl (%rdx), %r13d
movq %rsi, 0x38(%rsp)
movslq (%rsi), %r12
cmpl %r12d, %r13d
jge 0x5d13
movq 0x10(%rsp), %rax
andq $0x0, (%rax)
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r9d, %ebp
movq %r8, %r15
cmpl %r12d, %r13d
jne 0x5d30
movq 0x8(%rsp), %rax
movl 0x4(%rax), %eax
movq 0x38(%rsp), %rcx
cmpl 0x4(%rcx), %eax
jle 0x5cf8
pushq $0x8
popq %rdi
movq %r12, %rbx
callq 0x34f0
movq 0x10(%rsp), %rcx
movq %rax, (%rcx)
movl %ebp, 0x48(%rsp)
movslq %r13d, %rax
cmpq %rax, %rbx
jg 0x5d01
movq 0x80(%rsp), %rdi
movq (%rdi), %r12
movq %rbx, %rax
shlq $0x4, %rax
addq 0x30(%r12), %rax
movq %rax, 0x68(%rsp)
movl %ebx, %esi
callq 0x3440
movl %eax, %edx
xorl %eax, %eax
movq 0x38(%rsp), %rcx
movq %rbx, 0x28(%rsp)
cmpl %ebx, (%rcx)
jne 0x5d95
movq 0x38(%rsp), %rax
movl 0x4(%rax), %eax
cmpl %edx, %eax
jle 0x5d95
movl 0xc(%r12), %eax
cmpl 0x28(%rsp), %r13d
jne 0x5da8
movq 0x8(%rsp), %rcx
movl 0x4(%rcx), %ecx
cmpl %edx, %ecx
jle 0x5dad
movl 0xc(%r12), %ecx
cmpl %edx, %ecx
movl %edx, 0x44(%rsp)
cmovgel %edx, %ecx
cltq
movslq %ecx, %rcx
movq %rcx, 0x90(%rsp)
movq %rax, %rcx
xorl %eax, %eax
leaq 0x30(%rsp), %rbx
movq 0x10(%rsp), %r14
movq 0x68(%rsp), %r12
cmpq 0x90(%rsp), %rcx
jge 0x6056
movl %eax, 0x4c(%rsp)
movq (%r12), %rax
movq %rcx, 0xa0(%rsp)
shlq $0x5, %rcx
leaq (%rax,%rcx), %rdi
movq 0x8(%rax,%rcx), %rax
movq %rax, 0x98(%rsp)
testl %ebp, %ebp
je 0x6002
leaq 0x10(%rdi), %r13
movq %rdi, 0x70(%rsp)
leaq 0x18(%rsp), %rdi
movq %r13, %rsi
pushq $0xc
popq %rdx
callq 0x3520
movq 0x70(%rsp), %rdi
testl %eax, %eax
je 0x6002
andq $0x0, 0x60(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movl (%r13), %eax
cmpl %eax, 0x18(%rsp)
je 0x5e72
movl %eax, %ecx
shrl $0x18, %ecx
cmpl $0x1, %ecx
je 0x5e68
testl %ecx, %ecx
jne 0x5e72
movl $0x27, 0x50(%rsp)
jmp 0x5e72
movzbl %al, %eax
addl $0x1e, %eax
movl %eax, 0x50(%rsp)
movl 0x14(%rdi), %eax
cmpl %eax, 0x1c(%rsp)
je 0x5e94
movl %eax, %edx
shrl $0x18, %edx
pushq $0x31
popq %rcx
je 0x5e90
cmpl $0x1, %edx
jne 0x5e94
movzbl %al, %ecx
addl $0x28, %ecx
movl %ecx, 0x54(%rsp)
movq %r13, 0x88(%rsp)
movb 0x20(%rsp), %cl
movl %ecx, %esi
andb $0x1, %sil
movb 0x18(%rdi), %al
movl %eax, %edx
andb $0x1, %dl
cmpb %dl, %sil
je 0x5ec2
testb %dl, %dl
pushq $0x1
popq %rdx
pushq $0x15
popq %rsi
cmovel %esi, %edx
movl %edx, 0x58(%rsp)
movl %ecx, %esi
shrb %sil
andb $0x1, %sil
movl %eax, %edx
shrb %dl
andb $0x1, %dl
cmpb %dl, %sil
je 0x5ee6
testb %dl, %dl
pushq $0x3
popq %rdx
pushq $0x17
popq %rsi
cmovel %esi, %edx
movl %edx, 0x5c(%rsp)
movl %ecx, %esi
shrb $0x2, %sil
andb $0x1, %sil
movl %eax, %edx
shrb $0x2, %dl
andb $0x1, %dl
cmpb %dl, %sil
je 0x5f0c
testb %dl, %dl
pushq $0x4
popq %rdx
pushq $0x18
popq %rsi
cmovel %esi, %edx
movl %edx, 0x60(%rsp)
shrb $0x3, %cl
andb $0x1, %cl
shrb $0x3, %al
andb $0x1, %al
cmpb %al, %cl
je 0x5f2a
testb %al, %al
pushq $0x7
popq %rax
pushq $0x1b
popq %rcx
cmovel %ecx, %eax
movl %eax, 0x64(%rsp)
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
leaq 0x3a3b(%rip), %rcx      # 0x9975
pushq $0x2
popq %r8
callq 0x732c
pushq $0x1
popq %r12
xorl %ebp, %ebp
cmpq $0x6, %rbp
je 0x5fba
movl 0x50(%rsp,%rbp,4), %ebx
testl %ebx, %ebx
je 0x5fb5
testl %r12d, %r12d
leaq 0x30(%rsp), %rdx
movq 0x10(%rsp), %r13
jne 0x5f7c
movq %r13, %rdi
movq %r15, %rsi
leaq 0x3546(%rip), %rcx      # 0x94b9
pushq $0x1
popq %r8
callq 0x732c
xorl %r12d, %r12d
leaq 0x7d(%rsp), %r14
movq %r14, %rdi
leaq 0x39ea(%rip), %rsi      # 0x9978
movl %ebx, %edx
xorl %eax, %eax
callq 0x3600
movq %r14, %rdi
callq 0x31e0
movq %r13, %rdi
movq %r15, %rsi
leaq 0x30(%rsp), %rdx
movq %r14, %rcx
movq %rax, %r8
callq 0x732c
incq %rbp
jmp 0x5f49
movq 0x10(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
leaq 0x30(%rsp), %rbx
movq %rbx, %rdx
leaq 0x39a7(%rip), %rcx      # 0x997b
pushq $0x1
popq %r8
callq 0x732c
movq 0x88(%rsp), %rcx
movl 0x8(%rcx), %eax
movl %eax, 0x20(%rsp)
movq (%rcx), %rax
movq %rax, 0x18(%rsp)
movl 0x48(%rsp), %ebp
movq 0x68(%rsp), %r12
movq 0x70(%rsp), %rdi
cmpl $0x0, 0x4c(%rsp)
jne 0x603f
cmpq $0x0, 0x8(%rdi)
movq %rdi, %rcx
leaq 0x3965(%rip), %rax      # 0x997d
cmoveq %rax, %rcx
pushq $0x1
popq %rax
movq 0x98(%rsp), %r8
cmoveq %rax, %r8
movq %rdi, %r13
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x732c
movq %r13, %rdi
movzbl 0x1c(%rdi), %eax
andl $0x1, %eax
movq 0xa0(%rsp), %rcx
incq %rcx
jmp 0x5dd7
movq 0x8(%rsp), %rax
movl (%rax), %eax
cmpl 0x28(%rsp), %eax
jne 0x6079
movq 0x28(%rsp), %rcx
movl %ecx, %r13d
movq 0x8(%rsp), %rcx
movl 0x44(%rsp), %edx
cmpl %edx, 0x4(%rcx)
jle 0x60a5
testb $0x1, 0x8(%r12)
movl %eax, %r13d
jne 0x60a5
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
leaq 0x37db(%rip), %rcx      # 0x986f
pushq $0x1
popq %r8
callq 0x732c
movq 0x8(%rsp), %rax
movl (%rax), %r13d
movq 0x28(%rsp), %rbx
incq %rbx
jmp 0x5d47