// File: /workspace/github_cpp_makefile_star2_O3/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 0x2546c(%rip), %xmm0    # 0x348e0
leaq 0x10(%rsp), %r14
movaps %xmm0, (%r14)
movl $0xa, 0x10(%r14)
leaq 0x8(%rsp), %r15
movl $0x5, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x12484
movq (%r15), %rdi
movq $0x0, 0x8(%r14)
movq %rbx, (%r14)
leaq 0x7e7(%rip), %rax       # 0xfc96
movq %rax, 0x18(%r14)
leaq 0x94c(%rip), %rax       # 0xfe06
movq %rax, 0x10(%r14)
addq $0x58, %rdi
leaq 0x10(%rsp), %rsi
callq 0x10366
movq 0x20(%rsp), %rax
testq %rax, %rax
je 0xf4e5
leaq 0x10(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
leaq 0x8(%rbx), %rdi
movq 0x8(%rsp), %rax
movq $0x0, 0x8(%rsp)
movq %rsp, %rsi
movq %rax, (%rsi)
movl $0x1, %edx
callq 0xa31a
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xf516
movq (%rdi), %rax
callq *0x8(%rax)
movq $0x0, (%rsp)
movq 0x120(%rbx), %r14
movq 0x128(%rbx), %r15
cmpq %r15, %r14
je 0xf540
movq (%r14), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
addq $0x8, %r14
jmp 0xf52c
leaq 0x37679(%rip), %rdi     # 0x46bc0
callq 0x12363
testb %al, %al
jne 0xf668
leaq 0x3752d(%rip), %r12     # 0x46a88
leaq 0x10(%rsp), %r15
leaq 0x37659(%rip), %r13     # 0x46bc0
leaq 0x2503d(%rip), %rbp     # 0x345ab
movq %rbx, %rdi
callq 0xf6f4
cmpb $0x0, (%rbx)
jne 0xf649
movl (%r12), %eax
xorl %esi, %esi
cmpl $0x2, %eax
setge %sil
movq %r15, %rdi
leaq 0x254c7(%rip), %rdx     # 0x34a5d
callq 0x96e6
cmpb $0x1, 0x10(%rsp)
jne 0xf5b8
movq 0x18(%rsp), %rdi
movl $0x1b, %edx
leaq 0x25408(%rip), %rsi     # 0x349bb
callq 0x7570
movq %r15, %rdi
callq 0x8f4e
xorl %r14d, %r14d
movq %r13, %rdi
callq 0x12363
testb %al, %al
jne 0xf668
movq $0x0, 0x10(%rsp)
movq $0x5f5e100, 0x18(%rsp)  # imm = 0x5F5E100
movq %r15, %rdi
movq %r15, %rsi
callq 0x7110
cmpl $-0x1, %eax
jne 0xf5ff
callq 0x7040
cmpl $0x4, (%rax)
je 0xf5e5
incl %r14d
cmpl $0x32, %r14d
jne 0xf5c3
movl (%r12), %eax
xorl %esi, %esi
testl %eax, %eax
setg %sil
movq %r15, %rdi
movq %rbp, %rdx
callq 0x96e6
cmpb $0x1, 0x10(%rsp)
jne 0xf63c
movq 0x18(%rsp), %rdi
movl $0x3f, %edx
leaq 0x253a0(%rip), %rsi     # 0x349d7
callq 0x7570
movq %r15, %rdi
callq 0x8f4e
jmp 0xf56e
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0xf659
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 0xf88a
xorl %edi, %edi
callq 0x72d0
jmp 0xf6b3
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xf68b
movq (%rdi), %rax
callq *0x8(%rax)
movq $0x0, (%rsp)
jmp 0xf6db
movq %rax, %rbx
movq 0x20(%rsp), %rax
testq %rax, %rax
je 0xf6db
leaq 0x10(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0xf6db
movq %rax, %rdi
callq 0x8f80
jmp 0xf6bd
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x8f4e
jmp 0xf6db
jmp 0xf6d8
jmp 0xf6d8
jmp 0xf6d8
jmp 0xf6d8
jmp 0xf6d8
jmp 0xf6d8
movq %rax, %rbx
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0xf6eb
movq (%rdi), %rax
callq *0x8(%rax)
movq %rbx, %rdi
callq 0x79a0
nop