// File: /workspace/github_cpp_makefile_star2_O3/roberty65[P]bServer/serverd/src/main.cpp
// Function: main
int main(int argc, char **argv)
{
	const char *etcFile = "../conf/server.conf";
	int daemonMode = 1;
	int ch, showVersion = 0;

	while ((ch = getopt(argc, argv, "dc:hv")) != EOF) {
		switch (ch) {
		case 'd':
			daemonMode = 0;
			break;
		case 'c':
			etcFile = optarg;
			break;
		case 'v':
			showVersion = 1;
			break;
		case 'h':
			usage(argv[0]);
			exit(0);
		default:
			fprintf(stderr, "invalid argument: %c\n", ch);
			usage(argv[0]);
			exit(1);
		}	
	}

	if (loadConfig(etcFile) < 0) {
		fprintf(stderr, "load config from %s failed: %m\n", etcFile);
		exit(1);
	}

	if (daemonMode) {
		int retval = daemon(1, 1);
		assert(retval == 0);

		if (fork() != 0) exit(0); // exit if not child
		setsid();

		struct sigaction act;
		act.sa_flags = 0;
		act.sa_handler = sigExit;
		sigaction(SIGTERM, &act, NULL);
		sigaction(SIGINT, &act, NULL);

		signal(SIGHUP, SIG_IGN);
		signal(SIGQUIT, SIG_IGN);
		signal(SIGPIPE, SIG_IGN);
		signal(SIGCHLD, SIG_IGN);
		signal(SIGTSTP, SIG_IGN);
		signal(SIGTTIN, SIG_IGN);
		signal(SIGTTOU, SIG_IGN);

		// TODO: mask other signal???

		// fork again?
		//setpgrp();
	}

	if (showVersion) {
		//Processor *processor = loadProcessor(businessProcessor);
		// TODO:
		exit(0);
	}

	if (BPROF_INIT(items, sizeof(items)/sizeof(items[0])-1) < 0) {
		fprintf(stderr, "bprof-init failed: %m\n");
		exit(1);
	}

	SYSLOG_INIT(logFileName, logLevel, logFileMaxSize, logMaxBackup);
	if (sysLogHandle == NULL) {
		fprintf(stderr, "SYS-LOG-INIT failed: %m\n");
		exit(1);
	}
	else {
		SYSLOG_INFO("serverd started");
	}
	
	Queue<Message> *inQ = new Queue<Message>(systemInQueueSize);
	Queue<Message> *outQ = new Queue<Message>(systemOutQueueSize);

	Processor *processor = loadProcessor(businessProcessor);
	processor->setSendFunc(async_send_delegator, outQ);

	EventManager *emgr = new EventManager(outQ, 1024);
	emgr->setMessageExpire(systemInMessageExpire, systemOutMessageExpire);

	emgr->setConnectionMaxIdle(connectionIdleTimeout);
	emgr->setConnectionOutQueueSize(connectionOutQueueSize);

	emgr->setHelloInterval(connectorHelloInterval);
	emgr->setReconnectDelay(reconnectDelay);

	int retval = emgr->addListener(listenAddress, inQ, processor, listenMaxConnection);
	if (retval < 0) {
		SYSLOG_FATAL("addListener at %s failed", listenAddress);
		return 1;
	}

	if (!connectors.empty()) for (ConnectorMap::iterator iter = connectors.begin(); iter != connectors.end(); ++iter) {
		int cid = emgr->addConnector(iter->second.address, inQ, processor, 1);
		if (cid < 0) {
			SYSLOG_FATAL("addConnector(%s) failed", iter->first.c_str());
			return 2;
		}
	
		iter->second.flow = cid;
		SYSLOG_DEBUG("connector=%s, flow=%d", iter->first.c_str(), cid);
	}

	// init here for it may call add/getConnector
	if (processor->onInit() < 0) {
		SYSLOG_FATAL("processor onInit failed: %m");
		exit(1);
	}

	// start worker before event manager starts
	MtWorker *mtWorker = new MtWorker(inQ, processor, systemThreadCount);
	mtWorker->start();

	emgr->start();

	// nothing
	processor->onExit();
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movq %rsi, %rbx
movl %edi, %ebp
leaq 0x6741(%rip), %r13      # 0xa117
movl $0x1, 0x4(%rsp)
movb $0x1, %r12b
leaq 0x6743(%rip), %r14      # 0xa12b
movq 0xc5d1(%rip), %r15      # 0xffc0
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x30a0
cmpl $0x63, %eax
jne 0x3a06
movq (%r15), %r13
jmp 0x39ef
cmpl $0x67, %eax
jg 0x3a23
cmpl $-0x1, %eax
je 0x3a31
cmpl $0x64, %eax
jne 0x41a7
movl $0x0, 0x4(%rsp)
jmp 0x39ef
cmpl $0x76, %eax
jne 0x4176
xorl %r12d, %r12d
jmp 0x39ef
leaq 0x98(%rsp), %rax
movl $0x0, (%rax)
xorl %ecx, %ecx
movq %rcx, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %rcx, 0x20(%rax)
leaq 0x90(%rsp), %rdi
movq %r13, %rsi
callq 0x81a4
testl %eax, %eax
movq %r13, 0x40(%rsp)
js 0x4190
leaq 0x67bb(%rip), %rsi      # 0xa230
leaq 0x67bd(%rip), %rdx      # 0xa239
leaq 0x90(%rsp), %rdi
callq 0x8526
movq %rax, %rbx
leaq 0x67ac(%rip), %rsi      # 0xa23f
movq %rax, %rdi
callq 0x34b0
testl %eax, %eax
je 0x3af6
leaq 0x6793(%rip), %rsi      # 0xa239
movq %rbx, %rdi
callq 0x34b0
testl %eax, %eax
je 0x3afd
leaq 0x678c(%rip), %rsi      # 0xa245
movq %rbx, %rdi
callq 0x34b0
testl %eax, %eax
je 0x3b04
leaq 0x677e(%rip), %rsi      # 0xa24a
movq %rbx, %rdi
callq 0x34b0
testl %eax, %eax
je 0x3b0b
leaq 0x6770(%rip), %rsi      # 0xa24f
movq %rbx, %rdi
callq 0x34b0
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
leal (%rcx,%rcx,2), %ebp
addl $0x2, %ebp
jmp 0x3b10
movl $0x1, %ebp
jmp 0x3b10
movl $0x2, %ebp
jmp 0x3b10
movl $0x3, %ebp
jmp 0x3b10
movl $0x4, %ebp
leaq 0x673e(%rip), %rsi      # 0xa255
leaq 0x6743(%rip), %rdx      # 0xa261
leaq 0x90(%rsp), %rdi
callq 0x8526
movq %rax, %rdi
callq 0x3610
movq %rax, 0x58(%rsp)
testq %rax, %rax
je 0x4190
leaq 0x672c(%rip), %rsi      # 0xa274
leaq 0x90(%rsp), %rdi
movl $0xa00000, %edx         # imm = 0xA00000
callq 0x8608
movl %eax, 0x3c(%rsp)
leaq 0x671e(%rip), %rsi      # 0xa283
leaq 0x90(%rsp), %rdi
movl $0xa, %edx
callq 0x8608
movl %eax, 0x38(%rsp)
leaq 0x670e(%rip), %rsi      # 0xa290
leaq 0x6715(%rip), %rdx      # 0xa29e
leaq 0x90(%rsp), %rdi
callq 0x8526
movq %rax, %rdi
callq 0x3610
testq %rax, %rax
je 0x4190
movq %rax, %r14
leaq 0x66fa(%rip), %rsi      # 0xa2ab
leaq 0x90(%rsp), %rdi
movl $0x400, %edx            # imm = 0x400
callq 0x8608
leaq 0x66ef(%rip), %rsi      # 0xa2b9
leaq 0x90(%rsp), %rdi
movl $0x2000, %edx           # imm = 0x2000
callq 0x8608
movl %eax, %r15d
leaq 0x66e7(%rip), %rsi      # 0xa2cd
leaq 0x90(%rsp), %rdi
movl $0x8, %edx
callq 0x8608
movl %eax, %ebx
leaq 0x66de(%rip), %rsi      # 0xa2df
leaq 0x90(%rsp), %rdi
movl $0x2000, %edx           # imm = 0x2000
callq 0x8608
movl %eax, 0x34(%rsp)
leaq 0x66d3(%rip), %rsi      # 0xa2f1
leaq 0x90(%rsp), %rdi
movl $0x4000, %edx           # imm = 0x4000
callq 0x8608
movl %eax, 0x30(%rsp)
leaq 0x66c9(%rip), %rsi      # 0xa304
leaq 0x90(%rsp), %rdi
movl $0x1e, %edx
callq 0x8608
movl %eax, 0x2c(%rsp)
leaq 0x66c2(%rip), %rsi      # 0xa31a
leaq 0x90(%rsp), %rdi
movl $0x1e, %edx
callq 0x8608
movl %eax, 0x28(%rsp)
leaq 0x66bc(%rip), %rsi      # 0xa331
leaq 0x66c7(%rip), %rdx      # 0xa343
leaq 0x90(%rsp), %rdi
callq 0x8526
movq %rax, %rdi
callq 0x3610
movq %rax, 0x50(%rsp)
testq %rax, %rax
je 0x4190
leaq 0x66af(%rip), %rsi      # 0xa355
leaq 0x90(%rsp), %rdi
movl $0xa, %edx
callq 0x8608
movl %eax, 0x1c(%rsp)
leaq 0x66a8(%rip), %rsi      # 0xa36b
leaq 0x90(%rsp), %rdi
movl $0x64, %edx
callq 0x8608
movl %eax, 0x18(%rsp)
leaq 0x66a2(%rip), %rsi      # 0xa382
leaq 0x90(%rsp), %rdi
movl $0x3, %edx
callq 0x8608
movl %eax, 0x14(%rsp)
leaq 0x669c(%rip), %rsi      # 0xa399
leaq 0x90(%rsp), %rdi
movl $0x2, %edx
callq 0x8608
movl %eax, 0x10(%rsp)
movl %r15d, 0xc(%rsp)
movl %ebp, 0x20(%rsp)
movq %r14, 0x48(%rsp)
movl %ebx, 0x8(%rsp)
movl %r12d, 0x24(%rsp)
leaq 0x6677(%rip), %rdx      # 0xa3a8
leaq 0x128(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x8844
leaq 0x130(%rsp), %r15
movq 0x10(%r15), %rbx
cmpq %r15, %rbx
je 0x3e0c
leaq 0x70(%rsp), %r13
leaq 0x60(%rsp), %r12
leaq 0xc76c(%rip), %r14      # 0x104d8
movq 0x20(%rbx), %rdx
leaq 0x90(%rsp), %rdi
leaq 0x6629(%rip), %rsi      # 0xa3a8
leaq 0x662c(%rip), %rcx      # 0xa3b2
xorl %r8d, %r8d
callq 0x85b0
testq %rax, %rax
je 0x4183
movq %rax, %rdi
callq 0x3610
testq %rax, %rax
je 0x4183
movq %rax, %rbp
movq %r13, 0x60(%rsp)
movq 0x20(%rbx), %rsi
movq 0x28(%rbx), %rdx
addq %rsi, %rdx
movq %r12, %rdi
callq 0x45ce
movq %rbp, 0x80(%rsp)
movl $0xffffffff, 0x88(%rsp) # imm = 0xFFFFFFFF
movq %r14, %rdi
movq %r12, %rsi
callq 0x4402
movq 0x60(%rsp), %rdi
cmpq %r13, %rdi
je 0x3df8
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x3340
movq %rbx, %rdi
callq 0x33d0
movq %rax, %rbx
cmpq %r15, %rax
jne 0x3d6c
leaq 0x128(%rsp), %rdi
callq 0x4664
leaq 0x90(%rsp), %rdi
callq 0x46dc
cmpl $0x0, 0x4(%rsp)
movl 0x24(%rsp), %ebp
je 0x3f01
movl $0x1, %edi
movl $0x1, %esi
callq 0x3400
testl %eax, %eax
jne 0x41b8
callq 0x30c0
testl %eax, %eax
jne 0x41b1
callq 0x3390
leaq 0x90(%rsp), %rbx
movl $0x0, 0x88(%rbx)
leaq 0x43b(%rip), %rax       # 0x42b2
movq %rax, (%rbx)
movl $0xf, %edi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x3430
movl $0x2, %edi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x3430
movl $0x1, %esi
movl $0x1, %edi
callq 0x33f0
movl $0x1, %esi
movl $0x3, %edi
callq 0x33f0
movl $0x1, %esi
movl $0xd, %edi
callq 0x33f0
movl $0x1, %esi
movl $0x11, %edi
callq 0x33f0
movl $0x1, %esi
movl $0x14, %edi
callq 0x33f0
movl $0x1, %esi
movl $0x15, %edi
callq 0x33f0
movl $0x1, %esi
movl $0x16, %edi
callq 0x33f0
testb $0x1, %bpl
je 0x41b1
movslq 0x3c(%rsp), %rbx
movslq 0x38(%rsp), %r15
movl $0x218, %edi            # imm = 0x218
callq 0x3330
movq %rax, %r14
movq %rax, %rdi
movq 0x58(%rsp), %rsi
movl 0x20(%rsp), %edx
movq %rbx, %rcx
movq %r15, %r8
callq 0x71d0
leaq 0xc5c8(%rip), %rax      # 0x10508
movq %r14, (%rax)
cmpl $0x4, (%r14)
jl 0x3f5f
leaq 0x6243(%rip), %rdx      # 0xa193
movq %r14, %rdi
movl $0x4, %esi
xorl %eax, %eax
callq 0x71fc
movl $0xb0, %edi
callq 0x3330
movq %rax, %r14
movslq 0x34(%rsp), %rsi
movq %rax, %rdi
callq 0x4324
movl $0xb0, %edi
callq 0x3330
movq %rax, %r15
movslq 0x30(%rsp), %rsi
movq %rax, %rdi
callq 0x4324
movq 0x50(%rsp), %rdi
callq 0x38db
movq %rax, %r12
leaq 0x1891(%rip), %rax      # 0x5838
movq %rax, 0x8(%r12)
movq %r15, 0x10(%r12)
movl $0x70, %edi
callq 0x3330
movq %rax, %r13
movq %rax, %rdi
movq %r15, %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x4a04
imull $0x3e8, 0x2c(%rsp), %eax # imm = 0x3E8
movl %eax, 0x58(%r13)
imull $0x3e8, 0x28(%rsp), %eax # imm = 0x3E8
movl %eax, 0x5c(%r13)
imull $0x3e8, 0x1c(%rsp), %eax # imm = 0x3E8
movl %eax, 0x60(%r13)
movl 0x18(%rsp), %eax
movl %eax, 0x64(%r13)
imull $0x3e8, 0x14(%rsp), %eax # imm = 0x3E8
movl %eax, 0x68(%r13)
imull $0x3e8, 0x10(%rsp), %eax # imm = 0x3E8
movl %eax, 0x6c(%r13)
movq %r13, %rdi
movq 0x48(%rsp), %rbx
movq %rbx, %rsi
movq %r14, %rdx
movq %r12, %rcx
movl 0xc(%rsp), %r8d
callq 0x4b94
testl %eax, %eax
js 0x410b
leaq 0xc49c(%rip), %rax      # 0x104d8
cmpq $0x0, 0x28(%rax)
sete %cl
movq 0x18(%rax), %rbx
addq $0x8, %rax
cmpq %rax, %rbx
sete %al
orb %cl, %al
jne 0x40bc
leaq 0xc483(%rip), %rbp      # 0x104e0
leaq 0x6170(%rip), %r15      # 0xa1d4
movq 0x40(%rbx), %rsi
movq %r13, %rdi
movq %r14, %rdx
movq %r12, %rcx
movl $0x1, %r8d
callq 0x4cde
testl %eax, %eax
js 0x4130
movl %eax, 0x48(%rbx)
leaq 0xc47a(%rip), %rcx      # 0x10508
movq (%rcx), %rdi
cmpl $0x5, (%rdi)
jl 0x40ac
movq 0x20(%rbx), %rcx
movl $0x5, %esi
movq %r15, %rdx
movl %eax, %r8d
xorl %eax, %eax
callq 0x71fc
movq %rbx, %rdi
callq 0x3200
movq %rax, %rbx
cmpq %rbp, %rax
jne 0x4064
movq (%r12), %rax
movq %r12, %rdi
callq *0x20(%rax)
testl %eax, %eax
js 0x41d7
movl $0x20, %edi
callq 0x3330
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
movq %r12, %rdx
movl 0x8(%rsp), %ecx
callq 0x5664
movq %r15, %rdi
callq 0x578e
movq %r13, %rdi
callq 0x556a
movq (%r12), %rax
movq %r12, %rdi
callq *0x28(%rax)
xorl %ebp, %ebp
jmp 0x4162
leaq 0xc3f6(%rip), %rax      # 0x10508
movq (%rax), %rdi
cmpl $0x0, (%rdi)
jle 0x415d
leaq 0x6082(%rip), %rdx      # 0xa1a3
movl $0x1, %ebp
movl $0x1, %esi
movq %rbx, %rcx
jmp 0x4154
leaq 0xc3d1(%rip), %rax      # 0x10508
movq (%rax), %rdi
movl $0x2, %ebp
cmpl $0x0, (%rdi)
jle 0x4162
movq 0x20(%rbx), %rcx
leaq 0x606d(%rip), %rdx      # 0xa1bc
movl $0x1, %esi
xorl %eax, %eax
callq 0x71fc
jmp 0x4162
movl $0x1, %ebp
movl %ebp, %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x68, %eax
jne 0x41a7
movq (%rbx), %rdi
callq 0x428f
leaq 0x128(%rsp), %rdi
callq 0x4664
leaq 0x90(%rsp), %rdi
callq 0x46dc
movq 0x40(%rsp), %rdi
callq 0x3650
movl %eax, %edi
movq %rbx, %rsi
callq 0x3674
xorl %edi, %edi
callq 0x3420
leaq 0x5fa8(%rip), %rdi      # 0xa167
leaq 0x5fad(%rip), %rsi      # 0xa173
leaq 0x5faf(%rip), %rcx      # 0xa17c
movl $0xd3, %edx
callq 0x31f0
leaq 0xc32a(%rip), %rax      # 0x10508
movq (%rax), %rdi
cmpl $0x0, (%rdi)
jle 0x41f9
leaq 0x5ffd(%rip), %rdx      # 0xa1ea
movl $0x1, %esi
xorl %eax, %eax
callq 0x71fc
movl $0x1, %edi
callq 0x3420
movq %rax, %rbx
movl $0x20, %esi
jmp 0x4222
movq %rax, %rbx
movl $0x70, %esi
movq %r13, %rdi
jmp 0x423c
movq %rax, %rbx
movl $0xb0, %esi
movq %r15, %rdi
jmp 0x423c
movq %rax, %rbx
movl $0xb0, %esi
jmp 0x4239
movq %rax, %rbx
movl $0x218, %esi            # imm = 0x218
movq %r14, %rdi
callq 0x3340
jmp 0x4287
jmp 0x4247
jmp 0x4247
movq %rax, %rbx
jmp 0x427a
jmp 0x424e
movq %rax, %rbx
jmp 0x426d
movq %rax, %rbx
movq 0x60(%rsp), %rdi
cmpq %r13, %rdi
je 0x426d
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x3340
leaq 0x128(%rsp), %rdi
callq 0x4664
leaq 0x90(%rsp), %rdi
callq 0x46dc
movq %rbx, %rdi
callq 0x35d0