// File: /workspace/github_cpp_makefile_star2_O1/akw0088[P]openssl/download.cpp
// Function: file_upload(char*, unsigned short)
int file_upload(char *file, unsigned short port)
{
	int			connfd;
	unsigned int		size = sizeof(struct sockaddr_in);
	struct sockaddr_in	servaddr, client;
	time_t			ticks;
	int listenfd;

#ifdef _WIN32
	WSADATA		WSAData;

	WSAStartup(MAKEWORD(2, 0), &WSAData);
#endif

	listenfd = socket(AF_INET, SOCK_STREAM, 0);
	if (listenfd == -1)
	{
		perror("socket error");
		return -1;
	}

	memset(&servaddr, 0, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port = htons(port);

	if ((::bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr))) == -1)
	{
		perror("bind error");
		return 0;
	}

	printf("Server listening on: %s:%d\n", inet_ntoa(servaddr.sin_addr), htons(servaddr.sin_port));

	if (listen(listenfd, 3) == -1)
	{
		perror("listen error");
		return -1;
	}

	for (;;)
	{
		char response[1024] = { 0 };

		printf("listening for connections...\n");
		connfd = accept(listenfd, (struct sockaddr *)&client, (socklen_t *)&size);
		if (connfd == INVALID_SOCKET)
			continue;

		ticks = time(NULL);
		snprintf(response, sizeof(response), "%.24s\r\n", ctime(&ticks));
		printf("Client: %s - %s", inet_ntoa(client.sin_addr), response);

		unsigned int file_size = 0;
		char *data = get_file(file, &file_size);
		char file_name[128] = { 0 };

		memcpy(file_name, file, MIN(127, strlen(file)));
		send(connfd, (char *)&file_size, sizeof(int), 0);
		send(connfd, (char *)&file_name, 128, 0);
		send(connfd, data, file_size, 0);
		closesocket(connfd);
	}
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4c8, %rsp            # imm = 0x4C8
movl %esi, %r12d
movq %rdi, -0x40(%rbp)
movl $0x10, -0x34(%rbp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1090
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl %eax, -0x2c(%rbp)
cmpl $-0x1, %eax
je 0x17bc
xorps %xmm0, %xmm0
leaq -0x60(%rbp), %rsi
movaps %xmm0, (%rsi)
movw $0x2, (%rsi)
movl $0x0, 0x4(%rsi)
rolw $0x8, %r12w
movw %r12w, 0x2(%rsi)
movl -0x2c(%rbp), %edi
movl $0x10, %edx
callq 0x1070
cmpl $-0x1, %eax
je 0x17c5
movl -0x5c(%rbp), %edi
callq 0x10e0
movzwl -0x5e(%rbp), %ecx
rolw $0x8, %cx
movzwl %cx, %edx
leaq 0xa19(%rip), %rdi       # 0x2070
movq %rax, %rsi
xorl %eax, %eax
callq 0x1060
movl -0x2c(%rbp), %edi
movl $0x3, %esi
callq 0x1180
cmpl $-0x1, %eax
je 0x17d1
leaq -0x4f0(%rbp), %rbx
movq -0x40(%rbp), %r12
movl $0x400, %edx            # imm = 0x400
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1100
leaq 0xbc9(%rip), %rdi       # 0x2261
callq 0x11d0
movl -0x2c(%rbp), %edi
leaq -0x70(%rbp), %rsi
leaq -0x34(%rbp), %rdx
callq 0x10a0
cmpl $-0x1, %eax
je 0x1682
movl %eax, %r13d
xorl %edi, %edi
callq 0x1140
movq %rax, -0x50(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x10c0
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdi
leaq 0x9c1(%rip), %rdx       # 0x2099
movq %rax, %rcx
xorl %eax, %eax
callq 0x11f0
movl -0x6c(%rbp), %edi
callq 0x10e0
leaq 0x9b0(%rip), %rdi       # 0x20a1
movq %rax, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1060
movl $0x0, -0x30(%rbp)
movq %r12, %rdi
leaq -0x30(%rbp), %r15
movq %r15, %rsi
callq 0x131c
movq %rax, -0x48(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x80(%rbp)
movaps %xmm0, -0x90(%rbp)
movaps %xmm0, -0xa0(%rbp)
movaps %xmm0, -0xb0(%rbp)
movaps %xmm0, -0xc0(%rbp)
movaps %xmm0, -0xd0(%rbp)
movaps %xmm0, -0xe0(%rbp)
movaps %xmm0, -0xf0(%rbp)
movq %r12, %rdi
callq 0x10d0
cmpq $0x7f, %rax
movl $0x7f, %ecx
cmovaeq %rcx, %rax
leaq -0xf0(%rbp), %r14
movq %r14, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x1110
movl $0x4, %edx
movl %r13d, %edi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x10f0
movl $0x80, %edx
movl %r13d, %edi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x10f0
movl -0x30(%rbp), %edx
movl %r13d, %edi
movq -0x48(%rbp), %rsi
xorl %ecx, %ecx
callq 0x10f0
movl %r13d, %edi
callq 0x1210
jmp 0x1682
leaq 0x895(%rip), %rdi       # 0x2058
jmp 0x17d8
leaq 0x899(%rip), %rdi       # 0x2065
xorl %r15d, %r15d
jmp 0x17d8
leaq 0x8b4(%rip), %rdi       # 0x208c
callq 0x1130
movl %r15d, %eax
addq $0x4c8, %rsp            # imm = 0x4C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq