// File: /workspace/github_cpp_makefile_star2_O3/akojo[P]quickfind/qselect.cpp
// Function: main
int main(void)
{
    vector<wstr> input;
    string line;

    while (getline(cin, line)) input.emplace_back(line);

    Term tty("/dev/tty");
    Term::Size screen_size = tty.get_screen_size();

    char ch;
    int selection = 1;
    Prompt prompt(tty, "> ");

    tty.erase_display(Term::FORWARD);
    for (;;) {
        auto last = match(input.begin(), input.end(), prompt.query());
        auto it = input.cbegin();
        int i = 1;
        for (; it != last; ++i, ++it) {
            string str = (*it).str.substr(0, screen_size.columns);
            if (i == selection) {
                tty.puts_highlighted(str);
            } else {
                tty.puts(str);
            }
            if (i == MAXLINES)
                break;
            tty.putchar('\n');
        }
        tty.cursor_up(i);
        tty.move_to_col(prompt.current_column());

        ch = tty.getchar();
        if (ch == Term::ESC) {
            ch = handle_escape(tty);
        }

        if (ch == Term::CTRL_C || ch == Term::ENTER) {
            break;
        } else if (ch == Term::CTRL_N) {
            selection = min(selection + 1, i - 1);
        } else if (ch == Term::CTRL_P) {
            selection = max(selection - 1, 1);
        } else if (prompt.handle_key(static_cast<Term::Key>(ch))) {
            selection = 1;
        }
        tty.putchar('\n');
        tty.erase_display(Term::FORWARD);
    }

    tty.move_to_col(1);
    tty.erase_display(Term::FORWARD);

    if (ch == Term::ENTER) {
        cout << input[selection - 1].str.c_str() << endl;
        return 0;
    }
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rbx
movaps %xmm0, (%rbx)
xorl %eax, %eax
movq %rax, 0x10(%rbx)
leaq 0x58(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb $0x0, (%rcx)
movq 0x4ac3(%rip), %r14      # 0x6fd8
leaq 0x48(%rsp), %r15
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2160
movsbl %al, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x2300
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x2557
movq %rbx, %rdi
movq %r15, %rsi
callq 0x29e6
jmp 0x251a
leaq 0x78(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x2a9d(%rip), %rsi      # 0x5004
leaq 0x2a9e(%rip), %rdx      # 0x500c
leaq 0x68(%rsp), %rdi
callq 0x2dd0
leaq 0xf0(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x3cd8
movq 0x68(%rsp), %rdi
cmpq %r14, %rdi
je 0x25a1
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x2180
leaq 0xf0(%rsp), %rdi
callq 0x3f9c
movq %rax, 0x28(%rsp)
leaq 0x18(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x2a4a(%rip), %rsi      # 0x500d
leaq 0x2a45(%rip), %rdx      # 0x500f
leaq 0x8(%rsp), %rdi
callq 0x2dd0
leaq 0x68(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x4060
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x2607
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2180
leaq 0xf0(%rsp), %rdi
xorl %esi, %esi
callq 0x4038
sarq $0x20, 0x28(%rsp)
leaq 0x8(%rsp), %r14
leaq 0xf0(%rsp), %r15
movl $0x1, %r12d
movq 0x30(%rsp), %r13
movq 0x38(%rsp), %rbp
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0xd0(%rsp), %rsi
movq 0xd8(%rsp), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x2ad4
movq %r13, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x2e68
movq %rax, %r13
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x268b
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2180
movq 0x30(%rsp), %rbx
movl $0x1, %ebp
cmpq %r13, %rbx
je 0x270a
leaq 0x8(%rbx), %rsi
movq %r14, %rdi
xorl %edx, %edx
movq 0x28(%rsp), %rcx
callq 0x2190
cmpl %ebp, %r12d
jne 0x26bf
movq %r15, %rdi
movq %r14, %rsi
callq 0x3f18
jmp 0x26ca
movq %r15, %rdi
movq %r14, %rsi
callq 0x3f0a
cmpl $0x14, %ebp
je 0x26dc
movq %r15, %rdi
movl $0xa, %esi
callq 0x3f2c
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x26f8
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2180
cmpl $0x14, %ebp
je 0x2705
incl %ebp
addq $0x28, %rbx
jmp 0x2695
movl $0x14, %ebp
movq %r15, %rdi
movl %ebp, %esi
callq 0x3fd4
movl 0xb8(%rsp), %eax
movl 0xd8(%rsp), %ecx
leal (%rcx,%rax), %esi
incl %esi
movq %r15, %rdi
callq 0x4024
movq %r15, %rdi
callq 0x3f38
movl %eax, %r13d
cmpb $0x1b, %r13b
jne 0x2773
movq %r15, %rdi
callq 0x3f38
movl %eax, %r13d
cmpb $0x5b, %r13b
jne 0x2773
movq %r15, %rdi
callq 0x3f38
cmpb $0x41, %al
je 0x2798
movzbl %al, %ecx
cmpl $0x44, %ecx
je 0x2798
andl $0xfe, %eax
xorl %esi, %esi
cmpl $0x42, %eax
je 0x2785
jmp 0x27b4
movsbl %r13b, %esi
movzbl %sil, %eax
cmpl $0xf, %eax
jg 0x2793
cmpl $0xe, %eax
jne 0x27aa
incl %r12d
decl %ebp
cmpl %r12d, %ebp
cmovll %ebp, %r12d
jmp 0x27c9
cmpl $0x10, %eax
jne 0x27b4
cmpl $0x3, %r12d
movl $0x2, %eax
cmovll %eax, %r12d
decl %r12d
jmp 0x27c9
cmpl $0x3, %eax
je 0x27e5
cmpl $0xa, %eax
je 0x27e5
leaq 0x68(%rsp), %rdi
callq 0x41ce
testb %al, %al
movl $0x1, %eax
cmovnel %eax, %r12d
movq %r15, %rdi
movl $0xa, %esi
callq 0x3f2c
movq %r15, %rdi
xorl %esi, %esi
callq 0x4038
jmp 0x262f
leaq 0xf0(%rsp), %rdi
movl $0x1, %esi
callq 0x4024
leaq 0xf0(%rsp), %rdi
xorl %esi, %esi
callq 0x4038
movl $0x1, %ebx
cmpb $0xa, %r13b
jne 0x2896
movq 0x30(%rsp), %rax
movslq %r12d, %rcx
leaq (%rcx,%rcx,4), %rcx
movq -0x20(%rax,%rcx,8), %rbx
testq %rbx, %rbx
je 0x2847
movq %rbx, %rdi
callq 0x20c0
movq 0x477e(%rip), %rdi      # 0x6fb8
movq %rbx, %rsi
movq %rax, %rdx
callq 0x21e0
jmp 0x2865
movq 0x476a(%rip), %rax      # 0x6fb8
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
leaq (%rax,%rcx), %rdi
movl 0x20(%rax,%rcx), %esi
orl $0x1, %esi
callq 0x22b0
movq 0x474c(%rip), %rdi      # 0x6fb8
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x2160
movsbl %al, %esi
movq 0x4731(%rip), %rdi      # 0x6fb8
callq 0x2030
xorl %ebx, %ebx
movq %rax, %rdi
callq 0x20f0
leaq 0xe0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x28b7
movq 0xe0(%rsp), %rsi
incq %rsi
callq 0x2180
leaq 0xc0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x28d8
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x2180
leaq 0x68(%rsp), %rdi
callq 0x3eea
leaq 0xf0(%rsp), %rdi
callq 0x3eea
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x290b
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x2180
leaq 0x30(%rsp), %rdi
callq 0x2a76
movl %ebx, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x29aa
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2180
jmp 0x29aa
jmp 0x294c
movq %rax, %rbx
jmp 0x29aa
movq %rax, %rbx
movq 0x68(%rsp), %rdi
cmpq %r14, %rdi
je 0x29b7
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x2180
jmp 0x29b7
jmp 0x2975
jmp 0x297c
jmp 0x297c
jmp 0x2981
movq %rax, %rbx
jmp 0x29b7
jmp 0x297c
movq %rax, %rbx
jmp 0x29a0
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x29a0
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2180
leaq 0x68(%rsp), %rdi
callq 0x2a38
leaq 0xf0(%rsp), %rdi
callq 0x3eea
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x29d3
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x2180
leaq 0x30(%rsp), %rdi
callq 0x2a76
movq %rbx, %rdi
callq 0x22d0
nop