// File: nilnilnil[P]autocomplete/src/main.cpp
// Function: handle_suggest(mg_event, mg_connection*, mg_request_info const*)
static void*
handle_suggest(enum mg_event event,
               struct mg_connection *conn,
               const struct mg_request_info *request_info) {

    ++nreq;
    if (building) {
        print_HTTP_response(conn, 412, "Busy");
        return (void*)"";
    }

    std::string q    = get_qs(request_info, "q");
    std::string sn   = get_qs(request_info, "n");
    std::string cb   = get_qs(request_info, "callback");
    std::string type = get_qs(request_info, "type");

    DCERR("handle_suggest::q:"<<q<<", sn:"<<sn<<", callback: "<<cb<<endl);

    unsigned int n = sn.empty() ? NMAX : atoi(sn.c_str());
    if (n > NMAX) {
        n = NMAX;
    }

    const bool has_cb = !cb.empty();
    if (has_cb && !is_valid_cb(cb)) {
        print_HTTP_response(conn, 400, "Invalid Request");
        return (void*)"";
    }

    print_HTTP_response(conn, 200, "OK");

    str_lowercase(q);
    vp_t results = suggest(pm, st, q, n);

    /*
      for (size_t i = 0; i < results.size(); ++i) {
      mg_printf(conn, "%s:%d\n", results[i].first.c_str(), results[i].second);
      }
    */
    if (has_cb) {
        mg_printf(conn, "%s(%s);\n", cb.c_str(), results_json(q, results, type).c_str());
    }
    else {
        mg_printf(conn, "%s\n", results_json(q, results, type).c_str());
    }

    return (void*)"";
}

// Assembly:
subq $0x278, %rsp            # imm = 0x278
movl %edi, 0x26c(%rsp)
movq %rsi, 0x260(%rsp)
movq %rdx, 0x258(%rsp)
movq 0x1f9db(%rip), %rax     # 0x2bb40
addq $0x1, %rax
movq %rax, 0x1f9d0(%rip)     # 0x2bb40
testb $0x1, 0x1f9c1(%rip)     # 0x2bb38
je 0xc1ad
movq 0x260(%rsp), %rdi
movl $0x19c, %esi            # imm = 0x19C
leaq 0x16448(%rip), %rdx     # 0x225d5
leaq 0x16446(%rip), %rcx     # 0x225da
callq 0x9d00
leaq 0x16150(%rip), %rax     # 0x222f0
movq %rax, 0x270(%rsp)
jmp 0xc8a6
movq 0x258(%rsp), %rax
movq %rax, 0x58(%rsp)
leaq 0x217(%rsp), %rdi
movq %rdi, 0x60(%rsp)
callq 0x5a50
movq 0x60(%rsp), %rdx
leaq 0x1641c(%rip), %rsi     # 0x225f4
leaq 0x218(%rsp), %rdi
callq 0xddd0
jmp 0xc1e7
movq 0x58(%rsp), %rsi
leaq 0x238(%rsp), %rdi
leaq 0x218(%rsp), %rdx
callq 0x9b20
jmp 0xc203
leaq 0x218(%rsp), %rdi
callq 0x5320
leaq 0x217(%rsp), %rdi
callq 0x55e0
movq 0x258(%rsp), %rax
movq %rax, 0x48(%rsp)
leaq 0x1bf(%rsp), %rdi
movq %rdi, 0x50(%rsp)
callq 0x5a50
movq 0x50(%rsp), %rdx
leaq 0x16d87(%rip), %rsi     # 0x22fcf
leaq 0x1c0(%rsp), %rdi
callq 0xddd0
jmp 0xc257
movq 0x48(%rsp), %rsi
leaq 0x1e0(%rsp), %rdi
leaq 0x1c0(%rsp), %rdx
callq 0x9b20
jmp 0xc273
leaq 0x1c0(%rsp), %rdi
callq 0x5320
leaq 0x1bf(%rsp), %rdi
callq 0x55e0
movq 0x258(%rsp), %rax
movq %rax, 0x38(%rsp)
leaq 0x177(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0x5a50
movq 0x40(%rsp), %rdx
leaq 0x16973(%rip), %rsi     # 0x22c2b
leaq 0x178(%rsp), %rdi
callq 0xddd0
jmp 0xc2c7
movq 0x38(%rsp), %rsi
leaq 0x198(%rsp), %rdi
leaq 0x178(%rsp), %rdx
callq 0x9b20
jmp 0xc2e3
leaq 0x178(%rsp), %rdi
callq 0x5320
leaq 0x177(%rsp), %rdi
callq 0x55e0
movq 0x258(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0x12f(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x5a50
movq 0x30(%rsp), %rdx
leaq 0x162ce(%rip), %rsi     # 0x225f6
leaq 0x130(%rsp), %rdi
callq 0xddd0
jmp 0xc337
movq 0x28(%rsp), %rsi
leaq 0x150(%rsp), %rdi
leaq 0x130(%rsp), %rdx
callq 0x9b20
jmp 0xc353
leaq 0x130(%rsp), %rdi
callq 0x5320
leaq 0x12f(%rsp), %rdi
callq 0x55e0
leaq 0x1e0(%rsp), %rdi
callq 0x5a00
testb $0x1, %al
jne 0xc380
jmp 0xc38b
movl $0x20, %eax
movl %eax, 0x24(%rsp)
jmp 0xc3a4
leaq 0x1e0(%rsp), %rdi
callq 0x5200
movq %rax, %rdi
callq 0x5950
movl %eax, 0x24(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x128(%rsp)
cmpl $0x20, 0x128(%rsp)
jbe 0xc4f1
movl $0x20, 0x128(%rsp)
jmp 0xc4f1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
jmp 0xc404
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
leaq 0x218(%rsp), %rdi
callq 0x5320
leaq 0x217(%rsp), %rdi
callq 0x55e0
jmp 0xc8b6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
jmp 0xc44d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
leaq 0x1c0(%rsp), %rdi
callq 0x5320
leaq 0x1bf(%rsp), %rdi
callq 0x55e0
jmp 0xc897
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
jmp 0xc496
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
leaq 0x178(%rsp), %rdi
callq 0x5320
leaq 0x177(%rsp), %rdi
callq 0x55e0
jmp 0xc88a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
jmp 0xc4df
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
leaq 0x130(%rsp), %rdi
callq 0x5320
leaq 0x12f(%rsp), %rdi
callq 0x55e0
jmp 0xc87d
leaq 0x198(%rsp), %rdi
callq 0x5a00
xorb $-0x1, %al
andb $0x1, %al
movb %al, 0x127(%rsp)
testb $0x1, 0x127(%rsp)
je 0xc57e
leaq 0x198(%rsp), %rdi
callq 0xb620
testb $0x1, %al
jne 0xc57e
movq 0x260(%rsp), %rdi
leaq 0x160c8(%rip), %rdx     # 0x225fb
leaq 0x160a0(%rip), %rcx     # 0x225da
movl $0x190, %esi            # imm = 0x190
callq 0x9d00
jmp 0xc546
leaq 0x15da3(%rip), %rax     # 0x222f0
movq %rax, 0x270(%rsp)
movl $0x1, 0x120(%rsp)
jmp 0xc83a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
jmp 0xc870
movq 0x260(%rsp), %rdi
leaq 0x1607e(%rip), %rdx     # 0x2260b
leaq 0x16046(%rip), %rcx     # 0x225da
movl $0xc8, %esi
callq 0x9d00
jmp 0xc5a0
leaq 0x238(%rsp), %rdi
callq 0xed60
jmp 0xc5af
leaq 0xe8(%rsp), %rdi
leaq 0x238(%rsp), %rsi
callq 0x5220
jmp 0xc5c6
movl 0x128(%rsp), %r8d
leaq 0x1f51b(%rip), %rsi     # 0x2baf0
leaq 0x1f52c(%rip), %rdx     # 0x2bb08
leaq 0x108(%rsp), %rdi
leaq 0xe8(%rsp), %rcx
callq 0x7760
jmp 0xc5f3
leaq 0xe8(%rsp), %rdi
callq 0x5320
testb $0x1, 0x127(%rsp)
je 0xc73c
movq 0x260(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x198(%rsp), %rdi
callq 0x5200
movq %rax, 0x18(%rsp)
leaq 0xa8(%rsp), %rdi
leaq 0x238(%rsp), %rsi
callq 0x5220
jmp 0xc644
leaq 0xc8(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
leaq 0x108(%rsp), %rdx
leaq 0x150(%rsp), %rcx
callq 0xa880
jmp 0xc66b
leaq 0xc8(%rsp), %rdi
callq 0x5200
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rdx
movq %rax, %rcx
leaq 0x15f82(%rip), %rsi     # 0x2260e
xorl %eax, %eax
callq 0x17030
jmp 0xc695
leaq 0xc8(%rsp), %rdi
callq 0x5320
leaq 0xa8(%rsp), %rdi
callq 0x5320
jmp 0xc802
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x5320
jmp 0xc870
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
jmp 0xc82b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
jmp 0xc72a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x5320
leaq 0xa8(%rsp), %rdi
callq 0x5320
jmp 0xc82b
movq 0x260(%rsp), %rax
movq %rax, 0x8(%rsp)
leaq 0x68(%rsp), %rdi
leaq 0x238(%rsp), %rsi
callq 0x5220
jmp 0xc75d
leaq 0x88(%rsp), %rdi
leaq 0x68(%rsp), %rsi
leaq 0x108(%rsp), %rdx
leaq 0x150(%rsp), %rcx
callq 0xa880
jmp 0xc781
leaq 0x88(%rsp), %rdi
callq 0x5200
movq 0x8(%rsp), %rdi
movq %rax, %rdx
leaq 0x15f07(%rip), %rsi     # 0x226a4
xorl %eax, %eax
callq 0x17030
jmp 0xc7a6
leaq 0x88(%rsp), %rdi
callq 0x5320
leaq 0x68(%rsp), %rdi
callq 0x5320
jmp 0xc802
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
jmp 0xc7f6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
leaq 0x88(%rsp), %rdi
callq 0x5320
leaq 0x68(%rsp), %rdi
callq 0x5320
jmp 0xc82b
leaq 0x15ae7(%rip), %rax     # 0x222f0
movq %rax, 0x270(%rsp)
movl $0x1, 0x120(%rsp)
leaq 0x108(%rsp), %rdi
callq 0xe170
jmp 0xc83a
leaq 0x108(%rsp), %rdi
callq 0xe170
jmp 0xc870
leaq 0x150(%rsp), %rdi
callq 0x5320
leaq 0x198(%rsp), %rdi
callq 0x5320
leaq 0x1e0(%rsp), %rdi
callq 0x5320
leaq 0x238(%rsp), %rdi
callq 0x5320
jmp 0xc8a6
leaq 0x150(%rsp), %rdi
callq 0x5320
leaq 0x198(%rsp), %rdi
callq 0x5320
leaq 0x1e0(%rsp), %rdi
callq 0x5320
leaq 0x238(%rsp), %rdi
callq 0x5320
jmp 0xc8b6
movq 0x270(%rsp), %rax
addq $0x278, %rsp            # imm = 0x278
retq
movq 0x208(%rsp), %rdi
callq 0x5a40
nopw %cs:(%rax,%rax)