// File: /workspace/github_cpp_makefile_star2_O2/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 $0x168, %rsp            # imm = 0x168
movq %rsi, %rbx
movl %edi, %ebp
leaq 0x67ff(%rip), %rax      # 0xa117
movq %rax, 0x10(%rsp)
pushq $0x1
popq %rax
movl %eax, 0xc(%rsp)
movb $0x1, %al
movl %eax, 0x8(%rsp)
leaq 0x67fa(%rip), %r14      # 0xa12b
movq 0xc688(%rip), %r15      # 0xffc0
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x30a0
cmpl $0x63, %eax
jne 0x3954
movq (%r15), %rax
movq %rax, 0x10(%rsp)
jmp 0x3938
cmpl $-0x1, %eax
je 0x397b
cmpl $0x64, %eax
je 0x3971
cmpl $0x76, %eax
jne 0x3fed
movl $0x0, 0x8(%rsp)
jmp 0x3938
movl $0x0, 0xc(%rsp)
jmp 0x3938
leaq 0x78(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq 0x70(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x7b5a
testl %eax, %eax
js 0x4021
leaq 0x687d(%rip), %rsi      # 0xa230
leaq 0x687f(%rip), %rdx      # 0xa239
leaq 0x70(%rsp), %rdi
callq 0x7dcc
movq %rax, %rbx
leaq 0x6871(%rip), %rsi      # 0xa23f
movq %rax, %rdi
callq 0x34e0
testl %eax, %eax
je 0x3a31
leaq 0x6858(%rip), %rsi      # 0xa239
movq %rbx, %rdi
callq 0x34e0
testl %eax, %eax
je 0x3a35
leaq 0x6851(%rip), %rsi      # 0xa245
movq %rbx, %rdi
callq 0x34e0
testl %eax, %eax
je 0x3a39
leaq 0x6843(%rip), %rsi      # 0xa24a
movq %rbx, %rdi
callq 0x34e0
testl %eax, %eax
je 0x3a3d
leaq 0x6835(%rip), %rsi      # 0xa24f
movq %rbx, %rdi
callq 0x34e0
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
leal (%rcx,%rcx,2), %ebp
addl $0x2, %ebp
jmp 0x3a40
pushq $0x1
jmp 0x3a3f
pushq $0x2
jmp 0x3a3f
pushq $0x3
jmp 0x3a3f
pushq $0x4
popq %rbp
leaq 0x680e(%rip), %rsi      # 0xa255
leaq 0x6813(%rip), %rdx      # 0xa261
leaq 0x70(%rsp), %rdi
callq 0x7dcc
movq %rax, %rdi
callq 0x3640
movq %rax, 0x68(%rsp)
testq %rax, %rax
je 0x4021
leaq 0x67ff(%rip), %rsi      # 0xa274
leaq 0x70(%rsp), %rdi
movl $0xa00000, %edx         # imm = 0xA00000
callq 0x7e8c
movl %eax, %r12d
leaq 0x67f5(%rip), %rsi      # 0xa283
leaq 0x70(%rsp), %rdi
pushq $0xa
popq %rdx
callq 0x7e8c
movl %eax, %r15d
leaq 0x67eb(%rip), %rsi      # 0xa290
leaq 0x67f2(%rip), %rdx      # 0xa29e
leaq 0x70(%rsp), %rdi
callq 0x7dcc
movq %rax, %rdi
callq 0x3640
testq %rax, %rax
je 0x4021
movq %rax, %r14
leaq 0x67da(%rip), %rsi      # 0xa2ab
leaq 0x70(%rsp), %rdi
movl $0x400, %edx            # imm = 0x400
callq 0x7e8c
leaq 0x67d2(%rip), %rsi      # 0xa2b9
leaq 0x70(%rsp), %rdi
movl $0x2000, %edx           # imm = 0x2000
callq 0x7e8c
movl %eax, 0x40(%rsp)
leaq 0x67cc(%rip), %rsi      # 0xa2cd
leaq 0x70(%rsp), %rdi
pushq $0x8
popq %rdx
callq 0x7e8c
movl %eax, %ebx
leaq 0x67c8(%rip), %rsi      # 0xa2df
leaq 0x70(%rsp), %rdi
movl $0x2000, %edx           # imm = 0x2000
callq 0x7e8c
movl %eax, 0x44(%rsp)
leaq 0x67c0(%rip), %rsi      # 0xa2f1
leaq 0x70(%rsp), %rdi
movl $0x4000, %edx           # imm = 0x4000
callq 0x7e8c
movl %eax, 0x3c(%rsp)
leaq 0x67b9(%rip), %rsi      # 0xa304
leaq 0x70(%rsp), %rdi
pushq $0x1e
popq %rdx
callq 0x7e8c
movl %eax, 0x38(%rsp)
leaq 0x67b7(%rip), %rsi      # 0xa31a
leaq 0x70(%rsp), %rdi
pushq $0x1e
popq %rdx
callq 0x7e8c
movl %eax, 0x34(%rsp)
leaq 0x67b6(%rip), %rsi      # 0xa331
leaq 0x67c1(%rip), %rdx      # 0xa343
leaq 0x70(%rsp), %rdi
callq 0x7dcc
movq %rax, %rdi
callq 0x3640
movq %rax, 0x58(%rsp)
testq %rax, %rax
je 0x4021
leaq 0x67ac(%rip), %rsi      # 0xa355
leaq 0x70(%rsp), %rdi
pushq $0xa
popq %rdx
callq 0x7e8c
movl %eax, 0x2c(%rsp)
leaq 0x67aa(%rip), %rsi      # 0xa36b
leaq 0x70(%rsp), %rdi
pushq $0x64
popq %rdx
callq 0x7e8c
movl %eax, 0x28(%rsp)
leaq 0x67a9(%rip), %rsi      # 0xa382
leaq 0x70(%rsp), %rdi
pushq $0x3
popq %rdx
callq 0x7e8c
movl %eax, 0x24(%rsp)
leaq 0x67a8(%rip), %rsi      # 0xa399
leaq 0x70(%rsp), %rdi
pushq $0x2
popq %rdx
callq 0x7e8c
movl %eax, 0x20(%rsp)
movl %ebp, 0x30(%rsp)
movq %r14, 0x60(%rsp)
movl %ebx, 0x1c(%rsp)
leaq 0x6792(%rip), %rdx      # 0xa3a8
leaq 0x108(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x80c6
movslq %r12d, %rax
movq %rax, 0x50(%rsp)
movslq %r15d, %rax
movq %rax, 0x48(%rsp)
leaq 0x110(%rsp), %rbp
movq 0x10(%rbp), %r15
leaq 0x70(%rsp), %r12
leaq 0x138(%rsp), %rbx
leaq 0xc8a0(%rip), %r14      # 0x104f8
cmpq %rbp, %r15
je 0x3cd3
movq 0x20(%r15), %rdx
movq %r12, %rdi
leaq 0x673d(%rip), %rsi      # 0xa3a8
leaq 0x6740(%rip), %rcx      # 0xa3b2
xorl %r8d, %r8d
callq 0x7e34
testq %rax, %rax
je 0x4014
movq %rax, %rdi
callq 0x3640
testq %rax, %rax
je 0x4014
movq %rax, %r13
leaq 0x20(%r15), %rsi
movq %rbx, %rdi
callq 0x34b0
movq %r13, 0x158(%rsp)
orl $-0x1, 0x160(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4248
movq %rbx, %rdi
callq 0x3150
movq %r15, %rdi
callq 0x33e0
movq %rax, %r15
jmp 0x3c58
leaq 0x108(%rsp), %rdi
callq 0x444a
leaq 0x70(%rsp), %rdi
callq 0x44ae
cmpl $0x0, 0xc(%rsp)
movq 0x60(%rsp), %rbp
je 0x3d9e
pushq $0x1
popq %rdi
movl %edi, %esi
callq 0x3410
testl %eax, %eax
jne 0x4057
callq 0x30c0
testl %eax, %eax
jne 0x4050
callq 0x33a0
leaq 0x70(%rsp), %rbx
andl $0x0, 0x88(%rbx)
leaq 0x405(%rip), %rax       # 0x4136
movq %rax, (%rbx)
pushq $0xf
popq %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x3440
pushq $0x2
popq %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x3440
pushq $0x1
popq %rdi
pushq $0x1
popq %rbx
movq %rbx, %rsi
callq 0x3400
pushq $0x3
popq %rdi
movq %rbx, %rsi
callq 0x3400
pushq $0xd
popq %rdi
movq %rbx, %rsi
callq 0x3400
pushq $0x11
popq %rdi
movq %rbx, %rsi
callq 0x3400
pushq $0x14
popq %rdi
movq %rbx, %rsi
callq 0x3400
pushq $0x15
popq %rdi
movq %rbx, %rsi
callq 0x3400
pushq $0x16
popq %rdi
movq %rbx, %rsi
callq 0x3400
testb $0x1, 0x8(%rsp)
je 0x4050
movl $0x218, %edi            # imm = 0x218
callq 0x3340
movq %rax, %r14
movq %rax, %rdi
movq 0x68(%rsp), %rsi
movl 0x30(%rsp), %edx
movq 0x50(%rsp), %rcx
movq 0x48(%rsp), %r8
callq 0x6cf0
leaq 0xc750(%rip), %rax      # 0x10528
movq %r14, (%rax)
cmpl $0x4, (%r14)
jl 0x3df5
leaq 0x63ab(%rip), %rdx      # 0xa193
pushq $0x4
popq %rsi
movq %r14, %rdi
xorl %eax, %eax
callq 0x6d1a
movl $0xb0, %edi
callq 0x3340
movq %rax, %r14
movslq 0x44(%rsp), %rsi
movq %rax, %rdi
callq 0x41a4
movl $0xb0, %edi
callq 0x3340
movq %rax, %r13
movslq 0x3c(%rsp), %rsi
movq %rax, %rdi
callq 0x41a4
movq 0x58(%rsp), %rdi
callq 0x3825
movq %rax, %r15
leaq 0x1783(%rip), %rax      # 0x55c0
movq %rax, 0x8(%r15)
movq %r13, 0x10(%r15)
pushq $0x70
popq %rdi
callq 0x3340
movq %rax, %r12
movq %rax, %rdi
movq %r13, %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x4858
imull $0x3e8, 0x38(%rsp), %eax # imm = 0x3E8
movl %eax, 0x58(%r12)
imull $0x3e8, 0x34(%rsp), %eax # imm = 0x3E8
movl %eax, 0x5c(%r12)
imull $0x3e8, 0x2c(%rsp), %eax # imm = 0x3E8
movl %eax, 0x60(%r12)
movl 0x28(%rsp), %eax
movl %eax, 0x64(%r12)
imull $0x3e8, 0x24(%rsp), %eax # imm = 0x3E8
movl %eax, 0x68(%r12)
imull $0x3e8, 0x20(%rsp), %eax # imm = 0x3E8
movl %eax, 0x6c(%r12)
movq %r12, %rdi
movq %rbp, %rsi
movq %r14, %rdx
movq %r15, %rcx
movl 0x40(%rsp), %r8d
callq 0x49cc
testl %eax, %eax
js 0x3f87
cmpq $0x0, 0xc650(%rip)      # 0x10520
je 0x3f3c
movq 0xc637(%rip), %rbx      # 0x10510
leaq 0xc620(%rip), %r13      # 0x10500
pushq $0x1
popq %rbp
cmpq %r13, %rbx
je 0x3f3c
movq 0x40(%rbx), %rsi
movq %r12, %rdi
movq %r14, %rdx
movq %r15, %rcx
movl %ebp, %r8d
callq 0x4b0a
testl %eax, %eax
js 0x3fae
movl %eax, 0x48(%rbx)
leaq 0xc619(%rip), %rcx      # 0x10528
movq (%rcx), %rdi
cmpl $0x5, (%rdi)
jl 0x3f2f
movq 0x20(%rbx), %rcx
pushq $0x5
popq %rsi
leaq 0x62af(%rip), %rdx      # 0xa1d4
movl %eax, %r8d
xorl %eax, %eax
callq 0x6d1a
movq %rbx, %rdi
callq 0x3210
movq %rax, %rbx
jmp 0x3ee3
movq (%r15), %rax
movq %r15, %rdi
callq *0x20(%rax)
testl %eax, %eax
js 0x4076
pushq $0x20
popq %rdi
callq 0x3340
movq %rax, %r13
movq %rax, %rdi
movq %r14, %rsi
movq %r15, %rdx
movl 0x1c(%rsp), %ecx
callq 0x53ec
movq %r13, %rdi
callq 0x551c
movq %r12, %rdi
callq 0x52e4
movq (%r15), %rax
movq %r15, %rdi
callq *0x28(%rax)
xorl %ebx, %ebx
jmp 0x3fd9
leaq 0xc59a(%rip), %rax      # 0x10528
movq (%rax), %rdi
cmpl $0x0, (%rdi)
jle 0x3fd6
leaq 0x6206(%rip), %rdx      # 0xa1a3
pushq $0x1
popq %rbx
movl %ebx, %esi
movq %rbp, %rcx
xorl %eax, %eax
callq 0x6d1a
jmp 0x3fd9
leaq 0xc573(%rip), %rax      # 0x10528
movq (%rax), %rdi
cmpl $0x0, (%rdi)
jle 0x3fd2
movq 0x20(%rbx), %rcx
leaq 0x61f4(%rip), %rdx      # 0xa1bc
pushq $0x1
popq %rsi
xorl %eax, %eax
callq 0x6d1a
pushq $0x2
jmp 0x3fd8
pushq $0x1
popq %rbx
movl %ebx, %eax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x68, %eax
je 0x400c
movq 0xbfdf(%rip), %rcx      # 0xffd8
movq (%rcx), %rdi
leaq 0x612e(%rip), %rsi      # 0xa131
movl %eax, %edx
xorl %eax, %eax
callq 0x3510
movq (%rbx), %rdi
callq 0x4113
leaq 0x108(%rsp), %rdi
callq 0x444a
leaq 0x70(%rsp), %rdi
callq 0x44ae
movq 0xbfa6(%rip), %rax      # 0xffd8
movq (%rax), %rdi
leaq 0x610b(%rip), %rsi      # 0xa147
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x3510
pushq $0x1
popq %rdi
callq 0x3430
xorl %edi, %edi
callq 0x3430
leaq 0x6109(%rip), %rdi      # 0xa167
leaq 0x610e(%rip), %rsi      # 0xa173
leaq 0x6110(%rip), %rcx      # 0xa17c
movl $0xd3, %edx
callq 0x3200
leaq 0xc4ab(%rip), %rax      # 0x10528
movq (%rax), %rdi
cmpl $0x0, (%rdi)
jle 0x4048
leaq 0x615e(%rip), %rdx      # 0xa1ea
pushq $0x1
popq %rsi
xorl %eax, %eax
callq 0x6d1a
jmp 0x4048
movq %rax, %rbx
pushq $0x20
popq %rsi
jmp 0x40b3
movq %rax, %rbx
pushq $0x70
popq %rsi
movq %r12, %rdi
jmp 0x40cd
movq %rax, %rbx
movl $0xb0, %esi
movq %r13, %rdi
jmp 0x40cd
movq %rax, %rbx
movl $0xb0, %esi
jmp 0x40ca
movq %rax, %rbx
movl $0x218, %esi            # imm = 0x218
movq %r14, %rdi
callq 0x3350
jmp 0x410b
jmp 0x40d8
jmp 0x40d8
movq %rax, %rbx
jmp 0x4101
jmp 0x40f1
movq %rax, %rbx
leaq 0x138(%rsp), %rdi
callq 0x3150
jmp 0x40f4
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
callq 0x444a
leaq 0x70(%rsp), %rdi
callq 0x44ae
movq %rbx, %rdi
callq 0x3600