// File: /workspace/github_cpp_makefile_star2_O3/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
movl $0x2, %edi
movl $0x1, %esi
movl $0x6, %edx
callq 0x1090
movl %eax, %r15d
leaq -0x48(%rbp), %rax
movq $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 0xb70(%rip), %rdi       # 0x202a
movq %r12, %rsi
xorl %eax, %eax
callq 0x1060
movl %r15d, %edi
movq %r13, %rsi
movl $0x10, %edx
callq 0x1120
cmpl $-0x1, %eax
je 0x1555
leaq 0xc91(%rip), %rdi       # 0x2171
callq 0x11d0
movslq -0x2c(%rbp), %rdx
xorl %r12d, %r12d
movq %r14, %rdi
xorl %esi, %esi
callq 0x1100
leaq -0x30(%rbp), %r13
movl $0x0, (%r13)
movl $0x4, %edx
movl %r15d, %edi
movq %r13, %rsi
xorl %ecx, %ecx
callq 0x10b0
movl $0x80, %edx
movl %r15d, %edi
movq -0x38(%rbp), %rsi
xorl %ecx, %ecx
callq 0x10b0
movl (%rbx), %eax
movl (%r13), %edx
cmpl %edx, %eax
jae 0x154b
movl %eax, %esi
addq %r14, %rsi
subl %eax, %edx
movl %r15d, %edi
xorl %ecx, %ecx
callq 0x10b0
addl (%rbx), %eax
movl %eax, (%rbx)
movl -0x30(%rbp), %edx
jmp 0x152d
movl %r15d, %edi
callq 0x1210
jmp 0x15b2
callq 0x1050
movl (%rax), %esi
cmpl $0x6e, %esi
jg 0x1574
cmpl $0x65, %esi
je 0x1597
cmpl $0x6e, %esi
jne 0x1587
leaq 0xc76(%rip), %rdi       # 0x21e8
jmp 0x15a7
cmpl $0x6f, %esi
je 0x15a0
cmpl $0x71, %esi
jne 0x1587
leaq 0xc04(%rip), %rdi       # 0x2189
jmp 0x15a7
leaq 0xab9(%rip), %rdi       # 0x2047
xorl %eax, %eax
callq 0x1060
jmp 0x15ac
leaq 0xc6d(%rip), %rdi       # 0x220b
jmp 0x15a7
leaq 0xc21(%rip), %rdi       # 0x21c8
callq 0x11d0
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl %r12d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq