// File: /workspace/github_cpp_makefile_star2_O2/lega911[P]ijson/src/server.cpp
// Function: Loop::client_request(ISlice, Connect*)
int Loop::client_request(ISlice name, Connect *client) {
    stat_call++;
    QueueLine *ql = server->get_queue(name);
    if(!ql) {
        if(server->log & 4) std::cout << ltime() << "404 no method " << name.as_string() << std::endl;
        client->send.status("404 Not Found")->done(-32601);
        return -1;
    }

    Connect *worker = NULL;
    Queue *q;

    LOCK _l(ql->mutex);
    int rloop = _nloop;
    for(int index=-1;index<server->threads;index++) {
        if(index == _nloop) continue;
        if(index >= 0) rloop = index;
        q = &ql->queue[rloop];

        auto it=q->workers.begin();
        while (it != q->workers.end()) {
            worker = *it;
            if(client->required_worker) {
                if(client->required_worker != worker->connection_id) {
                    worker = NULL;
                    it++;
                    continue;
                }
            }
            it = q->workers.erase(it);
            worker->unlink();

            if(worker->is_closed()) {
                if(server->log & 8) std::cout << ltime() << "worker closed " << worker << std::endl;
                worker = NULL;
                continue;
            }

            bool busy = true;
            if(worker->status == Status::worker_wait_job) {
                worker->mutex.lock();
                if(worker->status == Status::worker_wait_job) {
                    worker->status = Status::busy;
                    busy = false;
                }
                worker->mutex.unlock();
            }

            if(busy) {
                if(server->log & 8) std::cout << "worker is not ready " << worker << std::endl;
                worker = NULL;
                continue;
            }
            break;
        };

        if(worker) break;
    }

    if(worker) {
        if(client->no_response) {
            if(worker->worker_mode) worker->status = Status::worker_mode_async;
            else worker->status = Status::net;
            worker->send.status("200 OK")->header("Name", name)->header("Async", Slice("true"))->done(client->body);
            client->send.status("200 OK")->done(1);
        } else if(worker->noid) {
            worker->client = client;
            client->link();
            client->status = Status::client_wait_result;
            worker->status = Status::worker_wait_result;
            worker->send.status("200 OK")->header("Name", name)->done(client->body);
        } else {
            Slice id(client->id);
            if(id.empty()) {
                try {
                    while(client->json.scan()) {
                        if(client->json.key == "id") {
                            id = client->json.value;
                            client->id.set(client->json.value);
                            break;
                        }
                    }
                } catch (const error::InvalidData &e) {
                    worker->link();
                    worker->status = Status::worker_wait_job;
                    ql->queue[worker->nloop].workers.push_front(worker);
                    client->send.status("400 No Id")->done(-1);
                    if(server->log & 4) std::cout << ltime() << "400 no id " << name.as_string() << std::endl;
                    return -3;
                }
            }
            if(id.empty()) {
                generate_id(client->id);
                id.set(client->id);
            }
            std::string sid = id.as_string();

            server->wait_lock.lock();
            bool busy = server->wait_response.find(sid) != server->wait_response.end();
            server->wait_lock.unlock();
            if(busy) {
                worker->link();
                worker->status = Status::worker_wait_job;
                ql->queue[worker->nloop].workers.push_front(worker);
                client->send.status("400 Collision Id")->done(-1);
                if(server->log & 4) std::cout << ltime() << "400 collision id " << name.as_string() << std::endl;
                return -3;
            }
            if(worker->fail_on_disconnect) {
                worker->client = client;
                client->link();
            }
            worker->send.status("200 OK")->header("Id", id)->header("Name", name)->done(client->body);
            server->wait_lock.lock();
            server->wait_response[sid] = client;
            server->wait_lock.unlock();
            client->link();
            client->status = Status::client_wait_result;
            worker->status = Status::net;
        }
        if(server->log & 32) worker->start_time = get_time();
    } else {
        if(client->header_option == "if present" && !ql->workers.head) {
            client->send.status("204 No Content")->done(1);
            return 0;
        }
        bool inserted = false;
        auto *clients = &ql->queue[_nloop].clients;

        Message *msg = new Message();
        msg->priority = client->priority;
        msg->required_worker.move(&client->required_worker);
        if(client->no_response) {
            msg->name.set(name);
            msg->attach_buffer(&client->body);
        } else {
            client->name.set(name);
            msg->attach_client(client);
        }
        for(auto it=clients->crbegin(); it!=clients->crend(); it++) {
            if(msg->priority <= (*it)->priority) {
                clients->insert(it.base(), msg);
                inserted = true;
                break;
            }
        }
        if(!inserted) clients->push_front(msg);
        if(client->no_response) {
            client->send.status("200 OK")->done(1);
        } else {
            client->status = Status::client_wait_result;
        }
    };

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rcx, %r13
movq %rdi, %r15
movq %rsi, -0x90(%rbp)
movl %edx, -0x88(%rbp)
incq 0x80(%rdi)
movq 0x18(%rdi), %rdi
xorl %ecx, %ecx
callq 0x104d6
testq %rax, %rax
je 0x11819
andq $0x0, -0x30(%rbp)
pushq $-0x1
popq %r14
movq %rax, -0x80(%rbp)
movq %rax, %rdi
addq $0x18, %rdi
movq %rdi, -0x98(%rbp)
callq 0xd764
movl 0x4(%r15), %eax
movl %eax, -0x74(%rbp)
movq %r13, %r12
addq $0x110, %r13            # imm = 0x110
leaq -0xb8(%rbp), %rbx
movq %r15, -0x38(%rbp)
movq 0x18(%r15), %rax
cmpl 0x28(%rax), %r14d
jge 0x11775
cmpl 0x4(%r15), %r14d
je 0x1176d
testl %r14d, %r14d
movl -0x74(%rbp), %edx
cmovnsl %r14d, %edx
movq -0x80(%rbp), %rax
movq 0x40(%rax), %rcx
movl %edx, -0x74(%rbp)
movslq %edx, %rax
imulq $0xa0, %rax, %rdx
leaq (%rcx,%rdx), %r15
movq 0x10(%rcx,%rdx), %rax
movq %rax, -0x60(%rbp)
movq 0x18(%rcx,%rdx), %rsi
movq %rsi, -0x58(%rbp)
movq 0x20(%rcx,%rdx), %rsi
movq %rsi, -0x50(%rbp)
movq 0x28(%rcx,%rdx), %rcx
movq %rcx, -0x48(%rbp)
cmpq 0x30(%r15), %rax
je 0x1175c
movq (%rax), %rsi
movq %rsi, -0x30(%rbp)
cmpl $0x0, 0x118(%r12)
je 0x1163c
addq $0x100, %rsi            # imm = 0x100
movq %r13, %rdi
callq 0x135aa
testb %al, %al
je 0x11638
andq $0x0, -0x30(%rbp)
movq %rbx, %rdi
leaq -0x60(%rbp), %rsi
xorl %edx, %edx
callq 0x136d8
jmp 0x11740
movq -0x60(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0xd0(%rbp)
movups -0x50(%rbp), %xmm0
movups %xmm0, -0xc8(%rbp)
movq %rbx, %rdi
movq %r15, %rsi
leaq -0xd8(%rbp), %rdx
callq 0x1369a
movups -0xb8(%rbp), %xmm0
movups -0xa8(%rbp), %xmm1
movaps %xmm1, -0x50(%rbp)
movaps %xmm0, -0x60(%rbp)
movq -0x30(%rbp), %rdi
callq 0xa84c
movq -0x30(%rbp), %rdi
cmpl $-0x1, (%rdi)
je 0x116f6
cmpl $0x1, 0xcc(%rdi)
jne 0x116bf
addq $0x50, %rdi
callq 0xd764
movq -0x30(%rbp), %rdi
cmpl $0x1, 0xcc(%rdi)
je 0x11749
addq $0x50, %rdi
callq 0xa220
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
testb $0x8, 0x20(%rax)
je 0x1173b
movq 0xe8fc(%rip), %rdi      # 0x1ffd0
leaq 0x7ee7(%rip), %rsi      # 0x195c2
callq 0xa2e0
movq -0x30(%rbp), %rsi
movq %rax, %rdi
callq 0xa230
movq %rax, %rdi
callq 0xa160
jmp 0x1173b
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
testb $0x8, 0x20(%rax)
je 0x1173b
callq 0x16a89
movq 0xe8c0(%rip), %rdi      # 0x1ffd0
movq %rax, %rsi
callq 0xa2e0
movq %rax, %rdi
leaq 0x7e91(%rip), %rsi      # 0x195b3
callq 0xa2e0
movq -0x30(%rbp), %rsi
movq %rax, %rdi
callq 0xa230
movq %rax, %rdi
callq 0xa160
andq $0x0, -0x30(%rbp)
movq -0x60(%rbp), %rax
jmp 0x115f1
movl $0x5, 0xcc(%rdi)
addq $0x50, %rdi
callq 0xa220
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
movq -0x38(%rbp), %r15
jne 0x118a3
incl %r14d
jmp 0x11592
leaq 0xa8(%r12), %rdi
leaq 0x7e65(%rip), %rsi      # 0x195e9
callq 0xd9a0
testb %al, %al
movq -0x80(%rbp), %rax
je 0x1179f
cmpq $0x0, 0x80(%rax)
je 0x11ab7
movq 0x40(%rax), %r14
movslq 0x4(%r15), %r15
pushq $0x38
popq %rdi
callq 0xfb38
movq %rax, %rbx
leaq 0x8(%rax), %rdi
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
andq $0x0, 0x30(%rax)
movups %xmm0, (%rax)
movq %rax, -0x70(%rbp)
movl 0xf4(%r12), %eax
movl %eax, (%rbx)
movq %r13, %rsi
movq %r12, %r13
callq 0x1375c
cmpb $0x1, 0xf2(%r12)
jne 0x11946
movq %rbx, %rdi
addq $0x28, %rdi
leaq -0x90(%rbp), %rsi
callq 0xd3d0
leaq 0xd0(%r12), %rsi
movq %rbx, %rdi
callq 0x12c50
jmp 0x11962
movq 0x18(%r15), %rax
testb $0x4, 0x20(%rax)
je 0x1187b
callq 0x16a89
movq 0xe7a1(%rip), %rdi      # 0x1ffd0
movq %rax, %rsi
callq 0xa2e0
leaq 0x6e70(%rip), %rsi      # 0x186ae
movq %rax, %rdi
callq 0xa2e0
movq %rax, %rbx
leaq -0x60(%rbp), %r15
leaq -0x90(%rbp), %rsi
pushq $-0x1
popq %rdx
movq %r15, %rdi
callq 0xd636
movq %rbx, %rdi
movq %r15, %rsi
callq 0xa280
movq %rax, %rdi
callq 0xa160
leaq -0x60(%rbp), %rdi
callq 0xa130
addq $0x18, %r13
leaq 0x6e37(%rip), %rsi      # 0x186bd
movq %r13, %rdi
callq 0xb93e
movq %rax, %rdi
movl $0xffff80a7, %esi       # imm = 0xFFFF80A7
callq 0xb9b2
pushq $-0x1
popq %rbx
jmp 0x11d76
movq %r12, %r14
cmpb $0x1, 0xf2(%r12)
jne 0x11a4d
xorl %eax, %eax
cmpb $0x0, 0xf3(%rdi)
pushq $0x6
popq %rcx
cmovel %eax, %ecx
movl %ecx, 0xcc(%rdi)
addq $0x18, %rdi
leaq 0x6964(%rip), %rsi      # 0x18239
callq 0xb93e
leaq 0x7cc7(%rip), %rsi      # 0x195a8
leaq -0x90(%rbp), %rdx
movq %rax, %rdi
callq 0xcdbe
leaq 0x7259(%rip), %rcx      # 0x18b50
leaq -0x60(%rbp), %rdx
movq %rcx, (%rdx)
movl $0x4, 0x8(%rdx)
leaq 0x7ca1(%rip), %rsi      # 0x195ad
movq %rax, %rdi
callq 0xcdbe
leaq 0xd0(%r14), %rsi
movq %rax, %rdi
callq 0xbe84
addq $0x18, %r14
leaq 0x690b(%rip), %rsi      # 0x18239
movq %r14, %rdi
callq 0xb93e
pushq $0x1
popq %rsi
movq %rax, %rdi
callq 0xb9b2
jmp 0x11d4e
leaq 0xb8(%r13), %rdi
leaq -0x90(%rbp), %rsi
callq 0xd3d0
movq %r12, 0x18(%rbx)
incl 0x8(%r12)
imulq $0xa0, %r15, %rax
leaq (%r14,%rax), %rbx
addq $0x50, %rbx
movq 0x30(%rbx), %rax
movups 0x38(%rbx), %xmm0
movq 0x48(%rbx), %rcx
leaq -0x60(%rbp), %r15
movq %rax, (%r15)
movups %xmm0, 0x8(%r15)
movq %rcx, 0x18(%r15)
movl $0x200, %r14d           # imm = 0x200
leaq -0xb8(%rbp), %r13
cmpq 0x10(%rbx), %rax
je 0x119d8
movq -0x58(%rbp), %rcx
movq %rax, %rdx
cmpq %rcx, %rax
jne 0x119b7
movq -0x48(%rbp), %rdx
movq -0x8(%rdx), %rdx
addq %r14, %rdx
movq -0x70(%rbp), %rsi
movl (%rsi), %esi
movq -0x8(%rdx), %rdx
cmpl (%rdx), %esi
jle 0x119e6
movq %r13, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x1383c
movq -0x60(%rbp), %rax
jmp 0x1199a
leaq -0x70(%rbp), %rsi
movq %rbx, %rdi
callq 0x13876
jmp 0x11a0f
leaq -0xf8(%rbp), %rdx
movq %rax, (%rdx)
movq %rcx, 0x8(%rdx)
movups -0x50(%rbp), %xmm0
movups %xmm0, 0x10(%rdx)
leaq -0xb8(%rbp), %rdi
leaq -0x70(%rbp), %rcx
movq %rbx, %rsi
callq 0x13784
cmpb $0x1, 0xf2(%r12)
jne 0x11a3a
addq $0x18, %r12
leaq 0x6814(%rip), %rsi      # 0x18239
movq %r12, %rdi
callq 0xb93e
pushq $0x1
popq %rsi
movq %rax, %rdi
callq 0xb9b2
jmp 0x11a46
movl $0x4, 0xcc(%r12)
xorl %ebx, %ebx
jmp 0x11d6a
cmpb $0x1, 0xf1(%rdi)
jne 0x11adc
movq %r14, 0x120(%rdi)
incl 0x8(%r14)
movl $0x4, 0xcc(%r14)
movl $0x2, 0xcc(%rdi)
addq $0x18, %rdi
leaq 0x67b4(%rip), %rsi      # 0x18239
callq 0xb93e
leaq 0x7b17(%rip), %rsi      # 0x195a8
leaq -0x90(%rbp), %rdx
movq %rax, %rdi
callq 0xcdbe
addq $0xd0, %r14
movq %rax, %rdi
movq %r14, %rsi
callq 0xbe84
jmp 0x11d4e
addq $0x18, %r12
leaq 0x7b32(%rip), %rsi      # 0x195f4
movq %r12, %rdi
callq 0xb93e
xorl %ebx, %ebx
pushq $0x1
popq %rsi
movq %rax, %rdi
callq 0xb9b2
jmp 0x11d6a
movq 0xe0(%r14), %rax
movl 0xe8(%r14), %ecx
movq %rax, -0x70(%rbp)
movl %ecx, -0x68(%rbp)
testl %ecx, %ecx
jne 0x11b70
leaq 0xe0(%r14), %rbx
leaq 0x128(%r14), %r15
leaq 0x6842(%rip), %r13      # 0x1834c
movq %r15, %rdi
callq 0xe798
testb %al, %al
je 0x11b49
movq %r15, %rdi
movq %r13, %rsi
callq 0xd9a0
testb %al, %al
je 0x11b0a
leaq 0x138(%r14), %rsi
movl 0x140(%r14), %eax
movl %eax, -0x68(%rbp)
movq 0x138(%r14), %rax
movq %rax, -0x70(%rbp)
movq %rbx, %rdi
callq 0xd3d0
cmpl $0x0, -0x68(%rbp)
movq -0x38(%rbp), %r15
jne 0x11b70
movq %rbx, %rdi
callq 0x16ce9
movq 0xe0(%r14), %rax
movl 0xe8(%r14), %ecx
movq %rax, -0x70(%rbp)
movl %ecx, -0x68(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0x70(%rbp), %rsi
pushq $-0x1
popq %rdx
callq 0xd636
movl $0x20158, %edi          # imm = 0x20158
addq 0x18(%r15), %rdi
callq 0xd764
movl $0x20128, %edi          # imm = 0x20128
addq 0x18(%r15), %rdi
leaq -0x60(%rbp), %rsi
callq 0x14a7e
movq %rax, %rbx
movq 0x18(%r15), %rdi
leaq 0x20130(%rdi), %r15
addq $0x20158, %rdi          # imm = 0x20158
callq 0xa220
movq -0x30(%rbp), %rdi
cmpq %r15, %rbx
je 0x11c84
incl 0x8(%rdi)
movl $0x1, 0xcc(%rdi)
movslq 0x38(%rdi), %rax
imulq $0xa0, %rax, %rdi
movq -0x80(%rbp), %rax
addq 0x40(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1373a
addq $0x18, %r14
leaq 0x799c(%rip), %rsi      # 0x19597
movq %r14, %rdi
callq 0xb93e
pushq $-0x1
popq %rsi
movq %rax, %rdi
callq 0xb9b2
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
testb $0x4, 0x20(%rax)
je 0x11d37
callq 0x16a89
movq 0xe3a4(%rip), %rdi      # 0x1ffd0
movq %rax, %rsi
callq 0xa2e0
leaq 0x799c(%rip), %rsi      # 0x195d7
movq %rax, %rdi
callq 0xa2e0
movq %rax, %r14
leaq -0xb8(%rbp), %rdi
leaq -0x90(%rbp), %rsi
pushq $-0x1
popq %rdx
callq 0xd636
leaq -0xb8(%rbp), %rsi
movq %r14, %rdi
callq 0xa280
movq %rax, %rdi
callq 0xa160
leaq -0xb8(%rbp), %rdi
callq 0xa130
jmp 0x11d37
cmpb $0x1, 0xf0(%rdi)
jne 0x11c98
movq %r14, 0x120(%rdi)
incl 0x8(%r14)
addq $0x18, %rdi
leaq 0x6596(%rip), %rsi      # 0x18239
callq 0xb93e
leaq 0x78f6(%rip), %rsi      # 0x195a5
leaq -0x70(%rbp), %rdx
movq %rax, %rdi
callq 0xcdbe
leaq 0x78e6(%rip), %rsi      # 0x195a8
leaq -0x90(%rbp), %rdx
movq %rax, %rdi
callq 0xcdbe
leaq 0xd0(%r14), %rsi
movq %rax, %rdi
callq 0xbe84
movl $0x20158, %edi          # imm = 0x20158
movq -0x38(%rbp), %rax
addq 0x18(%rax), %rdi
callq 0xd764
movl $0x20128, %edi          # imm = 0x20128
movq -0x38(%rbp), %rax
addq 0x18(%rax), %rdi
leaq -0x60(%rbp), %rsi
callq 0x13630
movq %r14, (%rax)
movl $0x20158, %edi          # imm = 0x20158
movq -0x38(%rbp), %rax
addq 0x18(%rax), %rdi
callq 0xa220
incl 0x8(%r14)
movl $0x4, 0xcc(%r14)
movq -0x30(%rbp), %rax
andl $0x0, 0xcc(%rax)
leaq -0x60(%rbp), %rdi
callq 0xa130
cmpq %r15, %rbx
movq -0x38(%rbp), %r15
je 0x11d4e
pushq $-0x3
popq %rbx
jmp 0x11d6a
movq 0x18(%r15), %rax
xorl %ebx, %ebx
testb $0x20, 0x20(%rax)
je 0x11d6a
callq 0x16a40
movq -0x30(%rbp), %rcx
movq %rax, 0x1e0(%rcx)
movq -0x98(%rbp), %rdi
callq 0xa220
movl %ebx, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x11ed0
jmp 0x11dc8
jmp 0x11db7
movq %rax, %rbx
leaq -0xb8(%rbp), %rdi
callq 0xa130
jmp 0x11dba
jmp 0x11db7
jmp 0x11db7
jmp 0x11ed0
jmp 0x11ed0
jmp 0x11ed0
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0xa130
jmp 0x11ed3
movq %rax, %rbx
cmpl $0x1, %edx
jne 0x11ed3
movq %rbx, %rdi
callq 0xa110
leaq -0x30(%rbp), %rsi
movq (%rsi), %rax
incl 0x8(%rax)
movl $0x1, 0xcc(%rax)
movslq 0x38(%rax), %rax
imulq $0xa0, %rax, %rdi
movq -0x80(%rbp), %rax
addq 0x40(%rax), %rdi
callq 0x1373a
addq $0x18, %r12
leaq 0x7761(%rip), %rsi      # 0x19574
movq %r12, %rdi
callq 0xb93e
pushq $-0x1
popq %rsi
movq %rax, %rdi
callq 0xb9b2
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
testb $0x4, 0x20(%rax)
je 0x11e8a
callq 0x16a89
movq 0xe190(%rip), %rdi      # 0x1ffd0
movq %rax, %rsi
callq 0xa2e0
leaq 0x772f(%rip), %rsi      # 0x1957e
movq %rax, %rdi
callq 0xa2e0
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
leaq -0x90(%rbp), %rsi
pushq $-0x1
popq %rdx
callq 0xd636
leaq -0x60(%rbp), %rsi
movq %rbx, %rdi
callq 0xa280
movq %rax, %rdi
callq 0xa160
leaq -0x60(%rbp), %rdi
callq 0xa130
callq 0xa430
jmp 0x11d49
jmp 0x11ea6
jmp 0x11ed0
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0xa130
jmp 0x11ea9
movq %rax, %rbx
callq 0xa430
jmp 0x11ed3
movq %rax, %rdi
callq 0xd334
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0xa130
jmp 0x11edf
jmp 0x11ed0
jmp 0x11ed0
jmp 0x11ed0
jmp 0x11ed0
jmp 0x11ed0
movq %rax, %rbx
movq -0x98(%rbp), %rdi
callq 0xa220
movq %rbx, %rdi
callq 0xa470
nop