// File: /workspace/github_c_makefile_star2_O0/GangZhuo[P]dohclient/src/dohclient-cache.c
// Function: send_by_udp
static int send_by_udp(const char *pkg, int len)
{
	struct sockaddr *to;
	socklen_t tolen;
	sock_t sock;
	fd_set readset, writeset, errorset;
	struct timeval tv;
	int is_sent = FALSE;
	time_t begin_time;
	time_t now;

	to = (struct sockaddr*)(&server_addr->addr);
	tolen = server_addr->addrlen;

	sock = socket(to->sa_family, SOCK_DGRAM, IPPROTO_UDP);

	if (sock == -1) {
		fprintf(stderr, "send_by_udp() error: "
				"create socket error. errno=%d, %s\n",
				errno, strerror(errno));
		return -1;
	}

	if (setnonblock(sock) != 0) {
		fprintf(stderr, "send_by_udp() error: set sock non-block failed. errno=%d, %s\n",
			errno, strerror(errno));
		close(sock);
		return -1;
	}

	if (setreuseaddr(sock) != 0) {
		fprintf(stderr, "send_by_udp() error: set sock reuse address failed. errno=%d, %s\n",
			errno, strerror(errno));
		close(sock);
		return -1;
	}

#ifdef WINDOWS
	disable_udp_connreset(sock);
#endif

	begin_time = time(NULL);

	while (1) {

		FD_ZERO(&readset);
		FD_ZERO(&writeset);
		FD_ZERO(&errorset);

		if (is_sent) {
			FD_SET(sock, &readset);
		}
		else {
			FD_SET(sock, &writeset);
		}

		FD_SET(sock, &errorset);

		tv.tv_sec = 0;
		tv.tv_usec = 50 * 1000;

		if (select(sock + 1, &readset, &writeset, &errorset, &tv) == -1) {
			fprintf(stderr, "select() error: errno=%d, %s \n",
				errno, strerror(errno));
			close(sock);
			return -1;
		}

		if (FD_ISSET(sock, &errorset)) {
			fprintf(stderr, "send_by_udp() error: sock error: errno=%d, %s\n",
					errno, strerror(errno));
			close(sock);
			return -1;
		}

		if (FD_ISSET(sock, &readset)) {
			int nread;
			struct sockaddr_storage from[1] = {0};
			socklen_t fromlen[1] = { sizeof(*from) };

			nread = recvfrom(sock, recvbuffer, sizeof(recvbuffer) - 1, 0,
					(struct sockaddr*)from, fromlen);
			if (nread > 0) {
				recvbuffer[nread] = '\0';
				fprintf(stdout, "%s\n", recvbuffer);
				break;
			}
			else {
				fprintf(stderr, "send_by_udp() error: recvfrom() error: errno=%d, %s\n",
						errno, strerror(errno));
				close(sock);
				return -1;
			}
		}

		if (FD_ISSET(sock, &writeset)) {
			int nsend = sendto(sock, pkg, len, 0, to, tolen);
			if (nsend == -1) {
				fprintf(stderr, "send_by_udp() error: sendto() error: to=%s, errno=%d, %s\n",
					get_addrname(to), errno, strerror(errno));
				close(sock);
				return -1;
			}
			else {
				is_sent = TRUE;
			}
		}

		now = time(NULL);

		if ((int)(now - begin_time) > timeout) {
			fprintf(stderr, "send_by_udp() error: timeout\n");
			close(sock);
			return -1;
		}
	}

	close(sock);

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x330, %rsp            # imm = 0x330
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl $0x0, -0x1bc(%rbp)
leaq 0x57ad(%rip), %rax      # 0xe3e0
movq %rax, -0x20(%rbp)
movl 0x5823(%rip), %eax      # 0xe460
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rax
movzwl (%rax), %edi
movl $0x2, %esi
movl $0x11, %edx
callq 0x20b0
movl %eax, -0x28(%rbp)
cmpl $-0x1, -0x28(%rbp)
jne 0x8cb3
movq 0x5372(%rip), %rax      # 0xdfd8
movq (%rax), %rax
movq %rax, -0x2a8(%rbp)
callq 0x2030
movl (%rax), %eax
movl %eax, -0x29c(%rbp)
callq 0x2030
movl (%rax), %edi
callq 0x20d0
movq -0x2a8(%rbp), %rdi
movl -0x29c(%rbp), %edx
movq %rax, %rcx
leaq 0x24a2(%rip), %rsi      # 0xb142
movb $0x0, %al
callq 0x2300
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x92e9
movl -0x28(%rbp), %edi
callq 0x2fc0
cmpl $0x0, %eax
je 0x8d1c
movq 0x5311(%rip), %rax      # 0xdfd8
movq (%rax), %rax
movq %rax, -0x2b8(%rbp)
callq 0x2030
movl (%rax), %eax
movl %eax, -0x2ac(%rbp)
callq 0x2030
movl (%rax), %edi
callq 0x20d0
movq -0x2b8(%rbp), %rdi
movl -0x2ac(%rbp), %edx
movq %rax, %rcx
leaq 0x2479(%rip), %rsi      # 0xb17a
movb $0x0, %al
callq 0x2300
movl -0x28(%rbp), %edi
callq 0x23a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x92e9
movl -0x28(%rbp), %edi
callq 0x30d0
cmpl $0x0, %eax
je 0x8d85
movq 0x52a8(%rip), %rax      # 0xdfd8
movq (%rax), %rax
movq %rax, -0x2c8(%rbp)
callq 0x2030
movl (%rax), %eax
movl %eax, -0x2bc(%rbp)
callq 0x2030
movl (%rax), %edi
callq 0x20d0
movq -0x2c8(%rbp), %rdi
movl -0x2bc(%rbp), %edx
movq %rax, %rcx
leaq 0x244e(%rip), %rsi      # 0xb1b8
movb $0x0, %al
callq 0x2300
movl -0x28(%rbp), %edi
callq 0x23a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x92e9
xorl %eax, %eax
movl %eax, %edi
callq 0x21f0
movq %rax, -0x1c8(%rbp)
jmp 0x8d97
leaq -0xa8(%rbp), %rax
movq %rax, -0x1e0(%rbp)
movl $0x0, -0x1d4(%rbp)
movl -0x1d4(%rbp), %eax
cmpq $0x10, %rax
jae 0x8de1
movq -0x1e0(%rbp), %rax
movl -0x1d4(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x1d4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1d4(%rbp)
jmp 0x8daf
jmp 0x8de3
jmp 0x8de5
leaq -0x128(%rbp), %rax
movq %rax, -0x1f0(%rbp)
movl $0x0, -0x1e4(%rbp)
movl -0x1e4(%rbp), %eax
cmpq $0x10, %rax
jae 0x8e2f
movq -0x1f0(%rbp), %rax
movl -0x1e4(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x1e4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1e4(%rbp)
jmp 0x8dfd
jmp 0x8e31
jmp 0x8e33
leaq -0x1a8(%rbp), %rax
movq %rax, -0x200(%rbp)
movl $0x0, -0x1f4(%rbp)
movl -0x1f4(%rbp), %eax
cmpq $0x10, %rax
jae 0x8e7d
movq -0x200(%rbp), %rax
movl -0x1f4(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x1f4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1f4(%rbp)
jmp 0x8e4b
jmp 0x8e7f
cmpl $0x0, -0x1bc(%rbp)
je 0x8ec1
movl -0x28(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0x28(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0xa8(%rbp,%rax,8), %rcx
movq %rcx, -0xa8(%rbp,%rax,8)
jmp 0x8ef8
movl -0x28(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0x28(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0x128(%rbp,%rax,8), %rcx
movq %rcx, -0x128(%rbp,%rax,8)
movl -0x28(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0x28(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0x1a8(%rbp,%rax,8), %rcx
movq %rcx, -0x1a8(%rbp,%rax,8)
movq $0x0, -0x1b8(%rbp)
movq $0xc350, -0x1b0(%rbp) # imm = 0xC350
movl -0x28(%rbp), %edi
addl $0x1, %edi
leaq -0xa8(%rbp), %rsi
leaq -0x128(%rbp), %rdx
leaq -0x1a8(%rbp), %rcx
leaq -0x1b8(%rbp), %r8
callq 0x2200
cmpl $-0x1, %eax
jne 0x8fcd
movq 0x5060(%rip), %rax      # 0xdfd8
movq (%rax), %rax
movq %rax, -0x2d8(%rbp)
callq 0x2030
movl (%rax), %eax
movl %eax, -0x2cc(%rbp)
callq 0x2030
movl (%rax), %edi
callq 0x20d0
movq -0x2d8(%rbp), %rdi
movl -0x2cc(%rbp), %edx
movq %rax, %rcx
leaq 0x20b3(%rip), %rsi      # 0xb065
movb $0x0, %al
callq 0x2300
movl -0x28(%rbp), %edi
callq 0x23a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x92e9
movl -0x28(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0x1a8(%rbp,%rax,8), %rax
movq %rax, -0x2e0(%rbp)
movl -0x28(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x2e0(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x906d
movq 0x4fc0(%rip), %rax      # 0xdfd8
movq (%rax), %rax
movq %rax, -0x2f0(%rbp)
callq 0x2030
movl (%rax), %eax
movl %eax, -0x2e4(%rbp)
callq 0x2030
movl (%rax), %edi
callq 0x20d0
movq -0x2f0(%rbp), %rdi
movl -0x2e4(%rbp), %edx
movq %rax, %rcx
leaq 0x21a8(%rip), %rsi      # 0xb1fa
movb $0x0, %al
callq 0x2300
movl -0x28(%rbp), %edi
callq 0x23a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x92e9
movl -0x28(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0xa8(%rbp,%rax,8), %rax
movq %rax, -0x2f8(%rbp)
movl -0x28(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x2f8(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x9199
leaq -0x290(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x2120
movl 0x21ea(%rip), %eax      # 0xb2b8
movl %eax, -0x294(%rbp)
movl -0x28(%rbp), %edi
leaq -0x290(%rbp), %r8
leaq -0x294(%rbp), %r9
leaq 0x5384(%rip), %rsi      # 0xe470
movl $0x1fffff, %edx         # imm = 0x1FFFFF
xorl %ecx, %ecx
callq 0x2190
movl %eax, -0x204(%rbp)
cmpl $0x0, -0x204(%rbp)
jle 0x913d
movslq -0x204(%rbp), %rcx
leaq 0x535b(%rip), %rax      # 0xe470
movb $0x0, (%rax,%rcx)
movq 0x4e98(%rip), %rax      # 0xdfb8
movq (%rax), %rdi
leaq 0x193f(%rip), %rsi      # 0xaa69
leaq 0x533f(%rip), %rdx      # 0xe470
movb $0x0, %al
callq 0x2300
jmp 0x92da
movq 0x4e94(%rip), %rax      # 0xdfd8
movq (%rax), %rax
movq %rax, -0x308(%rbp)
callq 0x2030
movl (%rax), %eax
movl %eax, -0x2fc(%rbp)
callq 0x2030
movl (%rax), %edi
callq 0x20d0
movq -0x308(%rbp), %rdi
movl -0x2fc(%rbp), %edx
movq %rax, %rcx
leaq 0x20ab(%rip), %rsi      # 0xb229
movb $0x0, %al
callq 0x2300
movl -0x28(%rbp), %edi
callq 0x23a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x92e9
movl -0x28(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0x128(%rbp,%rax,8), %rax
movq %rax, -0x310(%rbp)
movl -0x28(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x310(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x9286
movl -0x28(%rbp), %edi
movq -0x10(%rbp), %rsi
movslq -0x14(%rbp), %rdx
movq -0x20(%rbp), %r8
movl -0x24(%rbp), %r9d
xorl %ecx, %ecx
callq 0x2060
movl %eax, -0x298(%rbp)
cmpl $-0x1, -0x298(%rbp)
jne 0x927a
movq 0x4dc7(%rip), %rax      # 0xdfd8
movq (%rax), %rax
movq %rax, -0x328(%rbp)
movq -0x20(%rbp), %rdi
callq 0x32b0
movq %rax, -0x320(%rbp)
callq 0x2030
movl (%rax), %eax
movl %eax, -0x314(%rbp)
callq 0x2030
movl (%rax), %edi
callq 0x20d0
movq -0x328(%rbp), %rdi
movq -0x320(%rbp), %rdx
movl -0x314(%rbp), %ecx
movq %rax, %r8
leaq 0x1ffc(%rip), %rsi      # 0xb25e
movb $0x0, %al
callq 0x2300
movl -0x28(%rbp), %edi
callq 0x23a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x92e9
movl $0x1, -0x1bc(%rbp)
jmp 0x9286
xorl %eax, %eax
movl %eax, %edi
callq 0x21f0
movq %rax, -0x1d0(%rbp)
movq -0x1d0(%rbp), %rax
subq -0x1c8(%rbp), %rax
cmpl 0x500e(%rip), %eax      # 0xe2b8
jle 0x92d5
movq 0x4d25(%rip), %rax      # 0xdfd8
movq (%rax), %rdi
leaq 0x1fdb(%rip), %rsi      # 0xb298
movb $0x0, %al
callq 0x2300
movl -0x28(%rbp), %edi
callq 0x23a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x92e9
jmp 0x8d95
movl -0x28(%rbp), %edi
callq 0x23a0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x330, %rsp            # imm = 0x330
popq %rbp
retq
nopw %cs:(%rax,%rax)