// File: /workspace/github_c_makefile_star2_O2/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 0x3320
movl %eax, %edi
callq 0x3360
leaq 0x6f8(%rip), %rbx       # 0x1d6a6
pushq $0x2
popq %rdi
movq %rbx, %rsi
callq 0x3450
pushq $0xf
popq %rdi
movq %rbx, %rsi
callq 0x3450
movq 0x1168d(%rip), %rdi     # 0x2e658
testq %rdi, %rdi
je 0x1cfd5
cmpb $0x0, (%rdi)
jne 0x1cfe6
movq 0x11684(%rip), %rdi     # 0x2e660
testq %rdi, %rdi
je 0x1cfeb
cmpb $0x0, (%rdi)
je 0x1cfeb
callq 0x168f3
cmpl $0x0, 0x116ea(%rip)     # 0x2e6dc
sete %al
movq 0x1166c(%rip), %rsi     # 0x2e668
testq %rsi, %rsi
setne %cl
andb %al, %cl
pushq $-0x1
popq %rbx
cmpb $0x1, %cl
jne 0x1d043
cmpb $0x0, (%rsi)
je 0x1d043
leaq 0x11628(%rip), %rdi     # 0x2e640
xorl %edx, %edx
callq 0x1265e
testl %eax, %eax
jne 0x1d053
movl $0x1, 0x116af(%rip)     # 0x2e6dc
movq 0x11624(%rip), %rdi     # 0x2e658
testq %rdi, %rdi
je 0x1d043
cmpb $0x0, (%rdi)
je 0x1d043
callq 0x168f3
leaq 0x115f6(%rip), %rdi     # 0x2e640
callq 0x12105
testl %eax, %eax
je 0x1d064
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x11666(%rip), %ebp     # 0x2e6d0
xorl %eax, %eax
callq 0x165f5
movl %ebp, (%rax)
movq 0x115f6(%rip), %rdi     # 0x2e670
testq %rdi, %rdi
je 0x1d0c3
cmpb $0x0, (%rdi)
je 0x1d0c3
leaq 0x11d25(%rip), %rsi     # 0x2edb0
pushq $0x8
popq %rdx
callq 0x17408
movl %eax, 0x124d7(%rip)     # 0x2f570
cmpl $-0x1, %eax
je 0x1d284
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
leaq 0x11d90(%rip), %rdx     # 0x2ee40
cmpq %rcx, %rax
je 0x1d0c3
movl %ecx, (%rdx)
incq %rcx
addq $0xf8, %rdx
jmp 0x1d0b0
movq 0x115ae(%rip), %rax     # 0x2e678
testq %rax, %rax
je 0x1d137
cmpb $0x0, (%rax)
je 0x1d137
xorl %eax, %eax
callq 0x11ce2
movq %rax, 0x12496(%rip)     # 0x2f578
testq %rax, %rax
je 0x1d2c4
movq 0x11586(%rip), %rsi     # 0x2e678
movq %rax, %rdi
callq 0x116bc
testl %eax, %eax
je 0x1d137
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x1d053
movq 0x11563(%rip), %r9      # 0x2e678
leaq 0x62a6(%rip), %rsi      # 0x233c2
leaq 0x6eb4(%rip), %rdx      # 0x23fd7
leaq 0x6ecf(%rip), %r8       # 0x23ff9
pushq $0x3
popq %rdi
movl $0x44b, %ecx            # imm = 0x44B
jmp 0x1d2b8
movq 0x11542(%rip), %rax     # 0x2e680
testq %rax, %rax
je 0x1d1ab
cmpb $0x0, (%rax)
je 0x1d1ab
xorl %eax, %eax
callq 0x11ce2
movq %rax, 0x1242a(%rip)     # 0x2f580
testq %rax, %rax
je 0x1d2f6
movq 0x1151a(%rip), %rsi     # 0x2e680
movq %rax, %rdi
callq 0x116bc
testl %eax, %eax
je 0x1d1ab
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x1d053
movq 0x114f7(%rip), %r9      # 0x2e680
leaq 0x6232(%rip), %rsi      # 0x233c2
leaq 0x6e40(%rip), %rdx      # 0x23fd7
leaq 0x6e72(%rip), %r8       # 0x24010
pushq $0x3
popq %rdi
movl $0x456, %ecx            # imm = 0x456
jmp 0x1d2b8
movq 0x114d6(%rip), %r15     # 0x2e688
testq %r15, %r15
je 0x1d362
cmpb $0x0, (%r15)
je 0x1d362
movq %r15, %rdi
callq 0x3170
addl $0x7, %eax
movslq %eax, %r12
movq %r12, %rdi
callq 0x34c0
testq %rax, %rax
je 0x1d328
movq %rax, %r14
leaq 0x6e3a(%rip), %rdx      # 0x24028
movq %rax, %rdi
movq %r12, %rsi
movq %r15, %rcx
xorl %eax, %eax
callq 0x35a0
movl 0x1236b(%rip), %r9d     # 0x2f570
subq $0x8, %rsp
leaq 0x12378(%rip), %rdi     # 0x2f588
leaq 0x33c7(%rip), %rsi      # 0x205de
leaq 0x11422(%rip), %rcx     # 0x2e640
leaq 0x11b8b(%rip), %r8      # 0x2edb0
movq %r14, %rdx
pushq $0x0
pushq 0x12350(%rip)           # 0x2f580
pushq 0x12342(%rip)           # 0x2f578
callq 0x7cd8
addq $0x20, %rsp
testl %eax, %eax
je 0x1d35a
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x1d277
leaq 0x6168(%rip), %rsi      # 0x233c2
leaq 0x6d76(%rip), %rdx      # 0x23fd7
leaq 0x6dc9(%rip), %r8       # 0x24031
pushq $0x3
popq %rdi
movl $0x465, %ecx            # imm = 0x465
xorl %eax, %eax
callq 0x1662e
movq %r14, %rdi
callq 0x3460
jmp 0x1d053
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x1d053
movq 0x113d5(%rip), %r9      # 0x2e670
leaq 0x6120(%rip), %rsi      # 0x233c2
leaq 0x6d2e(%rip), %rdx      # 0x23fd7
leaq 0x54e4(%rip), %r8       # 0x22794
pushq $0x3
popq %rdi
movl $0x43d, %ecx            # imm = 0x43D
xorl %eax, %eax
callq 0x1662e
jmp 0x1d053
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x1d053
leaq 0x60e7(%rip), %rsi      # 0x233c2
leaq 0x6cf5(%rip), %rdx      # 0x23fd7
leaq 0x6cfd(%rip), %r8       # 0x23fe6
pushq $0x3
popq %rdi
movl $0x447, %ecx            # imm = 0x447
jmp 0x1d424
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x1d053
leaq 0x60b5(%rip), %rsi      # 0x233c2
leaq 0x6cc3(%rip), %rdx      # 0x23fd7
leaq 0x6ccb(%rip), %r8       # 0x23fe6
pushq $0x3
popq %rdi
movl $0x452, %ecx            # imm = 0x452
jmp 0x1d424
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x1d053
leaq 0x6083(%rip), %rsi      # 0x233c2
leaq 0x6c91(%rip), %rdx      # 0x23fd7
leaq 0x4566(%rip), %r8       # 0x218b3
pushq $0x3
popq %rdi
movl $0x45f, %ecx            # imm = 0x45F
jmp 0x1d424
movq %r14, %rdi
callq 0x3460
leaq 0x112d7(%rip), %rdi     # 0x2e640
callq 0x13d74
testl %eax, %eax
je 0x1d3a4
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x1d053
leaq 0x6039(%rip), %rsi      # 0x233c2
leaq 0x6c47(%rip), %rdx      # 0x23fd7
leaq 0x6cb8(%rip), %r8       # 0x2404f
pushq $0x3
popq %rdi
movl $0x46d, %ecx            # imm = 0x46D
jmp 0x1d424
movl 0x121c5(%rip), %r9d     # 0x2f570
movq 0x121c6(%rip), %rax     # 0x2f578
movq 0x121c7(%rip), %rcx     # 0x2f580
subq $0x20, %rsp
andq $0x0, 0x10(%rsp)
movq %rcx, 0x8(%rsp)
movq %rax, (%rsp)
leaq 0x121bd(%rip), %rdi     # 0x2f590
leaq 0x3d70(%rip), %rsi      # 0x2114a
leaq 0x1125f(%rip), %rcx     # 0x2e640
leaq 0x119c8(%rip), %r8      # 0x2edb0
xorl %edx, %edx
callq 0x7cd8
addq $0x20, %rsp
testl %eax, %eax
je 0x1d430
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x1d053
leaq 0x5fb4(%rip), %rsi      # 0x233c2
leaq 0x6bc2(%rip), %rdx      # 0x23fd7
leaq 0x6c46(%rip), %r8       # 0x24062
pushq $0x3
popq %rdi
movl $0x473, %ecx            # imm = 0x473
xorl %eax, %eax
callq 0x1662e
jmp 0x1d053
movq 0x11279(%rip), %r12     # 0x2e6b0
movslq 0x1215a(%rip), %rdi     # 0x2f598
leal 0x1(%rdi), %eax
shlq $0x3, %rdi
movq %r12, %rcx
testq %rcx, %rcx
je 0x1d465
cmpq $0x0, (%rcx)
je 0x1d465
addq $0x8, %rcx
movl %eax, 0x1213b(%rip)     # 0x2f598
addq $0x8, %rdi
incl %eax
jmp 0x1d448
callq 0x34c0
movq %rax, 0x1212f(%rip)     # 0x2f5a0
testq %rax, %rax
je 0x1d610
movq 0x11237(%rip), %r13     # 0x2e6b8
xorl %ebp, %ebp
leaq 0x111b6(%rip), %r14     # 0x2e640
leaq 0x1191f(%rip), %r15     # 0x2edb0
movq %r12, %rax
addq %rbp, %rax
je 0x1d4e6
movq (%rax), %rsi
testq %rsi, %rsi
je 0x1d4e6
movq 0x120f8(%rip), %rdi     # 0x2f5a0
addq %rbp, %rdi
movq (%r13,%rbp), %rdx
movl 0x120b9(%rip), %r9d     # 0x2f570
subq $0x8, %rsp
movq %r14, %rcx
movq %r15, %r8
pushq $0x0
pushq 0x120b7(%rip)           # 0x2f580
pushq 0x120a9(%rip)           # 0x2f578
callq 0x7cd8
addq $0x20, %rsp
testl %eax, %eax
jne 0x1d642
addq $0x8, %rbp
jmp 0x1d491
movq 0x11153(%rip), %rdi     # 0x2e640
movq 0x11154(%rip), %r8      # 0x2e648
leaq 0x120b5(%rip), %rsi     # 0x2f5b0
pushq $0x8
popq %rdx
movl $0x90, %ecx
callq 0x174e2
movl %eax, 0x12522(%rip)     # 0x2fa30
testl %eax, %eax
js 0x1d053
je 0x1d674
leaq 0x1208d(%rip), %rdi     # 0x2f5b0
movl %eax, %esi
callq 0x178c6
testl %eax, %eax
jne 0x1d053
xorl %eax, %eax
callq 0x165f5
cmpl $0x5, (%rax)
jl 0x1d569
leaq 0x5e7d(%rip), %rsi      # 0x233c2
leaq 0x6a8b(%rip), %rdx      # 0x23fd7
leaq 0x5e5b(%rip), %r8       # 0x233ae
leaq 0x6a13(%rip), %r9       # 0x23f6d
pushq $0x5
popq %rdi
movl $0x4af, %ecx            # imm = 0x4AF
xorl %eax, %eax
callq 0x1662e
movl 0x124c1(%rip), %esi     # 0x2fa30
leaq 0x1203a(%rip), %rdi     # 0x2f5b0
callq 0x17919
leaq 0x110be(%rip), %rdi     # 0x2e640
callq 0x12249
movq 0x11112(%rip), %rax     # 0x2e6a0
testq %rax, %rax
je 0x1d5e0
cmpb $0x0, (%rax)
je 0x1d5e0
cmpq $0x0, 0x11ff0(%rip)     # 0x2f590
je 0x1d5e0
movb $0x1, 0x1248b(%rip)     # 0x2fa34
xorl %eax, %eax
callq 0x165f5
cmpl $0x5, (%rax)
jl 0x1d5e0
movq 0x110e4(%rip), %r9      # 0x2e6a0
leaq 0x5dff(%rip), %rsi      # 0x233c2
leaq 0x6a0d(%rip), %rdx      # 0x23fd7
leaq 0x6ace(%rip), %r8       # 0x2409f
pushq $0x5
popq %rdi
movl $0x4b6, %ecx            # imm = 0x4B6
xorl %eax, %eax
callq 0x1662e
movq 0x110b1(%rip), %rsi     # 0x2e698
xorl %ebx, %ebx
testq %rsi, %rsi
je 0x1d053
cmpb $0x0, (%rsi)
je 0x1d053
movq 0x11f8e(%rip), %rdi     # 0x2f590
xorl %ebx, %ebx
xorl %edx, %edx
callq 0x9123
jmp 0x1d053
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x1d053
leaq 0x5d9b(%rip), %rsi      # 0x233c2
leaq 0x69a9(%rip), %rdx      # 0x23fd7
leaq 0x6a41(%rip), %r8       # 0x24076
pushq $0x3
popq %rdi
movl $0x48a, %ecx            # imm = 0x48A
jmp 0x1d424
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x1d053
leaq 0x5d69(%rip), %rsi      # 0x233c2
leaq 0x6977(%rip), %rdx      # 0x23fd7
leaq 0x6a20(%rip), %r8       # 0x24087
pushq $0x3
popq %rdi
movl $0x495, %ecx            # imm = 0x495
jmp 0x1d424
xorl %eax, %eax
callq 0x165f5
cmpl $0x3, (%rax)
jl 0x1d053
leaq 0x5d37(%rip), %rsi      # 0x233c2
leaq 0x6945(%rip), %rdx      # 0x23fd7
leaq 0x69fa(%rip), %r8       # 0x24093
pushq $0x3
popq %rdi
movl $0x4a8, %ecx            # imm = 0x4A8
jmp 0x1d424