// File: /workspace/github_cpp_makefile_star2_O2/cybozu[P]yrmcds/src/memcache/sockets.cpp
// Function: yrmcds::memcache::memcache_socket::memcache_socket(int, std::function<cybozu::worker* ()> const&, cybozu::hash_map<yrmcds::memcache::object>&, std::vector<yrmcds::memcache::repl_socket*, std::allocator<yrmcds::memcache::repl_socket*>> const&)
memcache_socket::memcache_socket(int fd,
                                 const std::function<cybozu::worker*()>& finder,
                                 cybozu::hash_map<object>& hash,
                                 const std::vector<repl_socket*>& slaves)
    : cybozu::tcp_socket(fd),
      m_busy(false),
      m_finder(finder),
      m_hash(hash),
      m_pending(0),
      m_slaves_origin(slaves) {
    m_slaves.reserve(MAX_SLAVES);
    g_stats.curr_connections.fetch_add(1, relaxed);
    g_stats.total_connections.fetch_add(1, relaxed);

    m_recvjob = [this](cybozu::dynbuf& buf) {
        // set lock context for objects.
        with_fd([](int fd) -> bool {
            g_context = fd;
            return true;
        });

        // load pending data
        if( ! m_pending.empty() ) {
            buf.append(m_pending.data(), m_pending.size());
            m_pending.reset();
        }

        while( true ) {
            auto res = receive(buf, MAX_RECVSIZE);
            if( res == recv_result::AGAIN )
                break;
            if( res == recv_result::RESET || res == recv_result::NONE ) {
                buf.reset();
                unlock_all();
                break;
            }

            const char* head = buf.data();
            std::size_t len = buf.size();
            while( len > 0 ) {
                if( mc::is_binary_request(head) ) {
                    mc::binary_request parser(head, len);
                    std::size_t c = parser.length();
                    if( c == 0 ) break;
                    head += c;
                    len -= c;
                    cmd_bin(parser);
                } else {
                    mc::text_request parser(head, len);
                    std::size_t c = parser.length();
                    if( c == 0 ) break;
                    head += c;
                    len -= c;
                    cmd_text(parser);
                }
            }
            if( len > MAX_REQUEST_LENGTH ) {
                cybozu::logger::warning() << "denied too large request of "
                                          << len << " bytes.";
                buf.reset();
                unlock_all();
                invalidate_and_close();
                break;
            }
            buf.erase(head - buf.data());
        }

        // recv returns EAGAIN, or some error happens.
        if( buf.size() > 0 )
            m_pending.append(buf.data(), buf.size());

        m_busy.store(false, std::memory_order_release);
    };

    m_sendjob = [this](cybozu::dynbuf&) {
        with_fd([=](int fd) -> bool {
            return write_pending_data(fd);
        });
    };
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r8, %r14
movq %rcx, %r15
movq %rdx, %r12
movq %rdi, %rbx
xorl %edx, %edx
callq 0x1bcf6
leaq 0x23ee8(%rip), %rax     # 0x3a958
movq %rax, (%rbx)
movb $0x0, 0x100(%rbx)
movq %r12, 0x108(%rbx)
movq %r15, 0x110(%rbx)
andq $0x0, 0x138(%rbx)
leaq 0x118(%rbx), %r15
xorps %xmm0, %xmm0
movups %xmm0, 0x118(%rbx)
movups %xmm0, 0x121(%rbx)
movq %r14, 0x140(%rbx)
leaq 0x148(%rbx), %r13
leaq 0x160(%rbx), %r12
leaq 0x180(%rbx), %r14
movups %xmm0, 0x148(%rbx)
movups %xmm0, 0x158(%rbx)
movups %xmm0, 0x168(%rbx)
movups %xmm0, 0x178(%rbx)
movups %xmm0, 0x188(%rbx)
movups %xmm0, 0x198(%rbx)
movups %xmm0, 0x1a8(%rbx)
pushq $0x5
popq %rsi
movq %r13, %rdi
callq 0x14ecc
leaq 0x253b9(%rip), %rax     # 0x3bec0
lock
incq 0x100(%rax)
lock
incq 0x108(%rax)
leaq 0x10(%rsp), %r15
andq $0x0, 0x8(%r15)
movq %rbx, (%r15)
movaps (%r15), %xmm0
movaps %xmm0, (%rsp)
movups (%r12), %xmm1
movaps %xmm1, (%r15)
movups %xmm0, (%r12)
movaps 0x170(%rbx), %xmm0
leaq 0x2000(%rip), %rax      # 0x18b48
movq %rax, 0x170(%rbx)
movaps %xmm0, 0x10(%r15)
leaq 0x1df7(%rip), %rax      # 0x18952
movq %rax, 0x178(%rbx)
movq %r15, %rdi
callq 0x8d90
andq $0x0, 0x8(%r15)
movq %rbx, (%r15)
movaps (%r15), %xmm0
movaps %xmm0, (%rsp)
movups (%r14), %xmm1
movaps %xmm1, (%r15)
movups %xmm0, (%r14)
movaps 0x190(%rbx), %xmm0
leaq 0x2034(%rip), %rax      # 0x18bc8
movq %rax, 0x190(%rbx)
movaps %xmm0, 0x10(%r15)
leaq 0x1fc7(%rip), %rax      # 0x18b6e
movq %rax, 0x198(%rbx)
leaq 0x10(%rsp), %rdi
callq 0x8d90
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbp
leaq 0x1a0(%rbx), %rdi
callq 0x1b162
movq %r14, %rdi
callq 0x8d90
movq %r12, %rdi
callq 0x8d90
movq %r13, %rdi
callq 0x15606
movq %r15, %rdi
callq 0x8dac
movq %rbx, %rdi
callq 0x12ee2
movq %rbp, %rdi
callq 0x7990