// File: /workspace/github_c_makefile_star2_O0/enzo1982[P]smooth/classes/io/drivers/driver_socks4.cpp
// Function: smooth::IO::DriverSOCKS4::DriverSOCKS4(smooth::String const&, int, smooth::String const&, int)
S::IO::DriverSOCKS4::DriverSOCKS4(const String &proxy, Int socksPort, const String &hostName, Int port) : Driver()
{
	closeStream = False;

	stream	    = -1;
	size	    = -1;

	if (hostName == NIL || hostName.Length() > 255) { lastError = IO_ERROR_BADPARAM; return; }

	/* Open TCP/IP socket.
	 */
	stream = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

	if (stream == (unsigned) (~0)) { lastError = IO_ERROR_UNEXPECTED; return; }

	closeStream = True;

	/* Get proxy and server hostname.
	 */
	hostent		*host = gethostbyname(proxy);

	if (host == NIL) { Close(); lastError = IO_ERROR_UNEXPECTED; return; }

	/* Connect to proxy.
	 */
	sockaddr_in	 saddr;

	saddr.sin_family = AF_INET;
	saddr.sin_addr	 = *((in_addr *) *host->h_addr_list);
	saddr.sin_port	 = htons((short) socksPort);

	memset(&saddr.sin_zero, 0, sizeof(saddr.sin_zero));

	if (connect(stream, (sockaddr *) &saddr, sizeof(struct sockaddr)) == -1) { Close(); lastError = IO_ERROR_UNEXPECTED; return; }

	/* Send connect request.
	 */
	hostent		*server_hostent = gethostbyname(hostName);

	if (server_hostent != NULL)
	{
		unsigned char	*socksdata = new unsigned char [9];

		socksdata[0] = 4;
		socksdata[1] = 1;
		socksdata[2] = htons((short) port) % 256;
		socksdata[3] = htons((short) port) / 256;
		socksdata[4] = server_hostent->h_addr_list[0][0];
		socksdata[5] = server_hostent->h_addr_list[0][1];
		socksdata[6] = server_hostent->h_addr_list[0][2];
		socksdata[7] = server_hostent->h_addr_list[0][3];
		socksdata[8] = 0;

		if (send(stream, (char *) socksdata, 9, 0) < 9) { delete [] socksdata; Close(); lastError = IO_ERROR_UNEXPECTED; return; }

		delete [] socksdata;
	}
	else if (inet_addr(hostName) != INADDR_NONE)
	{
		unsigned char	*socksdata = new unsigned char [9];

		socksdata[0] = 4;
		socksdata[1] = 1;
		socksdata[2] = htons((short) port) % 256;
		socksdata[3] = htons((short) port) / 256;
		socksdata[4] = IOGetByte(inet_addr(hostName), 0);
		socksdata[5] = IOGetByte(inet_addr(hostName), 1);
		socksdata[6] = IOGetByte(inet_addr(hostName), 2);
		socksdata[7] = IOGetByte(inet_addr(hostName), 3);
		socksdata[8] = 0;

		if (send(stream, (char *) socksdata, 9, 0) < 9) { delete [] socksdata; Close(); lastError = IO_ERROR_UNEXPECTED; return; }

		delete [] socksdata;
	}
	else
	{
		Int	 hostNameLen = strlen(hostName);

		unsigned char	*socksdata = new unsigned char [10 + hostNameLen];

		socksdata[0] = 4;
		socksdata[1] = 1;
		socksdata[2] = htons((short) port) % 256;
		socksdata[3] = htons((short) port) / 256;
		socksdata[4] = 0;
		socksdata[5] = 0;
		socksdata[6] = 0;
		socksdata[7] = 1;
		socksdata[8] = 0;

		for (Int i = 0; i < hostNameLen; i++) socksdata[9 + i] = hostName[i];

		socksdata[9 + hostNameLen] = 0;

		if (send(stream, (char *) socksdata, 10 + hostNameLen, 0) < 10 + hostNameLen) { delete [] socksdata; Close(); lastError = IO_ERROR_UNEXPECTED; return; }

		delete [] socksdata;
	}

	/* Receive answer.
	 */
	unsigned char	*socksdata = new unsigned char [8];
	int		 recbytes  = 0;

	while (recbytes != 8)
	{
		int	 bytes = recv(stream, (char *) socksdata + recbytes, 8 - recbytes, 0);

		if (bytes <= 0) { delete [] socksdata; Close(); lastError = IO_ERROR_UNEXPECTED; return; }

		recbytes += bytes;
	}

	/* Check if connect attempt was successful.
	 */
	if (socksdata[1] != 90) { delete [] socksdata; Close(); lastError = IO_ERROR_UNEXPECTED; return; }

	delete [] socksdata;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xa0(%rbp)
callq 0x5cff0
movq -0xa0(%rbp), %rax
movq 0x11dcec(%rip), %rcx    # 0x2088c8
addq $0x10, %rcx
movq %rcx, (%rax)
movb $0x0, 0x3c(%rax)
movl $0xffffffff, 0x38(%rax) # imm = 0xFFFFFFFF
movq $-0x1, 0x8(%rax)
movq -0x20(%rbp), %rdi
xorl %esi, %esi
callq 0x59320
movb %al, -0x91(%rbp)
jmp 0xeac09
movb -0x91(%rbp), %al
testb $0x1, %al
jne 0xeac33
jmp 0xeac15
movq -0x20(%rbp), %rdi
callq 0x5b800
movl %eax, -0xa4(%rbp)
jmp 0xeac26
movl -0xa4(%rbp), %eax
cmpl $0xff, %eax
jle 0xeac63
movq -0xa0(%rbp), %rax
movl $0x2, 0x18(%rax)
jmp 0xeb3c0
movq -0xa0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
callq 0x5b1a0
jmp 0xeb3c9
movl $0x2, %edi
movl $0x1, %esi
movl $0x6, %edx
callq 0x5ab00
movl %eax, %ecx
movq -0xa0(%rbp), %rax
movl %ecx, 0x38(%rax)
cmpl $-0x1, 0x38(%rax)
jne 0xeac9c
movq -0xa0(%rbp), %rax
movl $0x3, 0x18(%rax)
jmp 0xeb3c0
movq -0xa0(%rbp), %rax
movb $0x1, 0x3c(%rax)
movq -0x10(%rbp), %rdi
callq 0x5d090
movq %rax, -0xb0(%rbp)
jmp 0xeacb9
movq -0xb0(%rbp), %rdi
callq 0x5a990
movq %rax, -0xb8(%rbp)
jmp 0xeacce
movq -0xb8(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0xead05
movq -0xa0(%rbp), %rdi
movq (%rdi), %rax
movq 0x38(%rax), %rax
callq *%rax
jmp 0xeacf2
movq -0xa0(%rbp), %rax
movl $0x3, 0x18(%rax)
jmp 0xeb3c0
movw $0x2, -0x50(%rbp)
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
movl (%rax), %eax
movl %eax, -0x4c(%rbp)
movzwl -0x14(%rbp), %edi
callq 0x5ba50
movw %ax, %cx
movq -0xa0(%rbp), %rax
movw %cx, -0x4e(%rbp)
movq $0x0, -0x48(%rbp)
movl 0x38(%rax), %edi
leaq -0x50(%rbp), %rsi
movl $0x10, %edx
callq 0x5a630
movl %eax, -0xbc(%rbp)
jmp 0xead53
movl -0xbc(%rbp), %eax
cmpl $-0x1, %eax
jne 0xead83
movq -0xa0(%rbp), %rdi
movq (%rdi), %rax
movq 0x38(%rax), %rax
callq *%rax
jmp 0xead70
movq -0xa0(%rbp), %rax
movl $0x3, 0x18(%rax)
jmp 0xeb3c0
movq -0x20(%rbp), %rdi
callq 0x5d090
movq %rax, -0xc8(%rbp)
jmp 0xead95
movq -0xc8(%rbp), %rdi
callq 0x5a990
movq %rax, -0xd0(%rbp)
jmp 0xeadaa
movq -0xd0(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
je 0xeaf06
movl $0x9, %edi
callq 0x5ab20
movq %rax, -0xd8(%rbp)
jmp 0xeadd3
movq -0xd8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movb $0x4, (%rax)
movq -0x60(%rbp), %rax
movb $0x1, 0x1(%rax)
movzwl -0x24(%rbp), %edi
callq 0x5ba50
movb %al, %cl
movq -0x60(%rbp), %rax
movb %cl, 0x2(%rax)
movzwl -0x24(%rbp), %edi
callq 0x5ba50
movw %ax, %cx
movq -0xa0(%rbp), %rax
movw %cx, %dx
movq -0x60(%rbp), %rcx
movb %dh, %dl
movb %dl, 0x3(%rcx)
movq -0x58(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq (%rcx), %rcx
movb (%rcx), %dl
movq -0x60(%rbp), %rcx
movb %dl, 0x4(%rcx)
movq -0x58(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq (%rcx), %rcx
movb 0x1(%rcx), %dl
movq -0x60(%rbp), %rcx
movb %dl, 0x5(%rcx)
movq -0x58(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq (%rcx), %rcx
movb 0x2(%rcx), %dl
movq -0x60(%rbp), %rcx
movb %dl, 0x6(%rcx)
movq -0x58(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq (%rcx), %rcx
movb 0x3(%rcx), %dl
movq -0x60(%rbp), %rcx
movb %dl, 0x7(%rcx)
movq -0x60(%rbp), %rcx
movb $0x0, 0x8(%rcx)
movl 0x38(%rax), %edi
movq -0x60(%rbp), %rsi
movl $0x9, %edx
xorl %ecx, %ecx
callq 0x5a4f0
movq %rax, -0xe0(%rbp)
jmp 0xeae95
movq -0xe0(%rbp), %rax
cmpq $0x9, %rax
jge 0xeaee4
movq -0x60(%rbp), %rax
movq %rax, -0xe8(%rbp)
cmpq $0x0, %rax
je 0xeaebf
movq -0xe8(%rbp), %rdi
callq 0x5a5e0
movq -0xa0(%rbp), %rdi
movq (%rdi), %rax
movq 0x38(%rax), %rax
callq *%rax
jmp 0xeaed1
movq -0xa0(%rbp), %rax
movl $0x3, 0x18(%rax)
jmp 0xeb3c0
movq -0x60(%rbp), %rax
movq %rax, -0xf0(%rbp)
cmpq $0x0, %rax
je 0xeaf01
movq -0xf0(%rbp), %rdi
callq 0x5a5e0
jmp 0xeb26d
movq -0x20(%rbp), %rdi
callq 0x5d090
movq %rax, -0xf8(%rbp)
jmp 0xeaf18
movq -0xf8(%rbp), %rdi
callq 0x5d7b0
cmpl $-0x1, %eax
je 0xeb0c3
movl $0x9, %edi
callq 0x5ab20
movq %rax, -0x100(%rbp)
jmp 0xeaf40
movq -0x100(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movb $0x4, (%rax)
movq -0x68(%rbp), %rax
movb $0x1, 0x1(%rax)
movzwl -0x24(%rbp), %edi
callq 0x5ba50
movb %al, %cl
movq -0x68(%rbp), %rax
movb %cl, 0x2(%rax)
movzwl -0x24(%rbp), %edi
callq 0x5ba50
movw %ax, %cx
movq -0x68(%rbp), %rax
movb %ch, %cl
movb %cl, 0x3(%rax)
movq -0x20(%rbp), %rdi
callq 0x5d090
movq %rax, -0x108(%rbp)
jmp 0xeaf93
movq -0x108(%rbp), %rdi
callq 0x5d7b0
movb %al, %cl
movq -0x68(%rbp), %rax
movb %cl, 0x4(%rax)
movq -0x20(%rbp), %rdi
callq 0x5d090
movq %rax, -0x110(%rbp)
jmp 0xeafba
movq -0x110(%rbp), %rdi
callq 0x5d7b0
movl %eax, %ecx
movq -0x68(%rbp), %rax
movb %ch, %cl
movb %cl, 0x5(%rax)
movq -0x20(%rbp), %rdi
callq 0x5d090
movq %rax, -0x118(%rbp)
jmp 0xeafe3
movq -0x118(%rbp), %rdi
callq 0x5d7b0
shrl $0x10, %eax
movb %al, %cl
movq -0x68(%rbp), %rax
movb %cl, 0x6(%rax)
movq -0x20(%rbp), %rdi
callq 0x5d090
movq %rax, -0x120(%rbp)
jmp 0xeb00d
movq -0x120(%rbp), %rdi
callq 0x5d7b0
movl %eax, %ecx
movq -0xa0(%rbp), %rax
shrl $0x18, %ecx
movb %cl, %dl
movq -0x68(%rbp), %rcx
movb %dl, 0x7(%rcx)
movq -0x68(%rbp), %rcx
movb $0x0, 0x8(%rcx)
movl 0x38(%rax), %edi
movq -0x68(%rbp), %rsi
movl $0x9, %edx
xorl %ecx, %ecx
callq 0x5a4f0
movq %rax, -0x128(%rbp)
jmp 0xeb052
movq -0x128(%rbp), %rax
cmpq $0x9, %rax
jge 0xeb0a1
movq -0x68(%rbp), %rax
movq %rax, -0x130(%rbp)
cmpq $0x0, %rax
je 0xeb07c
movq -0x130(%rbp), %rdi
callq 0x5a5e0
movq -0xa0(%rbp), %rdi
movq (%rdi), %rax
movq 0x38(%rax), %rax
callq *%rax
jmp 0xeb08e
movq -0xa0(%rbp), %rax
movl $0x3, 0x18(%rax)
jmp 0xeb3c0
movq -0x68(%rbp), %rax
movq %rax, -0x138(%rbp)
cmpq $0x0, %rax
je 0xeb0be
movq -0x138(%rbp), %rdi
callq 0x5a5e0
jmp 0xeb26b
movq -0x20(%rbp), %rdi
callq 0x5d090
movq %rax, -0x140(%rbp)
jmp 0xeb0d5
movq -0x140(%rbp), %rdi
callq 0x5a790
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
addl $0xa, %eax
movslq %eax, %rdi
callq 0x5ab20
movq %rax, -0x148(%rbp)
jmp 0xeb0fb
movq -0x148(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movb $0x4, (%rax)
movq -0x78(%rbp), %rax
movb $0x1, 0x1(%rax)
movl -0x24(%rbp), %eax
movzwl %ax, %edi
callq 0x5ba50
movzwl %ax, %eax
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
movb %dl, %cl
movq -0x78(%rbp), %rax
movb %cl, 0x2(%rax)
movl -0x24(%rbp), %eax
movzwl %ax, %edi
callq 0x5ba50
movzwl %ax, %eax
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
movb %al, %cl
movq -0x78(%rbp), %rax
movb %cl, 0x3(%rax)
movq -0x78(%rbp), %rax
movb $0x0, 0x4(%rax)
movq -0x78(%rbp), %rax
movb $0x0, 0x5(%rax)
movq -0x78(%rbp), %rax
movb $0x0, 0x6(%rax)
movq -0x78(%rbp), %rax
movb $0x1, 0x7(%rax)
movq -0x78(%rbp), %rax
movb $0x0, 0x8(%rax)
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpl -0x6c(%rbp), %eax
jge 0xeb1c1
movq -0x20(%rbp), %rdi
movl -0x7c(%rbp), %esi
callq 0x5c900
movl %eax, -0x14c(%rbp)
jmp 0xeb19e
movl -0x14c(%rbp), %eax
movb %al, %dl
movq -0x78(%rbp), %rax
movl -0x7c(%rbp), %ecx
addl $0x9, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0xeb182
movq -0xa0(%rbp), %rax
movq -0x78(%rbp), %rdx
movslq -0x6c(%rbp), %rcx
movb $0x0, 0x9(%rcx,%rdx)
movl 0x38(%rax), %edi
movq -0x78(%rbp), %rsi
movl -0x6c(%rbp), %eax
addl $0xa, %eax
movslq %eax, %rdx
xorl %ecx, %ecx
callq 0x5a4f0
movq %rax, -0x158(%rbp)
jmp 0xeb1f5
movq -0x158(%rbp), %rax
movl -0x6c(%rbp), %ecx
addl $0xa, %ecx
movslq %ecx, %rcx
cmpq %rcx, %rax
jge 0xeb24c
movq -0x78(%rbp), %rax
movq %rax, -0x160(%rbp)
cmpq $0x0, %rax
je 0xeb227
movq -0x160(%rbp), %rdi
callq 0x5a5e0
movq -0xa0(%rbp), %rdi
movq (%rdi), %rax
movq 0x38(%rax), %rax
callq *%rax
jmp 0xeb239
movq -0xa0(%rbp), %rax
movl $0x3, 0x18(%rax)
jmp 0xeb3c0
movq -0x78(%rbp), %rax
movq %rax, -0x168(%rbp)
cmpq $0x0, %rax
je 0xeb269
movq -0x168(%rbp), %rdi
callq 0x5a5e0
jmp 0xeb26b
jmp 0xeb26d
movl $0x8, %edi
callq 0x5ab20
movq %rax, -0x170(%rbp)
jmp 0xeb280
movq -0x170(%rbp), %rax
movq %rax, -0x88(%rbp)
movl $0x0, -0x8c(%rbp)
cmpl $0x8, -0x8c(%rbp)
je 0xeb34e
movq -0xa0(%rbp), %rax
movl 0x38(%rax), %edi
movq -0x88(%rbp), %rsi
movslq -0x8c(%rbp), %rax
movl %eax, %ecx
addq %rax, %rsi
movl $0x8, %eax
subl %ecx, %eax
movslq %eax, %rdx
xorl %ecx, %ecx
callq 0x5ccb0
movq %rax, -0x178(%rbp)
jmp 0xeb2dc
movq -0x178(%rbp), %rax
movl %eax, -0x90(%rbp)
cmpl $0x0, -0x90(%rbp)
jg 0xeb337
movq -0x88(%rbp), %rax
movq %rax, -0x180(%rbp)
cmpq $0x0, %rax
je 0xeb312
movq -0x180(%rbp), %rdi
callq 0x5a5e0
movq -0xa0(%rbp), %rdi
movq (%rdi), %rax
movq 0x38(%rax), %rax
callq *%rax
jmp 0xeb324
movq -0xa0(%rbp), %rax
movl $0x3, 0x18(%rax)
jmp 0xeb3c0
movl -0x90(%rbp), %eax
addl -0x8c(%rbp), %eax
movl %eax, -0x8c(%rbp)
jmp 0xeb298
movq -0x88(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x5a, %eax
je 0xeb3a0
movq -0x88(%rbp), %rax
movq %rax, -0x188(%rbp)
cmpq $0x0, %rax
je 0xeb37e
movq -0x188(%rbp), %rdi
callq 0x5a5e0
movq -0xa0(%rbp), %rdi
movq (%rdi), %rax
movq 0x38(%rax), %rax
callq *%rax
jmp 0xeb390
movq -0xa0(%rbp), %rax
movl $0x3, 0x18(%rax)
jmp 0xeb3c0
movq -0x88(%rbp), %rax
movq %rax, -0x190(%rbp)
cmpq $0x0, %rax
je 0xeb3c0
movq -0x190(%rbp), %rdi
callq 0x5a5e0
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
movq -0x30(%rbp), %rdi
callq 0x5b6b0
nopw %cs:(%rax,%rax)