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

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

	s->array = (char*)pkg - 2;
	s->pos = 0;
	s->size = len + 2;
	s->cap = len + 2;

	stream_seti16(s, 0, len);

	sock = socket(to->sa_family, SOCK_STREAM, IPPROTO_TCP);

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

	if (setnonblock(sock) != 0) {
		fprintf(stderr, "send_by_tcp() 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_tcp() error: set sock reuse address failed. errno=%d, %s\n",
			errno, strerror(errno));
		close(sock);
		return -1;
	}

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

	if (connect(sock, to, tolen) != 0) {
		int err = errno;
		if (!is_eagain(err)) {
			fprintf(stderr, "connect() error: errno=%d, %s - %s\n",
				errno, strerror(errno), get_addrname(to));
			close(sock);
			return -1;
		}
	}

	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, "send_by_tcp() error: select() error: errno=%d, %s \n",
				errno, strerror(errno));
			close(sock);
			return -1;
		}

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

		if (FD_ISSET(sock, &readset)) {
			int nread;

			nread = tcp_recv(sock, s->array + s->size, s->cap - s->size - 1);
			if (nread > 0) {
				s->array[s->size + nread] = '\0';
				if (totalsize == -1) {
					s->size += nread;
					if (s->size >= 2) {
						totalsize = stream_readi16(s);
						readsize = s->size - 2;
						s->size = 0;
						fprintf(stdout, "%s", s->array + 2);
					}
				}
				else {
					fprintf(stdout, "%s", s->array);
					readsize += nread;
				}
				if (readsize >= totalsize) {
					fprintf(stdout, "\n");
					break;
				}
			}
			else if (nread < 0) {
				if (errno != 0) {
					fprintf(stderr, "send_by_tcp() error: tcp_recv() error: errno=%d, %s\n",
						errno, strerror(errno));
				}
				close(sock);
				return -1;
			}
		}

		if (FD_ISSET(sock, &writeset)) {
			int nsend = tcp_send(sock, s);
			if (nsend == -1) {
				fprintf(stderr, "send_by_tcp() error: tcp_send() error: to=%s, errno=%d, %s\n",
					get_addrname(to), errno, strerror(errno));
				close(sock);
				return -1;
			}
			else { /* Prepare recv stream */
				s->array = recvbuffer;
				s->pos = 0;
				s->size = 0;
				s->cap = sizeof(recvbuffer);
				is_sent = TRUE;
			}
		}

		now = time(NULL);

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

	close(sock);

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2e0, %rsp            # imm = 0x2E0
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
leaq -0x1d0(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x2120
movl $0x0, -0x1d4(%rbp)
movl $0xffffffff, -0x1ec(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x1f0(%rbp)
leaq 0x60f6(%rip), %rax      # 0xe3e0
movq %rax, -0x20(%rbp)
movl 0x616c(%rip), %eax      # 0xe460
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rax
addq $-0x2, %rax
movq %rax, -0x1d0(%rbp)
movl $0x0, -0x1c4(%rbp)
movl -0x14(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x1c8(%rbp)
movl -0x14(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x1c0(%rbp)
leaq -0x1d0(%rbp), %rdi
movl -0x14(%rbp), %edx
xorl %esi, %esi
movl $0x2, %ecx
callq 0x5960
movq -0x20(%rbp), %rax
movzwl (%rax), %edi
movl $0x1, %esi
movl $0x6, %edx
callq 0x20b0
movl %eax, -0x28(%rbp)
cmpl $-0x1, -0x28(%rbp)
jne 0x83b1
movq 0x5c74(%rip), %rax      # 0xdfd8
movq (%rax), %rax
movq %rax, -0x238(%rbp)
callq 0x2030
movl (%rax), %eax
movl %eax, -0x22c(%rbp)
callq 0x2030
movl (%rax), %edi
callq 0x20d0
movq -0x238(%rbp), %rdi
movl -0x22c(%rbp), %edx
movq %rax, %rcx
leaq 0x2b9a(%rip), %rsi      # 0xaf38
movb $0x0, %al
callq 0x2300
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x8bf5
movl -0x28(%rbp), %edi
callq 0x2fc0
cmpl $0x0, %eax
je 0x841a
movq 0x5c13(%rip), %rax      # 0xdfd8
movq (%rax), %rax
movq %rax, -0x248(%rbp)
callq 0x2030
movl (%rax), %eax
movl %eax, -0x23c(%rbp)
callq 0x2030
movl (%rax), %edi
callq 0x20d0
movq -0x248(%rbp), %rdi
movl -0x23c(%rbp), %edx
movq %rax, %rcx
leaq 0x2b71(%rip), %rsi      # 0xaf70
movb $0x0, %al
callq 0x2300
movl -0x28(%rbp), %edi
callq 0x23a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x8bf5
movl -0x28(%rbp), %edi
callq 0x30d0
cmpl $0x0, %eax
je 0x8483
movq 0x5baa(%rip), %rax      # 0xdfd8
movq (%rax), %rax
movq %rax, -0x258(%rbp)
callq 0x2030
movl (%rax), %eax
movl %eax, -0x24c(%rbp)
callq 0x2030
movl (%rax), %edi
callq 0x20d0
movq -0x258(%rbp), %rdi
movl -0x24c(%rbp), %edx
movq %rax, %rcx
leaq 0x2b46(%rip), %rsi      # 0xafae
movb $0x0, %al
callq 0x2300
movl -0x28(%rbp), %edi
callq 0x23a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x8bf5
movl -0x28(%rbp), %edi
callq 0x3170
cmpl $0x0, %eax
je 0x84ec
movq 0x5b41(%rip), %rax      # 0xdfd8
movq (%rax), %rax
movq %rax, -0x268(%rbp)
callq 0x2030
movl (%rax), %eax
movl %eax, -0x25c(%rbp)
callq 0x2030
movl (%rax), %edi
callq 0x20d0
movq -0x268(%rbp), %rdi
movl -0x25c(%rbp), %edx
movq %rax, %rcx
leaq 0x2b1f(%rip), %rsi      # 0xaff0
movb $0x0, %al
callq 0x2300
movl -0x28(%rbp), %edi
callq 0x23a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x8bf5
movl -0x28(%rbp), %edi
movq -0x20(%rbp), %rsi
movl -0x24(%rbp), %edx
callq 0x21c0
cmpl $0x0, %eax
je 0x85b2
callq 0x2030
movl (%rax), %eax
movl %eax, -0x1f4(%rbp)
cmpl $0xb, -0x1f4(%rbp)
je 0x85b0
cmpl $0x73, -0x1f4(%rbp)
je 0x85b0
cmpl $0xb, -0x1f4(%rbp)
je 0x85b0
cmpl $0x73, -0x1f4(%rbp)
je 0x85b0
movq 0x5a94(%rip), %rax      # 0xdfd8
movq (%rax), %rax
movq %rax, -0x280(%rbp)
callq 0x2030
movl (%rax), %eax
movl %eax, -0x274(%rbp)
callq 0x2030
movl (%rax), %edi
callq 0x20d0
movq %rax, -0x270(%rbp)
movq -0x20(%rbp), %rdi
callq 0x32b0
movq -0x280(%rbp), %rdi
movl -0x274(%rbp), %edx
movq -0x270(%rbp), %rcx
movq %rax, %r8
leaq 0x2a97(%rip), %rsi      # 0xb02c
movb $0x0, %al
callq 0x2300
movl -0x28(%rbp), %edi
callq 0x23a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x8bf5
jmp 0x85b2
xorl %eax, %eax
movl %eax, %edi
callq 0x21f0
movq %rax, -0x1e0(%rbp)
jmp 0x85c4
leaq -0xa8(%rbp), %rax
movq %rax, -0x200(%rbp)
movl $0x0, -0x1f8(%rbp)
movl -0x1f8(%rbp), %eax
cmpq $0x10, %rax
jae 0x860e
movq -0x200(%rbp), %rax
movl -0x1f8(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x1f8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1f8(%rbp)
jmp 0x85dc
jmp 0x8610
jmp 0x8612
leaq -0x128(%rbp), %rax
movq %rax, -0x210(%rbp)
movl $0x0, -0x204(%rbp)
movl -0x204(%rbp), %eax
cmpq $0x10, %rax
jae 0x865c
movq -0x210(%rbp), %rax
movl -0x204(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x204(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x204(%rbp)
jmp 0x862a
jmp 0x865e
jmp 0x8660
leaq -0x1a8(%rbp), %rax
movq %rax, -0x220(%rbp)
movl $0x0, -0x214(%rbp)
movl -0x214(%rbp), %eax
cmpq $0x10, %rax
jae 0x86aa
movq -0x220(%rbp), %rax
movl -0x214(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x214(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x214(%rbp)
jmp 0x8678
jmp 0x86ac
cmpl $0x0, -0x1d4(%rbp)
je 0x86ee
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 0x8725
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 0x87fa
movq 0x5833(%rip), %rax      # 0xdfd8
movq (%rax), %rax
movq %rax, -0x290(%rbp)
callq 0x2030
movl (%rax), %eax
movl %eax, -0x284(%rbp)
callq 0x2030
movl (%rax), %edi
callq 0x20d0
movq -0x290(%rbp), %rdi
movl -0x284(%rbp), %edx
movq %rax, %rcx
leaq 0x2871(%rip), %rsi      # 0xb050
movb $0x0, %al
callq 0x2300
movl -0x28(%rbp), %edi
callq 0x23a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x8bf5
movl -0x28(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0x1a8(%rbp,%rax,8), %rax
movq %rax, -0x298(%rbp)
movl -0x28(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x298(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x889a
movq 0x5793(%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 0x2805(%rip), %rsi      # 0xb084
movb $0x0, %al
callq 0x2300
movl -0x28(%rbp), %edi
callq 0x23a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x8bf5
movl -0x28(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0xa8(%rbp,%rax,8), %rax
movq %rax, -0x2b0(%rbp)
movl -0x28(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x2b0(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x8a81
movl -0x28(%rbp), %edi
movq -0x1d0(%rbp), %rsi
movslq -0x1c8(%rbp), %rax
addq %rax, %rsi
movl -0x1c0(%rbp), %edx
subl -0x1c8(%rbp), %edx
subl $0x1, %edx
callq 0x49c0
movl %eax, -0x224(%rbp)
cmpl $0x0, -0x224(%rbp)
jle 0x8a0e
movq -0x1d0(%rbp), %rax
movl -0x1c8(%rbp), %ecx
addl -0x224(%rbp), %ecx
movslq %ecx, %rcx
movb $0x0, (%rax,%rcx)
cmpl $-0x1, -0x1ec(%rbp)
jne 0x89b0
movl -0x224(%rbp), %eax
addl -0x1c8(%rbp), %eax
movl %eax, -0x1c8(%rbp)
cmpl $0x2, -0x1c8(%rbp)
jl 0x89ae
leaq -0x1d0(%rbp), %rdi
movl $0x2, %esi
callq 0x5e40
movl %eax, -0x1ec(%rbp)
movl -0x1c8(%rbp), %eax
subl $0x2, %eax
movl %eax, -0x1f0(%rbp)
movl $0x0, -0x1c8(%rbp)
movq 0x5626(%rip), %rax      # 0xdfb8
movq (%rax), %rdi
movq -0x1d0(%rbp), %rdx
addq $0x2, %rdx
leaq 0x16f1(%rip), %rsi      # 0xa098
movb $0x0, %al
callq 0x2300
jmp 0x89e1
movq 0x5601(%rip), %rax      # 0xdfb8
movq (%rax), %rdi
movq -0x1d0(%rbp), %rdx
leaq 0x16d0(%rip), %rsi      # 0xa098
movb $0x0, %al
callq 0x2300
movl -0x224(%rbp), %eax
addl -0x1f0(%rbp), %eax
movl %eax, -0x1f0(%rbp)
movl -0x1f0(%rbp), %eax
cmpl -0x1ec(%rbp), %eax
jl 0x8a0c
movq 0x55c2(%rip), %rax      # 0xdfb8
movq (%rax), %rdi
leaq 0x2682(%rip), %rsi      # 0xb082
movb $0x0, %al
callq 0x2300
jmp 0x8be6
jmp 0x8a7f
cmpl $0x0, -0x224(%rbp)
jge 0x8a7d
callq 0x2030
cmpl $0x0, (%rax)
je 0x8a69
movq 0x55b0(%rip), %rax      # 0xdfd8
movq (%rax), %rax
movq %rax, -0x2c0(%rbp)
callq 0x2030
movl (%rax), %eax
movl %eax, -0x2b4(%rbp)
callq 0x2030
movl (%rax), %edi
callq 0x20d0
movq -0x2c0(%rbp), %rdi
movl -0x2b4(%rbp), %edx
movq %rax, %rcx
leaq 0x2651(%rip), %rsi      # 0xb0b3
movb $0x0, %al
callq 0x2300
movl -0x28(%rbp), %edi
callq 0x23a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x8bf5
jmp 0x8a7f
jmp 0x8a81
movl -0x28(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0x128(%rbp,%rax,8), %rax
movq %rax, -0x2c8(%rbp)
movl -0x28(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x2c8(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x8b92
movl -0x28(%rbp), %edi
leaq -0x1d0(%rbp), %rsi
callq 0x4830
movl %eax, -0x228(%rbp)
cmpl $-0x1, -0x228(%rbp)
jne 0x8b5a
movq 0x54ea(%rip), %rax      # 0xdfd8
movq (%rax), %rax
movq %rax, -0x2e0(%rbp)
movq -0x20(%rbp), %rdi
callq 0x32b0
movq %rax, -0x2d8(%rbp)
callq 0x2030
movl (%rax), %eax
movl %eax, -0x2cc(%rbp)
callq 0x2030
movl (%rax), %edi
callq 0x20d0
movq -0x2e0(%rbp), %rdi
movq -0x2d8(%rbp), %rdx
movl -0x2cc(%rbp), %ecx
movq %rax, %r8
leaq 0x25a9(%rip), %rsi      # 0xb0e8
movb $0x0, %al
callq 0x2300
movl -0x28(%rbp), %edi
callq 0x23a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x8bf5
leaq 0x590f(%rip), %rax      # 0xe470
movq %rax, -0x1d0(%rbp)
movl $0x0, -0x1c4(%rbp)
movl $0x0, -0x1c8(%rbp)
movl $0x200000, -0x1c0(%rbp) # imm = 0x200000
movl $0x1, -0x1d4(%rbp)
jmp 0x8b92
xorl %eax, %eax
movl %eax, %edi
callq 0x21f0
movq %rax, -0x1e8(%rbp)
movq -0x1e8(%rbp), %rax
subq -0x1e0(%rbp), %rax
cmpl 0x5702(%rip), %eax      # 0xe2b8
jle 0x8be1
movq 0x5419(%rip), %rax      # 0xdfd8
movq (%rax), %rdi
leaq 0x255b(%rip), %rsi      # 0xb124
movb $0x0, %al
callq 0x2300
movl -0x28(%rbp), %edi
callq 0x23a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x8bf5
jmp 0x85c2
movl -0x28(%rbp), %edi
callq 0x23a0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x2e0, %rsp            # imm = 0x2E0
popq %rbp
retq
nopw %cs:(%rax,%rax)