// File: /workspace/github_cpp_makefile_star2_O2/andrakis[P]alisp/contrib/linenoise-ng/src/linenoise.cpp
// Function: dynamicRefresh(PromptBase&, char32_t*, int, int)
static void dynamicRefresh(PromptBase& pi, char32_t* buf32, int len, int pos) {
  // calculate the position of the end of the prompt
  int xEndOfPrompt, yEndOfPrompt;
  calculateScreenPosition(0, 0, pi.promptScreenColumns, pi.promptChars,
                          xEndOfPrompt, yEndOfPrompt);
  pi.promptIndentation = xEndOfPrompt;

  // calculate the position of the end of the input line
  int xEndOfInput, yEndOfInput;
  calculateScreenPosition(xEndOfPrompt, yEndOfPrompt, pi.promptScreenColumns,
                          calculateColumnPosition(buf32, len), xEndOfInput,
                          yEndOfInput);

  // calculate the desired position of the cursor
  int xCursorPos, yCursorPos;
  calculateScreenPosition(xEndOfPrompt, yEndOfPrompt, pi.promptScreenColumns,
                          calculateColumnPosition(buf32, pos), xCursorPos,
                          yCursorPos);

#ifdef _WIN32
  // position at the start of the prompt, clear to end of previous input
  CONSOLE_SCREEN_BUFFER_INFO inf;
  GetConsoleScreenBufferInfo(console_out, &inf);
  inf.dwCursorPosition.X = 0;
  inf.dwCursorPosition.Y -= pi.promptCursorRowOffset /*- pi.promptExtraLines*/;
  SetConsoleCursorPosition(console_out, inf.dwCursorPosition);
  DWORD count;
  FillConsoleOutputCharacterA(console_out, ' ',
                              pi.promptPreviousLen + pi.promptPreviousInputLen,
                              inf.dwCursorPosition, &count);
  pi.promptPreviousLen = pi.promptIndentation;
  pi.promptPreviousInputLen = len;

  // display the prompt
  if (!pi.write()) return;

  // display the input line
  if (write32(1, buf32, len) == -1) return;

  // position the cursor
  GetConsoleScreenBufferInfo(console_out, &inf);
  inf.dwCursorPosition.X = xCursorPos;  // 0-based on Win32
  inf.dwCursorPosition.Y -= yEndOfInput - yCursorPos;
  SetConsoleCursorPosition(console_out, inf.dwCursorPosition);
#else  // _WIN32
  char seq[64];
  int cursorRowMovement = pi.promptCursorRowOffset - pi.promptExtraLines;
  if (cursorRowMovement > 0) {  // move the cursor up as required
    snprintf(seq, sizeof seq, "\x1b[%dA", cursorRowMovement);
    if (write(1, seq, strlen(seq)) == -1) return;
  }
  // position at the start of the prompt, clear to end of screen
  snprintf(seq, sizeof seq, "\x1b[1G\x1b[J");  // 1-based on VT100
  if (write(1, seq, strlen(seq)) == -1) return;

  // display the prompt
  if (!pi.write()) return;

  // display the input line
  if (write32(1, buf32, len) == -1) return;

  // we have to generate our own newline on line wrap
  if (xEndOfInput == 0 && yEndOfInput > 0)
    if (write(1, "\n", 1) == -1) return;

  // position the cursor
  cursorRowMovement = yEndOfInput - yCursorPos;
  if (cursorRowMovement > 0) {  // move the cursor up as required
    snprintf(seq, sizeof seq, "\x1b[%dA", cursorRowMovement);
    if (write(1, seq, strlen(seq)) == -1) return;
  }
  // position the cursor within the line
  snprintf(seq, sizeof seq, "\x1b[%dG", xCursorPos + 1);  // 1-based on VT100
  if (write(1, seq, strlen(seq)) == -1) return;
#endif

  pi.promptCursorRowOffset =
      pi.promptExtraLines + yCursorPos;  // remember row for next pass
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %ecx, 0x4(%rsp)
movl %edx, %r15d
movq %rsi, %r14
movq %rdi, %rbx
movl 0x18(%rdi), %ecx
movl 0x34(%rdi), %r12d
leaq 0x1c(%rsp), %r13
leaq 0x18(%rsp), %rbp
xorl %edi, %edi
xorl %esi, %esi
movl %r12d, %edx
movq %r13, %r8
movq %rbp, %r9
callq 0xe41f
movl (%r13), %r13d
movl %r13d, 0x24(%rbx)
movl (%rbp), %ebp
movq %r14, %rdi
movl %r15d, (%rsp)
movl %r15d, %esi
callq 0xe457
leaq 0x14(%rsp), %r8
leaq 0x10(%rsp), %r9
movl %r13d, %edi
movl %ebp, %esi
movl %r12d, %edx
movl %eax, %ecx
callq 0xe41f
movl 0x34(%rbx), %r12d
movq %r14, %rdi
movl 0x4(%rsp), %esi
callq 0xe457
leaq 0xc(%rsp), %r8
leaq 0x8(%rsp), %r9
movl %r13d, %edi
movl %ebp, %esi
movl %r12d, %edx
movl %eax, %ecx
callq 0xe41f
movl 0x30(%rbx), %ecx
subl 0x20(%rbx), %ecx
jle 0xf80b
leaq 0xd313(%rip), %rdx      # 0x1caec
leaq 0x20(%rsp), %r15
pushq $0x40
popq %rsi
movq %r15, %rdi
xorl %eax, %eax
callq 0x7570
movq %r15, %rdi
callq 0x7170
pushq $0x1
popq %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x7120
cmpq $-0x1, %rax
je 0xf919
movabsq $0x4a5b1b47315b1b, %rax # imm = 0x4A5B1B47315B1B
leaq 0x20(%rsp), %r15
movq %rax, (%r15)
movq %r15, %rdi
callq 0x7170
pushq $0x1
popq %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x7120
cmpq $-0x1, %rax
je 0xf919
movq %rbx, %rdi
callq 0x11a0c
testb %al, %al
je 0xf919
movq %r14, %rdi
movl (%rsp), %esi
callq 0xe46b
cmpl $-0x1, %eax
je 0xf919
cmpl $0x0, 0x14(%rsp)
sete %al
movl 0x10(%rsp), %ebp
testl %ebp, %ebp
setg %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xf895
leaq 0xd36e(%rip), %rsi      # 0x1cbee
pushq $0x1
popq %rdi
pushq $0x1
popq %rdx
callq 0x7120
cmpq $-0x1, %rax
je 0xf919
movl 0x8(%rsp), %r15d
subl %r15d, %ebp
jle 0xf8d6
leaq 0xd246(%rip), %rdx      # 0x1caec
leaq 0x20(%rsp), %r14
pushq $0x40
popq %rsi
movq %r14, %rdi
movl %ebp, %ecx
xorl %eax, %eax
callq 0x7570
movq %r14, %rdi
callq 0x7170
pushq $0x1
popq %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x7120
cmpq $-0x1, %rax
je 0xf919
movl 0xc(%rsp), %ecx
incl %ecx
leaq 0xd218(%rip), %rdx      # 0x1cafb
leaq 0x20(%rsp), %r14
pushq $0x40
popq %rsi
movq %r14, %rdi
xorl %eax, %eax
callq 0x7570
movq %r14, %rdi
callq 0x7170
pushq $0x1
popq %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x7120
cmpq $-0x1, %rax
je 0xf919
addl 0x20(%rbx), %r15d
movl %r15d, 0x30(%rbx)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq