// File: cybozu[P]yrmcds/src/server.cpp
// Function: yrmcds::server::serve_slave()
void server::serve_slave() {
    for( auto it1 = m_handlers.begin(); it1 != m_handlers.end(); ++it1 ) {
        if( ! (*it1)->on_slave_start() ) {
            // failed to start. stop already started handlers.
            for( auto it2 = m_handlers.begin(); it2 != it1; ++it2 )
                (*it2)->on_slave_end();
            return;
        }
    }

    cybozu::logger::info() << "Slave start";

    m_reactor.run([this](cybozu::reactor& r) {
            if( is_master() ) {
                cybozu::logger::info() << "Detected that this node is eligible to be master. Exit slave mode.";
                r.quit();
                return;
            }

            std::time_t now = std::time(nullptr);
            g_current_time.store(now, std::memory_order_relaxed);

            for( auto& handler: m_handlers )
                handler->on_slave_interval();

            r.fix_garbage();
            r.gc();
        });
    for( auto& handler: m_handlers )
        handler->on_slave_end();

    cybozu::logger::info() << "Slave end";
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xa0(%rbp)
addq $0x120, %rdi            # imm = 0x120
callq 0x22270
movq %rax, -0x10(%rbp)
movq -0xa0(%rbp), %rdi
addq $0x120, %rdi            # imm = 0x120
callq 0x222a0
movq %rax, -0x18(%rbp)
leaq -0x10(%rbp), %rdi
leaq -0x18(%rbp), %rsi
callq 0x222d0
testb $0x1, %al
jne 0x20007
jmp 0x20089
leaq -0x10(%rbp), %rdi
callq 0x22310
movq %rax, %rdi
callq 0x22330
movq %rax, %rdi
movq (%rdi), %rax
callq *0x38(%rax)
testb $0x1, %al
jne 0x20079
movq -0xa0(%rbp), %rdi
addq $0x120, %rdi            # imm = 0x120
callq 0x22270
movq %rax, -0x20(%rbp)
leaq -0x20(%rbp), %rdi
leaq -0x10(%rbp), %rsi
callq 0x222d0
testb $0x1, %al
jne 0x2004f
jmp 0x20074
leaq -0x20(%rbp), %rdi
callq 0x22310
movq %rax, %rdi
callq 0x22330
movq %rax, %rdi
movq (%rdi), %rax
callq *0x48(%rax)
leaq -0x20(%rbp), %rdi
callq 0x22350
jmp 0x2003c
jmp 0x201d7
jmp 0x2007b
leaq -0x10(%rbp), %rdi
callq 0x22350
jmp 0x1ffda
leaq -0x30(%rbp), %rdi
movq %rdi, -0xa8(%rbp)
callq 0x22390
movq -0xa8(%rbp), %rdi
leaq 0x5aa19(%rip), %rsi     # 0x7aac0
callq 0x225a0
jmp 0x200ae
leaq -0x30(%rbp), %rdi
callq 0xa770
movq -0xa0(%rbp), %rax
movq %rax, %rcx
addq $0x8, %rcx
movq %rcx, -0xb8(%rbp)
movq %rax, -0x68(%rbp)
leaq -0x60(%rbp), %rdi
movq %rdi, -0xb0(%rbp)
leaq -0x68(%rbp), %rsi
callq 0x205a0
movq -0xb8(%rbp), %rdi
movq -0xb0(%rbp), %rsi
movl $0x1, %edx
callq 0xeb90
jmp 0x200fe
leaq -0x60(%rbp), %rdi
callq 0x225e0
movq -0xa0(%rbp), %rax
addq $0x120, %rax            # imm = 0x120
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rdi
callq 0x22270
movq %rax, -0x78(%rbp)
movq -0x70(%rbp), %rdi
callq 0x222a0
movq %rax, -0x80(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x222d0
testb $0x1, %al
jne 0x20145
jmp 0x201a3
leaq -0x78(%rbp), %rdi
callq 0x22310
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
callq 0x22330
movq %rax, %rdi
movq (%rdi), %rax
callq *0x48(%rax)
leaq -0x78(%rbp), %rdi
callq 0x22350
jmp 0x20132
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x30(%rbp), %rdi
callq 0xa770
jmp 0x201f8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x225e0
jmp 0x201f8
leaq -0x98(%rbp), %rdi
movq %rdi, -0xc0(%rbp)
callq 0x22390
movq -0xc0(%rbp), %rdi
leaq 0x5a908(%rip), %rsi     # 0x7aacc
callq 0x22600
jmp 0x201cb
leaq -0x98(%rbp), %rdi
callq 0xa770
addq $0xc0, %rsp
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x98(%rbp), %rdi
callq 0xa770
movq -0x38(%rbp), %rdi
callq 0x8bc0
nopw %cs:(%rax,%rax)