// File: 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
movq %rsp, %rbp
subq $0x270, %rsp            # imm = 0x270
movl $0x0, -0x4(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x3d00
leaq -0x40(%rbp), %rdi
callq 0x3320
movq 0x79cd(%rip), %rdi      # 0xafd8
leaq -0x40(%rbp), %rsi
callq 0x3050
movq %rax, -0x208(%rbp)
jmp 0x361d
movq -0x208(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x32f0
movb %al, -0x209(%rbp)
jmp 0x363b
movb -0x209(%rbp), %al
testb $0x1, %al
jne 0x3647
jmp 0x3669
leaq -0x20(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x3d20
jmp 0x3656
jmp 0x3604
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x3c5b
leaq -0xb9(%rbp), %rdi
movq %rdi, -0x218(%rbp)
callq 0x33f0
movq -0x218(%rbp), %rdx
leaq 0x497a(%rip), %rsi      # 0x8004
leaq -0xb8(%rbp), %rdi
callq 0x3da0
jmp 0x3698
leaq -0x98(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x6bc0
jmp 0x36ad
leaq -0xb8(%rbp), %rdi
callq 0x3140
leaq -0xb9(%rbp), %rdi
callq 0x3250
leaq -0x98(%rbp), %rdi
callq 0x6fe0
movq %rax, -0x220(%rbp)
jmp 0x36da
movq -0x220(%rbp), %rax
movq %rax, -0xc4(%rbp)
movl $0x1, -0xcc(%rbp)
leaq -0x179(%rbp), %rdi
movq %rdi, -0x228(%rbp)
callq 0x33f0
movq -0x228(%rbp), %rdx
leaq 0x48fa(%rip), %rsi      # 0x800d
leaq -0x178(%rbp), %rdi
callq 0x3da0
jmp 0x3721
leaq -0x158(%rbp), %rdi
leaq -0x98(%rbp), %rsi
leaq -0x178(%rbp), %rdx
callq 0x71d0
jmp 0x373d
leaq -0x178(%rbp), %rdi
callq 0x3140
leaq -0x179(%rbp), %rdi
callq 0x3250
leaq -0x98(%rbp), %rdi
xorl %esi, %esi
callq 0x7120
jmp 0x3765
jmp 0x3767
leaq -0x20(%rbp), %rdi
movq %rdi, -0x230(%rbp)
callq 0x3e50
movq -0x230(%rbp), %rdi
movq %rax, -0x190(%rbp)
callq 0x3e80
movq %rax, -0x198(%rbp)
leaq -0x1b8(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0x3eb0
jmp 0x37a6
movq -0x190(%rbp), %rdi
movq -0x198(%rbp), %rsi
leaq -0x1b8(%rbp), %rdx
callq 0x50b0
movq %rax, -0x238(%rbp)
jmp 0x37c9
movq -0x238(%rbp), %rax
movq %rax, -0x188(%rbp)
leaq -0x1b8(%rbp), %rdi
callq 0x3140
leaq -0x20(%rbp), %rdi
callq 0x3ee0
movq %rax, -0x1c0(%rbp)
movl $0x1, -0x1c4(%rbp)
leaq -0x1c0(%rbp), %rdi
leaq -0x188(%rbp), %rsi
callq 0x3f10
testb $0x1, %al
jne 0x3819
jmp 0x39c0
leaq -0x1c0(%rbp), %rdi
callq 0x3f50
movq %rax, %rsi
addq $0x8, %rsi
movslq -0xc0(%rbp), %rcx
xorl %eax, %eax
movl %eax, %edx
leaq -0x1e8(%rbp), %rdi
callq 0x3210
jmp 0x3845
movl -0x1c4(%rbp), %eax
cmpl -0xcc(%rbp), %eax
jne 0x393b
leaq -0x98(%rbp), %rdi
leaq -0x1e8(%rbp), %rsi
callq 0x6ed0
jmp 0x386c
jmp 0x3952
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x3897
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x3140
leaq -0xb9(%rbp), %rdi
callq 0x3250
jmp 0x3c5b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x3c4f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x38df
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x178(%rbp), %rdi
callq 0x3140
leaq -0x179(%rbp), %rdi
callq 0x3250
jmp 0x3c4f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x3c43
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x1b8(%rbp), %rdi
callq 0x3140
jmp 0x3c43
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x1e8(%rbp), %rdi
callq 0x3140
jmp 0x3c43
leaq -0x98(%rbp), %rdi
leaq -0x1e8(%rbp), %rsi
callq 0x6e90
jmp 0x3950
jmp 0x3952
cmpl $0x14, -0x1c4(%rbp)
jne 0x3967
movl $0x6, -0x1ec(%rbp)
jmp 0x3984
leaq -0x98(%rbp), %rdi
movl $0xa, %esi
callq 0x6f10
jmp 0x397a
movl $0x0, -0x1ec(%rbp)
leaq -0x1e8(%rbp), %rdi
callq 0x3140
movl -0x1ec(%rbp), %eax
testl %eax, %eax
je 0x399e
jmp 0x399c
jmp 0x39c0
jmp 0x39a0
movl -0x1c4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c4(%rbp)
leaq -0x1c0(%rbp), %rdi
callq 0x3f70
jmp 0x37fd
movl -0x1c4(%rbp), %esi
leaq -0x98(%rbp), %rdi
callq 0x7030
jmp 0x39d4
leaq -0x158(%rbp), %rdi
callq 0x3f90
movl %eax, -0x23c(%rbp)
jmp 0x39e8
movl -0x23c(%rbp), %esi
leaq -0x98(%rbp), %rdi
callq 0x70f0
jmp 0x39fc
leaq -0x98(%rbp), %rdi
callq 0x6f40
movl %eax, -0x240(%rbp)
jmp 0x3a10
movl -0x240(%rbp), %eax
movb %al, -0xc5(%rbp)
movsbl -0xc5(%rbp), %eax
cmpl $0x1b, %eax
jne 0x3a48
leaq -0x98(%rbp), %rdi
callq 0x3c80
movb %al, -0x241(%rbp)
jmp 0x3a3c
movb -0x241(%rbp), %al
movb %al, -0xc5(%rbp)
movsbl -0xc5(%rbp), %eax
cmpl $0x3, %eax
je 0x3a60
movsbl -0xc5(%rbp), %eax
cmpl $0xa, %eax
jne 0x3a65
jmp 0x3b6d
movsbl -0xc5(%rbp), %eax
cmpl $0xe, %eax
jne 0x3abd
movl -0xcc(%rbp), %eax
incl %eax
movl %eax, -0x1f0(%rbp)
movl -0x1c4(%rbp), %eax
decl %eax
movl %eax, -0x1f4(%rbp)
leaq -0x1f0(%rbp), %rdi
leaq -0x1f4(%rbp), %rsi
callq 0x3fe0
movq %rax, -0x250(%rbp)
jmp 0x3aa9
movq -0x250(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xcc(%rbp)
jmp 0x3b43
movsbl -0xc5(%rbp), %eax
cmpl $0x10, %eax
jne 0x3b0e
movl -0xcc(%rbp), %eax
decl %eax
movl %eax, -0x1f8(%rbp)
movl $0x1, -0x1fc(%rbp)
leaq -0x1f8(%rbp), %rdi
leaq -0x1fc(%rbp), %rsi
callq 0x4020
movq %rax, -0x258(%rbp)
jmp 0x3afd
movq -0x258(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xcc(%rbp)
jmp 0x3b41
movsbl -0xc5(%rbp), %esi
leaq -0x158(%rbp), %rdi
callq 0x7360
movb %al, -0x259(%rbp)
jmp 0x3b29
movb -0x259(%rbp), %al
testb $0x1, %al
jne 0x3b35
jmp 0x3b3f
movl $0x1, -0xcc(%rbp)
jmp 0x3b41
jmp 0x3b43
jmp 0x3b45
leaq -0x98(%rbp), %rdi
movl $0xa, %esi
callq 0x6f10
jmp 0x3b58
leaq -0x98(%rbp), %rdi
xorl %esi, %esi
callq 0x7120
jmp 0x3b68
jmp 0x3767
leaq -0x98(%rbp), %rdi
movl $0x1, %esi
callq 0x70f0
jmp 0x3b80
leaq -0x98(%rbp), %rdi
xorl %esi, %esi
callq 0x7120
jmp 0x3b90
movsbl -0xc5(%rbp), %eax
cmpl $0xa, %eax
jne 0x3bfc
movl -0xcc(%rbp), %eax
decl %eax
movslq %eax, %rsi
leaq -0x20(%rbp), %rdi
callq 0x4060
movq %rax, %rdi
addq $0x8, %rdi
callq 0x30d0
movq %rax, %rsi
movq 0x73f2(%rip), %rdi      # 0xafb8
callq 0x31e0
movq %rax, -0x268(%rbp)
jmp 0x3bd4
movq -0x268(%rbp), %rdi
movq 0x73be(%rip), %rsi      # 0xafa0
callq 0x3220
jmp 0x3be9
movl $0x0, -0x4(%rbp)
movl $0x1, -0x1ec(%rbp)
jmp 0x3c0d
movl $0x1, -0x4(%rbp)
movl $0x1, -0x1ec(%rbp)
leaq -0x158(%rbp), %rdi
callq 0x4080
leaq -0x98(%rbp), %rdi
callq 0x6e50
leaq -0x40(%rbp), %rdi
callq 0x3140
leaq -0x20(%rbp), %rdi
callq 0x40c0
movl -0x4(%rbp), %eax
addq $0x270, %rsp            # imm = 0x270
popq %rbp
retq
leaq -0x158(%rbp), %rdi
callq 0x4080
leaq -0x98(%rbp), %rdi
callq 0x6e50
leaq -0x40(%rbp), %rdi
callq 0x3140
leaq -0x20(%rbp), %rdi
callq 0x40c0
movq -0x48(%rbp), %rdi
callq 0x33d0
nopw %cs:(%rax,%rax)