// File: /workspace/github_c_makefile_star2_O3/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 %r8, %r15
movq %rcx, %r8
movq %rdi, 0x80(%rsp)
movq $0x8, 0x18(%rsp)
xorl %eax, %eax
movq %rax, 0x8(%rsp)
movl $0x0, 0x10(%rsp)
movq %rax, (%r15)
movl (%rdx), %ebp
movq %rsi, 0x30(%rsp)
movslq (%rsi), %rax
cmpl %eax, %ebp
jge 0x602b
movq $0x0, (%r8)
jmp 0x63f3
movl %r9d, %r13d
cmpl %eax, %ebp
jne 0x6045
movq %rax, %rsi
movl 0x4(%rdx), %eax
movq 0x30(%rsp), %rcx
cmpl 0x4(%rcx), %eax
movq %rsi, %rax
jle 0x601f
movl $0x8, %edi
movq %rdx, %r12
movq %r8, %r14
movq %rax, %rbx
callq 0x34f0
movq %r14, (%rsp)
movq %rax, (%r14)
movq %r12, %r14
movq %rbx, %r12
movl %r13d, 0x48(%rsp)
movq %r14, 0x78(%rsp)
movq 0x80(%rsp), %rdi
movq (%rdi), %rbx
movq %r12, %rax
shlq $0x4, %rax
addq 0x30(%rbx), %rax
movq %rax, 0x38(%rsp)
movl %r12d, %esi
callq 0x3440
movl %eax, %edx
movl $0x0, %eax
movq 0x30(%rsp), %rcx
movq %r12, 0x28(%rsp)
cmpl %r12d, (%rcx)
jne 0x60b7
movq 0x30(%rsp), %rax
movl 0x4(%rax), %eax
cmpl %edx, %eax
jle 0x60b7
movl 0xc(%rbx), %eax
cmpl 0x28(%rsp), %ebp
jne 0x60c5
movl 0x4(%r14), %ecx
cmpl %edx, %ecx
jle 0x60c8
movl 0xc(%rbx), %ecx
cmpl %edx, %ecx
cmovgel %edx, %ecx
movl %ecx, 0x24(%rsp)
cmpl %ecx, %eax
jge 0x639d
movl %edx, 0x44(%rsp)
movslq %eax, %rdx
xorl %esi, %esi
movq 0x38(%rsp), %r14
movl 0x24(%rsp), %ebp
movq (%r14), %rax
movq %rdx, %rcx
shlq $0x5, %rcx
leaq (%rax,%rcx), %rdi
movq 0x8(%rax,%rcx), %r8
testl %r13d, %r13d
je 0x633c
movq 0x8(%rsp), %rax
xorq 0x10(%rdi), %rax
movl 0x10(%rsp), %ecx
xorl 0x18(%rdi), %ecx
orq %rax, %rcx
je 0x633c
leaq 0x10(%rdi), %rax
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movq $0x0, 0x60(%rsp)
movq %rax, 0x88(%rsp)
movl (%rax), %eax
cmpl %eax, 0x8(%rsp)
je 0x6167
movl %eax, %ecx
shrl $0x18, %ecx
cmpl $0x1, %ecx
je 0x615d
testl %ecx, %ecx
jne 0x6167
movl $0x27, 0x50(%rsp)
jmp 0x6167
movzbl %al, %eax
addl $0x1e, %eax
movl %eax, 0x50(%rsp)
movl 0x14(%rdi), %eax
cmpl %eax, 0xc(%rsp)
je 0x618d
movl %eax, %ecx
shrl $0x18, %ecx
je 0x6184
cmpl $0x1, %ecx
jne 0x618d
movzbl %al, %eax
addl $0x28, %eax
jmp 0x6189
movl $0x31, %eax
movl %eax, 0x54(%rsp)
movl %esi, 0x4c(%rsp)
movq %rdx, 0xa0(%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 0x61c3
testb %dl, %dl
movl $0x1, %edx
movl $0x15, %esi
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 0x61eb
testb %dl, %dl
movl $0x3, %edx
movl $0x17, %esi
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 0x6215
testb %dl, %dl
movl $0x4, %edx
movl $0x18, %esi
cmovel %esi, %edx
movl %edx, 0x60(%rsp)
movq %r8, 0x90(%rsp)
movq %rdi, 0x98(%rsp)
shrb $0x3, %cl
andb $0x1, %cl
shrb $0x3, %al
andb $0x1, %al
cmpb %al, %cl
je 0x6247
testb %al, %al
movl $0x7, %eax
movl $0x1b, %ecx
cmovel %ecx, %eax
movl %eax, 0x64(%rsp)
movl $0x2, %r8d
movq (%rsp), %rdi
movq %r15, %rsi
leaq 0x18(%rsp), %rdx
leaq 0x3735(%rip), %rcx      # 0x9995
callq 0x77f3
movl $0x1, %eax
xorl %r13d, %r13d
movl 0x50(%rsp,%r13,4), %ebp
testl %ebp, %ebp
je 0x62d4
testl %eax, %eax
leaq 0x18(%rsp), %r14
movq (%rsp), %r12
jne 0x629e
movl $0x1, %r8d
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
leaq 0x3220(%rip), %rcx      # 0x94b9
callq 0x77f3
leaq 0x75(%rsp), %rbx
movq %rbx, %rdi
leaq 0x36eb(%rip), %rsi      # 0x9998
movl %ebp, %edx
xorl %eax, %eax
callq 0x35f0
movq %rbx, %rdi
callq 0x31e0
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %rbx, %rcx
movq %rax, %r8
callq 0x77f3
xorl %eax, %eax
incq %r13
cmpq $0x6, %r13
jne 0x626d
movl $0x1, %r8d
movq (%rsp), %rdi
movq %r15, %rsi
leaq 0x18(%rsp), %rdx
leaq 0x36a5(%rip), %rcx      # 0x999b
callq 0x77f3
movq 0x88(%rsp), %rcx
movl 0x8(%rcx), %eax
movl %eax, 0x10(%rsp)
movq (%rcx), %rax
movq %rax, 0x8(%rsp)
movl 0x48(%rsp), %r13d
movq 0x38(%rsp), %r14
movl 0x24(%rsp), %ebp
movq 0xa0(%rsp), %rdx
movl 0x4c(%rsp), %esi
movq 0x98(%rsp), %rdi
movq 0x90(%rsp), %r8
testl %esi, %esi
jne 0x637f
cmpq $0x0, 0x8(%rdi)
movq %rdi, %rcx
leaq 0x364e(%rip), %rax      # 0x999d
cmoveq %rax, %rcx
movl $0x1, %eax
cmoveq %rax, %r8
movl %r13d, %ebx
movq %rdi, %r13
movq (%rsp), %rdi
movq %r15, %rsi
movq %rdx, %r12
leaq 0x18(%rsp), %rdx
callq 0x77f3
movq %r13, %rdi
movl %ebx, %r13d
movq %r12, %rdx
movzbl 0x1c(%rdi), %esi
andl $0x1, %esi
incq %rdx
cmpl %edx, %ebp
jne 0x60eb
movq 0x78(%rsp), %r14
movl (%r14), %ebp
movl 0x44(%rsp), %edx
cmpl 0x28(%rsp), %ebp
jne 0x63ae
movq 0x28(%rsp), %rax
cmpl %edx, 0x4(%r14)
jle 0x63dc
movq 0x38(%rsp), %rax
testb $0x1, 0x8(%rax)
movl %ebp, %eax
jne 0x63dc
movl $0x1, %r8d
movq (%rsp), %rdi
movq %r15, %rsi
leaq 0x18(%rsp), %rdx
leaq 0x349b(%rip), %rcx      # 0x986f
callq 0x77f3
movl (%r14), %eax
movq 0x28(%rsp), %rsi
leaq 0x1(%rsi), %r12
movslq %eax, %rdx
movl %eax, %ebp
cmpq %rdx, %rsi
jl 0x606f
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq