// File: /workspace/github_cpp_makefile_star2_O1/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 $0x148, %rsp            # imm = 0x148
movq %rsi, %rbx
movl %edi, %ebp
leaq 0x6741(%rip), %r14      # 0xa117
movl $0x1, (%rsp)
movb $0x1, %r13b
leaq 0x6744(%rip), %r15      # 0xa12b
movq 0xc5d2(%rip), %r12      # 0xffc0
movl %ebp, %edi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x30a0
cmpl $0x63, %eax
jne 0x3a06
movq (%r12), %r14
jmp 0x39ee
cmpl $0x67, %eax
jg 0x3a22
cmpl $-0x1, %eax
je 0x3a30
cmpl $0x64, %eax
jne 0x41c0
movl $0x0, (%rsp)
jmp 0x39ee
cmpl $0x76, %eax
jne 0x41b3
xorl %r13d, %r13d
jmp 0x39ee
leaq 0x88(%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 0x80(%rsp), %rdi
movq %r14, %rsi
callq 0x82a4
testl %eax, %eax
js 0x41ca
leaq 0x67c1(%rip), %rsi      # 0xa230
leaq 0x67c3(%rip), %rdx      # 0xa239
leaq 0x80(%rsp), %rdi
callq 0x862a
movq %rax, %rbx
leaq 0x67b2(%rip), %rsi      # 0xa23f
movq %rax, %rdi
callq 0x34b0
testl %eax, %eax
je 0x3af0
leaq 0x6799(%rip), %rsi      # 0xa239
movq %rbx, %rdi
callq 0x34b0
testl %eax, %eax
je 0x3af7
leaq 0x6792(%rip), %rsi      # 0xa245
movq %rbx, %rdi
callq 0x34b0
testl %eax, %eax
je 0x3afe
leaq 0x6784(%rip), %rsi      # 0xa24a
movq %rbx, %rdi
callq 0x34b0
testl %eax, %eax
je 0x3b05
leaq 0x6776(%rip), %rsi      # 0xa24f
movq %rbx, %rdi
callq 0x34b0
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
leal (%rcx,%rcx,2), %eax
addl $0x2, %eax
jmp 0x3b0a
movl $0x1, %eax
jmp 0x3b0a
movl $0x2, %eax
jmp 0x3b0a
movl $0x3, %eax
jmp 0x3b0a
movl $0x4, %eax
movl %eax, 0xc980(%rip)      # 0x10490
leaq 0x673e(%rip), %rsi      # 0xa255
leaq 0x6743(%rip), %rdx      # 0xa261
leaq 0x80(%rsp), %rdi
callq 0x862a
movq %rax, %rdi
callq 0x3610
testq %rax, %rax
je 0x41ca
movq %rax, %r12
leaq 0x672e(%rip), %rsi      # 0xa274
leaq 0x80(%rsp), %rdi
movl $0xa00000, %edx         # imm = 0xA00000
callq 0x870c
movl %eax, 0x34(%rsp)
leaq 0x6720(%rip), %rsi      # 0xa283
leaq 0x80(%rsp), %rdi
movl $0xa, %edx
callq 0x870c
movl %eax, 0x30(%rsp)
leaq 0x6710(%rip), %rsi      # 0xa290
leaq 0x6717(%rip), %rdx      # 0xa29e
leaq 0x80(%rsp), %rdi
callq 0x862a
movq %rax, %rdi
callq 0x3610
testq %rax, %rax
je 0x41ca
movq %rax, %r15
leaq 0x66fc(%rip), %rsi      # 0xa2ab
leaq 0x80(%rsp), %rdi
movl $0x400, %edx            # imm = 0x400
callq 0x870c
leaq 0x66f1(%rip), %rsi      # 0xa2b9
leaq 0x80(%rsp), %rdi
movl $0x2000, %edx           # imm = 0x2000
callq 0x870c
movl %eax, %ebp
leaq 0x66ea(%rip), %rsi      # 0xa2cd
leaq 0x80(%rsp), %rdi
movl $0x8, %edx
callq 0x870c
movl %eax, %ebx
leaq 0x66e1(%rip), %rsi      # 0xa2df
leaq 0x80(%rsp), %rdi
movl $0x2000, %edx           # imm = 0x2000
callq 0x870c
movl %eax, 0x2c(%rsp)
leaq 0x66d6(%rip), %rsi      # 0xa2f1
leaq 0x80(%rsp), %rdi
movl $0x4000, %edx           # imm = 0x4000
callq 0x870c
movl %eax, 0x28(%rsp)
leaq 0x66cc(%rip), %rsi      # 0xa304
leaq 0x80(%rsp), %rdi
movl $0x1e, %edx
callq 0x870c
movl %eax, 0x24(%rsp)
leaq 0x66c5(%rip), %rsi      # 0xa31a
leaq 0x80(%rsp), %rdi
movl $0x1e, %edx
callq 0x870c
movl %eax, 0x20(%rsp)
leaq 0x66bf(%rip), %rsi      # 0xa331
leaq 0x66ca(%rip), %rdx      # 0xa343
leaq 0x80(%rsp), %rdi
callq 0x862a
movq %rax, %rdi
callq 0x3610
movq %rax, 0x40(%rsp)
testq %rax, %rax
je 0x41ca
leaq 0x66b2(%rip), %rsi      # 0xa355
leaq 0x80(%rsp), %rdi
movl $0xa, %edx
callq 0x870c
movl %eax, 0x18(%rsp)
leaq 0x66ab(%rip), %rsi      # 0xa36b
leaq 0x80(%rsp), %rdi
movl $0x64, %edx
callq 0x870c
movl %eax, 0x14(%rsp)
leaq 0x66a5(%rip), %rsi      # 0xa382
leaq 0x80(%rsp), %rdi
movl $0x3, %edx
callq 0x870c
movl %eax, 0x10(%rsp)
leaq 0x669f(%rip), %rsi      # 0xa399
leaq 0x80(%rsp), %rdi
movl $0x2, %edx
callq 0x870c
movl %eax, 0xc(%rsp)
movq %r12, 0x48(%rsp)
movl %ebp, 0x8(%rsp)
movq %r15, 0x38(%rsp)
movl %ebx, 0x4(%rsp)
movl %r13d, 0x1c(%rsp)
leaq 0x667a(%rip), %rdx      # 0xa3a8
leaq 0x118(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x8948
leaq 0x120(%rsp), %rax
movq 0x10(%rax), %r13
cmpq %rax, %r13
setne %r15b
je 0x3e0b
movb $0x1, %r15b
leaq 0x60(%rsp), %r12
leaq 0x50(%rsp), %rbp
movq 0x20(%r13), %rdx
leaq 0x80(%rsp), %rdi
leaq 0x662c(%rip), %rsi      # 0xa3a8
leaq 0x662f(%rip), %rcx      # 0xa3b2
xorl %r8d, %r8d
callq 0x86b4
testq %rax, %rax
je 0x3e0b
movq %rax, %rdi
callq 0x3610
testq %rax, %rax
je 0x3e0b
movq %rax, %rbx
movq %r12, 0x50(%rsp)
movq 0x20(%r13), %rsi
movq 0x28(%r13), %rdx
addq %rsi, %rdx
movq %rbp, %rdi
callq 0x45fc
movq %rbx, 0x70(%rsp)
movl $0xffffffff, 0x78(%rsp) # imm = 0xFFFFFFFF
leaq 0xc70c(%rip), %rdi      # 0x104d8
movq %rbp, %rsi
callq 0x4430
movq 0x50(%rsp), %rdi
cmpq %r12, %rdi
je 0x3deb
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x3340
movq %r13, %rdi
callq 0x33d0
movq %rax, %r13
leaq 0x120(%rsp), %rax
cmpq %rax, %r13
setne %r15b
jne 0x3d69
leaq 0x118(%rsp), %rdi
callq 0x468c
leaq 0x80(%rsp), %rdi
callq 0x4704
testb $0x1, %r15b
jne 0x41d7
cmpl $0x0, (%rsp)
movl 0x1c(%rsp), %ebp
movq 0x48(%rsp), %r12
je 0x3f0e
movl $0x1, %edi
movl $0x1, %esi
callq 0x3400
testl %eax, %eax
jne 0x41e6
callq 0x30c0
testl %eax, %eax
jne 0x41df
callq 0x3390
leaq 0x80(%rsp), %rbx
movl $0x0, 0x88(%rbx)
leaq 0x45c(%rip), %rax       # 0x42e0
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 0x41df
movslq 0x34(%rsp), %rbx
movslq 0x30(%rsp), %r15
movl $0x218, %edi            # imm = 0x218
callq 0x3330
movq %rax, %r14
movl 0xc55b(%rip), %edx      # 0x10490
movq %rax, %rdi
movq %r12, %rsi
movq %rbx, %rcx
movq %r15, %r8
callq 0x72e4
leaq 0xc5bb(%rip), %rax      # 0x10508
movq %r14, (%rax)
cmpl $0x4, (%r14)
jl 0x3f6c
leaq 0x6236(%rip), %rdx      # 0xa193
movq %r14, %rdi
movl $0x4, %esi
xorl %eax, %eax
callq 0x7310
movl $0xb0, %edi
callq 0x3330
movq %rax, %r14
movslq 0x2c(%rsp), %rsi
movq %rax, %rdi
callq 0x4352
movl $0xb0, %edi
callq 0x3330
movq %rax, %r15
movslq 0x28(%rsp), %rsi
movq %rax, %rdi
callq 0x4352
movq 0x40(%rsp), %rdi
callq 0x38db
movq %rax, %r12
leaq 0x1930(%rip), %rax      # 0x58e4
movq %rax, 0x8(%r12)
movq %r15, 0x10(%r12)
movl $0x70, %edi
callq 0x3330
movq %rax, %rbp
movq %rax, %rdi
movq %r15, %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x4a28
imull $0x3e8, 0x24(%rsp), %eax # imm = 0x3E8
movl %eax, 0x58(%rbp)
imull $0x3e8, 0x20(%rsp), %eax # imm = 0x3E8
movl %eax, 0x5c(%rbp)
imull $0x3e8, 0x18(%rsp), %eax # imm = 0x3E8
movl %eax, 0x60(%rbp)
movl 0x14(%rsp), %eax
movl %eax, 0x64(%rbp)
imull $0x3e8, 0x10(%rsp), %eax # imm = 0x3E8
movl %eax, 0x68(%rbp)
imull $0x3e8, 0xc(%rsp), %eax # imm = 0x3E8
movl %eax, 0x6c(%rbp)
movq %rbp, %rdi
movq 0x38(%rsp), %rbx
movq %rbx, %rsi
movq %r14, %rdx
movq %r12, %rcx
movl 0x8(%rsp), %r8d
callq 0x4bb4
testl %eax, %eax
js 0x410b
cmpq $0x0, 0xc4bc(%rip)      # 0x10500
je 0x4148
movq 0xc49f(%rip), %r15      # 0x104f0
leaq 0xc488(%rip), %rax      # 0x104e0
cmpq %rax, %r15
sete %bl
je 0x4138
movl $0x0, (%rsp)
movq 0x40(%r15), %rsi
movq %rbp, %rdi
movq %r14, %rdx
movq %r12, %rcx
movl $0x1, %r8d
callq 0x4cfe
movl %eax, %r13d
testl %eax, %eax
js 0x40b9
movl %r13d, 0x48(%r15)
leaq 0xc473(%rip), %rax      # 0x10508
movq (%rax), %rdi
cmpl $0x5, (%rdi)
jl 0x40e6
movq 0x20(%r15), %rcx
movl $0x5, %esi
leaq 0x6127(%rip), %rdx      # 0xa1d4
movl %r13d, %r8d
xorl %eax, %eax
callq 0x7310
jmp 0x40e6
leaq 0xc448(%rip), %rax      # 0x10508
movq (%rax), %rdi
movl $0x2, (%rsp)
cmpl $0x0, (%rdi)
jle 0x40e6
movq 0x20(%r15), %rcx
movl $0x1, %esi
leaq 0x60dd(%rip), %rdx      # 0xa1bc
xorl %eax, %eax
callq 0x7310
testl %r13d, %r13d
js 0x413f
movq %r15, %rdi
callq 0x3200
movq %rax, %r15
leaq 0xc3e3(%rip), %rax      # 0x104e0
cmpq %rax, %r15
sete %bl
jne 0x406b
jmp 0x413f
leaq 0xc3f6(%rip), %rax      # 0x10508
movq (%rax), %rdi
cmpl $0x0, (%rdi)
jle 0x4198
leaq 0x6082(%rip), %rdx      # 0xa1a3
movl $0x1, %r15d
movl $0x1, %esi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x7310
jmp 0x419e
movl $0x0, (%rsp)
testb $0x1, %bl
movl (%rsp), %r15d
je 0x419e
movq (%r12), %rax
movq %r12, %rdi
callq *0x20(%rax)
testl %eax, %eax
js 0x4205
movl $0x20, %edi
callq 0x3330
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
movq %r12, %rdx
movl 0x4(%rsp), %ecx
callq 0x56dc
movq %r15, %rdi
callq 0x5814
movq %rbp, %rdi
callq 0x55f2
movq (%r12), %rax
movq %r12, %rdi
callq *0x28(%rax)
xorl %r15d, %r15d
jmp 0x419e
movl $0x1, %r15d
movl %r15d, %eax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x68, %eax
jne 0x41c0
movq (%rbx), %rdi
callq 0x42bd
movl %eax, %edi
movq %rbx, %rsi
callq 0x3674
leaq 0x80(%rsp), %rdi
callq 0x4704
movq %r14, %rdi
callq 0x3650
xorl %edi, %edi
callq 0x3420
leaq 0x5f7a(%rip), %rdi      # 0xa167
leaq 0x5f7f(%rip), %rsi      # 0xa173
leaq 0x5f81(%rip), %rcx      # 0xa17c
movl $0xd3, %edx
callq 0x31f0
leaq 0xc2fc(%rip), %rax      # 0x10508
movq (%rax), %rdi
cmpl $0x0, (%rdi)
jle 0x4227
leaq 0x5fcf(%rip), %rdx      # 0xa1ea
movl $0x1, %esi
xorl %eax, %eax
callq 0x7310
movl $0x1, %edi
callq 0x3420
movq %rax, %rbx
movl $0x20, %esi
jmp 0x4250
movq %rax, %rbx
movl $0x70, %esi
movq %rbp, %rdi
jmp 0x426a
movq %rax, %rbx
movl $0xb0, %esi
movq %r15, %rdi
jmp 0x426a
movq %rax, %rbx
movl $0xb0, %esi
jmp 0x4267
movq %rax, %rbx
movl $0x218, %esi            # imm = 0x218
movq %r14, %rdi
callq 0x3340
jmp 0x42b5
jmp 0x42a5
jmp 0x42a5
movq %rax, %rbx
movq 0x50(%rsp), %rdi
cmpq %r12, %rdi
je 0x4296
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x3340
jmp 0x4296
jmp 0x4293
movq %rax, %rbx
leaq 0x118(%rsp), %rdi
callq 0x468c
jmp 0x42a8
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x4704
movq %rbx, %rdi
callq 0x35d0