// File: DoubleString[P]RtConverter/src/Rtklib/stream.cpp
// Function: bamboo::gentcp(bamboo::tcp_t*, int, char*)
static int gentcp(tcp_t *tcp, int type, char *msg)
	{
		struct hostent *hp;
#ifdef SVR_REUSEADDR
		int opt = 1;
#endif

		tracet(3, "gentcp: type=%d\n", type);

		/* generate socket */
		if ((tcp->sock = socket(AF_INET, SOCK_STREAM, 0)) == (socket_t)-1) {
			sprintf(msg, "socket error (%d)", errsock());
			tracet(1, "gentcp: socket error err=%d\n", errsock());
			tcp->state = -1;
			return 0;
		}
		if (!setsock(tcp->sock, msg)) {
			tcp->state = -1;
			return 0;
		}
		memset(&tcp->addr, 0, sizeof(tcp->addr));
		tcp->addr.sin_family = AF_INET;
		tcp->addr.sin_port = htons(tcp->port);

		if (type == 0) { /* server socket */

#ifdef SVR_REUSEADDR
		/* multiple-use of server socket */
			setsockopt(tcp->sock, SOL_SOCKET, SO_REUSEADDR, (const char *)&opt,
				sizeof(opt));
#endif
			if (::bind(tcp->sock, (struct sockaddr *)&tcp->addr, sizeof(tcp->addr)) == -1) {
				sprintf(msg, "bind error (%d) : %d", errsock(), tcp->port);
				tracet(1, "gentcp: bind error port=%d err=%d\n", tcp->port, errsock());
				closesocket(tcp->sock);
				tcp->state = -1;
				return 0;
			}
			listen(tcp->sock, 5);
		}
		else { /* client socket */
			if (!(hp = gethostbyname(tcp->saddr))) {
				sprintf(msg, "address error (%s)", tcp->saddr);
				tracet(1, "gentcp: gethostbyname error addr=%s err=%d\n", tcp->saddr, errsock());
				closesocket(tcp->sock);
				tcp->state = 0;
				tcp->tcon = ticonnect;
				tcp->tdis = tickget();
				return 0;
			}
			memcpy(&tcp->addr.sin_addr, hp->h_addr, hp->h_length);
		}
		tcp->state = 1;
		tcp->tact = tickget();
		tracet(5, "gentcp: exit sock=%d\n", tcp->sock);
		return 1;
	}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl -0x14(%rbp), %edx
movl $0x3, %edi
leaq 0x24d76(%rip), %rsi     # 0xc8ea8
movb $0x0, %al
callq 0x95890
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x91e0
movq -0x10(%rbp), %rcx
movl %eax, 0x118(%rcx)
cmpl $-0x1, %eax
jne 0xa41aa
movq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
callq 0xa43f0
movq -0x30(%rbp), %rdi
movl %eax, %edx
leaq 0x24d46(%rip), %rsi     # 0xc8eb9
movb $0x0, %al
callq 0x9150
callq 0xa43f0
movl %eax, %edx
movl $0x1, %edi
leaq 0x24d3e(%rip), %rsi     # 0xc8ecb
movb $0x0, %al
callq 0x95890
movq -0x10(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movl $0x0, -0x4(%rbp)
jmp 0xa43e5
movq -0x10(%rbp), %rax
movl 0x118(%rax), %edi
movq -0x20(%rbp), %rsi
callq 0xa4400
cmpl $0x0, %eax
jne 0xa41d8
movq -0x10(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movl $0x0, -0x4(%rbp)
jmp 0xa43e5
movq -0x10(%rbp), %rdi
addq $0x108, %rdi            # imm = 0x108
xorl %esi, %esi
movl $0x10, %edx
callq 0x93d0
movq -0x10(%rbp), %rax
movw $0x2, 0x108(%rax)
movq -0x10(%rbp), %rax
movl 0x104(%rax), %eax
movzwl %ax, %edi
callq 0x90b0
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x10a(%rax)
cmpl $0x0, -0x14(%rbp)
jne 0xa42dd
movq -0x10(%rbp), %rax
movl 0x118(%rax), %edi
movq -0x10(%rbp), %rsi
addq $0x108, %rsi            # imm = 0x108
movl $0x10, %edx
callq 0x9120
cmpl $-0x1, %eax
jne 0xa42c4
movq -0x20(%rbp), %rax
movq %rax, -0x40(%rbp)
callq 0xa43f0
movq -0x40(%rbp), %rdi
movl %eax, %edx
movq -0x10(%rbp), %rax
movl 0x104(%rax), %ecx
leaq 0x24c7a(%rip), %rsi     # 0xc8ee8
movb $0x0, %al
callq 0x9150
movq -0x10(%rbp), %rax
movl 0x104(%rax), %eax
movl %eax, -0x34(%rbp)
callq 0xa43f0
movl -0x34(%rbp), %edx
movl %eax, %ecx
movl $0x1, %edi
leaq 0x24c65(%rip), %rsi     # 0xc8efd
movb $0x0, %al
callq 0x95890
movq -0x10(%rbp), %rax
movl 0x118(%rax), %edi
callq 0x9e90
movq -0x10(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movl $0x0, -0x4(%rbp)
jmp 0xa43e5
movq -0x10(%rbp), %rax
movl 0x118(%rax), %edi
movl $0x5, %esi
callq 0x9860
jmp 0xa43a6
movq -0x10(%rbp), %rdi
addq $0x4, %rdi
callq 0x9880
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
jne 0xa437f
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rdx
addq $0x4, %rdx
leaq 0x24c15(%rip), %rsi     # 0xc8f20
movb $0x0, %al
callq 0x9150
movq -0x10(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x48(%rbp)
callq 0xa43f0
movq -0x48(%rbp), %rdx
movl %eax, %ecx
movl $0x1, %edi
leaq 0x24bfe(%rip), %rsi     # 0xc8f33
movb $0x0, %al
callq 0x95890
movq -0x10(%rbp), %rax
movl 0x118(%rax), %edi
callq 0x9e90
movq -0x10(%rbp), %rax
movl $0x0, (%rax)
movl 0x55715(%rip), %ecx     # 0xf9a70
movq -0x10(%rbp), %rax
movl %ecx, 0x11c(%rax)
callq 0x8f860
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x124(%rax)
movl $0x0, -0x4(%rbp)
jmp 0xa43e5
movq -0x10(%rbp), %rdi
addq $0x108, %rdi            # imm = 0x108
addq $0x4, %rdi
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rsi
movq -0x28(%rbp), %rax
movslq 0x14(%rax), %rdx
callq 0x9610
movq -0x10(%rbp), %rax
movl $0x1, (%rax)
callq 0x8f860
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x120(%rax)
movq -0x10(%rbp), %rax
movl 0x118(%rax), %edx
movl $0x5, %edi
leaq 0x24b88(%rip), %rsi     # 0xc8f5f
movb $0x0, %al
callq 0x95890
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nop