// File: /workspace/github_cpp_makefile_star2_O2/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, %r15d
movq %rdi, -0x40(%rbp)
movl $0x10, -0x34(%rbp)
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x1090
movl %eax, -0x2c(%rbp)
cmpl $-0x1, %eax
je 0x1776
leaq -0x60(%rbp), %rsi
andq $0x0, 0x8(%rsi)
movw $0x2, (%rsi)
andl $0x0, 0x4(%rsi)
rolw $0x8, %r15w
movw %r15w, 0x2(%rsi)
pushq $0x10
popq %rdx
movl -0x2c(%rbp), %edi
callq 0x1070
cmpl $-0x1, %eax
je 0x177f
movl -0x5c(%rbp), %edi
callq 0x10e0
movzwl -0x5e(%rbp), %ecx
rolw $0x8, %cx
movzwl %cx, %edx
leaq 0xa56(%rip), %rdi       # 0x2070
movq %rax, %rsi
xorl %eax, %eax
callq 0x1060
pushq $0x3
popq %rsi
movl -0x2c(%rbp), %edi
callq 0x1180
cmpl $-0x1, %eax
je 0x178f
leaq -0x4f0(%rbp), %rbx
movq -0x40(%rbp), %r12
movl $0x400, %edx            # imm = 0x400
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1100
leaq 0xc08(%rip), %rdi       # 0x2261
callq 0x11d0
movl -0x2c(%rbp), %edi
leaq -0x70(%rbp), %rsi
leaq -0x34(%rbp), %rdx
callq 0x10a0
cmpl $-0x1, %eax
je 0x1643
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 0xa00(%rip), %rdx       # 0x2099
movq %rax, %rcx
xorl %eax, %eax
callq 0x11f0
movl -0x6c(%rbp), %edi
callq 0x10e0
leaq 0x9ef(%rip), %rdi       # 0x20a1
movq %rax, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1060
andl $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
pushq $0x7f
popq %rcx
cmovaeq %rcx, %rax
leaq -0xf0(%rbp), %r14
movq %r14, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x1110
movl %r13d, %edi
movq %r15, %rsi
pushq $0x4
popq %rdx
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 0x1643
leaq 0x8db(%rip), %rdi       # 0x2058
jmp 0x1796
leaq 0x8df(%rip), %rdi       # 0x2065
callq 0x1130
xorl %eax, %eax
jmp 0x179e
leaq 0x8f6(%rip), %rdi       # 0x208c
callq 0x1130
pushq $-0x1
popq %rax
addq $0x4c8, %rsp            # imm = 0x4C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq