// File: /workspace/github_cpp_makefile_star2_O1/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 0x25400(%rip), %xmm0    # 0x3489c
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 0x1244c
movq (%r15), %rdi
xorps %xmm0, %xmm0
movaps %xmm0, (%r14)
movaps %xmm0, 0x10(%r14)
movq %rbx, (%r14)
leaq 0x83d(%rip), %rax       # 0xfd18
movq %rax, 0x18(%r14)
leaq 0x99e(%rip), %rax       # 0xfe84
movq %rax, 0x10(%r14)
addq $0x58, %rdi
leaq 0x10(%rsp), %rsi
callq 0x103cc
movq 0x20(%rsp), %rax
testq %rax, %rax
je 0xf511
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 0xa33e
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xf542
movq (%rdi), %rax
callq *0x8(%rax)
movq $0x0, (%rsp)
movq 0x120(%rbx), %r14
movq 0x128(%rbx), %r15
cmpq %r15, %r14
je 0xf56c
movq (%r14), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
addq $0x8, %r14
jmp 0xf558
leaq 0x3764d(%rip), %rdi     # 0x46bc0
callq 0x1232b
testb %al, %al
jne 0xf6a3
leaq 0x37511(%rip), %r12     # 0x46a98
leaq 0x2548b(%rip), %r14     # 0x34a19
leaq 0x10(%rsp), %r15
leaq 0x37626(%rip), %r13     # 0x46bc0
movq %rbx, %rdi
callq 0xf72e
cmpb $0x0, (%rbx)
jne 0xf684
movl (%r12), %eax
xorl %esi, %esi
cmpl $0x2, %eax
setge %sil
movq %r15, %rdi
movq %r14, %rdx
callq 0x9718
cmpb $0x1, 0x10(%rsp)
jne 0xf5e0
movq 0x18(%rsp), %rdi
movl $0x1b, %edx
leaq 0x2539c(%rip), %rsi     # 0x34977
callq 0x7580
movq %r15, %rdi
callq 0x8f94
xorl %ebp, %ebp
movq %r13, %rdi
callq 0x1232b
testb %al, %al
jne 0xf630
movq $0x0, 0x10(%rsp)
movq $0x5f5e100, 0x18(%rsp)  # imm = 0x5F5E100
movq %r15, %rdi
movq %r15, %rsi
callq 0x7110
cmpl $-0x1, %eax
jne 0xf622
callq 0x7040
cmpl $0x4, (%rax)
je 0xf608
incl %ebp
cmpl $0x32, %ebp
jne 0xf5ea
movl $0x7, %eax
jmp 0xf635
movl $0x4, %eax
cmpl $0x7, %eax
jne 0xf67f
movl (%r12), %eax
xorl %esi, %esi
testl %eax, %eax
setg %sil
movq %r15, %rdi
leaq 0x24f5b(%rip), %rdx     # 0x345ab
callq 0x9718
cmpb $0x1, 0x10(%rsp)
jne 0xf672
movq 0x18(%rsp), %rdi
movl $0x3f, %edx
leaq 0x25326(%rip), %rsi     # 0x34993
callq 0x7580
movq %r15, %rdi
callq 0x8f94
jmp 0xf59a
cmpl $0x4, %eax
je 0xf6a3
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0xf694
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 0xf8ea
xorl %edi, %edi
callq 0x72d0
jmp 0xf6ee
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xf6c6
movq (%rdi), %rax
callq *0x8(%rax)
movq $0x0, (%rsp)
jmp 0xf716
movq %rax, %rbx
movq 0x20(%rsp), %rax
testq %rax, %rax
je 0xf716
leaq 0x10(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0xf716
movq %rax, %rdi
callq 0x8fc6
jmp 0xf6f8
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x8f94
jmp 0xf716
jmp 0xf713
jmp 0xf713
jmp 0xf713
jmp 0xf713
jmp 0xf713
jmp 0xf713
movq %rax, %rbx
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0xf726
movq (%rdi), %rax
callq *0x8(%rax)
movq %rbx, %rdi
callq 0x79b0