// File: /workspace/github_c_makefile_star2_O1/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 0x71c(%rip), %rbx       # 0x1f539
movl $0x2, %edi
movq %rbx, %rsi
callq 0x3440
movl $0xf, %edi
movq %rbx, %rsi
callq 0x3440
movq 0xe80a(%rip), %rdi      # 0x2d648
testq %rdi, %rdi
je 0x1ee48
cmpb $0x0, (%rdi)
jne 0x1ee59
movq 0xe801(%rip), %rdi      # 0x2d650
testq %rdi, %rdi
je 0x1ee5e
cmpb $0x0, (%rdi)
je 0x1ee5e
callq 0x17ff3
cmpl $0x0, 0xe867(%rip)      # 0x2d6cc
sete %al
movq 0xe7e9(%rip), %rsi      # 0x2d658
testq %rsi, %rsi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x1eeb8
cmpb $0x0, (%rsi)
je 0x1eeb8
leaq 0xe7a8(%rip), %rdi      # 0x2d630
xorl %edx, %edx
callq 0x1379c
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x1eecd
movl $0x1, 0xe82a(%rip)      # 0x2d6cc
movq 0xe79f(%rip), %rdi      # 0x2d648
testq %rdi, %rdi
je 0x1eeb8
cmpb $0x0, (%rdi)
je 0x1eeb8
callq 0x17ff3
leaq 0xe771(%rip), %rdi      # 0x2d630
callq 0x131f5
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testl %eax, %eax
je 0x1eede
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0xe7dc(%rip), %ebp      # 0x2d6c0
xorl %eax, %eax
callq 0x17d59
movl %ebp, (%rax)
movq 0xe76c(%rip), %rdi      # 0x2d660
testq %rdi, %rdi
je 0x1ef3e
cmpb $0x0, (%rdi)
je 0x1ef3e
leaq 0xee9b(%rip), %rsi      # 0x2dda0
movl $0x8, %edx
callq 0x18bb0
movl %eax, 0xf64b(%rip)      # 0x2e560
cmpl $-0x1, %eax
je 0x1f105
testl %eax, %eax
jle 0x1ef3e
movl %eax, %eax
leaq 0xef05(%rip), %rcx      # 0x2de30
xorl %edx, %edx
movl %edx, (%rcx)
incq %rdx
addq $0xf8, %rcx
cmpq %rdx, %rax
jne 0x1ef2d
movq 0xe723(%rip), %rax      # 0x2d668
testq %rax, %rax
je 0x1efb4
cmpb $0x0, (%rax)
je 0x1efb4
xorl %eax, %eax
callq 0x12d86
movq %rax, 0xf60b(%rip)      # 0x2e568
testq %rax, %rax
je 0x1f147
movq 0xe6fb(%rip), %rsi      # 0x2d668
movq %rax, %rdi
callq 0x126db
testl %eax, %eax
je 0x1efb4
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x1eecd
movq 0xe6d8(%rip), %r9       # 0x2d668
leaq 0x63fb(%rip), %rsi      # 0x25392
leaq 0x7009(%rip), %rdx      # 0x25fa7
leaq 0x7024(%rip), %r8       # 0x25fc9
movl $0x3, %edi
movl $0x44b, %ecx            # imm = 0x44B
jmp 0x1f13b
movq 0xe6b5(%rip), %rax      # 0x2d670
testq %rax, %rax
je 0x1f02a
cmpb $0x0, (%rax)
je 0x1f02a
xorl %eax, %eax
callq 0x12d86
movq %rax, 0xf59d(%rip)      # 0x2e570
testq %rax, %rax
je 0x1f17b
movq 0xe68d(%rip), %rsi      # 0x2d670
movq %rax, %rdi
callq 0x126db
testl %eax, %eax
je 0x1f02a
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x1eecd
movq 0xe66a(%rip), %r9       # 0x2d670
leaq 0x6385(%rip), %rsi      # 0x25392
leaq 0x6f93(%rip), %rdx      # 0x25fa7
leaq 0x6fc5(%rip), %r8       # 0x25fe0
movl $0x3, %edi
movl $0x456, %ecx            # imm = 0x456
jmp 0x1f13b
movq 0xe647(%rip), %r15      # 0x2d678
testq %r15, %r15
je 0x1f1eb
cmpb $0x0, (%r15)
je 0x1f1eb
movq %r15, %rdi
callq 0x3170
addl $0x7, %eax
movslq %eax, %r12
movq %r12, %rdi
callq 0x34b0
testq %rax, %rax
je 0x1f1af
movq %rax, %r14
leaq 0x6f8b(%rip), %rdx      # 0x25ff8
movq %rax, %rdi
movq %r12, %rsi
movq %r15, %rcx
xorl %eax, %eax
callq 0x3590
movl 0xf4dc(%rip), %r9d      # 0x2e560
subq $0x8, %rsp
leaq 0xf4e9(%rip), %rdi      # 0x2e578
leaq 0x3568(%rip), %rsi      # 0x225fe
leaq 0xe593(%rip), %rcx      # 0x2d630
leaq 0xecfc(%rip), %r8       # 0x2dda0
movq %r14, %rdx
pushq $0x0
pushq 0xf4c1(%rip)            # 0x2e570
pushq 0xf4b3(%rip)            # 0x2e568
callq 0x831c
addq $0x20, %rsp
testl %eax, %eax
je 0x1f1e3
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x1f0f8
leaq 0x62b9(%rip), %rsi      # 0x25392
leaq 0x6ec7(%rip), %rdx      # 0x25fa7
leaq 0x6f1a(%rip), %r8       # 0x26001
movl $0x3, %edi
movl $0x465, %ecx            # imm = 0x465
xorl %eax, %eax
callq 0x17d92
movq %r14, %rdi
callq 0x3450
jmp 0x1eecd
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x1eecd
movq 0xe544(%rip), %r9       # 0x2d660
leaq 0x626f(%rip), %rsi      # 0x25392
leaq 0x6e7d(%rip), %rdx      # 0x25fa7
leaq 0x5633(%rip), %r8       # 0x24764
movl $0x3, %edi
movl $0x43d, %ecx            # imm = 0x43D
xorl %eax, %eax
callq 0x17d92
jmp 0x1eecd
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x1eecd
leaq 0x6234(%rip), %rsi      # 0x25392
leaq 0x6e42(%rip), %rdx      # 0x25fa7
leaq 0x6e4a(%rip), %r8       # 0x25fb6
movl $0x3, %edi
movl $0x447, %ecx            # imm = 0x447
jmp 0x1f29f
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x1eecd
leaq 0x6200(%rip), %rsi      # 0x25392
leaq 0x6e0e(%rip), %rdx      # 0x25fa7
leaq 0x6e16(%rip), %r8       # 0x25fb6
movl $0x3, %edi
movl $0x452, %ecx            # imm = 0x452
jmp 0x1f29f
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x1eecd
leaq 0x61cc(%rip), %rsi      # 0x25392
leaq 0x6dda(%rip), %rdx      # 0x25fa7
leaq 0x46cf(%rip), %r8       # 0x238a3
movl $0x3, %edi
movl $0x45f, %ecx            # imm = 0x45F
jmp 0x1f29f
movq %r14, %rdi
callq 0x3450
leaq 0xe43e(%rip), %rdi      # 0x2d630
callq 0x1502f
testl %eax, %eax
je 0x1f22c
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x1eecd
leaq 0x6180(%rip), %rsi      # 0x25392
leaq 0x6d8e(%rip), %rdx      # 0x25fa7
leaq 0x6dff(%rip), %r8       # 0x2601f
movl $0x3, %edi
movl $0x46d, %ecx            # imm = 0x46D
jmp 0x1f29f
movl 0xf32d(%rip), %r9d      # 0x2e560
subq $0x8, %rsp
leaq 0xf342(%rip), %rdi      # 0x2e580
leaq 0x3efd(%rip), %rsi      # 0x23142
leaq 0xe3e4(%rip), %rcx      # 0x2d630
leaq 0xeb4d(%rip), %r8       # 0x2dda0
xorl %edx, %edx
pushq $0x0
pushq 0xf313(%rip)            # 0x2e570
pushq 0xf305(%rip)            # 0x2e568
callq 0x831c
addq $0x20, %rsp
testl %eax, %eax
je 0x1f2ab
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x1eecd
leaq 0x610b(%rip), %rsi      # 0x25392
leaq 0x6d19(%rip), %rdx      # 0x25fa7
leaq 0x6d9d(%rip), %r8       # 0x26032
movl $0x3, %edi
movl $0x473, %ecx            # imm = 0x473
xorl %eax, %eax
callq 0x17d92
jmp 0x1eecd
movq 0xe3ee(%rip), %r12      # 0x2d6a0
testq %r12, %r12
je 0x1f2db
cmpq $0x0, (%r12)
je 0x1f2db
movl 0xf2c4(%rip), %eax      # 0x2e588
leaq 0x8(%r12), %rcx
incl %eax
cmpq $0x0, (%rcx)
leaq 0x8(%rcx), %rcx
jne 0x1f2c9
movl %eax, 0xf2ad(%rip)      # 0x2e588
movslq 0xf2a6(%rip), %rdi      # 0x2e588
shlq $0x3, %rdi
callq 0x34b0
movq %rax, 0xf29e(%rip)      # 0x2e590
testq %rax, %rax
je 0x1f49d
testq %r12, %r12
je 0x1f36d
movq (%r12), %rsi
testq %rsi, %rsi
je 0x1f36d
movq 0xe398(%rip), %r13      # 0x2d6a8
xorl %ebp, %ebp
leaq 0xe317(%rip), %r14      # 0x2d630
leaq 0xea80(%rip), %r15      # 0x2dda0
movq 0xf269(%rip), %rdi      # 0x2e590
addq %rbp, %rdi
movq (%r13,%rbp), %rdx
movl 0xf22a(%rip), %r9d      # 0x2e560
subq $0x8, %rsp
movq %r14, %rcx
movq %r15, %r8
pushq $0x0
pushq 0xf228(%rip)            # 0x2e570
pushq 0xf21a(%rip)            # 0x2e568
callq 0x831c
addq $0x20, %rsp
testl %eax, %eax
jne 0x1f4d1
movq 0x8(%r12,%rbp), %rsi
addq $0x8, %rbp
testq %rsi, %rsi
jne 0x1f320
movq 0xe2bc(%rip), %rdi      # 0x2d630
movq 0xe2bd(%rip), %r8       # 0x2d638
leaq 0xf21e(%rip), %rsi      # 0x2e5a0
movl $0x8, %edx
movl $0x90, %ecx
callq 0x18ca3
movl %eax, 0xf689(%rip)      # 0x2ea20
testl %eax, %eax
js 0x1eecd
je 0x1f505
leaq 0xf1f4(%rip), %rdi      # 0x2e5a0
movl %eax, %esi
callq 0x190bb
testl %eax, %eax
jne 0x1eecd
xorl %eax, %eax
callq 0x17d59
cmpl $0x5, (%rax)
jl 0x1f3f4
leaq 0x5fc4(%rip), %rsi      # 0x25392
leaq 0x6bd2(%rip), %rdx      # 0x25fa7
leaq 0x5fa2(%rip), %r8       # 0x2537e
leaq 0x6b5a(%rip), %r9       # 0x25f3d
movl $0x5, %edi
movl $0x4af, %ecx            # imm = 0x4AF
xorl %eax, %eax
callq 0x17d92
movl 0xf626(%rip), %esi      # 0x2ea20
leaq 0xf19f(%rip), %rdi      # 0x2e5a0
callq 0x19111
leaq 0xe223(%rip), %rdi      # 0x2d630
callq 0x13354
movq 0xe277(%rip), %rax      # 0x2d690
testq %rax, %rax
je 0x1f46d
cmpb $0x0, (%rax)
je 0x1f46d
cmpq $0x0, 0xf155(%rip)      # 0x2e580
je 0x1f46d
movb $0x1, 0xf5f0(%rip)      # 0x2ea24
xorl %eax, %eax
callq 0x17d59
cmpl $0x5, (%rax)
jl 0x1f46d
movq 0xe249(%rip), %r9       # 0x2d690
leaq 0x5f44(%rip), %rsi      # 0x25392
leaq 0x6b52(%rip), %rdx      # 0x25fa7
leaq 0x6c13(%rip), %r8       # 0x2606f
movl $0x5, %edi
movl $0x4b6, %ecx            # imm = 0x4B6
xorl %eax, %eax
callq 0x17d92
movq 0xe214(%rip), %rsi      # 0x2d688
xorl %ebx, %ebx
testq %rsi, %rsi
je 0x1eecd
cmpb $0x0, (%rsi)
je 0x1eecd
movq 0xf0f1(%rip), %rdi      # 0x2e580
xorl %ebx, %ebx
xorl %edx, %edx
callq 0x97d0
jmp 0x1eecd
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x1eecd
leaq 0x5ede(%rip), %rsi      # 0x25392
leaq 0x6aec(%rip), %rdx      # 0x25fa7
leaq 0x6b84(%rip), %r8       # 0x26046
movl $0x3, %edi
movl $0x48a, %ecx            # imm = 0x48A
jmp 0x1f29f
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x1eecd
leaq 0x5eaa(%rip), %rsi      # 0x25392
leaq 0x6ab8(%rip), %rdx      # 0x25fa7
leaq 0x6b61(%rip), %r8       # 0x26057
movl $0x3, %edi
movl $0x495, %ecx            # imm = 0x495
jmp 0x1f29f
xorl %eax, %eax
callq 0x17d59
cmpl $0x3, (%rax)
jl 0x1eecd
leaq 0x5e76(%rip), %rsi      # 0x25392
leaq 0x6a84(%rip), %rdx      # 0x25fa7
leaq 0x6b39(%rip), %r8       # 0x26063
movl $0x3, %edi
movl $0x4a8, %ecx            # imm = 0x4A8
jmp 0x1f29f