// File: /workspace/github_cpp_makefile_star2_O2/cybozu[P]yrmcds/src/server.cpp
// Function: yrmcds::server::serve()
void server::serve() {
    auto res = cybozu::signal_setup({SIGHUP, SIGQUIT, SIGTERM, SIGINT, SIGUSR1});
    res->set_handler([this](const struct signalfd_siginfo& si,
                            cybozu::reactor& r) {
                         switch(si.ssi_signo) {
                         case SIGUSR1:
                             for( auto& handler: m_handlers )
                                 handler->dump_stats();
                             break;
                         case SIGHUP:
                             cybozu::logger::instance().reopen();
                             cybozu::logger::info() << "got SIGHUP.";
                             break;
                         case SIGQUIT:
                         case SIGTERM:
                         case SIGINT:
                             cybozu::logger::info() << "got signal (signo=" << si.ssi_signo << ").";
                             m_signaled = true;
                             r.quit();
                             break;
                         }
                     });
    m_reactor.add_resource(std::move(res), cybozu::reactor::EVENT_IN);

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

    if( is_master() )
        goto MASTER_ENTRY;
    while( true ) {
        serve_slave();
        if( m_signaled ) return;

        // disconnected from the master
        cybozu::logger::info() << "Try to promote to master...";
        for( int i = 0; i < MASTER_CHECKS; ++i ) {
            if( is_master() )
                goto MASTER_ENTRY;
            std::this_thread::sleep_for( std::chrono::milliseconds(100) );
        }
        cybozu::logger::warning() << "Could not promote to master. Join the cluster again as a slave.";
    }

  MASTER_ENTRY:
    serve_master();
    std::quick_exit(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movups 0x185ae(%rip), %xmm0    # 0x268bc
leaq 0x10(%rsp), %r14
movaps %xmm0, (%r14)
movl $0xa, 0x10(%r14)
leaq 0x8(%rsp), %r15
pushq $0x5
popq %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x10cb0
andq $0x0, 0x8(%r14)
movq (%r15), %rdi
movq %rbx, (%r14)
leaq 0x576(%rip), %rax       # 0xe8ba
movq %rax, 0x18(%r14)
leaq 0x669(%rip), %rax       # 0xe9b8
movq %rax, 0x10(%r14)
addq $0x58, %rdi
leaq 0x10(%rsp), %rsi
callq 0xef18
leaq 0x10(%rsp), %rdi
callq 0x8d90
movq 0x8(%rsp), %rax
andq $0x0, 0x8(%rsp)
leaq 0x8(%rbx), %rdi
movq %rsp, %rsi
movq %rax, (%rsi)
pushq $0x1
popq %rdx
callq 0x98ce
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xe397
movq (%rdi), %rax
callq *0x8(%rax)
andq $0x0, (%rsp)
movq 0x120(%rbx), %r14
movq 0x128(%rbx), %r15
cmpq %r15, %r14
je 0xe3be
movq (%r14), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
addq $0x8, %r14
jmp 0xe3aa
callq 0xec8a
testb %al, %al
jne 0xe46a
leaq 0x10(%rsp), %r14
leaq 0x185c0(%rip), %r15     # 0x26997
pushq $0x32
popq %rbp
leaq 0x185d2(%rip), %r12     # 0x269b3
movq %rbx, %rdi
callq 0xe4da
cmpb $0x0, (%rbx)
jne 0xe44b
movq %r14, %rdi
callq 0xec96
movq %r14, %rdi
movq %r15, %rsi
callq 0xecbe
movq %r14, %rdi
callq 0x86e4
movl %ebp, %r13d
subl $0x1, %r13d
jb 0xe42e
callq 0xec8a
testb %al, %al
jne 0xe46a
movq $0x64, 0x10(%rsp)
movq %r14, %rdi
callq 0xecd5
jmp 0xe40c
movq %r14, %rdi
callq 0xd0b4
movq %r14, %rdi
movq %r12, %rsi
callq 0xed2a
movq %r14, %rdi
callq 0x86e4
jmp 0xe3e1
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0xe45b
movq (%rdi), %rax
callq *0x8(%rax)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0xe5fc
xorl %edi, %edi
callq 0x72e0
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xe48b
movq (%rdi), %rax
callq *0x8(%rax)
andq $0x0, (%rsp)
jmp 0xe4c1
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x8d90
jmp 0xe4c1
jmp 0xe4be
jmp 0xe4a5
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x86e4
jmp 0xe4c1
jmp 0xe4be
jmp 0xe4be
jmp 0xe4be
jmp 0xe4be
jmp 0xe4be
movq %rax, %rbx
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0xe4d1
movq (%rdi), %rax
callq *0x8(%rax)
movq %rbx, %rdi
callq 0x7990
nop