// File: sangwoojun[P]rv32emulator/./linenoise.hpp
// Function: linenoise::refreshMultiLine(linenoise::linenoiseState*)
inline void refreshMultiLine(struct linenoiseState *l) {
    char seq[64];
    int pcolwid = unicodeColumnPos(l->prompt.c_str(), static_cast<int>(l->prompt.length()));
    int colpos = unicodeColumnPosForMultiLine(l->buf, l->len, l->len, l->cols, pcolwid);
    int colpos2; /* cursor column position. */
    int rows = (pcolwid+colpos+l->cols-1)/l->cols; /* rows used by current buf. */
    int rpos = (pcolwid+l->oldcolpos+l->cols)/l->cols; /* cursor relative row. */
    int rpos2; /* rpos after refresh. */
    int col; /* colum position, zero-based. */
    int old_rows = (int)l->maxrows;
    int fd = l->ofd, j;
    std::string ab;

    /* Update maxrows if needed. */
    if (rows > (int)l->maxrows) l->maxrows = rows;

    /* First step: clear all the lines used before. To do so start by
     * going to the last row. */
    if (old_rows-rpos > 0) {
        snprintf(seq,64,"\x1b[%dB", old_rows-rpos);
        ab += seq;
    }

    /* Now for every row clear it, go up. */
    for (j = 0; j < old_rows-1; j++) {
        snprintf(seq,64,"\r\x1b[0K\x1b[1A");
        ab += seq;
    }

    /* Clean the top line. */
    snprintf(seq,64,"\r\x1b[0K");
    ab += seq;

    /* Write the prompt and the current buffer content */
    ab += l->prompt;
    ab.append(l->buf, l->len);

    /* Get text width to cursor position */
    colpos2 = unicodeColumnPosForMultiLine(l->buf, l->len, l->pos, l->cols, pcolwid);

    /* If we are at the very end of the screen with our prompt, we need to
     * emit a newline and move the prompt to the first column. */
    if (l->pos &&
        l->pos == l->len &&
        (colpos2+pcolwid) % l->cols == 0)
    {
        ab += "\n";
        snprintf(seq,64,"\r");
        ab += seq;
        rows++;
        if (rows > (int)l->maxrows) l->maxrows = rows;
    }

    /* Move cursor to right position. */
    rpos2 = (pcolwid+colpos2+l->cols)/l->cols; /* current cursor relative row. */

    /* Go up till we reach the expected positon. */
    if (rows-rpos2 > 0) {
        snprintf(seq,64,"\x1b[%dA", rows-rpos2);
        ab += seq;
    }

    /* Set column. */
    col = (pcolwid + colpos2) % l->cols;
    if (col)
        snprintf(seq,64,"\r\x1b[%dC", col);
    else
        snprintf(seq,64,"\r");
    ab += seq;

    l->oldcolpos = colpos2;

    if (write(fd,ab.c_str(), static_cast<int>(ab.length())) == -1) {} /* Can't recover from write error. */
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
addq $0x18, %rdi
callq 0x20e0
movq %rax, -0xb0(%rbp)
movq -0x8(%rbp), %rdi
addq $0x18, %rdi
callq 0x2350
movq -0xb0(%rbp), %rdi
movl %eax, %esi
callq 0x96b0
movl %eax, -0x54(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x8(%rbp), %rax
movl 0x40(%rax), %esi
movq -0x8(%rbp), %rax
movl 0x40(%rax), %edx
movq -0x8(%rbp), %rax
movl 0x44(%rax), %ecx
movl -0x54(%rbp), %r8d
callq 0x9770
movl %eax, -0x58(%rbp)
movl -0x54(%rbp), %eax
addl -0x58(%rbp), %eax
movq -0x8(%rbp), %rcx
addl 0x44(%rcx), %eax
subl $0x1, %eax
movq -0x8(%rbp), %rcx
cltd
idivl 0x44(%rcx)
movl %eax, -0x60(%rbp)
movl -0x54(%rbp), %eax
movq -0x8(%rbp), %rcx
addl 0x3c(%rcx), %eax
movq -0x8(%rbp), %rcx
addl 0x44(%rcx), %eax
movq -0x8(%rbp), %rcx
cltd
idivl 0x44(%rcx)
movl %eax, -0x64(%rbp)
movq -0x8(%rbp), %rax
movl 0x48(%rax), %eax
movl %eax, -0x70(%rbp)
movq -0x8(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x74(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x22e0
movl -0x60(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x48(%rcx), %eax
jle 0x9087
movl -0x60(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x48(%rax)
movl -0x70(%rbp), %eax
subl -0x64(%rbp), %eax
cmpl $0x0, %eax
jle 0x90f2
movl -0x70(%rbp), %ecx
movl -0x64(%rbp), %eax
subl %eax, %ecx
leaq 0x2917(%rip), %rdx      # 0xb9b8
xorl %eax, %eax
leaq -0x50(%rbp), %rdi
movq %rdi, -0xb8(%rbp)
movl $0x40, %esi
callq 0x23b0
movq -0xb8(%rbp), %rsi
leaq -0x98(%rbp), %rdi
callq 0x21d0
jmp 0x90cd
jmp 0x90f2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x2160
jmp 0x93a5
movl $0x0, -0x78(%rbp)
movl -0x78(%rbp), %eax
movl -0x70(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x9146
leaq 0x28b1(%rip), %rdx      # 0xb9be
xorl %eax, %eax
leaq -0x50(%rbp), %rdi
movq %rdi, -0xc0(%rbp)
movl $0x40, %esi
callq 0x23b0
movq -0xc0(%rbp), %rsi
leaq -0x98(%rbp), %rdi
callq 0x21d0
jmp 0x9139
jmp 0x913b
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x90f9
leaq 0x287b(%rip), %rdx      # 0xb9c8
xorl %eax, %eax
leaq -0x50(%rbp), %rdi
movq %rdi, -0xc8(%rbp)
movl $0x40, %esi
callq 0x23b0
movq -0xc8(%rbp), %rsi
leaq -0x98(%rbp), %rdi
callq 0x21d0
jmp 0x9179
movq -0x8(%rbp), %rsi
addq $0x18, %rsi
leaq -0x98(%rbp), %rdi
callq 0x2290
jmp 0x918f
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rsi
movslq 0x40(%rax), %rdx
leaq -0x98(%rbp), %rdi
callq 0x2050
jmp 0x91a9
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
movl 0x40(%rax), %esi
movl 0x38(%rax), %edx
movl 0x44(%rax), %ecx
movl -0x54(%rbp), %r8d
callq 0x9770
movl %eax, -0xcc(%rbp)
jmp 0x91cb
movl -0xcc(%rbp), %eax
movl %eax, -0x5c(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x0, 0x38(%rax)
je 0x926e
movq -0x8(%rbp), %rax
movl 0x38(%rax), %eax
movq -0x8(%rbp), %rcx
cmpl 0x40(%rcx), %eax
jne 0x926e
movl -0x5c(%rbp), %eax
addl -0x54(%rbp), %eax
movq -0x8(%rbp), %rcx
cltd
idivl 0x44(%rcx)
cmpl $0x0, %edx
jne 0x926e
leaq 0x2606(%rip), %rsi      # 0xb812
leaq -0x98(%rbp), %rdi
callq 0x21d0
jmp 0x921a
leaq 0x27ad(%rip), %rdx      # 0xb9ce
xorl %eax, %eax
leaq -0x50(%rbp), %rdi
movq %rdi, -0xd8(%rbp)
movl $0x40, %esi
callq 0x23b0
movq -0xd8(%rbp), %rsi
leaq -0x98(%rbp), %rdi
callq 0x21d0
jmp 0x924d
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
movl -0x60(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x48(%rcx), %eax
jle 0x926c
movl -0x60(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x48(%rax)
jmp 0x926e
movl -0x54(%rbp), %eax
addl -0x5c(%rbp), %eax
movq -0x8(%rbp), %rcx
addl 0x44(%rcx), %eax
movq -0x8(%rbp), %rcx
cltd
idivl 0x44(%rcx)
movl %eax, -0x68(%rbp)
movl -0x60(%rbp), %eax
subl -0x68(%rbp), %eax
cmpl $0x0, %eax
jle 0x92ce
movl -0x60(%rbp), %ecx
movl -0x68(%rbp), %eax
subl %eax, %ecx
leaq 0x2730(%rip), %rdx      # 0xb9d0
xorl %eax, %eax
leaq -0x50(%rbp), %rdi
movq %rdi, -0xe0(%rbp)
movl $0x40, %esi
callq 0x23b0
movq -0xe0(%rbp), %rsi
leaq -0x98(%rbp), %rdi
callq 0x21d0
jmp 0x92cc
jmp 0x92ce
movl -0x54(%rbp), %eax
addl -0x5c(%rbp), %eax
movq -0x8(%rbp), %rcx
cltd
idivl 0x44(%rcx)
movl %edx, -0x6c(%rbp)
cmpl $0x0, -0x6c(%rbp)
je 0x9301
leaq -0x50(%rbp), %rdi
movl -0x6c(%rbp), %ecx
movl $0x40, %esi
leaq 0x26de(%rip), %rdx      # 0xb9d6
movb $0x0, %al
callq 0x23b0
jmp 0x9318
leaq -0x50(%rbp), %rdi
movl $0x40, %esi
leaq 0x26bd(%rip), %rdx      # 0xb9ce
movb $0x0, %al
callq 0x23b0
leaq -0x98(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x21d0
jmp 0x932a
movl -0x5c(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x3c(%rax)
movl -0x74(%rbp), %eax
movl %eax, -0xf4(%rbp)
leaq -0x98(%rbp), %rdi
movq %rdi, -0x100(%rbp)
callq 0x20e0
movq -0x100(%rbp), %rdi
movq %rax, -0xf0(%rbp)
callq 0x2350
movl -0xf4(%rbp), %edi
movq -0xf0(%rbp), %rsi
movslq %eax, %rdx
callq 0x20a0
movq %rax, -0xe8(%rbp)
jmp 0x9381
movq -0xe8(%rbp), %rax
cmpq $-0x1, %rax
jne 0x9390
jmp 0x9390
leaq -0x98(%rbp), %rdi
callq 0x2160
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
movq -0xa0(%rbp), %rdi
callq 0x23e0
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)