// File: andrakis[P]alisp/contrib/linenoise-ng/src/linenoise.cpp
// Function: linenoiseHistoryAdd
int linenoiseHistoryAdd(const char* line) {
  if (historyMaxLen == 0) {
    return 0;
  }
  if (history == NULL) {
    history =
        reinterpret_cast<char8_t**>(malloc(sizeof(char8_t*) * historyMaxLen));
    if (history == NULL) {
      return 0;
    }
    memset(history, 0, (sizeof(char*) * historyMaxLen));
  }
  char8_t* linecopy = strdup8(line);
  if (!linecopy) {
    return 0;
  }

  // convert newlines in multi-line code to spaces before storing
  char8_t* p = linecopy;
  while (*p) {
    if (*p == '\n') {
      *p = ' ';
    }
    ++p;
  }

  // prevent duplicate history entries
  if (historyLen > 0 && history[historyLen - 1] != nullptr &&
      strcmp(reinterpret_cast<char const*>(history[historyLen - 1]),
             reinterpret_cast<char const*>(linecopy)) == 0) {
    free(linecopy);
    return 0;
  }

  if (historyLen == historyMaxLen) {
    free(history[0]);
    memmove(history, history + 1, sizeof(char*) * (historyMaxLen - 1));
    --historyLen;
    if (--historyPreviousIndex < -1) {
      historyPreviousIndex = -2;
    }
  }

  history[historyLen] = linecopy;
  ++historyLen;
  return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x10(%rbp)
cmpl $0x0, 0x35461(%rip)     # 0x57474
jne 0x22021
movl $0x0, -0x4(%rbp)
jmp 0x221bf
cmpq $0x0, 0x35a67(%rip)     # 0x57a90
jne 0x22071
movslq 0x35442(%rip), %rdi     # 0x57474
shlq $0x3, %rdi
callq 0x85e0
movq %rax, 0x35a4e(%rip)     # 0x57a90
cmpq $0x0, 0x35a46(%rip)     # 0x57a90
jne 0x22058
movl $0x0, -0x4(%rbp)
jmp 0x221bf
movq 0x35a31(%rip), %rdi     # 0x57a90
movslq 0x3540e(%rip), %rdx     # 0x57474
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x8270
movq -0x10(%rbp), %rdi
callq 0x1fc10
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x22091
movl $0x0, -0x4(%rbp)
jmp 0x221bf
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
cmpb $0x0, (%rax)
je 0x220c3
movq -0x20(%rbp), %rax
movzbl (%rax), %eax
cmpl $0xa, %eax
jne 0x220b5
movq -0x20(%rbp), %rax
movb $0x20, (%rax)
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x22099
cmpl $0x0, 0x359ce(%rip)     # 0x57a98
jle 0x22120
movq 0x359bd(%rip), %rax     # 0x57a90
movl 0x359bf(%rip), %ecx     # 0x57a98
subl $0x1, %ecx
movslq %ecx, %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x22120
movq 0x359a3(%rip), %rax     # 0x57a90
movl 0x359a5(%rip), %ecx     # 0x57a98
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
movq -0x18(%rbp), %rsi
callq 0x8630
cmpl $0x0, %eax
jne 0x22120
movq -0x18(%rbp), %rdi
callq 0x8510
movl $0x0, -0x4(%rbp)
jmp 0x221bf
movl 0x35972(%rip), %eax     # 0x57a98
cmpl 0x35348(%rip), %eax     # 0x57474
jne 0x22193
movq 0x3595b(%rip), %rax     # 0x57a90
movq (%rax), %rdi
callq 0x8510
movq 0x3594c(%rip), %rdi     # 0x57a90
movq 0x35945(%rip), %rsi     # 0x57a90
addq $0x8, %rsi
movl 0x3531f(%rip), %eax     # 0x57474
subl $0x1, %eax
movslq %eax, %rdx
shlq $0x3, %rdx
callq 0x8710
movl 0x3592e(%rip), %eax     # 0x57a98
addl $-0x1, %eax
movl %eax, 0x35925(%rip)     # 0x57a98
movl 0x352f7(%rip), %eax     # 0x57470
addl $-0x1, %eax
movl %eax, 0x352ee(%rip)     # 0x57470
cmpl $-0x1, %eax
jge 0x22191
movl $0xfffffffe, 0x352df(%rip) # imm = 0xFFFFFFFE
jmp 0x22193
movq -0x18(%rbp), %rdx
movq 0x358f2(%rip), %rax     # 0x57a90
movslq 0x358f3(%rip), %rcx     # 0x57a98
movq %rdx, (%rax,%rcx,8)
movl 0x358e9(%rip), %eax     # 0x57a98
addl $0x1, %eax
movl %eax, 0x358e0(%rip)     # 0x57a98
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x20, %rsp
popq %rbp
retq
nopl (%rax,%rax)