// File: /workspace/github_cpp_makefile_star2_O2/andrakis[P]alisp/contrib/linenoise-ng/src/linenoise.cpp
// Function: InputBuffer::completeLine(PromptBase&)
int InputBuffer::completeLine(PromptBase& pi) {
  linenoiseCompletions lc;
  char32_t c = 0;

  // completionCallback() expects a parsable entity, so find the previous break
  // character and
  // extract a copy to parse.  we also handle the case where tab is hit while
  // not at end-of-line.
  int startIndex = pos;
  while (--startIndex >= 0) {
    if (strchr(breakChars, buf32[startIndex])) {
      break;
    }
  }
  ++startIndex;
  int itemLength = pos - startIndex;
  Utf32String unicodeCopy(&buf32[startIndex], itemLength);
  Utf8String parseItem(unicodeCopy);

  // get a list of completions
  completionCallback(parseItem.get(), &lc);

  // if no completions, we are done
  if (lc.completionStrings.size() == 0) {
    beep();
    freeCompletions(&lc);
    return 0;
  }

  // at least one completion
  int longestCommonPrefix = 0;
  int displayLength = 0;
  if (lc.completionStrings.size() == 1) {
    longestCommonPrefix = static_cast<int>(lc.completionStrings[0].length());
  } else {
    bool keepGoing = true;
    while (keepGoing) {
      for (size_t j = 0; j < lc.completionStrings.size() - 1; ++j) {
        char32_t c1 = lc.completionStrings[j][longestCommonPrefix];
        char32_t c2 = lc.completionStrings[j + 1][longestCommonPrefix];
        if ((0 == c1) || (0 == c2) || (c1 != c2)) {
          keepGoing = false;
          break;
        }
      }
      if (keepGoing) {
        ++longestCommonPrefix;
      }
    }
  }
  if (lc.completionStrings.size() != 1) {  // beep if ambiguous
    beep();
  }

  // if we can extend the item, extend it and return to main loop
  if (longestCommonPrefix > itemLength) {
    displayLength = len + longestCommonPrefix - itemLength;
    if (displayLength > buflen) {
      longestCommonPrefix -= displayLength - buflen;  // don't overflow buffer
      displayLength = buflen;                         // truncate the insertion
      beep();                                         // and make a noise
    }
    Utf32String displayText(displayLength + 1);
    memcpy(displayText.get(), buf32, sizeof(char32_t) * startIndex);
    memcpy(&displayText[startIndex], &lc.completionStrings[0][0],
           sizeof(char32_t) * longestCommonPrefix);
    int tailIndex = startIndex + longestCommonPrefix;
    memcpy(&displayText[tailIndex], &buf32[pos],
           sizeof(char32_t) * (displayLength - tailIndex + 1));
    copyString32(buf32, displayText.get(), displayLength);
    pos = startIndex + longestCommonPrefix;
    len = displayLength;
    refreshLine(pi);
    return 0;
  }

  // we can't complete any further, wait for second tab
  do {
    c = linenoiseReadChar();
    c = cleanupCtrl(c);
  } while (c == static_cast<char32_t>(-1));

  // if any character other than tab, pass it to the main loop
  if (c != ctrlChar('I')) {
    freeCompletions(&lc);
    return c;
  }

  // we got a second tab, maybe show list of possible completions
  bool showCompletions = true;
  bool onNewLine = false;
  if (lc.completionStrings.size() > completionCountCutoff) {
    int savePos =
        pos;  // move cursor to EOL to avoid overwriting the command line
    pos = len;
    refreshLine(pi);
    pos = savePos;
    printf("\nDisplay all %u possibilities? (y or n)",
           static_cast<unsigned int>(lc.completionStrings.size()));
    fflush(stdout);
    onNewLine = true;
    while (c != 'y' && c != 'Y' && c != 'n' && c != 'N' && c != ctrlChar('C')) {
      do {
        c = linenoiseReadChar();
        c = cleanupCtrl(c);
      } while (c == static_cast<char32_t>(-1));
    }
    switch (c) {
      case 'n':
      case 'N':
        showCompletions = false;
        freeCompletions(&lc);
        break;
      case ctrlChar('C'):
        showCompletions = false;
        freeCompletions(&lc);
        if (write(1, "^C", 2) == -1) return -1;  // Display the ^C we got
        c = 0;
        break;
    }
  }

  // if showing the list, do it the way readline does it
  bool stopList = false;
  if (showCompletions) {
    int longestCompletion = 0;
    for (size_t j = 0; j < lc.completionStrings.size(); ++j) {
      itemLength = static_cast<int>(lc.completionStrings[j].length());
      if (itemLength > longestCompletion) {
        longestCompletion = itemLength;
      }
    }
    longestCompletion += 2;
    int columnCount = pi.promptScreenColumns / longestCompletion;
    if (columnCount < 1) {
      columnCount = 1;
    }
    if (!onNewLine) {  // skip this if we showed "Display all %d possibilities?"
      int savePos =
          pos;  // move cursor to EOL to avoid overwriting the command line
      pos = len;
      refreshLine(pi);
      pos = savePos;
    }
    size_t pauseRow = getScreenRows() - 1;
    size_t rowCount =
        (lc.completionStrings.size() + columnCount - 1) / columnCount;
    for (size_t row = 0; row < rowCount; ++row) {
      if (row == pauseRow) {
        printf("\n--More--");
        fflush(stdout);
        c = 0;
        bool doBeep = false;
        while (c != ' ' && c != '\r' && c != '\n' && c != 'y' && c != 'Y' &&
               c != 'n' && c != 'N' && c != 'q' && c != 'Q' &&
               c != ctrlChar('C')) {
          if (doBeep) {
            beep();
          }
          doBeep = true;
          do {
            c = linenoiseReadChar();
            c = cleanupCtrl(c);
          } while (c == static_cast<char32_t>(-1));
        }
        switch (c) {
          case ' ':
          case 'y':
          case 'Y':
            printf("\r        \r");
            pauseRow += getScreenRows() - 1;
            break;
          case '\r':
          case '\n':
            printf("\r        \r");
            ++pauseRow;
            break;
          case 'n':
          case 'N':
          case 'q':
          case 'Q':
            printf("\r        \r");
            stopList = true;
            break;
          case ctrlChar('C'):
            if (write(1, "^C", 2) == -1) return -1;  // Display the ^C we got
            stopList = true;
            break;
        }
      } else {
        printf("\n");
      }
      if (stopList) {
        break;
      }
      for (int column = 0; column < columnCount; ++column) {
        size_t index = (column * rowCount) + row;
        if (index < lc.completionStrings.size()) {
          itemLength = static_cast<int>(lc.completionStrings[index].length());
          fflush(stdout);
          if (write32(1, lc.completionStrings[index].get(), itemLength) == -1)
            return -1;
          if (((column + 1) * rowCount) + row < lc.completionStrings.size()) {
            for (int k = itemLength; k < longestCompletion; ++k) {
              printf(" ");
            }
          }
        }
      }
    }
    fflush(stdout);
    freeCompletions(&lc);
  }

  // display the prompt on a new line, then redisplay the input buffer
  if (!stopList || c == ctrlChar('C')) {
    if (write(1, "\n", 1) == -1) return 0;
  }
  if (!pi.write()) return 0;
#ifndef _WIN32
  // we have to generate our own newline on line wrap on Linux
  if (pi.promptIndentation == 0 && pi.promptExtraLines > 0)
    if (write(1, "\n", 1) == -1) return 0;
#endif
  pi.promptCursorRowOffset = pi.promptExtraLines;
  refreshLine(pi);
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, 0x20(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
andq $0x0, 0x10(%rsp)
movl 0x18(%rdi), %r15d
movq %rdi, 0x38(%rsp)
movq (%rdi), %rax
movl %r15d, %r13d
sarl $0x1f, %r13d
andl %r15d, %r13d
movq %rax, 0x28(%rsp)
leaq (%rax,%r15,4), %r14
addq $-0x4, %r14
leaq 0xe51c(%rip), %rbx      # 0x1ca90
pushq $0x18
popq %r12
movl %r15d, %ebp
testl %ebp, %ebp
jle 0xe59d
movl (%r14), %esi
movq %rbx, %rdi
movq %r12, %rdx
callq 0x7370
decl %ebp
addq $-0x4, %r14
testq %rax, %rax
je 0xe57b
incl %ebp
movl %ebp, %r13d
subl %r13d, %r15d
movslq %r13d, %r14
movq 0x28(%rsp), %rax
leaq (%rax,%r14,4), %rsi
leaq 0x68(%rsp), %rdi
movl %r15d, %edx
callq 0x11930
leaq 0x40(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x11996
movq 0x38(%rsp), %r13
movq 0x40(%rsp), %rdi
movq %rsp, %rsi
callq *0x1d395(%rip)          # 0x2b970
movq (%rsp), %rax
movq 0x8(%rsp), %rdx
movq %rdx, %rcx
subq %rax, %rcx
subq %rax, %rdx
je 0xe5f9
cmpq $0x10, %rdx
jne 0xe60b
movl (%rax), %ebp
jmp 0xe664
callq 0xebf9
movq %rsp, %rdi
callq 0x124be
jmp 0xe802
sarq $0x4, %rcx
addq $0x18, %rax
movb $0x1, %dl
xorl %ebp, %ebp
testb $0x1, %dl
je 0xe65f
movl %ebp, %edi
movq %rax, %r8
movq %rcx, %r9
decq %r9
je 0xe64f
movq -0x10(%r8), %rsi
movl (%rsi,%rdi,4), %r10d
xorl %esi, %esi
testl %r10d, %r10d
je 0xe651
movq (%r8), %r11
movl (%r11,%rdi,4), %r11d
testl %r11d, %r11d
je 0xe651
addq $0x10, %r8
cmpl %r11d, %r10d
je 0xe624
jmp 0xe651
movl %edx, %esi
movl %esi, %edx
andb $0x1, %dl
movzbl %dl, %edx
addl %edx, %ebp
movl %esi, %edx
jmp 0xe617
callq 0xebf9
movl %ebp, %eax
subl %r15d, %eax
jle 0xe68b
addl 0x14(%r13), %eax
movl 0x10(%r13), %r15d
cmpl %r15d, %eax
jle 0xe756
subl %eax, %ebp
addl %r15d, %ebp
callq 0xebf9
jmp 0xe759
movq 0x20(%rsp), %r14
callq 0xec30
movl %eax, %edi
callq 0xeda5
cmpl $-0x1, %eax
je 0xe690
movl %eax, %ebx
cmpl $0x9, %eax
jne 0xe831
movq (%rsp), %rax
movq 0x8(%rsp), %rbx
subq %rax, %rbx
cmpq $0x640, %rbx            # imm = 0x640
jbe 0xe83b
movl 0x14(%r13), %eax
movl 0x18(%r13), %ebp
movl %eax, 0x18(%r13)
movq %r13, %rdi
movq %r14, %rsi
callq 0xe072
movl %ebp, 0x18(%r13)
movq 0x8(%rsp), %rsi
subq (%rsp), %rsi
shrq $0x4, %rsi
leaq 0xe40d(%rip), %rdi      # 0x1cb01
xorl %eax, %eax
callq 0x7060
movq 0x1c88e(%rip), %rax     # 0x2af90
movq (%rax), %rdi
callq 0x73a0
pushq $0x9
popq %r15
cmpl $0x3, %r15d
je 0xeb06
cmpl $0x4e, %r15d
je 0xeafc
cmpl $0x59, %r15d
je 0xe844
cmpl $0x6e, %r15d
je 0xeafc
cmpl $0x79, %r15d
je 0xe844
callq 0xec30
movl %eax, %edi
callq 0xeda5
cmpl $-0x1, %eax
je 0xe740
movl %eax, %r15d
jmp 0xe70e
movl %eax, %r15d
leal 0x1(%r15), %esi
leaq 0x48(%rsp), %rdi
callq 0x119cc
movq 0x50(%rsp), %rdi
movq (%r13), %rsi
leaq (,%r14,4), %rbx
movq %rbx, %rdx
callq 0x7290
addq 0x50(%rsp), %rbx
movq (%rsp), %rax
movq 0x8(%rax), %rsi
movslq %ebp, %r12
leaq (,%r12,4), %rdx
movq %rbx, %rdi
callq 0x7290
addl %r14d, %r12d
movslq %r12d, %rbx
leaq (,%rbx,4), %rdi
addq 0x50(%rsp), %rdi
movslq 0x18(%r13), %rsi
shlq $0x2, %rsi
addq (%r13), %rsi
movl %r15d, %eax
subl %ebx, %eax
incl %eax
movslq %eax, %rdx
shlq $0x2, %rdx
callq 0x7290
movq (%r13), %rdi
movq 0x50(%rsp), %rsi
movslq %r15d, %rdx
callq 0xec15
movl %ebx, 0x18(%r13)
movl %r15d, 0x14(%r13)
movq %r13, %rdi
movq 0x20(%rsp), %rsi
callq 0xe072
leaq 0x48(%rsp), %rdi
callq 0x118be
xorl %ebx, %ebx
leaq 0x40(%rsp), %rdi
callq 0x11a24
leaq 0x68(%rsp), %rdi
callq 0x118be
movq %rsp, %rdi
callq 0x122be
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsp, %rdi
callq 0x124be
jmp 0xe804
pushq $0x9
popq %r15
movq %rbx, %rcx
jmp 0xe850
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
subq %rax, %rcx
sarq $0x4, %rcx
xorl %r14d, %r14d
subq $0x1, %rcx
jb 0xe86c
movl (%rax), %edx
cmpl %edx, %r14d
cmovlel %edx, %r14d
addq $0x10, %rax
jmp 0xe857
addl $0x2, %r14d
movq 0x20(%rsp), %rax
movl 0x34(%rax), %eax
cltd
idivl %r14d
cmpl $0x2, %eax
pushq $0x1
popq %r12
cmovgel %eax, %r12d
cmpq $0x640, %rbx            # imm = 0x640
ja 0xe8ad
movl 0x14(%r13), %eax
movl 0x18(%r13), %ebx
movl %eax, 0x18(%r13)
movq %r13, %rdi
movq 0x20(%rsp), %rsi
callq 0xe072
movl %ebx, 0x18(%r13)
callq 0xede3
decl %eax
cltq
movq %rax, 0x30(%rsp)
movq 0x8(%rsp), %rax
subq (%rsp), %rax
sarq $0x4, %rax
movl %r12d, %ecx
addq %rcx, %rax
decq %rax
xorl %ebx, %ebx
xorl %edx, %edx
movq %rcx, 0x60(%rsp)
divq %rcx
movq %rax, %rbp
movabsq $0x900000009, %r12      # imm = 0x900000009
xorl %r13d, %r13d
cmpq %rbp, %r13
je 0xeace
cmpq 0x30(%rsp), %r13
jne 0xe97b
xorl %r15d, %r15d
leaq 0xe225(%rip), %rdi      # 0x1cb2c
xorl %eax, %eax
callq 0x7060
movq 0x1c67b(%rip), %rax     # 0x2af90
movq (%rax), %rdi
callq 0x73a0
xorl %eax, %eax
leal -0x4e(%r15), %ecx
cmpl $0x2b, %ecx
ja 0xe942
btq %rcx, %r12
jb 0xeabe
movabsq $0x80000000800, %rdx    # imm = 0x80000000800
btq %rcx, %rdx
jb 0xe985
cmpl $0x3, %r15d
je 0xe9c1
cmpl $0xa, %r15d
je 0xe9ac
cmpl $0xd, %r15d
je 0xe9ac
cmpl $0x20, %r15d
je 0xe985
testb $0x1, %al
je 0xe963
callq 0xebf9
callq 0xec30
movl %eax, %edi
callq 0xeda5
cmpl $-0x1, %eax
je 0xe963
movl %eax, %r15d
movb $0x1, %al
jmp 0xe91f
pushq $0xa
popq %rdi
callq 0x74a0
jmp 0xe9e5
leaq 0xe1aa(%rip), %rdi      # 0x1cb36
xorl %eax, %eax
callq 0x7060
callq 0xede3
movl %eax, %eax
movq 0x30(%rsp), %rcx
addq %rax, %rcx
decq %rcx
movq %rcx, 0x30(%rsp)
jmp 0xe9e5
leaq 0xe183(%rip), %rdi      # 0x1cb36
xorl %eax, %eax
callq 0x7060
incq 0x30(%rsp)
jmp 0xe9e5
pushq $0x1
popq %rdi
leaq 0xe15e(%rip), %rsi      # 0x1cb29
pushq $0x2
popq %rdx
callq 0x7120
movb $0x1, %bl
pushq $0x3
popq %rcx
movl %ecx, %r15d
cmpq $-0x1, %rax
je 0xeab6
testb $0x1, %bl
jne 0xeace
movq %r13, 0x28(%rsp)
movq %rbx, 0x58(%rsp)
xorl %r12d, %r12d
cmpq 0x60(%rsp), %r12
je 0xea9a
movq %rbp, %r13
imulq %r12, %rbp
addq 0x28(%rsp), %rbp
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
subq %rax, %rcx
sarq $0x4, %rcx
cmpq %rcx, %rbp
jae 0xea8f
shlq $0x4, %rbp
movl (%rax,%rbp), %ebx
movq 0x1c55b(%rip), %rax     # 0x2af90
movq (%rax), %rdi
callq 0x73a0
movq (%rsp), %rax
movq 0x8(%rax,%rbp), %rdi
movl %ebx, %esi
callq 0xe46b
cmpl $-0x1, %eax
je 0xeab6
movq %r13, %rbp
incq %r12
movq %r13, %rax
imulq %r12, %rax
addq 0x28(%rsp), %rax
movq 0x8(%rsp), %rcx
subq (%rsp), %rcx
sarq $0x4, %rcx
cmpq %rcx, %rax
pushq $0x20
popq %r13
jae 0xe9fb
cmpl %r14d, %ebx
jge 0xe9fb
movl %r13d, %edi
callq 0x74a0
incl %ebx
jmp 0xea7a
incq %r12
movq %r13, %rbp
jmp 0xe9fb
movq 0x28(%rsp), %r13
incq %r13
movq 0x58(%rsp), %rbx
movabsq $0x900000009, %r12      # imm = 0x900000009
jmp 0xe8ed
pushq $-0x1
popq %rbx
jmp 0xe804
leaq 0xe071(%rip), %rdi      # 0x1cb36
xorl %eax, %eax
callq 0x7060
movb $0x1, %bl
movq 0x20(%rsp), %r14
movq 0x38(%rsp), %r13
movq 0x1c4b1(%rip), %rax     # 0x2af90
movq (%rax), %rdi
callq 0x73a0
movq %rsp, %rdi
callq 0x124be
testb $0x1, %bl
je 0xeb2d
cmpl $0x3, %r15d
jne 0xeb49
jmp 0xeb2d
movq %rsp, %rdi
callq 0x124be
jmp 0xeb2d
movq %rsp, %rdi
callq 0x124be
leaq 0xe014(%rip), %rsi      # 0x1cb29
pushq $0x1
popq %rdi
pushq $0x2
popq %rdx
callq 0x7120
cmpq $-0x1, %rax
pushq $-0x1
popq %rbx
je 0xe804
leaq 0xe0ba(%rip), %rsi      # 0x1cbee
pushq $0x1
popq %rdi
pushq $0x1
popq %rdx
callq 0x7120
cmpq $-0x1, %rax
je 0xe802
movq %r14, %rdi
callq 0x11a0c
xorl %ebx, %ebx
testb %al, %al
je 0xe804
cmpl $0x0, 0x24(%r14)
sete %cl
movl 0x20(%r14), %eax
testl %eax, %eax
setg %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0xeb93
leaq 0xe074(%rip), %rsi      # 0x1cbee
pushq $0x1
popq %rdi
pushq $0x1
popq %rdx
callq 0x7120
cmpq $-0x1, %rax
je 0xe804
movl 0x20(%r14), %eax
movl %eax, 0x30(%r14)
movq %r13, %rdi
movq %r14, %rsi
callq 0xe072
jmp 0xe804
jmp 0xebd2
jmp 0xebd2
jmp 0xebd2
jmp 0xebd2
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x118be
jmp 0xebd5
jmp 0xebd2
jmp 0xebd2
movq %rax, %rbx
jmp 0xebdf
movq %rax, %rbx
jmp 0xebe9
jmp 0xebd2
jmp 0xebd2
jmp 0xebd2
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x11a24
leaq 0x68(%rsp), %rdi
callq 0x118be
movq %rsp, %rdi
callq 0x122be
movq %rbx, %rdi
callq 0x7600