// File: /workspace/github_c_makefile_star2_O2/neovim[P]libvterm/src/state.c
// Function: vterm_state_send_selection
void vterm_state_send_selection(VTermState *state, VTermSelectionMask mask, VTermStringFragment frag)
{
  VTerm *vt = state->vt;

  if(frag.initial) {
    /* TODO: support sending more than one mask bit */
    const static char selection_chars[] = "cpqs";
    int idx;
    for(idx = 0; idx < 4; idx++)
      if(mask & (1 << idx))
        break;

    vterm_push_output_sprintf_str(vt, C1_OSC, false, "52;%c;", selection_chars[idx]);

    state->tmp.selection.sendpartial = 0;
  }

  if(frag.len) {
    size_t bufcur = 0;
    char *buffer = state->selection.buffer;

    uint32_t x = 0;
    int n = 0;

    if(state->tmp.selection.sendpartial) {
      n = state->tmp.selection.sendpartial >> 24;
      x = state->tmp.selection.sendpartial & 0xFFFFFF;

      state->tmp.selection.sendpartial = 0;
    }

    while((state->selection.buflen - bufcur) >= 4 && frag.len) {
      x = (x << 8) | frag.str[0];
      n++;
      frag.str++, frag.len--;

      if(n == 3) {
        buffer[0] = base64_one((x >> 18) & 0x3F);
        buffer[1] = base64_one((x >> 12) & 0x3F);
        buffer[2] = base64_one((x >>  6) & 0x3F);
        buffer[3] = base64_one((x >>  0) & 0x3F);

        buffer += 4, bufcur += 4;
        x = 0;
        n = 0;
      }

      if(!frag.len || (state->selection.buflen - bufcur) < 4) {
        if(bufcur)
          vterm_push_output_bytes(vt, state->selection.buffer, bufcur);

        buffer = state->selection.buffer;
        bufcur = 0;
      }
    }

    if(n)
      state->tmp.selection.sendpartial = (n << 24) | x;
  }

  if(frag.final) {
    if(state->tmp.selection.sendpartial) {
      int n      = state->tmp.selection.sendpartial >> 24;
      uint32_t x = state->tmp.selection.sendpartial & 0xFFFFFF;
      char *buffer = state->selection.buffer;

      /* n is either 1 or 2 now */
      x <<= (n == 1) ? 16 : 8;

      buffer[0] = base64_one((x >> 18) & 0x3F);
      buffer[1] = base64_one((x >> 12) & 0x3F);
      buffer[2] = (n == 1) ? '=' : base64_one((x >>  6) & 0x3F);
      buffer[3] = '=';

      vterm_push_output_sprintf_str(vt, 0, true, "%.*s", 4, buffer);
    }
    else
      vterm_push_output_sprintf_str(vt, 0, true, "");
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %r15
movq %rdx, %rcx
movq (%rdi), %rax
movq %rax, 0x8(%rsp)
btl $0x1e, %r15d
movq %rdi, 0x10(%rsp)
jae 0x64b5
xorl %eax, %eax
cmpl $0x4, %eax
je 0x647d
btl %eax, %esi
jb 0x647d
incq %rax
jmp 0x646e
movq %rcx, %rbx
leaq 0x44c9(%rip), %rcx      # 0xa950
movsbl (%rax,%rcx), %r8d
leaq 0x44c2(%rip), %rcx      # 0xa955
movq 0x8(%rsp), %rdi
movl $0x9d, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x9919
movq %rbx, %rcx
movq 0x10(%rsp), %rdi
andl $0x0, 0x1b0(%rdi)
movq %r15, %rax
shlq $0x22, %rax
je 0x65f7
movl 0x1b0(%rdi), %ebp
testl %ebp, %ebp
je 0x64e0
movl %ebp, %ebx
shrl $0x18, %ebx
andl $0xffffff, %ebp         # imm = 0xFFFFFF
andl $0x0, 0x1b0(%rdi)
jmp 0x64e4
xorl %ebp, %ebp
xorl %ebx, %ebx
movq 0x1c8(%rdi), %r13
xorl %r14d, %r14d
movq 0x1d0(%rdi), %rax
subq %r14, %rax
cmpq $0x4, %rax
jb 0x65e8
movl %r15d, %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
je 0x65e8
shll $0x8, %ebp
movsbl (%rcx), %r12d
orl %r12d, %ebp
incl %ebx
cmpl $0x3, %ebx
je 0x6526
movq %r14, %rdx
jmp 0x6584
movl %ebp, %edi
shrl $0x12, %edi
andl $0x3f, %edi
movq %rcx, %rbx
callq 0x66d3
movb %al, (%r13)
movl %ebp, %edi
shrl $0xc, %edi
andl $0x3f, %edi
callq 0x66d3
movb %al, 0x1(%r13)
shrl $0x6, %ebp
andl $0x3f, %ebp
movl %ebp, %edi
callq 0x66d3
movb %al, 0x2(%r13)
andb $0x3f, %r12b
movzbl %r12b, %edi
callq 0x66d3
movq %rbx, %rcx
movq 0x10(%rsp), %rdi
movb %al, 0x3(%r13)
addq $0x4, %r13
addq $0x4, %r14
xorl %ebp, %ebp
movq %r14, %rdx
xorl %ebx, %ebx
incq %rcx
leal -0x1(%r15), %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
andl $0xc0000000, %r15d      # imm = 0xC0000000
orl %eax, %r15d
testl %eax, %eax
je 0x65b5
movq 0x1d0(%rdi), %rax
subq %rdx, %rax
movq %rdx, %r14
cmpq $0x3, %rax
ja 0x64ee
movq 0x1c8(%rdi), %r13
movl $0x0, %r14d
testq %rdx, %rdx
je 0x64ee
movq 0x8(%rsp), %rdi
movq %r13, %rsi
movq %rcx, %r14
callq 0x96ec
movq %r14, %rcx
movq 0x10(%rsp), %rdi
jmp 0x64e4
testl %ebx, %ebx
je 0x65f7
shll $0x18, %ebx
orl %ebp, %ebx
movl %ebx, 0x1b0(%rdi)
testl %r15d, %r15d
js 0x660b
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x1b0(%rdi), %ebx
testl %ebx, %ebx
je 0x66ad
movl %ebx, %ebp
andl $0xffffff, %ebp         # imm = 0xFFFFFF
andl $0xff000000, %ebx       # imm = 0xFF000000
cmpl $0x1000000, %ebx        # imm = 0x1000000
sete %cl
shlb $0x3, %cl
addb $0x8, %cl
shll %cl, %ebp
movq 0x1c8(%rdi), %r14
movl %ebp, %edi
shrl $0x12, %edi
andl $0x3f, %edi
callq 0x66d3
movb %al, (%r14)
movl %ebp, %edi
shrl $0xc, %edi
andl $0x3f, %edi
callq 0x66d3
movb %al, 0x1(%r14)
movb $0x3d, %al
cmpl $0x1000000, %ebx        # imm = 0x1000000
je 0x6677
shrl $0x6, %ebp
andl $0x3c, %ebp
movl %ebp, %edi
callq 0x66d3
movb %al, 0x2(%r14)
movb $0x3d, 0x3(%r14)
leaq 0x42d5(%rip), %rcx      # 0xa95c
pushq $0x1
popq %rdx
pushq $0x4
popq %r8
movq 0x8(%rsp), %rdi
xorl %esi, %esi
movq %r14, %r9
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9919
leaq 0x3bfa(%rip), %rcx      # 0xa2ae
pushq $0x1
popq %rdx
movq 0x8(%rsp), %rdi
xorl %esi, %esi
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9919