// File: /workspace/github_cpp_makefile_star2_O3/cybozu[P]yrmcds/src/memcache/sockets.cpp
// Function: yrmcds::memcache::repl_client_socket::on_readable(int)
bool repl_client_socket::on_readable(int fd) {
    // This function is executed in the same thread as the function that sends
    // heartbeats. If this function takes a very long time, no heartbeats will
    // be sent, and this process will be judged dead by the master. To prevent
    // this misjudgment, set an upper limit on the number of iterations.
    static const size_t MAX_ITER = 50;

    size_t n_iter = 0;
    while( true ) {
        char* p = m_recvbuf.prepare(MAX_RECVSIZE);
        ssize_t n = ::recv(fd, p, MAX_RECVSIZE, 0);
        if( n == -1 ) {
            if( errno == EAGAIN || errno == EWOULDBLOCK )
                break;
            if( errno == EINTR )
                continue;
            if( errno == ECONNRESET ) {
                cybozu::logger::warning() << "The connection to master has been reset.";
                m_reactor->quit();
                return invalidate();
            }
            cybozu::throw_unix_error(errno, "recv");
        }
        if( n == 0 ) {
            cybozu::logger::info() << "The connection to master has been closed.";
            m_reactor->quit();
            return invalidate();
        }
        m_recvbuf.consume(n);

        std::size_t c = repl_recv(m_recvbuf.data(), m_recvbuf.size(), m_hash);
        m_recvbuf.erase(c);

        n_iter++;
        if (n_iter >= MAX_ITER) {
            m_reactor->add_readable(*this);
            return true;
        }
    }
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x18, %rsp
movl %esi, %ebp
movq %rdi, %rbx
leaq 0x108(%rdi), %r14
xorl %r15d, %r15d
movq 0x118(%rbx), %rax
movq 0x128(%rbx), %rsi
subq %rsi, %rax
cmpq $0x1fffff, %rax         # imm = 0x1FFFFF
ja 0x1bb20
movl $0x200000, %esi         # imm = 0x200000
movq %r14, %rdi
callq 0x1516c
movq 0x128(%rbx), %rsi
addq (%r14), %rsi
movl $0x200000, %edx         # imm = 0x200000
movl %ebp, %edi
xorl %ecx, %ecx
callq 0x7130
cmpq $-0x1, %rax
jne 0x1bb45
callq 0x7040
movl (%rax), %edi
cmpl $0x4, %edi
je 0x1baf3
jmp 0x1bbb5
testq %rax, %rax
je 0x1bc0a
addq 0x128(%rbx), %rax
movq %rax, 0x128(%rbx)
movq 0x100(%rbx), %rdx
movq 0x108(%rbx), %rdi
movq %rax, %rsi
callq 0x32b6b
movq %r14, %rdi
movq %rax, %rsi
callq 0x1505a
leaq 0x1(%r15), %rax
cmpq $0x31, %r15
movq %rax, %r15
jb 0x1baf3
movq 0x8(%rbx), %rdi
addq $0x50, %rbx
movq 0x48(%rdi), %rsi
cmpq 0x50(%rdi), %rsi
je 0x1bc7e
movl (%rbx), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x48(%rdi)
jmp 0x1bc8a
cmpl $0xb, %edi
je 0x1bc8a
cmpl $0x68, %edi
jne 0x1bc9a
leaq 0x2aeba(%rip), %rax     # 0x46a88
movl (%rax), %eax
xorl %esi, %esi
testl %eax, %eax
setg %sil
leaq 0x189cc(%rip), %rdx     # 0x345ab
leaq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x96e6
cmpb $0x1, (%r14)
jne 0x1bc4c
movq 0x10(%rsp), %rdi
leaq 0x1a821(%rip), %rsi     # 0x3641f
movl $0x28, %edx
callq 0x7570
jmp 0x1bc4c
leaq 0x2ae77(%rip), %rax     # 0x46a88
movl (%rax), %eax
xorl %esi, %esi
cmpl $0x2, %eax
setge %sil
leaq 0x18e3a(%rip), %rdx     # 0x34a5d
leaq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x96e6
cmpb $0x1, (%r14)
jne 0x1bc4c
movq 0x10(%rsp), %rdi
leaq 0x1a806(%rip), %rsi     # 0x36448
movl $0x29, %edx
callq 0x7570
leaq 0x8(%rsp), %rdi
callq 0x8f4e
movq 0x8(%rbx), %rax
xorl %ecx, %ecx
movb %cl, 0x4(%rax)
movb $0x1, %al
lock
cmpxchgb %cl, 0x10(%rbx)
sete %bpl
jne 0x1bc78
movl 0x50(%rbx), %esi
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
xorb $0x1, %bpl
jmp 0x1bc8d
addq $0x40, %rdi
movq %rbx, %rdx
callq 0xaa5a
movb $0x1, %bpl
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x195b5(%rip), %rsi     # 0x35256
callq 0x8d8d
jmp 0x1bca8
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x8f4e
movq %rbx, %rdi
callq 0x79a0
nop