// File: /workspace/github_c_makefile_star2_O3/GangZhuo[P]dohclient/src/main.c
// Function: init_dohclient
static int init_dohclient()
{
	int i;
	char** ch, ** args;

	srand((unsigned int)time(NULL));

#ifdef WINDOWS
	if (0 == SetConsoleCtrlHandler((PHANDLER_ROUTINE)sig_handler, TRUE)) {
		loge("can not set control handler\n");
		return EXIT_FAILURE;
	}
#else
	signal(SIGINT, sig_handler);
	signal(SIGTERM, sig_handler);
#endif

	if (conf.log_file && *conf.log_file) {
		open_logfile(conf.log_file);
	}
	else if (conf.launch_log && *conf.launch_log) {
		open_logfile(conf.launch_log);
	}

	if (!conf.is_config_file_readed && conf.config_file && *conf.config_file) {
		if (conf_load_from_file(&conf, conf.config_file, FALSE)) {
			return -1;
		}
		conf.is_config_file_readed = 1;
		if (conf.log_file && *conf.log_file) {
			open_logfile(conf.log_file);
		}
	}

	if (conf_check(&conf))
		return -1;

	loglevel = conf.log_level;

	if (conf.proxy && *conf.proxy) {
		proxy_num = str2proxies(
			conf.proxy,
			proxy_list,
			MAX_PROXY);
		if (proxy_num == -1) {
			loge("parse \"%s\" failed\n",
				conf.proxy);
			return -1;
		}
		for (i = 0; i < proxy_num; i++) {
			proxy_list[i].proxy_index = i;
		}
	}

	if (conf.chnroute && *conf.chnroute) {
		if ((chnr = chnroute_create()) == NULL) {
			loge("chnroute_create()\n");
			return -1;
		}
		if (chnroute_parse(chnr, conf.chnroute)) {
			loge("invalid chnroute \"%s\"\n", conf.chnroute);
			return -1;
		}
	}

	if (conf.blacklist && *conf.blacklist) {
		if ((blacklist = chnroute_create()) == NULL) {
			loge("chnroute_create()\n");
			return -1;
		}
		if (chnroute_parse(blacklist, conf.blacklist)) {
			loge("invalid blacklist \"%s\"\n", conf.blacklist);
			return -1;
		}
	}

	if (conf.hosts && *conf.hosts) {
		int argslen = strlen(conf.hosts) + sizeof("hosts=");
		char *args = (char*)malloc(argslen);
		if (!args) {
			loge("alloc\n");
			return -1;
		}
		snprintf(args, argslen, "hosts=%s", conf.hosts);
		if (channel_create(&hosts, "hosts", args,
			&conf, proxy_list, proxy_num, chnr, blacklist, NULL) != CHANNEL_OK) {
			loge("create \"hosts\" channel error\n");
			free(args);
			return -1;
		}
		free(args);
	}

	if (http_init(&conf) != 0) {
		loge("http_init() error\n");
		return -1;
	}

	if (channel_create(&cache, "cache", NULL,
		&conf, proxy_list, proxy_num, chnr, blacklist, NULL) != CHANNEL_OK) {
		loge("create cache error\n");
		return -1;
	}

#if DOHCLIENT_CACHE_API
	hsconf->cache = cache;
	hsconf->wwwroot = conf.wwwroot;
	if (conf.cache_api && *conf.cache_api) {
		if (cache_api_config(conf.cache_api) != 0) {
			loge("cache_api_config() error\n");
			return -1;
		}
	}
#endif

	ch = conf.channels;
	while (ch && *ch) {
		ch++;
		channel_num++;
	}

	channels = (channel_t**)malloc(sizeof(channel_t*) * channel_num);
	if (!channels) {
		loge("create channels\n");
		return -1;
	}

	ch = conf.channels;
	args = conf.channel_args;
	i = 0;
	while (ch && *ch) {
		if (channel_create(
			&channels[i], *ch, *args,
			&conf, proxy_list, proxy_num, chnr, blacklist, NULL) != CHANNEL_OK) {
			loge("no channel\n");
			return -1;
		}
		ch++;
		args++;
		i++;
	}

	listen_num = str2listens(
		conf.listen_addr,
		listens,
		MAX_LISTEN,
		sizeof(listen_t),
		conf.listen_port);

	if (listen_num < 0)
		return -1;

	if (listen_num == 0) {
		loge("no listens\n");
		return -1;
	}

	if (init_listens(listens, listen_num) != 0)
		return -1;

	logn(DOHCLIENT_NAME " v" DOHCLIENT_VERSION "%s\n", git_version());
	print_listens(listens, listen_num);
	conf_print(&conf);

	if (conf.cachedb_autosave && (*conf.cachedb_autosave) && cache) {
		save_cache_when_close = 1;
		logn("Set service flags to save cache to \"%s\" when service closing\n",
				conf.cachedb_autosave);
	}

	if (conf.cachedb && *conf.cachedb) {
		cache_load_cachedbs(cache, conf.cachedb, 0);
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
xorl %edi, %edi
callq 0x3310
movl %eax, %edi
callq 0x3350
leaq 0x7a5(%rip), %rbx       # 0x1edc6
movl $0x2, %edi
movq %rbx, %rsi
callq 0x3440
movl $0xf, %edi
movq %rbx, %rsi
callq 0x3440
movq 0xf006(%rip), %rdi      # 0x2d648
testq %rdi, %rdi
je 0x1e64c
cmpb $0x0, (%rdi)
jne 0x1e65d
movq 0xeffd(%rip), %rdi      # 0x2d650
testq %rdi, %rdi
je 0x1e662
cmpb $0x0, (%rdi)
je 0x1e662
callq 0x179aa
cmpl $0x0, 0xf063(%rip)      # 0x2d6cc
sete %al
movq 0xefe5(%rip), %rsi      # 0x2d658
testq %rsi, %rsi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x1e6bc
cmpb $0x0, (%rsi)
je 0x1e6bc
leaq 0xefa4(%rip), %rdi      # 0x2d630
xorl %edx, %edx
callq 0x13230
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x1e6d1
movl $0x1, 0xf026(%rip)      # 0x2d6cc
movq 0xef9b(%rip), %rdi      # 0x2d648
testq %rdi, %rdi
je 0x1e6bc
cmpb $0x0, (%rdi)
je 0x1e6bc
callq 0x179aa
leaq 0xef6d(%rip), %rdi      # 0x2d630
callq 0x12c89
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testl %eax, %eax
je 0x1e6e2
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0xefd8(%rip), %ebp      # 0x2d6c0
xorl %eax, %eax
callq 0x176e5
movl %ebp, (%rax)
movq 0xef68(%rip), %rdi      # 0x2d660
testq %rdi, %rdi
je 0x1e7cf
cmpb $0x0, (%rdi)
je 0x1e7cf
leaq 0xf68f(%rip), %rsi      # 0x2dda0
movl $0x8, %edx
callq 0x18541
movl %eax, 0xfe3f(%rip)      # 0x2e560
cmpl $-0x1, %eax
je 0x1e996
testl %eax, %eax
jle 0x1e7cf
movl %eax, %ecx
incl %eax
andl $-0x2, %eax
decq %rcx
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movdqa 0x6c42(%rip), %xmm1     # 0x25390
leaq 0xf6db(%rip), %rcx      # 0x2de30
xorl %edx, %edx
movdqa 0x6c41(%rip), %xmm2     # 0x253a0
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movdqa 0x6c41(%rip), %xmm4     # 0x253b0
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
movd %xmm5, %esi
notl %esi
testb $0x1, %sil
je 0x1e7a3
movl %edx, (%rcx)
pxor %xmm3, %xmm5
pextrw $0x4, %xmm5, %esi
testb $0x1, %sil
je 0x1e7bb
leal 0x1(%rdx), %esi
movl %esi, 0xf8(%rcx)
addq $0x2, %rdx
paddq %xmm4, %xmm1
addq $0x1f0, %rcx            # imm = 0x1F0
cmpq %rdx, %rax
jne 0x1e76f
movq 0xee92(%rip), %rax      # 0x2d668
testq %rax, %rax
je 0x1e845
cmpb $0x0, (%rax)
je 0x1e845
xorl %eax, %eax
callq 0x1282c
movq %rax, 0xfd7a(%rip)      # 0x2e568
testq %rax, %rax
je 0x1e9d8
movq 0xee6a(%rip), %rsi      # 0x2d668
movq %rax, %rdi
callq 0x12115
testl %eax, %eax
je 0x1e845
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x1e6d1
movq 0xee47(%rip), %r9       # 0x2d668
leaq 0x6bac(%rip), %rsi      # 0x253d4
leaq 0x77ba(%rip), %rdx      # 0x25fe9
leaq 0x77d5(%rip), %r8       # 0x2600b
movl $0x3, %edi
movl $0x44b, %ecx            # imm = 0x44B
jmp 0x1e9cc
movq 0xee24(%rip), %rax      # 0x2d670
testq %rax, %rax
je 0x1e8bb
cmpb $0x0, (%rax)
je 0x1e8bb
xorl %eax, %eax
callq 0x1282c
movq %rax, 0xfd0c(%rip)      # 0x2e570
testq %rax, %rax
je 0x1ea0c
movq 0xedfc(%rip), %rsi      # 0x2d670
movq %rax, %rdi
callq 0x12115
testl %eax, %eax
je 0x1e8bb
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x1e6d1
movq 0xedd9(%rip), %r9       # 0x2d670
leaq 0x6b36(%rip), %rsi      # 0x253d4
leaq 0x7744(%rip), %rdx      # 0x25fe9
leaq 0x7776(%rip), %r8       # 0x26022
movl $0x3, %edi
movl $0x456, %ecx            # imm = 0x456
jmp 0x1e9cc
movq 0xedb6(%rip), %r15      # 0x2d678
testq %r15, %r15
je 0x1ea7c
cmpb $0x0, (%r15)
je 0x1ea7c
movq %r15, %rdi
callq 0x3170
addl $0x7, %eax
movslq %eax, %r12
movq %r12, %rdi
callq 0x34b0
testq %rax, %rax
je 0x1ea40
movq %rax, %r14
leaq 0x773c(%rip), %rdx      # 0x2603a
movq %rax, %rdi
movq %r12, %rsi
movq %r15, %rcx
xorl %eax, %eax
callq 0x3590
movl 0xfc4b(%rip), %r9d      # 0x2e560
subq $0x8, %rsp
leaq 0xfc58(%rip), %rdi      # 0x2e578
leaq 0x3cb7(%rip), %rsi      # 0x225de
leaq 0xed02(%rip), %rcx      # 0x2d630
leaq 0xf46b(%rip), %r8       # 0x2dda0
movq %r14, %rdx
pushq $0x0
pushq 0xfc30(%rip)            # 0x2e570
pushq 0xfc22(%rip)            # 0x2e568
callq 0x8024
addq $0x20, %rsp
testl %eax, %eax
je 0x1ea74
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x1e989
leaq 0x6a6a(%rip), %rsi      # 0x253d4
leaq 0x7678(%rip), %rdx      # 0x25fe9
leaq 0x76cb(%rip), %r8       # 0x26043
movl $0x3, %edi
movl $0x465, %ecx            # imm = 0x465
xorl %eax, %eax
callq 0x1771e
movq %r14, %rdi
callq 0x3450
jmp 0x1e6d1
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x1e6d1
movq 0xecb3(%rip), %r9       # 0x2d660
leaq 0x6a20(%rip), %rsi      # 0x253d4
leaq 0x762e(%rip), %rdx      # 0x25fe9
leaq 0x5db2(%rip), %r8       # 0x24774
movl $0x3, %edi
movl $0x43d, %ecx            # imm = 0x43D
xorl %eax, %eax
callq 0x1771e
jmp 0x1e6d1
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x1e6d1
leaq 0x69e5(%rip), %rsi      # 0x253d4
leaq 0x75f3(%rip), %rdx      # 0x25fe9
leaq 0x75fb(%rip), %r8       # 0x25ff8
movl $0x3, %edi
movl $0x447, %ecx            # imm = 0x447
jmp 0x1eb30
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x1e6d1
leaq 0x69b1(%rip), %rsi      # 0x253d4
leaq 0x75bf(%rip), %rdx      # 0x25fe9
leaq 0x75c7(%rip), %r8       # 0x25ff8
movl $0x3, %edi
movl $0x452, %ecx            # imm = 0x452
jmp 0x1eb30
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x1e6d1
leaq 0x697d(%rip), %rsi      # 0x253d4
leaq 0x758b(%rip), %rdx      # 0x25fe9
leaq 0x4e4e(%rip), %r8       # 0x238b3
movl $0x3, %edi
movl $0x45f, %ecx            # imm = 0x45F
jmp 0x1eb30
movq %r14, %rdi
callq 0x3450
leaq 0xebad(%rip), %rdi      # 0x2d630
callq 0x14a9b
testl %eax, %eax
je 0x1eabd
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x1e6d1
leaq 0x6931(%rip), %rsi      # 0x253d4
leaq 0x753f(%rip), %rdx      # 0x25fe9
leaq 0x75b0(%rip), %r8       # 0x26061
movl $0x3, %edi
movl $0x46d, %ecx            # imm = 0x46D
jmp 0x1eb30
movl 0xfa9c(%rip), %r9d      # 0x2e560
subq $0x8, %rsp
leaq 0xfab1(%rip), %rdi      # 0x2e580
leaq 0x4674(%rip), %rsi      # 0x2314a
leaq 0xeb53(%rip), %rcx      # 0x2d630
leaq 0xf2bc(%rip), %r8       # 0x2dda0
xorl %edx, %edx
pushq $0x0
pushq 0xfa82(%rip)            # 0x2e570
pushq 0xfa74(%rip)            # 0x2e568
callq 0x8024
addq $0x20, %rsp
testl %eax, %eax
je 0x1eb3c
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x1e6d1
leaq 0x68bc(%rip), %rsi      # 0x253d4
leaq 0x74ca(%rip), %rdx      # 0x25fe9
leaq 0x754e(%rip), %r8       # 0x26074
movl $0x3, %edi
movl $0x473, %ecx            # imm = 0x473
xorl %eax, %eax
callq 0x1771e
jmp 0x1e6d1
movq 0xeb5d(%rip), %r12      # 0x2d6a0
movl 0xfa3f(%rip), %eax      # 0x2e588
testq %r12, %r12
je 0x1eb6c
cmpq $0x0, (%r12)
je 0x1eb6c
leaq 0x8(%r12), %rcx
incl %eax
cmpq $0x0, (%rcx)
leaq 0x8(%rcx), %rcx
jne 0x1eb5a
movl %eax, 0xfa1c(%rip)      # 0x2e588
movslq %eax, %rdi
shlq $0x3, %rdi
callq 0x34b0
movq %rax, 0xfa11(%rip)      # 0x2e590
testq %rax, %rax
je 0x1ed2a
testq %r12, %r12
je 0x1ebfa
movq (%r12), %rsi
testq %rsi, %rsi
je 0x1ebfa
movq 0xeb0b(%rip), %r13      # 0x2d6a8
xorl %ebp, %ebp
leaq 0xea8a(%rip), %r14      # 0x2d630
leaq 0xf1f3(%rip), %r15      # 0x2dda0
movq 0xf9dc(%rip), %rdi      # 0x2e590
addq %rbp, %rdi
movq (%r13,%rbp), %rdx
movl 0xf99d(%rip), %r9d      # 0x2e560
subq $0x8, %rsp
movq %r14, %rcx
movq %r15, %r8
pushq $0x0
pushq 0xf99b(%rip)            # 0x2e570
pushq 0xf98d(%rip)            # 0x2e568
callq 0x8024
addq $0x20, %rsp
testl %eax, %eax
jne 0x1ed5e
movq 0x8(%r12,%rbp), %rsi
addq $0x8, %rbp
testq %rsi, %rsi
jne 0x1ebad
movq 0xea2f(%rip), %rdi      # 0x2d630
movq 0xea30(%rip), %r8       # 0x2d638
leaq 0xf991(%rip), %rsi      # 0x2e5a0
movl $0x8, %edx
movl $0x90, %ecx
callq 0x18634
movl %eax, 0xfdfc(%rip)      # 0x2ea20
testl %eax, %eax
js 0x1e6d1
je 0x1ed92
leaq 0xf967(%rip), %rdi      # 0x2e5a0
movl %eax, %esi
callq 0x18a47
testl %eax, %eax
jne 0x1e6d1
xorl %eax, %eax
callq 0x176e5
cmpl $0x5, (%rax)
jl 0x1ec81
leaq 0x6779(%rip), %rsi      # 0x253d4
leaq 0x7387(%rip), %rdx      # 0x25fe9
leaq 0x6757(%rip), %r8       # 0x253c0
leaq 0x730f(%rip), %r9       # 0x25f7f
movl $0x5, %edi
movl $0x4af, %ecx            # imm = 0x4AF
xorl %eax, %eax
callq 0x1771e
movl 0xfd99(%rip), %esi      # 0x2ea20
leaq 0xf912(%rip), %rdi      # 0x2e5a0
callq 0x18a9d
leaq 0xe996(%rip), %rdi      # 0x2d630
callq 0x12de8
movq 0xe9ea(%rip), %rax      # 0x2d690
testq %rax, %rax
je 0x1ecfa
cmpb $0x0, (%rax)
je 0x1ecfa
cmpq $0x0, 0xf8c8(%rip)      # 0x2e580
je 0x1ecfa
movb $0x1, 0xfd63(%rip)      # 0x2ea24
xorl %eax, %eax
callq 0x176e5
cmpl $0x5, (%rax)
jl 0x1ecfa
movq 0xe9bc(%rip), %r9       # 0x2d690
leaq 0x66f9(%rip), %rsi      # 0x253d4
leaq 0x7307(%rip), %rdx      # 0x25fe9
leaq 0x73c8(%rip), %r8       # 0x260b1
movl $0x5, %edi
movl $0x4b6, %ecx            # imm = 0x4B6
xorl %eax, %eax
callq 0x1771e
movq 0xe987(%rip), %rsi      # 0x2d688
xorl %ebx, %ebx
testq %rsi, %rsi
je 0x1e6d1
cmpb $0x0, (%rsi)
je 0x1e6d1
movq 0xf864(%rip), %rdi      # 0x2e580
xorl %ebx, %ebx
xorl %edx, %edx
callq 0x94a4
jmp 0x1e6d1
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x1e6d1
leaq 0x6693(%rip), %rsi      # 0x253d4
leaq 0x72a1(%rip), %rdx      # 0x25fe9
leaq 0x7339(%rip), %r8       # 0x26088
movl $0x3, %edi
movl $0x48a, %ecx            # imm = 0x48A
jmp 0x1eb30
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x1e6d1
leaq 0x665f(%rip), %rsi      # 0x253d4
leaq 0x726d(%rip), %rdx      # 0x25fe9
leaq 0x7316(%rip), %r8       # 0x26099
movl $0x3, %edi
movl $0x495, %ecx            # imm = 0x495
jmp 0x1eb30
xorl %eax, %eax
callq 0x176e5
cmpl $0x3, (%rax)
jl 0x1e6d1
leaq 0x662b(%rip), %rsi      # 0x253d4
leaq 0x7239(%rip), %rdx      # 0x25fe9
leaq 0x72ee(%rip), %r8       # 0x260a5
movl $0x3, %edi
movl $0x4a8, %ecx            # imm = 0x4A8
jmp 0x1eb30