// File: /workspace/github_cpp_makefile_star2_O2/lega911[P]ijson/src/server.cpp
// Function: Loop::_add_worker(Slice, Connect*)
int Loop::_add_worker(Slice name, Connect *worker) {
    if(!name.empty() && name.ptr()[0] == '/') name.remove(1);
    QueueLine *ql = server->get_queue(name, true);
    Queue *q;

    if(worker->info) {
        try {
            ql->info.set(worker->info);
            json::unescape(ql->info);
        } catch (Exception &e) {
            ql->info.clear();
        };
    }

    if(!worker->connection_id) generate_id(worker->connection_id);

    if(!worker->worker_mode && worker->worker_item && worker->worker_sub_name.compare(name)) {
        worker->worker_item->pop();

        GC gc;
        for(auto const &it : worker->direct_message) {
            gc.add(it, GC::DirectMessage);
        }
        worker->direct_message.clear();
    }

    if(!worker->direct_message.empty()) {
        worker->write_mode(true);
        return 1;
    }

    Message *msg = NULL;
    GC gc;
    int result;

    ql->last_worker = get_time_sec();
    std::string sid;

    ql->mutex.lock();
    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->clients.begin();
        while (it != q->clients.end()) {
            msg = *it;

            if(msg->required_worker) {
                if(msg->required_worker != worker->connection_id) {
                    msg = NULL;
                    it++;
                    continue;
                }
            }
            it = q->clients.erase(it);
            gc.add(msg, GC::Message);

            if(!msg->conn) break;
            Connect *client = msg->conn;
            if(client->is_closed()) {
                if(server->log & 8) std::cout << ltime() << "closed client " << msg->conn << std::endl;
                msg = NULL;
                continue;
            }

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

            if(skip) {
                if(server->log & 8) std::cout << ltime() << "client is busy!!! " << client << std::endl;
                msg = NULL;
                continue;
            }

            if(worker->noid) break;
            Slice id = client->id;
            if(id.empty()) {
                try {
                    while(client->json.scan()) {
                        if(client->json.key == "id") {
                            id = client->json.value;
                            client->id.set(id);
                            break;
                        }
                    }
                } catch (const error::InvalidData &e) {
                    client->send.status("400 No Id")->done(-1);
                    if(server->log & 4) std::cout << ltime() << "400 no id " << name.as_string() << std::endl;
                    msg = NULL;
                    continue;
                }
                if(id.empty()) {
                    generate_id(client->id);
                    id = client->id;
                };
            }
            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) {
                // colision id
                if(server->log & 2) std::cout << ltime() << "collision id\n";
                client->send.status("400 Collision Id")->done(-1);
                client->status = Status::net;
                msg = NULL;
                continue;
            }
            break;
        }
        if(msg) break;
    }

    if(msg) {
        if(!msg->conn) {
            if(worker->worker_mode) worker->status = Status::worker_mode_async;
            else worker->status = Status::net;
            worker->send.status("200 OK")->header("Name", msg->name)->header("Async", Slice("true"))->done(*msg->buf);
        } else {
            auto client = msg->conn;
            if(worker->fail_on_disconnect) {
                worker->client = client;
                worker->client->link();
            }
            if(worker->noid) {
                worker->status = Status::worker_wait_result;
                worker->send.status("200 OK")->header("Name", client->name)->done(client->body);
            } else {
                worker->send.status("200 OK")->header("Id", client->id)->header("Name", client->name)->done(client->body);
                server->wait_lock.lock();
                server->wait_response[sid] = client;
                server->wait_lock.unlock();
                client->link();
                worker->status = Status::net;
            }
            result = 1;
        }
        if(server->log & 32) worker->start_time = get_time();
    } else {
        Queue *q0 = &ql->queue[_nloop];
        if(q0->workers.size()) {
            auto it=q0->workers.begin();
            while (it != q0->workers.end()) {
                Connect *conn = *it;
                if(conn->is_closed() || conn == worker) {
                    it = q0->workers.erase(it);
                    conn->unlink();
                } else {
                    it++;
                }
            }
        }

        q0->workers.push_back(worker);
        worker->link();
        worker->status = Status::worker_wait_job;
        result = 0;
    }

    if(!worker->worker_item) {
        if(worker->worker_mode || worker->noid || worker->status == Status::worker_wait_job) worker->worker_item = ql->workers.push(worker);
        if(!worker->worker_mode && worker->noid) worker->worker_sub_name.set(name);
    }

    ql->mutex.unlock();

    return result;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rcx, %rbx
movq %rdi, %r13
movq %rsi, -0x98(%rbp)
movl %edx, -0x90(%rbp)
movq %rcx, -0x30(%rbp)
testl %edx, %edx
je 0x1229c
cmpb $0x2f, (%rsi)
jne 0x1229e
pushq $0x1
popq %rax
testl %edx, %edx
cmovlel %edx, %eax
cltq
addq %rax, %rsi
movq %rsi, -0x98(%rbp)
subl %eax, %edx
movl %edx, -0x90(%rbp)
jmp 0x1229e
xorl %edx, %edx
movq 0x18(%r13), %rdi
pushq $0x1
popq %rcx
movl %ecx, %r15d
callq 0x104d6
movq %rax, -0x38(%rbp)
cmpl $0x0, 0x170(%rbx)
je 0x122db
leaq 0x168(%rbx), %rsi
movq -0x38(%rbp), %rax
leaq 0x48(%rax), %r14
movq %r14, %rdi
callq 0xd3d0
movq %r14, %rdi
xorl %esi, %esi
callq 0xe598
cmpl $0x0, 0x108(%rbx)
jne 0x122f0
leaq 0x100(%rbx), %rdi
callq 0x16ce9
cmpb $0x0, 0xf3(%rbx)
jne 0x123cc
cmpq $0x0, 0x178(%rbx)
je 0x123cc
leaq 0x180(%rbx), %rdi
leaq -0x98(%rbp), %rsi
callq 0xddfa
testl %eax, %eax
je 0x123cc
movq %r13, -0x88(%rbp)
movq 0x178(%rbx), %rdi
callq 0x1700a
xorps %xmm0, %xmm0
leaq -0xe0(%rbp), %rax
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq 0x1a0(%rbx), %r13
movq 0x1b0(%rbx), %r14
movq 0x1b8(%rbx), %rax
movq %rax, -0x40(%rbp)
movq 0x1c0(%rbx), %r15
pushq $0x2
popq %r12
cmpq %r15, %r13
je 0x123a9
movq (%r13), %rsi
leaq -0xe0(%rbp), %rdi
movl %r12d, %edx
callq 0x16d24
addq $0x8, %r13
cmpq %r14, %r13
jne 0x1236f
movq -0x40(%rbp), %rax
movq 0x8(%rax), %r13
addq $0x8, %rax
movq %rax, -0x40(%rbp)
leaq 0x200(%r13), %r14
jmp 0x1236f
leaq 0x190(%rbx), %rdi
callq 0x13574
leaq -0xe0(%rbp), %rdi
callq 0xd2dc
pushq $0x1
popq %r15
movq -0x88(%rbp), %r13
movq 0x1c0(%rbx), %rax
cmpq 0x1a0(%rbx), %rax
je 0x123ef
movq %rbx, %rdi
movl %r15d, %ebx
movl %r15d, %esi
callq 0xa8dc
jmp 0x12ba3
xorps %xmm0, %xmm0
movaps %xmm0, -0x110(%rbp)
andq $0x0, -0x100(%rbp)
callq 0x16a69
movl %eax, %eax
movq -0x38(%rbp), %rcx
movq %rax, 0x10(%rcx)
leaq -0xd0(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x18(%rcx), %rdi
movq %rdi, -0xe8(%rbp)
callq 0xd764
movl 0x4(%r13), %eax
movl %eax, -0x4c(%rbp)
pushq $-0x1
popq %rax
pushq $0x1
popq %rcx
movl %ecx, -0x44(%rbp)
movl %eax, %ecx
movq %r13, -0x88(%rbp)
movq 0x18(%r13), %rax
cmpl 0x28(%rax), %ecx
jge 0x12a25
movl %ecx, %eax
cmpl 0x4(%r13), %ecx
je 0x1286c
testl %eax, %eax
movl -0x4c(%rbp), %ecx
movl %eax, -0x48(%rbp)
cmovnsl %eax, %ecx
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rax
movl %ecx, -0x4c(%rbp)
movslq %ecx, %rcx
imulq $0xa0, %rcx, %rcx
leaq (%rax,%rcx), %rdx
addq $0x50, %rdx
movq 0x10(%rdx), %rax
movq %rax, -0x70(%rbp)
movq 0x18(%rdx), %rcx
movq %rcx, -0x68(%rbp)
movq 0x20(%rdx), %rcx
movq %rcx, -0x60(%rbp)
movq %rdx, -0x40(%rbp)
movq 0x28(%rdx), %rcx
movq %rcx, -0x58(%rbp)
movq -0x40(%rbp), %rcx
cmpq 0x30(%rcx), %rax
je 0x12869
movq (%rax), %rbx
cmpl $0x0, 0x10(%rbx)
leaq -0xb8(%rbp), %r14
je 0x124ff
leaq 0x8(%rbx), %rdi
movq -0x30(%rbp), %rsi
movl $0x100, %eax            # imm = 0x100
addq %rax, %rsi
callq 0x135aa
testb %al, %al
je 0x124fb
movq %r14, %rdi
leaq -0x70(%rbp), %rsi
xorl %edx, %edx
callq 0x135b8
jmp 0x127b9
movq -0x70(%rbp), %rax
movq %rax, -0x150(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x148(%rbp)
movups -0x60(%rbp), %xmm0
movups %xmm0, -0x140(%rbp)
movq %r14, %rdi
movq -0x40(%rbp), %rsi
leaq -0x150(%rbp), %rdx
callq 0x135f2
movups -0xb8(%rbp), %xmm0
movups -0xa8(%rbp), %xmm1
movaps %xmm1, -0x60(%rbp)
movaps %xmm0, -0x70(%rbp)
leaq -0x110(%rbp), %rdi
movq %rbx, %rsi
pushq $0x1
popq %rdx
callq 0x16d24
movq 0x18(%rbx), %r14
testq %r14, %r14
je 0x1287d
cmpl $-0x1, (%r14)
je 0x1272a
cmpl $0x4, 0xcc(%r14)
jne 0x12779
leaq 0x50(%r14), %r12
movq %r12, %rdi
callq 0xd764
cmpl $0x4, 0xcc(%r14)
jne 0x12771
movl $0x5, 0xcc(%r14)
movq %r12, %rdi
callq 0xa220
movq -0x30(%rbp), %rax
cmpb $0x0, 0xf1(%rax)
leaq -0xb8(%rbp), %r15
jne 0x1287d
movq 0xe0(%r14), %rax
movl 0xe8(%r14), %ecx
movq %rax, -0x80(%rbp)
movl %ecx, -0x78(%rbp)
testl %ecx, %ecx
jne 0x12652
leaq 0xe0(%r14), %r15
leaq 0x128(%r14), %r12
movq %r12, %rdi
callq 0xe798
testb %al, %al
je 0x12628
movq %r12, %rdi
leaq 0x5d4e(%rip), %rsi      # 0x1834c
callq 0xd9a0
testb %al, %al
je 0x125e8
movl 0x140(%r14), %eax
movl %eax, -0x78(%rbp)
movq 0x138(%r14), %rax
movq %rax, -0x80(%rbp)
movq %r15, %rdi
leaq -0x80(%rbp), %rsi
callq 0xd3d0
movq %r15, %rdi
cmpl $0x0, -0x78(%rbp)
leaq -0xb8(%rbp), %r15
jne 0x12652
callq 0x16ce9
movq 0xe0(%r14), %rax
movl 0xe8(%r14), %ecx
movq %rax, -0x80(%rbp)
movl %ecx, -0x78(%rbp)
movq %r15, %rdi
leaq -0x80(%rbp), %rsi
pushq $-0x1
popq %rdx
callq 0xd636
leaq -0xe0(%rbp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0xa360
movq %r15, %rdi
callq 0xa130
movq -0x88(%rbp), %r13
movq 0x18(%r13), %rdi
movl $0x20158, %eax          # imm = 0x20158
addq %rax, %rdi
callq 0xd764
movq 0x18(%r13), %rdi
movl $0x20128, %eax          # imm = 0x20128
addq %rax, %rdi
movq %r12, %rsi
callq 0x14a7e
movq %rax, %r12
movq 0x18(%r13), %rdi
leaq 0x20130(%rdi), %rax
movq %rax, -0xf0(%rbp)
addq $0x20158, %rdi          # imm = 0x20158
callq 0xa220
cmpq -0xf0(%rbp), %r12
je 0x12875
movq 0x18(%r13), %rax
testb $0x2, 0x20(%rax)
je 0x12702
callq 0x16a89
movq 0xd8e5(%rip), %rdi      # 0x1ffd0
movq %rax, %rsi
callq 0xa2e0
movq %rax, %rdi
leaq 0x6e8c(%rip), %rsi      # 0x19589
callq 0xa2e0
leaq 0x18(%r14), %rdi
leaq 0x6e8a(%rip), %rsi      # 0x19597
callq 0xb93e
movq %rax, %rdi
pushq $-0x1
popq %rsi
callq 0xb9b2
andl $0x0, 0xcc(%r14)
jmp 0x127b9
movq 0x18(%r13), %rax
testb $0x8, 0x20(%rax)
je 0x127b9
callq 0x16a89
movq 0xd88c(%rip), %rdi      # 0x1ffd0
movq %rax, %rsi
callq 0xa2e0
movq %rax, %rdi
leaq 0x6dfc(%rip), %rsi      # 0x19552
callq 0xa2e0
movq 0x18(%rbx), %rsi
movq %rax, %rdi
callq 0xa230
movq %rax, %rdi
callq 0xa160
jmp 0x127b9
movq %r12, %rdi
callq 0xa220
movq 0x18(%r13), %rax
testb $0x8, 0x20(%rax)
je 0x127b9
callq 0x16a89
movq 0xd841(%rip), %rdi      # 0x1ffd0
movq %rax, %rsi
callq 0xa2e0
movq %rax, %rdi
leaq 0x6dc0(%rip), %rsi      # 0x19561
callq 0xa2e0
movq %rax, %rdi
movq %r14, %rsi
callq 0xa230
movq %rax, %rdi
callq 0xa160
movq -0x70(%rbp), %rax
jmp 0x124b1
jmp 0x127c4
movq %rax, %r12
cmpl $0x2, %edx
jne 0x12c30
movq %r12, %rdi
callq 0xa110
addq $0x18, %r14
movq %r14, %rdi
leaq 0x6d8e(%rip), %rsi      # 0x19574
callq 0xb93e
movq %rax, %rdi
pushq $-0x1
popq %rsi
callq 0xb9b2
movq 0x18(%r13), %rax
testb $0x4, 0x20(%rax)
je 0x1285f
callq 0x16a89
movq 0xd7c4(%rip), %rdi      # 0x1ffd0
movq %rax, %rsi
callq 0xa2e0
movq %rax, %rdi
leaq 0x6d60(%rip), %rsi      # 0x1957e
callq 0xa2e0
movq %rax, %rbx
leaq -0xb8(%rbp), %rdi
leaq -0x98(%rbp), %rsi
pushq $-0x1
popq %rdx
callq 0xd636
movq %rbx, %rdi
leaq -0xb8(%rbp), %rsi
callq 0xa280
movq %rax, %rdi
callq 0xa160
leaq -0xb8(%rbp), %rdi
callq 0xa130
callq 0xa430
jmp 0x127b9
movl -0x48(%rbp), %eax
incl %eax
movl %eax, %ecx
jmp 0x1244c
testq %rbx, %rbx
movl -0x48(%rbp), %eax
je 0x1286c
movq 0x18(%rbx), %r14
movq -0x30(%rbp), %r12
testq %r14, %r14
je 0x128fc
cmpb $0x1, 0xf0(%r12)
jne 0x128a1
movq %r14, 0x120(%r12)
incl 0x8(%r14)
cmpb $0x1, 0xf1(%r12)
jne 0x12970
movl $0x2, 0xcc(%r12)
addq $0x18, %r12
leaq 0x5972(%rip), %rsi      # 0x18239
movq %r12, %rdi
callq 0xb93e
leaq 0xb8(%r14), %rdx
leaq 0x6ccb(%rip), %rsi      # 0x195a8
movq %rax, %rdi
callq 0xcdbe
addq $0xd0, %r14
movq %rax, %rdi
movq %r14, %rsi
callq 0xbe84
jmp 0x129fd
xorl %eax, %eax
cmpb $0x0, 0xf3(%r12)
pushq $0x6
popq %rcx
cmovel %eax, %ecx
movl %ecx, 0xcc(%r12)
addq $0x18, %r12
leaq 0x5919(%rip), %rsi      # 0x18239
movq %r12, %rdi
callq 0xb93e
leaq 0x28(%rbx), %rdx
leaq 0x6c75(%rip), %rsi      # 0x195a8
movq %rax, %rdi
callq 0xcdbe
leaq 0x620e(%rip), %rcx      # 0x18b50
leaq -0x70(%rbp), %rdx
movq %rcx, (%rdx)
movl $0x4, 0x8(%rdx)
leaq 0x6c56(%rip), %rsi      # 0x195ad
movq %rax, %rdi
callq 0xcdbe
movq 0x20(%rbx), %rsi
movq %rax, %rdi
callq 0xbe84
jmp 0x129fd
leaq 0x18(%r12), %rdi
leaq 0x58bd(%rip), %rsi      # 0x18239
callq 0xb93e
leaq 0xe0(%r14), %rdx
leaq 0x6c16(%rip), %rsi      # 0x195a5
movq %rax, %rdi
callq 0xcdbe
leaq 0xb8(%r14), %rdx
leaq 0x6c03(%rip), %rsi      # 0x195a8
movq %rax, %rdi
callq 0xcdbe
leaq 0xd0(%r14), %rsi
movq %rax, %rdi
callq 0xbe84
movl $0x20158, %edi          # imm = 0x20158
addq 0x18(%r13), %rdi
callq 0xd764
movl $0x20128, %edi          # imm = 0x20128
addq 0x18(%r13), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x13630
movq %r14, (%rax)
movl $0x20158, %edi          # imm = 0x20158
addq 0x18(%r13), %rdi
callq 0xa220
incl 0x8(%r14)
andl $0x0, 0xcc(%r12)
movq 0x18(%r13), %rax
testb $0x20, 0x20(%rax)
jne 0x12a10
movq -0x30(%rbp), %rsi
jmp 0x12b12
callq 0x16a40
movq -0x30(%rbp), %rsi
movq %rax, 0x1e0(%rsi)
jmp 0x12b12
movslq 0x4(%r13), %rax
imulq $0xa0, %rax, %rbx
movq -0x38(%rbp), %rax
addq 0x40(%rax), %rbx
movq %rbx, %rdi
callq 0xd74e
testq %rax, %rax
je 0x12aee
movq 0x10(%rbx), %rax
leaq -0x70(%rbp), %r14
movq %rax, (%r14)
movq 0x18(%rbx), %rcx
movq %rcx, 0x8(%r14)
movq 0x20(%rbx), %rcx
movq %rcx, 0x10(%r14)
movq 0x28(%rbx), %rcx
movq %rcx, 0x18(%r14)
leaq -0xb8(%rbp), %r15
leaq -0x130(%rbp), %r12
cmpq 0x30(%rbx), %rax
je 0x12aee
movq (%rax), %r13
cmpl $-0x1, (%r13)
je 0x12a9f
cmpq -0x30(%rbp), %r13
je 0x12a9f
movq %r15, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x136d8
jmp 0x12ae8
movq %rax, -0x130(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x128(%rbp)
movups -0x60(%rbp), %xmm0
movups %xmm0, -0x120(%rbp)
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x1369a
movups -0xb8(%rbp), %xmm0
movups -0xa8(%rbp), %xmm1
movaps %xmm1, -0x60(%rbp)
movaps %xmm0, -0x70(%rbp)
movq %r13, %rdi
callq 0xa84c
movq -0x70(%rbp), %rax
jmp 0x12a7a
leaq -0x30(%rbp), %rsi
movq %rbx, %rdi
callq 0x13712
movq -0x30(%rbp), %rsi
incl 0x8(%rsi)
movl $0x1, 0xcc(%rsi)
movl $0x0, -0x44(%rbp)
cmpq $0x0, 0x178(%rsi)
jne 0x12b7c
movb 0xf3(%rsi), %al
testb %al, %al
jne 0x12b38
cmpb $0x0, 0xf1(%rsi)
jne 0x12b38
cmpl $0x1, 0xcc(%rsi)
jne 0x12b56
movq -0x38(%rbp), %rdi
addq $0x58, %rdi
callq 0x1706e
movq -0x30(%rbp), %rsi
movq %rax, 0x178(%rsi)
movb 0xf3(%rsi), %al
testb $0x1, %al
jne 0x12b7c
cmpb $0x1, 0xf1(%rsi)
jne 0x12b7c
addq $0x180, %rsi            # imm = 0x180
leaq -0x98(%rbp), %rax
movq %rsi, %rdi
movq %rax, %rsi
callq 0xd3d0
movq -0xe8(%rbp), %rdi
callq 0xa220
leaq -0xe0(%rbp), %rdi
callq 0xa130
leaq -0x110(%rbp), %rdi
callq 0xd2dc
movl -0x44(%rbp), %ebx
movl %ebx, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x12c2d
movq %rax, %r12
leaq -0xb8(%rbp), %rdi
callq 0xa130
jmp 0x12bcf
jmp 0x12bcc
movq %rax, %r12
callq 0xa430
jmp 0x12c30
movq %rax, %rdi
callq 0xd334
jmp 0x12c2d
jmp 0x12c2d
jmp 0x12c2d
movq %rax, %r12
jmp 0x12c3c
jmp 0x12c2d
jmp 0x12c2d
jmp 0x12c2d
jmp 0x12c2d
movq %rax, %r12
cmpl $0x1, %edx
jne 0x12c48
movq %r12, %rdi
callq 0xa110
movq -0x38(%rbp), %rax
andl $0x0, 0x50(%rax)
callq 0xa430
jmp 0x122db
jmp 0x12c2d
jmp 0x12c2d
jmp 0x12c2d
jmp 0x12c2d
movq %rax, %r12
leaq -0xe0(%rbp), %rdi
jmp 0x12c43
jmp 0x12c2d
jmp 0x12c2d
jmp 0x12c2d
movq %rax, %r12
leaq -0xe0(%rbp), %rdi
callq 0xa130
leaq -0x110(%rbp), %rdi
callq 0xd2dc
movq %r12, %rdi
callq 0xa470