// File: /workspace/github_cpp_makefile_star2_O2/akw0088[P]openssl/download.cpp
// Function: file_download(char*, unsigned short, char*, int, unsigned int*, char*)
int file_download(char *ip_str, unsigned short int port, char *response, int size, unsigned int *download_size, char *file_name)
{
	struct sockaddr_in	servaddr;
	SOCKET sock;
	int ret;

	sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

	memset(&servaddr, 0, sizeof(struct sockaddr_in));
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = inet_addr(ip_str);
	servaddr.sin_port = htons(port);

	// 3 way handshake
	printf("Attempting to connect to %s\n", ip_str);
	ret = connect(sock, (struct sockaddr *)&servaddr, sizeof(struct sockaddr_in));
	if (ret == SOCKET_ERROR)
	{
#ifdef _WIN32
		ret = WSAGetLastError();

		switch (ret)
		{
		case WSAETIMEDOUT:
			printf("Fatal Error: Connection timed out.\n");
			break;
		case WSAECONNREFUSED:
			printf("Fatal Error: Connection refused\n");
			break;
		case WSAEHOSTUNREACH:
			printf("Fatal Error: Router sent ICMP packet (destination unreachable)\n");
			break;
		default:
			printf("Fatal Error: %d\n", ret);
			break;
		}
#else
		ret = errno;

	        switch(ret)
	        {
		case ENETUNREACH:
			printf("Fatal Error: The network is unreachable from this host at this time.\n(Bad IP address)\n");
			break;
	        case ETIMEDOUT:
	                printf("Fatal Error: Connecting timed out.\n");
	                break;
	        case ECONNREFUSED:
	                printf("Fatal Error: Connection refused\n");
	                break;
	        case EHOSTUNREACH:
	                printf("Fatal Error: router sent ICMP packet (destination unreachable)\n");
	                break;
	        default:
	                printf("Fatal Error: %d\n", ret);
	                break;
	        }
#endif
		return -1;
	}
	printf("TCP handshake completed\n");

	memset(response, 0, size);

	int expected_size = 0;
	recv(sock, (char *)&expected_size, 4, 0);
	recv(sock, (char *)file_name, 128, 0);

	while (*download_size < expected_size)
	{
		*download_size += recv(sock, &response[*download_size], expected_size - *download_size, 0);
	}
	closesocket(sock);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, -0x38(%rbp)
movq %r8, %rbx
movl %ecx, -0x2c(%rbp)
movq %rdx, %r14
movl %esi, %r13d
movq %rdi, %r12
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
pushq $0x6
popq %rdx
callq 0x1090
movl %eax, %r15d
leaq -0x48(%rbp), %rax
andq $0x0, 0x8(%rax)
movw $0x2, (%rax)
movq %r12, %rdi
callq 0x1160
leaq -0x48(%rbp), %rcx
movl %eax, 0x4(%rcx)
rolw $0x8, %r13w
movw %r13w, 0x2(%rcx)
movq %rcx, %r13
leaq 0xb81(%rip), %rdi       # 0x202a
movq %r12, %rsi
xorl %eax, %eax
callq 0x1060
pushq $0x10
popq %rdx
movl %r15d, %edi
movq %r13, %rsi
callq 0x1120
cmpl $-0x1, %eax
je 0x1531
leaq 0xca4(%rip), %rdi       # 0x2171
callq 0x11d0
movslq -0x2c(%rbp), %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x1100
leaq -0x30(%rbp), %rsi
andl $0x0, (%rsi)
pushq $0x4
popq %rdx
movl %r15d, %edi
xorl %ecx, %ecx
callq 0x10b0
movl $0x80, %edx
movl %r15d, %edi
movq -0x38(%rbp), %rsi
xorl %ecx, %ecx
callq 0x10b0
movl (%rbx), %eax
movl -0x30(%rbp), %edx
subl %eax, %edx
jbe 0x1525
movl %eax, %esi
addq %r14, %rsi
movl %r15d, %edi
xorl %ecx, %ecx
callq 0x10b0
addl (%rbx), %eax
movl %eax, (%rbx)
jmp 0x1509
movl %r15d, %edi
callq 0x1210
xorl %eax, %eax
jmp 0x1576
callq 0x1050
movl (%rax), %esi
cmpl $0x71, %esi
je 0x1567
cmpl $0x6e, %esi
je 0x1555
cmpl $0x6f, %esi
je 0x155e
cmpl $0x65, %esi
jne 0x1585
leaq 0xcb8(%rip), %rdi       # 0x220b
jmp 0x156e
leaq 0xc8c(%rip), %rdi       # 0x21e8
jmp 0x156e
leaq 0xc63(%rip), %rdi       # 0x21c8
jmp 0x156e
leaq 0xc1b(%rip), %rdi       # 0x2189
callq 0x11d0
pushq $-0x1
popq %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xabb(%rip), %rdi       # 0x2047
xorl %eax, %eax
callq 0x1060
jmp 0x1573