// File: andrakis[P]alisp/contrib/linenoise-ng/src/linenoise.cpp
// Function: linenoisePreloadBuffer
void linenoisePreloadBuffer(const char* preloadText) {
  if (!preloadText) {
    return;
  }
  int bufferSize = static_cast<int>(strlen(preloadText) + 1);
  unique_ptr<char[]> tempBuffer(new char[bufferSize]);
  strncpy(&tempBuffer[0], preloadText, bufferSize);

  // remove characters that won't display correctly
  char* pIn = &tempBuffer[0];
  char* pOut = pIn;
  bool controlsStripped = false;
  bool whitespaceSeen = false;
  while (*pIn) {
    unsigned char c =
        *pIn++;       // we need unsigned so chars 0x80 and above are allowed
    if ('\r' == c) {  // silently skip CR
      continue;
    }
    if ('\n' == c || '\t' == c) {  // note newline or tab
      whitespaceSeen = true;
      continue;
    }
    if (isControlChar(
            c)) {  // remove other control characters, flag for message
      controlsStripped = true;
      *pOut++ = ' ';
      continue;
    }
    if (whitespaceSeen) {  // convert whitespace to a single space
      *pOut++ = ' ';
      whitespaceSeen = false;
    }
    *pOut++ = c;
  }
  *pOut = 0;
  int processedLength = static_cast<int>(pOut - tempBuffer.get());
  bool lineTruncated = false;
  if (processedLength > (LINENOISE_MAX_LINE - 1)) {
    lineTruncated = true;
    tempBuffer[LINENOISE_MAX_LINE - 1] = 0;
  }
  preloadedBufferContents = tempBuffer.get();
  if (controlsStripped) {
    preloadErrorMessage +=
        " [Edited line: control characters were converted to spaces]\n";
  }
  if (lineTruncated) {
    preloadErrorMessage += " [Edited line: the line length was reduced from ";
    char buf[128];
    snprintf(buf, sizeof(buf), "%d to %d]\n", processedLength,
             (LINENOISE_MAX_LINE - 1));
    preloadErrorMessage += buf;
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x8(%rbp)
cmpq $0x0, -0x8(%rbp)
jne 0x2227b
jmp 0x224d3
movq -0x8(%rbp), %rdi
callq 0x8210
incl %eax
movl %eax, -0xc(%rbp)
movslq -0xc(%rbp), %rdi
callq 0x8040
movq %rax, %rsi
leaq -0x18(%rbp), %rdi
movq %rdi, -0xe0(%rbp)
callq 0x23f80
movq -0xe0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x24ae0
movq %rax, -0xd8(%rbp)
jmp 0x222be
movq -0xd8(%rbp), %rdi
movq -0x8(%rbp), %rsi
movslq -0xc(%rbp), %rdx
callq 0x8250
xorl %eax, %eax
movl %eax, %esi
leaq -0x18(%rbp), %rdi
callq 0x24ae0
movq %rax, -0xe8(%rbp)
jmp 0x222e8
movq -0xe8(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x38(%rbp)
movb $0x0, -0x39(%rbp)
movb $0x0, -0x3a(%rbp)
movq -0x30(%rbp), %rax
cmpb $0x0, (%rax)
je 0x223da
movq -0x30(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x30(%rbp)
movb (%rax), %al
movb %al, -0x3b(%rbp)
movzbl -0x3b(%rbp), %ecx
movl $0xd, %eax
cmpl %ecx, %eax
jne 0x2234d
jmp 0x22303
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x23fe0
jmp 0x224dc
movzbl -0x3b(%rbp), %ecx
movl $0xa, %eax
cmpl %ecx, %eax
je 0x22367
movzbl -0x3b(%rbp), %ecx
movl $0x9, %eax
cmpl %ecx, %eax
jne 0x2236d
movb $0x1, -0x3a(%rbp)
jmp 0x22303
movzbl -0x3b(%rbp), %edi
callq 0x20020
movb %al, -0xe9(%rbp)
jmp 0x2237e
movb -0xe9(%rbp), %al
testb $0x1, %al
jne 0x2238a
jmp 0x223a5
movb $0x1, -0x39(%rbp)
movq -0x38(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x38(%rbp)
movb $0x20, (%rax)
jmp 0x22303
testb $0x1, -0x3a(%rbp)
je 0x223c1
movq -0x38(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x38(%rbp)
movb $0x20, (%rax)
movb $0x0, -0x3a(%rbp)
movb -0x3b(%rbp), %cl
movq -0x38(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x38(%rbp)
movb %cl, (%rax)
jmp 0x22303
movq -0x38(%rbp), %rax
movb $0x0, (%rax)
movq -0x38(%rbp), %rax
movq %rax, -0xf8(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x23fb0
movq %rax, %rcx
movq -0xf8(%rbp), %rax
subq %rcx, %rax
movl %eax, -0x40(%rbp)
movb $0x0, -0x41(%rbp)
cmpl $0xfff, -0x40(%rbp)     # imm = 0xFFF
jle 0x22437
movb $0x1, -0x41(%rbp)
leaq -0x18(%rbp), %rdi
movl $0xfff, %esi            # imm = 0xFFF
callq 0x24ae0
movq %rax, -0x100(%rbp)
jmp 0x2242d
movq -0x100(%rbp), %rax
movb $0x0, (%rax)
leaq -0x18(%rbp), %rdi
callq 0x23fb0
movq %rax, %rsi
leaq 0x3566e(%rip), %rdi     # 0x57ab8
callq 0x8600
jmp 0x22451
testb $0x1, -0x39(%rbp)
je 0x2246e
leaq 0x3567a(%rip), %rdi     # 0x57ad8
leaq 0x1a5dc(%rip), %rsi     # 0x3ca41
callq 0x83c0
jmp 0x2246c
jmp 0x2246e
testb $0x1, -0x41(%rbp)
je 0x224ca
leaq 0x3565d(%rip), %rdi     # 0x57ad8
leaq 0x1a5fc(%rip), %rsi     # 0x3ca7e
callq 0x83c0
jmp 0x22489
movl -0x40(%rbp), %ecx
leaq 0x1a61c(%rip), %rdx     # 0x3caaf
xorl %eax, %eax
leaq -0xd0(%rbp), %rdi
movq %rdi, -0x108(%rbp)
movl $0x80, %esi
movl $0xfff, %r8d            # imm = 0xFFF
callq 0x86f0
movq -0x108(%rbp), %rsi
leaq 0x35617(%rip), %rdi     # 0x57ad8
callq 0x83c0
jmp 0x224c8
jmp 0x224ca
leaq -0x18(%rbp), %rdi
callq 0x23fe0
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
movq -0x20(%rbp), %rdi
callq 0x87b0
nopw %cs:(%rax,%rax)