// File: /workspace/github_c_makefile_star2_O0/neovim[P]libvterm/src/state.c
// Function: on_text
static int on_text(const char bytes[], size_t len, void *user)
{
  VTermState *state = user;

  VTermPos oldpos = state->pos;

  uint32_t *codepoints = (uint32_t *)(state->vt->tmpbuffer);
  size_t maxpoints = (state->vt->tmpbuffer_len) / sizeof(uint32_t);

  int npoints = 0;
  size_t eaten = 0;

  VTermEncodingInstance *encoding =
    state->gsingle_set     ? &state->encoding[state->gsingle_set] :
    !(bytes[eaten] & 0x80) ? &state->encoding[state->gl_set] :
    state->vt->mode.utf8   ? &state->encoding_utf8 :
                             &state->encoding[state->gr_set];

  (*encoding->enc->decode)(encoding->enc, encoding->data,
      codepoints, &npoints, state->gsingle_set ? 1 : maxpoints,
      bytes, &eaten, len);

  /* There's a chance an encoding (e.g. UTF-8) hasn't found enough bytes yet
   * for even a single codepoint
   */
  if(!npoints)
    return eaten;

  if(state->gsingle_set && npoints)
    state->gsingle_set = 0;

  int i = 0;

  /* This is a combining char. that needs to be merged with the previous
   * glyph output */
  if(vterm_unicode_is_combining(codepoints[i])) {
    /* See if the cursor has moved since */
    if(state->pos.row == state->combine_pos.row && state->pos.col == state->combine_pos.col + state->combine_width) {
#ifdef DEBUG_GLYPH_COMBINE
      int printpos;
      printf("DEBUG: COMBINING SPLIT GLYPH of chars {");
      for(printpos = 0; state->combine_chars[printpos]; printpos++)
        printf("U+%04x ", state->combine_chars[printpos]);
      printf("} + {");
#endif

      /* Find where we need to append these combining chars */
      int saved_i = 0;
      while(state->combine_chars[saved_i])
        saved_i++;

      /* Add extra ones */
      while(i < npoints && vterm_unicode_is_combining(codepoints[i])) {
        if(saved_i >= state->combine_chars_size)
          grow_combine_buffer(state);
        state->combine_chars[saved_i++] = codepoints[i++];
      }
      if(saved_i >= state->combine_chars_size)
        grow_combine_buffer(state);
      state->combine_chars[saved_i] = 0;

#ifdef DEBUG_GLYPH_COMBINE
      for(; state->combine_chars[printpos]; printpos++)
        printf("U+%04x ", state->combine_chars[printpos]);
      printf("}\n");
#endif

      /* Now render it */
      putglyph(state, state->combine_chars, state->combine_width, state->combine_pos);
    }
    else {
      DEBUG_LOG("libvterm: TODO: Skip over split char+combining\n");
    }
  }

  for(; i < npoints; i++) {
    // Try to find combining characters following this
    int glyph_starts = i;
    int glyph_ends;
    for(glyph_ends = i + 1;
        (glyph_ends < npoints) && (glyph_ends < glyph_starts + VTERM_MAX_CHARS_PER_CELL);
        glyph_ends++)
      if(!vterm_unicode_is_combining(codepoints[glyph_ends]))
        break;

    int width = 0;

    uint32_t chars[VTERM_MAX_CHARS_PER_CELL + 1];

    for( ; i < glyph_ends; i++) {
      chars[i - glyph_starts] = codepoints[i];
      int this_width = vterm_unicode_width(codepoints[i]);
#ifdef DEBUG
      if(this_width < 0) {
        fprintf(stderr, "Text with negative-width codepoint U+%04x\n", codepoints[i]);
        abort();
      }
#endif
      width += this_width;
    }

    while(i < npoints && vterm_unicode_is_combining(codepoints[i]))
      i++;

    chars[glyph_ends - glyph_starts] = 0;
    i--;

#ifdef DEBUG_GLYPH_COMBINE
    int printpos;
    printf("DEBUG: COMBINED GLYPH of %d chars {", glyph_ends - glyph_starts);
    for(printpos = 0; printpos < glyph_ends - glyph_starts; printpos++)
      printf("U+%04x ", chars[printpos]);
    printf("}, onscreen width %d\n", width);
#endif

    if(state->at_phantom || state->pos.col + width > THISROWWIDTH(state)) {
      linefeed(state);
      state->pos.col = 0;
      state->at_phantom = 0;
      state->lineinfo[state->pos.row].continuation = 1;
    }

    if(state->mode.insert) {
      /* TODO: This will be a little inefficient for large bodies of text, as
       * it'll have to 'ICH' effectively before every glyph. We should scan
       * ahead and ICH as many times as required
       */
      VTermRect rect = {
        .start_row = state->pos.row,
        .end_row   = state->pos.row + 1,
        .start_col = state->pos.col,
        .end_col   = THISROWWIDTH(state),
      };
      scroll(state, rect, 0, -1);
    }

    putglyph(state, chars, width, state->pos);

    if(i == npoints - 1) {
      /* End of the buffer. Save the chars in case we have to combine with
       * more on the next call */
      int save_i;
      for(save_i = 0; chars[save_i]; save_i++) {
        if(save_i >= state->combine_chars_size)
          grow_combine_buffer(state);
        state->combine_chars[save_i] = chars[save_i];
      }
      if(save_i >= state->combine_chars_size)
        grow_combine_buffer(state);
      state->combine_chars[save_i] = 0;
      state->combine_width = width;
      state->combine_pos = state->pos;
    }

    if(state->pos.col + width >= THISROWWIDTH(state)) {
      if(state->mode.autowrap)
        state->at_phantom = 1;
    }
    else {
      state->pos.col += width;
    }
  }

  updatecursor(state, &oldpos, 0);

#ifdef DEBUG
  if(state->pos.row < 0 || state->pos.row >= state->rows ||
     state->pos.col < 0 || state->pos.col >= state->cols) {
    fprintf(stderr, "Position out of bounds after text: (%d,%d)\n",
        state->pos.row, state->pos.col);
    abort();
  }
#endif

  return eaten;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x118(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x120(%rax), %rax
shrq $0x2, %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x28(%rbp), %rax
cmpl $0x0, 0x130(%rax)
je 0xafc7
movq -0x28(%rbp), %rax
addq $0xb0, %rax
movq -0x28(%rbp), %rcx
movslq 0x130(%rcx), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xb0(%rbp)
jmp 0xb068
movq -0x10(%rbp), %rax
movq -0x50(%rbp), %rcx
movsbl (%rax,%rcx), %eax
andl $0x80, %eax
cmpl $0x0, %eax
jne 0xb002
movq -0x28(%rbp), %rax
addq $0xb0, %rax
movq -0x28(%rbp), %rcx
movslq 0x128(%rcx), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xb8(%rbp)
jmp 0xb05a
movq -0x28(%rbp), %rax
movq (%rax), %rax
movb 0x18(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0xb029
movq -0x28(%rbp), %rax
addq $0x110, %rax            # imm = 0x110
movq %rax, -0xc0(%rbp)
jmp 0xb04c
movq -0x28(%rbp), %rax
addq $0xb0, %rax
movq -0x28(%rbp), %rcx
movslq 0x12c(%rcx), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0xe0(%rbp)
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xd8(%rbp)
movq -0x58(%rbp), %rax
addq $0x8, %rax
movq %rax, -0xd0(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x28(%rbp), %rax
cmpl $0x0, 0x130(%rax)
je 0xb0c8
movl $0x1, %eax
movq %rax, -0xe8(%rbp)
jmp 0xb0d3
movq -0x40(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xe0(%rbp), %rax
movq -0xc8(%rbp), %rdx
movq -0xd0(%rbp), %rsi
movq -0xd8(%rbp), %rdi
movq -0xe8(%rbp), %rcx
movl %ecx, %r8d
movq -0x10(%rbp), %r9
movq -0x18(%rbp), %r10
leaq -0x44(%rbp), %rcx
leaq -0x50(%rbp), %r11
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
callq *%rax
cmpl $0x0, -0x44(%rbp)
jne 0xb126
movq -0x50(%rbp), %rax
movl %eax, -0x4(%rbp)
jmp 0xb6f3
movq -0x28(%rbp), %rax
cmpl $0x0, 0x130(%rax)
je 0xb147
cmpl $0x0, -0x44(%rbp)
je 0xb147
movq -0x28(%rbp), %rax
movl $0x0, 0x130(%rax)
movl $0x0, -0x5c(%rbp)
movq -0x38(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movl (%rax,%rcx,4), %edi
callq 0x117b0
cmpl $0x0, %eax
je 0xb2b1
movq -0x28(%rbp), %rax
movl 0x38(%rax), %eax
movq -0x28(%rbp), %rcx
cmpl 0xa4(%rcx), %eax
jne 0xb2ad
movq -0x28(%rbp), %rax
movl 0x3c(%rax), %eax
movq -0x28(%rbp), %rcx
movl 0xa8(%rcx), %ecx
movq -0x28(%rbp), %rdx
addl 0xa0(%rdx), %ecx
cmpl %ecx, %eax
jne 0xb2ad
movl $0x0, -0x60(%rbp)
movq -0x28(%rbp), %rax
movq 0x90(%rax), %rax
movslq -0x60(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0xb1c8
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0xb1a8
jmp 0xb1ca
movl -0x5c(%rbp), %ecx
xorl %eax, %eax
cmpl -0x44(%rbp), %ecx
movb %al, -0xe9(%rbp)
jge 0xb1f6
movq -0x38(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movl (%rax,%rcx,4), %edi
callq 0x117b0
cmpl $0x0, %eax
setne %al
movb %al, -0xe9(%rbp)
movb -0xe9(%rbp), %al
testb $0x1, %al
jne 0xb202
jmp 0xb252
movslq -0x60(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq 0x98(%rcx), %rax
jb 0xb21c
movq -0x28(%rbp), %rdi
callq 0xf320
movq -0x38(%rbp), %rax
movl -0x5c(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x5c(%rbp)
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
movq -0x28(%rbp), %rax
movq 0x90(%rax), %rax
movl -0x60(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x60(%rbp)
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
jmp 0xb1ca
movslq -0x60(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq 0x98(%rcx), %rax
jb 0xb26c
movq -0x28(%rbp), %rdi
callq 0xf320
movq -0x28(%rbp), %rax
movq 0x90(%rax), %rax
movslq -0x60(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movq -0x28(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x90(%rax), %rsi
movq -0x28(%rbp), %rax
movl 0xa0(%rax), %edx
movq -0x28(%rbp), %rax
movq 0xa4(%rax), %rcx
callq 0xf3c0
jmp 0xb2af
jmp 0xb2af
jmp 0xb2b1
jmp 0xb2b3
movl -0x5c(%rbp), %eax
cmpl -0x44(%rbp), %eax
jge 0xb6dd
movl -0x5c(%rbp), %eax
movl %eax, -0x64(%rbp)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
movl -0x68(%rbp), %ecx
xorl %eax, %eax
cmpl -0x44(%rbp), %ecx
movb %al, -0xea(%rbp)
jge 0xb2f2
movl -0x68(%rbp), %eax
movl -0x64(%rbp), %ecx
addl $0x6, %ecx
cmpl %ecx, %eax
setl %al
movb %al, -0xea(%rbp)
movb -0xea(%rbp), %al
testb $0x1, %al
jne 0xb2fe
jmp 0xb322
movq -0x38(%rbp), %rax
movslq -0x68(%rbp), %rcx
movl (%rax,%rcx,4), %edi
callq 0x117b0
cmpl $0x0, %eax
jne 0xb315
jmp 0xb322
jmp 0xb317
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0xb2ce
movl $0x0, -0x6c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x68(%rbp), %eax
jge 0xb378
movq -0x38(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movl (%rax,%rcx,4), %ecx
movl -0x5c(%rbp), %eax
subl -0x64(%rbp), %eax
cltq
movl %ecx, -0x90(%rbp,%rax,4)
movq -0x38(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movl (%rax,%rcx,4), %edi
callq 0x114f0
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %eax
addl -0x6c(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0xb329
jmp 0xb37a
movl -0x5c(%rbp), %ecx
xorl %eax, %eax
cmpl -0x44(%rbp), %ecx
movb %al, -0xeb(%rbp)
jge 0xb3a6
movq -0x38(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movl (%rax,%rcx,4), %edi
callq 0x117b0
cmpl $0x0, %eax
setne %al
movb %al, -0xeb(%rbp)
movb -0xeb(%rbp), %al
testb $0x1, %al
jne 0xb3b2
jmp 0xb3bd
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0xb37a
movl -0x68(%rbp), %eax
subl -0x64(%rbp), %eax
cltq
movl $0x0, -0x90(%rbp,%rax,4)
movl -0x5c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x5c(%rbp)
movq -0x28(%rbp), %rax
cmpl $0x0, 0x40(%rax)
jne 0xb444
movq -0x28(%rbp), %rax
movl 0x3c(%rax), %eax
addl -0x6c(%rbp), %eax
movl %eax, -0xf0(%rbp)
movq -0x28(%rbp), %rax
movq 0x70(%rax), %rax
movq -0x28(%rbp), %rcx
movslq 0x38(%rcx), %rcx
movb (%rax,%rcx,4), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0xb427
movq -0x28(%rbp), %rax
movl 0x34(%rax), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0xf4(%rbp)
jmp 0xb434
movq -0x28(%rbp), %rax
movl 0x34(%rax), %eax
movl %eax, -0xf4(%rbp)
movl -0xf0(%rbp), %eax
movl -0xf4(%rbp), %ecx
cmpl %ecx, %eax
jle 0xb47f
movq -0x28(%rbp), %rdi
callq 0xf4a0
movq -0x28(%rbp), %rax
movl $0x0, 0x3c(%rax)
movq -0x28(%rbp), %rax
movl $0x0, 0x40(%rax)
movq -0x28(%rbp), %rax
movq 0x70(%rax), %rax
movq -0x28(%rbp), %rcx
movslq 0x38(%rcx), %rcx
movb (%rax,%rcx,4), %dl
andb $-0x9, %dl
orb $0x8, %dl
movb %dl, (%rax,%rcx,4)
movq -0x28(%rbp), %rax
movw 0xac(%rax), %ax
shrw $0x3, %ax
andw $0x1, %ax
movzwl %ax, %eax
cmpl $0x0, %eax
je 0xb549
movq -0x28(%rbp), %rax
movl 0x38(%rax), %eax
movl %eax, -0xa4(%rbp)
movq -0x28(%rbp), %rax
movl 0x38(%rax), %eax
addl $0x1, %eax
movl %eax, -0xa0(%rbp)
movq -0x28(%rbp), %rax
movl 0x3c(%rax), %eax
movl %eax, -0x9c(%rbp)
leaq -0xa4(%rbp), %rax
addq $0xc, %rax
movq %rax, -0x100(%rbp)
movq -0x28(%rbp), %rax
movq 0x70(%rax), %rax
movq -0x28(%rbp), %rcx
movslq 0x38(%rcx), %rcx
movb (%rax,%rcx,4), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0xb50e
movq -0x28(%rbp), %rax
movl 0x34(%rax), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0x104(%rbp)
jmp 0xb51b
movq -0x28(%rbp), %rax
movl 0x34(%rax), %eax
movl %eax, -0x104(%rbp)
movq -0x100(%rbp), %rax
movl -0x104(%rbp), %ecx
movl %ecx, (%rax)
movq -0x28(%rbp), %rdi
movq -0xa4(%rbp), %rsi
movq -0x9c(%rbp), %rdx
xorl %ecx, %ecx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
callq 0xf600
movq -0x28(%rbp), %rdi
leaq -0x90(%rbp), %rsi
movl -0x6c(%rbp), %edx
movq -0x28(%rbp), %rax
movq 0x38(%rax), %rcx
callq 0xf3c0
movl -0x5c(%rbp), %eax
movl -0x44(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0xb637
movl $0x0, -0xa8(%rbp)
movslq -0xa8(%rbp), %rax
cmpl $0x0, -0x90(%rbp,%rax,4)
je 0xb5e1
movslq -0xa8(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq 0x98(%rcx), %rax
jb 0xb5ad
movq -0x28(%rbp), %rdi
callq 0xf320
movslq -0xa8(%rbp), %rax
movl -0x90(%rbp,%rax,4), %edx
movq -0x28(%rbp), %rax
movq 0x90(%rax), %rax
movslq -0xa8(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0xa8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa8(%rbp)
jmp 0xb57f
movslq -0xa8(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq 0x98(%rcx), %rax
jb 0xb5fe
movq -0x28(%rbp), %rdi
callq 0xf320
movq -0x28(%rbp), %rax
movq 0x90(%rax), %rax
movslq -0xa8(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x6c(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0xa0(%rax)
movq -0x28(%rbp), %rax
movq -0x28(%rbp), %rcx
movq 0x38(%rcx), %rcx
movq %rcx, 0xa4(%rax)
movq -0x28(%rbp), %rax
movl 0x3c(%rax), %eax
addl -0x6c(%rbp), %eax
movl %eax, -0x108(%rbp)
movq -0x28(%rbp), %rax
movq 0x70(%rax), %rax
movq -0x28(%rbp), %rcx
movslq 0x38(%rcx), %rcx
movb (%rax,%rcx,4), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0xb67b
movq -0x28(%rbp), %rax
movl 0x34(%rax), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0x10c(%rbp)
jmp 0xb688
movq -0x28(%rbp), %rax
movl 0x34(%rax), %eax
movl %eax, -0x10c(%rbp)
movl -0x108(%rbp), %eax
movl -0x10c(%rbp), %ecx
cmpl %ecx, %eax
jl 0xb6c0
movq -0x28(%rbp), %rax
movw 0xac(%rax), %ax
shrw $0x2, %ax
andw $0x1, %ax
movzwl %ax, %eax
cmpl $0x0, %eax
je 0xb6be
movq -0x28(%rbp), %rax
movl $0x1, 0x40(%rax)
jmp 0xb6cd
movl -0x6c(%rbp), %ecx
movq -0x28(%rbp), %rax
addl 0x3c(%rax), %ecx
movl %ecx, 0x3c(%rax)
jmp 0xb6cf
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0xb2b3
movq -0x28(%rbp), %rdi
leaq -0x30(%rbp), %rsi
xorl %edx, %edx
callq 0xf9d0
movq -0x50(%rbp), %rax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nop