// File: /workspace/github_cpp_makefile_star2_O1/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 %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %esi, %ebx
movq %rdi, %r14
leaq 0x108(%rdi), %r15
leaq 0x50(%rdi), %rax
movq %rax, 0x18(%rsp)
movl $0x1, %eax
movl %eax, %eax
movq %rax, 0x10(%rsp)
leaq 0x1966e(%rip), %rbp     # 0x3578c
movl $0x2, %eax
movl %eax, %eax
movq %rax, 0x20(%rsp)
xorl %r12d, %r12d
movq 0x118(%r14), %rax
subq 0x128(%r14), %rax
cmpq $0x1fffff, %rax         # imm = 0x1FFFFF
ja 0x1c150
movl $0x200000, %esi         # imm = 0x200000
movq %r15, %rdi
callq 0x15236
movq 0x108(%r14), %rsi
addq 0x128(%r14), %rsi
movl $0x200000, %edx         # imm = 0x200000
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x7130
testq %rax, %rax
je 0x1c1b1
cmpq $-0x1, %rax
jne 0x1c22d
callq 0x7040
movl (%rax), %edi
cmpl $0x68, %edi
je 0x1c2a0
movl $0x3, %eax
cmpl $0xb, %edi
je 0x1c331
cmpl $0x4, %edi
jne 0x1c356
movq 0x20(%rsp), %rax
movslq (%rbp,%rax,4), %rax
addq %rbp, %rax
jmpq *%rax
leaq 0x2a8e0(%rip), %rax     # 0x46a98
movl (%rax), %eax
xorl %esi, %esi
cmpl $0x2, %eax
setge %sil
movq %rsp, %rdi
leaq 0x1884c(%rip), %rdx     # 0x34a19
callq 0x9718
cmpb $0x1, (%rsp)
jne 0x1c1ee
movq 0x8(%rsp), %rdi
movl $0x29, %edx
leaq 0x1a24f(%rip), %rsi     # 0x36438
callq 0x7580
movq %rsp, %rdi
callq 0x8f94
movq 0x8(%r14), %rax
xorl %ecx, %ecx
movb %cl, 0x4(%rax)
movb $0x1, %al
lock
cmpxchgb %cl, 0x10(%r14)
sete %r13b
jne 0x1c21a
movl 0x50(%r14), %esi
movq (%r14), %rax
movq %r14, %rdi
callq *0x10(%rax)
xorb $0x1, %r13b
movq 0x10(%rsp), %rax
movslq (%rbp,%rax,4), %rax
addq %rbp, %rax
jmpq *%rax
addq 0x128(%r14), %rax
movq %rax, 0x128(%r14)
movq 0x100(%r14), %rdx
movq 0x108(%r14), %rdi
movq %rax, %rsi
callq 0x33542
movq %r15, %rdi
movq %rax, %rsi
callq 0x15124
incq %r12
xorl %eax, %eax
cmpq $0x32, %r12
jb 0x1c331
movq 0x8(%r14), %rdi
movq 0x48(%rdi), %rsi
cmpq 0x50(%rdi), %rsi
je 0x1c31b
movq 0x18(%rsp), %rax
movl (%rax), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x48(%rdi)
movb $0x1, %r13b
movq 0x10(%rsp), %rax
movslq (%rbp,%rax,4), %rax
addq %rbp, %rax
jmpq *%rax
leaq 0x2a7f1(%rip), %rax     # 0x46a98
movl (%rax), %eax
xorl %esi, %esi
testl %eax, %eax
setg %sil
movq %rsp, %rdi
leaq 0x182f0(%rip), %rdx     # 0x345ab
callq 0x9718
cmpb $0x1, (%rsp)
jne 0x1c2dc
movq 0x8(%rsp), %rdi
movl $0x28, %edx
leaq 0x1a138(%rip), %rsi     # 0x3640f
callq 0x7580
movq %rsp, %rdi
callq 0x8f94
movq 0x8(%r14), %rax
xorl %ecx, %ecx
movb %cl, 0x4(%rax)
movb $0x1, %al
lock
cmpxchgb %cl, 0x10(%r14)
sete %r13b
jne 0x1c308
movl 0x50(%r14), %esi
movq (%r14), %rax
movq %r14, %rdi
callq *0x10(%rax)
xorb $0x1, %r13b
movq 0x10(%rsp), %rax
movslq (%rbp,%rax,4), %rax
addq %rbp, %rax
jmpq *%rax
addq $0x40, %rdi
movq 0x18(%rsp), %rdx
callq 0xaac6
movb $0x1, %r13b
movl $0x1, %eax
movl %eax, %eax
movslq (%rbp,%rax,4), %rax
addq %rbp, %rax
jmpq *%rax
movb $0x1, %r13b
andb $0x1, %r13b
movl %r13d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x18eb5(%rip), %rsi     # 0x35212
callq 0x8dd3
jmp 0x1c364
movq %rax, %rbx
movq %rsp, %rdi
callq 0x8f94
movq %rbx, %rdi
callq 0x79b0
nop