// File: /workspace/github_c_makefile_star2_O2/doy[P]libvt100/src/screen.c
// Function: vt100_screen_show_string_utf8
void vt100_screen_show_string_utf8(VT100Screen *vt, char *buf, size_t len)
{
    char *c = buf, *next;

    if (len) {
        vt->dirty = 1;

        if (vt->grid->cur.col > 0) {
            struct vt100_cell *cell;

            cell = vt100_screen_cell_at(
                vt, vt->grid->cur.row, vt->grid->cur.col - 1);
            if (cell->is_wide) {
                cell->len = 0;
            }
        }
    }

    while ((next = g_utf8_next_char(c))) {
        gunichar uc;
        struct vt100_cell *cell = NULL;
        int width;

        uc = g_utf8_get_char(c);
        width = vt100_char_width(uc);

        if (width == 0) {
            if (vt->grid->cur.col > 0) {
                cell = vt100_screen_cell_at(
                    vt, vt->grid->cur.row, vt->grid->cur.col - 1);
            }
            else if (vt->grid->cur.row > 0 && vt100_screen_row_at(vt, vt->grid->cur.row - 1)->wrapped) {
                cell = vt100_screen_cell_at(
                    vt, vt->grid->cur.row - 1, vt->grid->max.col - 1);
            }

            if (cell) {
                char *normal;

                memcpy(cell->contents + cell->len, c, next - c);
                cell->len += next - c;
                /* some fonts have combined characters but can't handle
                 * combining characters, so try to fix that here */
                /* XXX it'd be nice if there was a way to do this that didn't
                 * require an allocation */
                normal = g_utf8_normalize(
                    cell->contents, cell->len, G_NORMALIZE_NFC);
                cell->len = strlen(normal);
                memcpy(cell->contents, normal, cell->len);
                free(normal);
            }
        }
        else {
            vt100_screen_check_wrap(vt, width);
            cell = vt100_screen_cell_at(
                vt, vt->grid->cur.row, vt->grid->cur.col);

            cell->len = next - c;
            memcpy(cell->contents, c, cell->len);
            cell->attrs = vt->attrs;
            cell->is_wide = width == 2;

            vt->grid->cur.col += width;
        }

        c = next;
        if ((size_t)(c - buf) >= len) {
            break;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, 0x8(%rsp)
movq %rdi, %r15
movq %rdx, 0x20(%rsp)
testq %rdx, %rdx
je 0x6241
orb $0x20, 0x4a(%r15)
movq (%r15), %rcx
movslq 0x4(%rcx), %rax
testq %rax, %rax
jle 0x6241
movslq (%rcx), %rdx
movq 0x30(%rcx), %rsi
movslq 0x28(%rcx), %rcx
addq %rdx, %rcx
shlq $0x4, %rcx
movq (%rsi,%rcx), %rcx
shlq $0x5, %rax
testb $0x1, -0x4(%rcx,%rax)
je 0x6241
addq %rax, %rcx
andq $0x0, -0x18(%rcx)
movq 0x6da0(%rip), %rax      # 0xcfe8
movq (%rax), %rax
movq %rax, 0x18(%rsp)
leaq 0x3c(%r15), %rax
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %r12
movzbl (%r12), %eax
movq 0x18(%rsp), %rcx
movsbq (%rcx,%rax), %r13
movq %r12, %rdi
callq 0x3310
movl %eax, %edi
callq 0x30b0
testl %eax, %eax
je 0x62f6
movl %eax, %ebp
movq %r15, %rdi
movl %eax, %esi
callq 0x61b3
movq (%r15), %rax
movslq (%rax), %rcx
movslq 0x4(%rax), %rbx
movq 0x30(%rax), %rdx
movslq 0x28(%rax), %rax
addq %rcx, %rax
shlq $0x4, %rax
movq (%rdx,%rax), %r14
shlq $0x5, %rbx
leaq (%r14,%rbx), %rdi
movq %r13, 0x8(%r14,%rbx)
movq %r12, %rsi
movq %r13, %rdx
callq 0x3430
movq 0x10(%rsp), %rcx
movq (%rcx), %rax
movq %rax, 0x10(%r14,%rbx)
movl 0x8(%rcx), %eax
movl %eax, 0x18(%r14,%rbx)
cmpl $0x2, %ebp
sete %al
movb 0x1c(%r14,%rbx), %cl
andb $-0x2, %cl
orb %al, %cl
movb %cl, 0x1c(%r14,%rbx)
movq (%r15), %rax
addl %ebp, 0x4(%rax)
jmp 0x638e
movq (%r15), %rax
movslq 0x4(%rax), %rbp
movslq (%rax), %rcx
testq %rbp, %rbp
jle 0x6374
movq 0x30(%rax), %rdx
movslq 0x28(%rax), %rax
addq %rcx, %rax
shlq $0x4, %rax
shlq $0x5, %rbp
addq (%rdx,%rax), %rbp
movq %rbp, %r14
addq $-0x20, %r14
je 0x638e
movq -0x18(%rbp), %rdi
addq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x3430
movq -0x18(%rbp), %rsi
addq %r13, %rsi
movq %rsi, -0x18(%rbp)
movq %r14, %rdi
pushq $0x1
popq %rdx
callq 0x3110
movq %rax, %rbx
movq %rax, %rdi
callq 0x31e0
movq %rax, -0x18(%rbp)
movq %r14, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x3430
movq %rbx, %rdi
callq 0x3050
jmp 0x638e
testl %ecx, %ecx
jle 0x638e
movslq 0x28(%rax), %rdx
addq %rdx, %rcx
movq 0x30(%rax), %rdx
shlq $0x4, %rcx
testb $0x1, -0x8(%rcx,%rdx)
jne 0x63a6
addq %r13, %r12
movq %r12, %rax
subq 0x8(%rsp), %rax
cmpq 0x20(%rsp), %rax
jb 0x625e
jmp 0x63bd
addq %rdx, %rcx
addq $-0x10, %rcx
movslq 0xc(%rax), %rbp
shlq $0x5, %rbp
addq (%rcx), %rbp
jmp 0x631c
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq