// File: /workspace/github_cpp_makefile_star2_O2/akw0088[P]openssl/aes_download_reverse.cpp
// Function: aes_file_download_reverse(unsigned short, unsigned char*, int)
int aes_file_download_reverse(unsigned short int port, unsigned char *prikey, int max_malloc_size)
{
	int			connfd;
	unsigned int		socklen = 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;
	}

	printf("Allocating %d bytes\r\n", max_malloc_size);
	char *response = (char *)malloc(max_malloc_size);
	if (response == NULL)
	{
		perror("malloc failed");
		return -1;
	}


	for (;;)
	{
		unsigned int final_size = 0;
		char hash[33] = {0};
		unsigned char encrypted_key[256] = {0};
		unsigned int download_size = 0;
		char file_name[128] = {0};




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

		printf("Client: %s\r\n", inet_ntoa(client.sin_addr));

		int expected_size = 0;
		int magic = 0;
		recv(connfd, (char *)&magic, 4, 0);

		if (magic != 0xF0F0F0F0)
		{
			closesocket(connfd);
			continue;
		}

		printf("Client passed magic cookie %X: Downloading\r\n", magic);

		recv(connfd, (char *)&expected_size, 4, 0);
		recv(connfd, (char *)file_name, 128, 0);
		recv(connfd, (char *)&final_size, 4, 0);
		recv(connfd, (char *)hash, 32, 0);
		recv(connfd, (char *)encrypted_key, 256, 0);

		while (download_size < expected_size)
		{
			download_size += recv(connfd, &response[download_size], expected_size - download_size, 0);
		}

		if (download_size != expected_size)
		{
			printf("expected %d bytes, got only %d bytes\r\n", expected_size, download_size);
		}
		closesocket(connfd);
		process_download(file_name, hash, response, download_size, encrypted_key, prikey, max_malloc_size);
		expected_size = 0;
		memset(file_name, 0, 128);
		final_size = 0;
		memset(hash, 0, 33);
		memset(encrypted_key, 0, 256);

	}
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f8, %rsp            # imm = 0x1F8
movl %edx, -0x2c(%rbp)
movq %rsi, -0x48(%rbp)
movl %edi, %ebx
movl $0x10, -0x40(%rbp)
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x20a0
cmpl $-0x1, %eax
je 0x28d6
movl %eax, %r15d
leaq -0x68(%rbp), %rsi
andq $0x0, 0x8(%rsi)
movw $0x2, (%rsi)
andl $0x0, 0x4(%rsi)
rolw $0x8, %bx
movw %bx, 0x2(%rsi)
pushq $0x10
popq %rdx
movl %eax, %edi
callq 0x2060
cmpl $-0x1, %eax
je 0x28df
movl -0x64(%rbp), %edi
callq 0x2110
movzwl -0x66(%rbp), %ecx
rolw $0x8, %cx
movzwl %cx, %edx
leaq 0x986(%rip), %rdi       # 0x3047
movq %rax, %rsi
xorl %eax, %eax
callq 0x2050
pushq $0x3
popq %rsi
movl %r15d, %edi
callq 0x2210
cmpl $-0x1, %eax
je 0x28ef
leaq 0x98a(%rip), %rdi       # 0x3070
movl -0x2c(%rbp), %ebx
movl %ebx, %esi
xorl %eax, %eax
callq 0x2050
movslq %ebx, %rdi
callq 0x2270
testq %rax, %rax
je 0x28f8
movq %rax, %r12
leaq 0xb87(%rip), %r14       # 0x3294
leaq -0x58(%rbp), %r13
movl %r15d, -0x38(%rbp)
andl $0x0, -0x3c(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x80(%rbp)
movaps %xmm0, -0x90(%rbp)
movb $0x0, -0x70(%rbp)
movl $0x100, %edx            # imm = 0x100
leaq -0x210(%rbp), %rdi
xorl %esi, %esi
callq 0x2140
xorps %xmm0, %xmm0
movaps %xmm0, -0xa0(%rbp)
movaps %xmm0, -0xb0(%rbp)
movaps %xmm0, -0xc0(%rbp)
movaps %xmm0, -0xd0(%rbp)
movaps %xmm0, -0xe0(%rbp)
movaps %xmm0, -0xf0(%rbp)
movaps %xmm0, -0x100(%rbp)
movaps %xmm0, -0x110(%rbp)
movq %r14, %rdi
callq 0x22a0
movl %r15d, %edi
movq %r13, %rsi
leaq -0x40(%rbp), %rdx
callq 0x20b0
cmpl $-0x1, %eax
je 0x2715
movl %eax, %ebx
movq %r14, %r13
movl %r15d, %r14d
movl -0x54(%rbp), %edi
callq 0x2110
leaq 0x8e8(%rip), %rdi       # 0x3094
movq %rax, %rsi
xorl %eax, %eax
callq 0x2050
andl $0x0, -0x34(%rbp)
andl $0x0, -0x30(%rbp)
movl %ebx, %edi
leaq -0x30(%rbp), %rsi
pushq $0x4
popq %r15
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x20d0
cmpl $0xf0f0f0f0, -0x30(%rbp) # imm = 0xF0F0F0F0
jne 0x2873
xorl %r14d, %r14d
leaq 0x8b8(%rip), %rdi       # 0x30a1
movl $0xf0f0f0f0, %esi       # imm = 0xF0F0F0F0
xorl %eax, %eax
callq 0x2050
movl %ebx, %edi
leaq -0x34(%rbp), %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x20d0
movl $0x80, %edx
movl %ebx, %edi
leaq -0x110(%rbp), %rsi
xorl %ecx, %ecx
callq 0x20d0
movl %ebx, %edi
leaq -0x3c(%rbp), %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x20d0
movl %ebx, %edi
leaq -0x90(%rbp), %rsi
pushq $0x20
popq %rdx
xorl %ecx, %ecx
callq 0x20d0
movl $0x100, %edx            # imm = 0x100
movl %ebx, %edi
leaq -0x210(%rbp), %rsi
xorl %ecx, %ecx
callq 0x20d0
movl -0x34(%rbp), %esi
movl %esi, %edx
subl %r14d, %edx
jbe 0x2882
movl %r14d, %esi
addq %r12, %rsi
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x20d0
addl %r14d, %eax
movl %eax, %r14d
jmp 0x2852
movl %ebx, %edi
callq 0x22f0
movl %r14d, %r15d
movq %r13, %r14
jmp 0x28cd
je 0x2895
leaq 0x843(%rip), %rdi       # 0x30ce
movl %r14d, %edx
xorl %eax, %eax
callq 0x2050
movl %ebx, %edi
callq 0x22f0
movl -0x2c(%rbp), %eax
movl %eax, (%rsp)
leaq -0x110(%rbp), %rdi
leaq -0x90(%rbp), %rsi
movq %r12, %rdx
movl %r14d, %ecx
leaq -0x210(%rbp), %r8
movq -0x48(%rbp), %r9
callq 0x2919
movq %r13, %r14
movl -0x38(%rbp), %r15d
leaq -0x58(%rbp), %r13
jmp 0x2715
leaq 0x752(%rip), %rdi       # 0x302f
jmp 0x28ff
leaq 0x756(%rip), %rdi       # 0x303c
callq 0x21b0
xorl %eax, %eax
jmp 0x2907
leaq 0x76d(%rip), %rdi       # 0x3063
jmp 0x28ff
leaq 0x787(%rip), %rdi       # 0x3086
callq 0x21b0
pushq $-0x1
popq %rax
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq