// File: andrakis[P]alisp/contrib/linenoise-ng/src/linenoise.cpp
// Function: linenoise
char* linenoise(const char* prompt) {
#ifndef _WIN32
  gotResize = false;
#endif
  if (isatty(STDIN_FILENO)) {  // input is from a terminal
    char32_t buf32[LINENOISE_MAX_LINE];
    char charWidths[LINENOISE_MAX_LINE];
    if (!preloadErrorMessage.empty()) {
      printf("%s", preloadErrorMessage.c_str());
      fflush(stdout);
      preloadErrorMessage.clear();
    }
    PromptInfo pi(prompt, getScreenColumns());
    if (isUnsupportedTerm()) {
      if (!pi.write()) return 0;
      fflush(stdout);
      if (preloadedBufferContents.empty()) {
        unique_ptr<char[]> buf8(new char[LINENOISE_MAX_LINE]);
        if (fgets(buf8.get(), LINENOISE_MAX_LINE, stdin) == NULL) {
          return NULL;
        }
        size_t len = strlen(buf8.get());
        while (len && (buf8[len - 1] == '\n' || buf8[len - 1] == '\r')) {
          --len;
          buf8[len] = '\0';
        }
        return strdup(buf8.get());  // caller must free buffer
      } else {
        char* buf8 = strdup(preloadedBufferContents.c_str());
        preloadedBufferContents.clear();
        return buf8;  // caller must free buffer
      }
    } else {
      if (enableRawMode() == -1) {
        return NULL;
      }
      InputBuffer ib(buf32, charWidths, LINENOISE_MAX_LINE);
      if (!preloadedBufferContents.empty()) {
        ib.preloadBuffer(preloadedBufferContents.c_str());
        preloadedBufferContents.clear();
      }
      int count = ib.getInputLine(pi);
      disableRawMode();
      printf("\n");
      if (count == -1) {
        return NULL;
      }
      size_t bufferSize = sizeof(char32_t) * ib.length() + 1;
      unique_ptr<char[]> buf8(new char[bufferSize]);
      copyString32to8(buf8.get(), bufferSize, buf32);
      return strdup(buf8.get());  // caller must free buffer
    }
  } else {  // input not from a terminal, we should work with piped input, i.e.
            // redirected stdin
    unique_ptr<char[]> buf8(new char[LINENOISE_MAX_LINE]);
    if (fgets(buf8.get(), LINENOISE_MAX_LINE, stdin) == NULL) {
      return NULL;
    }

    // if fgets() gave us the newline, remove it
    int count = static_cast<int>(strlen(buf8.get()));
    if (count > 0 && buf8[count - 1] == '\n') {
      --count;
      buf8[count] = '\0';
    }
    return strdup(buf8.get());  // caller must free buffer
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x5150, %rsp           # imm = 0x5150
movq %rdi, -0x10(%rbp)
movb $0x0, 0x355ae(%rip)     # 0x57ab4
xorl %edi, %edi
callq 0x87a0
cmpl $0x0, %eax
je 0x229ea
leaq 0x355bb(%rip), %rdi     # 0x57ad8
callq 0x8750
testb $0x1, %al
jne 0x2255e
leaq 0x355ab(%rip), %rdi     # 0x57ad8
callq 0x81e0
movq %rax, %rsi
leaq 0x1a57e(%rip), %rdi     # 0x3caba
movb $0x0, %al
callq 0x8080
movq 0x34a36(%rip), %rax     # 0x56f80
movq (%rax), %rdi
callq 0x84f0
leaq 0x3557f(%rip), %rdi     # 0x57ad8
callq 0x85c0
movq -0x10(%rbp), %rax
movq %rax, -0x50c8(%rbp)
callq 0x221d0
movq -0x50c8(%rbp), %rsi
movl %eax, %edx
leaq -0x5050(%rbp), %rdi
callq 0x24b10
callq 0x22b50
movb %al, -0x50bd(%rbp)
jmp 0x22590
movb -0x50bd(%rbp), %al
testb $0x1, %al
jne 0x2259f
jmp 0x22813
leaq -0x5050(%rbp), %rdi
callq 0x23ee0
movb %al, -0x50c9(%rbp)
jmp 0x225b3
movb -0x50c9(%rbp), %al
testb $0x1, %al
jne 0x225ed
jmp 0x225bf
movq $0x0, -0x8(%rbp)
movl $0x1, -0x5060(%rbp)
jmp 0x229c8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x5058(%rbp)
movl %eax, -0x505c(%rbp)
jmp 0x229d9
movq 0x3498c(%rip), %rax     # 0x56f80
movq (%rax), %rdi
callq 0x84f0
jmp 0x225fe
leaq 0x354b3(%rip), %rdi     # 0x57ab8
callq 0x8750
testb $0x1, %al
jne 0x22613
jmp 0x227d2
movl $0x1000, %edi           # imm = 0x1000
callq 0x8040
movq %rax, -0x50d8(%rbp)
jmp 0x22626
movq -0x50d8(%rbp), %rsi
leaq -0x5068(%rbp), %rdi
movq %rdi, -0x50e8(%rbp)
callq 0x23f80
movq -0x50e8(%rbp), %rdi
callq 0x23fb0
movq %rax, %rdi
movq 0x3495a(%rip), %rax     # 0x56fb0
movq (%rax), %rdx
movl $0x1000, %esi           # imm = 0x1000
callq 0x86c0
movq %rax, -0x50e0(%rbp)
jmp 0x2266c
movq -0x50e0(%rbp), %rax
cmpq $0x0, %rax
jne 0x226b3
movq $0x0, -0x8(%rbp)
movl $0x1, -0x5060(%rbp)
jmp 0x227c1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x5058(%rbp)
movl %eax, -0x505c(%rbp)
leaq -0x5068(%rbp), %rdi
callq 0x23fe0
jmp 0x229d9
leaq -0x5068(%rbp), %rdi
callq 0x23fb0
movq %rax, %rdi
callq 0x8210
movq %rax, -0x5070(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x5070(%rbp)
movb %al, -0x50e9(%rbp)
je 0x22757
movq -0x5070(%rbp), %rsi
decq %rsi
leaq -0x5068(%rbp), %rdi
callq 0x24ae0
movq %rax, -0x50f8(%rbp)
jmp 0x226ff
movq -0x50f8(%rbp), %rax
movsbl (%rax), %ecx
movb $0x1, %al
cmpl $0xa, %ecx
movb %al, -0x50f9(%rbp)
je 0x2274b
movq -0x5070(%rbp), %rsi
decq %rsi
leaq -0x5068(%rbp), %rdi
callq 0x24ae0
movq %rax, -0x5108(%rbp)
jmp 0x22735
movq -0x5108(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xd, %eax
sete %al
movb %al, -0x50f9(%rbp)
movb -0x50f9(%rbp), %al
movb %al, -0x50e9(%rbp)
movb -0x50e9(%rbp), %al
testb $0x1, %al
jne 0x22763
jmp 0x2279f
movq -0x5070(%rbp), %rax
decq %rax
movq %rax, -0x5070(%rbp)
movq -0x5070(%rbp), %rsi
leaq -0x5068(%rbp), %rdi
callq 0x24ae0
movq %rax, -0x5110(%rbp)
jmp 0x22790
movq -0x5110(%rbp), %rax
movb $0x0, (%rax)
jmp 0x226ce
leaq -0x5068(%rbp), %rdi
callq 0x23fb0
movq %rax, %rdi
callq 0x8800
movq %rax, -0x8(%rbp)
movl $0x1, -0x5060(%rbp)
leaq -0x5068(%rbp), %rdi
callq 0x23fe0
jmp 0x229c8
leaq 0x352df(%rip), %rdi     # 0x57ab8
callq 0x81e0
movq %rax, %rdi
callq 0x8800
movq %rax, -0x5078(%rbp)
leaq 0x352c4(%rip), %rdi     # 0x57ab8
callq 0x85c0
movq -0x5078(%rbp), %rax
movq %rax, -0x8(%rbp)
movl $0x1, -0x5060(%rbp)
jmp 0x229c8
callq 0x1ff10
movl %eax, -0x5114(%rbp)
jmp 0x22820
movl -0x5114(%rbp), %eax
cmpl $-0x1, %eax
jne 0x22842
movq $0x0, -0x8(%rbp)
movl $0x1, -0x5060(%rbp)
jmp 0x229c8
leaq -0x5098(%rbp), %rdi
leaq -0x4010(%rbp), %rsi
leaq -0x5010(%rbp), %rdx
movl $0x1000, %ecx           # imm = 0x1000
callq 0x24040
jmp 0x22863
leaq 0x3524e(%rip), %rdi     # 0x57ab8
callq 0x8750
testb $0x1, %al
jne 0x2289c
leaq 0x3523e(%rip), %rdi     # 0x57ab8
callq 0x81e0
movq %rax, %rsi
leaq -0x5098(%rbp), %rdi
callq 0x24e90
jmp 0x22890
leaq 0x35221(%rip), %rdi     # 0x57ab8
callq 0x85c0
leaq -0x5098(%rbp), %rdi
leaq -0x5050(%rbp), %rsi
callq 0x20110
movl %eax, -0x5118(%rbp)
jmp 0x228b7
movl -0x5118(%rbp), %eax
movl %eax, -0x509c(%rbp)
callq 0x1fed0
jmp 0x228ca
leaq 0x1a35b(%rip), %rdi     # 0x3cc2c
xorl %eax, %eax
callq 0x8080
jmp 0x228da
cmpl $-0x1, -0x509c(%rbp)
jne 0x228fa
movq $0x0, -0x8(%rbp)
movl $0x1, -0x5060(%rbp)
jmp 0x229c8
leaq -0x5098(%rbp), %rdi
callq 0x24f00
movl %eax, -0x511c(%rbp)
jmp 0x2290e
movl -0x511c(%rbp), %eax
cltq
leaq 0x1(,%rax,4), %rax
movq %rax, -0x50a8(%rbp)
movq -0x50a8(%rbp), %rdi
callq 0x8040
movq %rax, -0x5128(%rbp)
jmp 0x2293a
movq -0x5128(%rbp), %rsi
leaq -0x50b0(%rbp), %rdi
movq %rdi, -0x5130(%rbp)
callq 0x23f80
movq -0x5130(%rbp), %rdi
callq 0x23fb0
movq %rax, %rdi
movq -0x50a8(%rbp), %rsi
leaq -0x4010(%rbp), %rdx
callq 0x1fbb0
jmp 0x22978
leaq -0x50b0(%rbp), %rdi
callq 0x23fb0
movq %rax, %rdi
callq 0x8800
movq %rax, -0x8(%rbp)
movl $0x1, -0x5060(%rbp)
leaq -0x50b0(%rbp), %rdi
callq 0x23fe0
jmp 0x229c8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x5058(%rbp)
movl %eax, -0x505c(%rbp)
leaq -0x50b0(%rbp), %rdi
callq 0x23fe0
jmp 0x229d9
leaq -0x5050(%rbp), %rdi
callq 0x24f20
jmp 0x22b31
leaq -0x5050(%rbp), %rdi
callq 0x24f20
jmp 0x22b3e
movl $0x1000, %edi           # imm = 0x1000
callq 0x8040
movq %rax, %rsi
leaq -0x50b8(%rbp), %rdi
movq %rdi, -0x5140(%rbp)
callq 0x23f80
movq -0x5140(%rbp), %rdi
callq 0x23fb0
movq %rax, %rdi
movq 0x34590(%rip), %rax     # 0x56fb0
movq (%rax), %rdx
movl $0x1000, %esi           # imm = 0x1000
callq 0x86c0
movq %rax, -0x5138(%rbp)
jmp 0x22a36
movq -0x5138(%rbp), %rax
cmpq $0x0, %rax
jne 0x22a7d
movq $0x0, -0x8(%rbp)
movl $0x1, -0x5060(%rbp)
jmp 0x22b25
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x5058(%rbp)
movl %eax, -0x505c(%rbp)
leaq -0x50b8(%rbp), %rdi
callq 0x23fe0
jmp 0x22b3e
leaq -0x50b8(%rbp), %rdi
callq 0x23fb0
movq %rax, %rdi
callq 0x8210
movl %eax, -0x50bc(%rbp)
cmpl $0x0, -0x50bc(%rbp)
jle 0x22b03
movl -0x50bc(%rbp), %eax
decl %eax
movslq %eax, %rsi
leaq -0x50b8(%rbp), %rdi
callq 0x24ae0
movq %rax, -0x5148(%rbp)
jmp 0x22ac0
movq -0x5148(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
jne 0x22b03
movl -0x50bc(%rbp), %eax
decl %eax
movl %eax, -0x50bc(%rbp)
movslq -0x50bc(%rbp), %rsi
leaq -0x50b8(%rbp), %rdi
callq 0x24ae0
movq %rax, -0x5150(%rbp)
jmp 0x22af9
movq -0x5150(%rbp), %rax
movb $0x0, (%rax)
leaq -0x50b8(%rbp), %rdi
callq 0x23fb0
movq %rax, %rdi
callq 0x8800
movq %rax, -0x8(%rbp)
movl $0x1, -0x5060(%rbp)
leaq -0x50b8(%rbp), %rdi
callq 0x23fe0
movq -0x8(%rbp), %rax
addq $0x5150, %rsp           # imm = 0x5150
popq %rbp
retq
movq -0x5058(%rbp), %rdi
callq 0x87b0
nopw (%rax,%rax)