// File: /workspace/github_cpp_makefile_star2_O1/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, -0x30(%rbp)
movq %rsi, -0x48(%rbp)
movl %edi, %r14d
movl $0x10, -0x40(%rbp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x20a0
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpl $-0x1, %eax
je 0x295a
movl %eax, %r15d
xorps %xmm0, %xmm0
leaq -0x80(%rbp), %rsi
movaps %xmm0, (%rsi)
movw $0x2, (%rsi)
movl $0x0, 0x4(%rsi)
rolw $0x8, %r14w
movw %r14w, 0x2(%rsi)
movl %eax, %edi
movl $0x10, %edx
callq 0x2060
cmpl $-0x1, %eax
je 0x2963
movl -0x7c(%rbp), %edi
callq 0x2110
movzwl -0x7e(%rbp), %ecx
rolw $0x8, %cx
movzwl %cx, %edx
leaq 0x976(%rip), %rdi       # 0x3047
movq %rax, %rsi
xorl %eax, %eax
callq 0x2050
movl %r15d, %edi
movl $0x3, %esi
callq 0x2210
cmpl $-0x1, %eax
je 0x296e
leaq 0x978(%rip), %rdi       # 0x3070
xorl %r13d, %r13d
movl -0x30(%rbp), %r14d
movl %r14d, %esi
xorl %eax, %eax
callq 0x2050
movslq %r14d, %rdi
callq 0x2270
testq %rax, %rax
je 0x2977
movq %rax, %r12
leaq -0x210(%rbp), %rbx
movl %r15d, -0x3c(%rbp)
movl $0x0, -0x38(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x60(%rbp)
movaps %xmm0, -0x70(%rbp)
movb $0x0, -0x50(%rbp)
movl $0x100, %edx            # imm = 0x100
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2140
xorps %xmm0, %xmm0
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)
movaps %xmm0, -0x100(%rbp)
leaq 0xb05(%rip), %rdi       # 0x3294
callq 0x22a0
movl %r15d, %edi
leaq -0x110(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq 0x20b0
cmpl $-0x1, %eax
je 0x2728
movl %eax, %r14d
movl -0x10c(%rbp), %edi
callq 0x2110
leaq 0x8cf(%rip), %rdi       # 0x3094
movq %rax, %rsi
xorl %eax, %eax
callq 0x2050
movl %r13d, -0x2c(%rbp)
movl %r13d, -0x34(%rbp)
movl $0x4, %edx
movl %r14d, %edi
leaq -0x34(%rbp), %rsi
xorl %ecx, %ecx
callq 0x20d0
cmpl $0xf0f0f0f0, -0x34(%rbp) # imm = 0xF0F0F0F0
jne 0x294d
movq %rbx, %r15
xorl %ebx, %ebx
leaq 0x89e(%rip), %rdi       # 0x30a1
movl $0xf0f0f0f0, %esi       # imm = 0xF0F0F0F0
xorl %eax, %eax
callq 0x2050
movl $0x4, %edx
movl %r14d, %edi
leaq -0x2c(%rbp), %rsi
xorl %ecx, %ecx
callq 0x20d0
movl $0x80, %edx
movl %r14d, %edi
leaq -0x100(%rbp), %rsi
xorl %ecx, %ecx
callq 0x20d0
movl $0x4, %edx
movl %r14d, %edi
leaq -0x38(%rbp), %rsi
xorl %ecx, %ecx
callq 0x20d0
movl $0x20, %edx
movl %r14d, %edi
leaq -0x70(%rbp), %rsi
xorl %ecx, %ecx
callq 0x20d0
movl $0x100, %edx            # imm = 0x100
movl %r14d, %edi
movq %r15, %r13
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x20d0
movl -0x2c(%rbp), %edx
testl %edx, %edx
je 0x28a0
xorl %r15d, %r15d
movl %r15d, %esi
addq %r12, %rsi
subl %r15d, %edx
movl %r14d, %edi
xorl %ecx, %ecx
callq 0x20d0
movq %rax, %rbx
addl %r15d, %ebx
movl -0x2c(%rbp), %edx
movl %ebx, %r15d
cmpl %edx, %ebx
jb 0x287d
cmpl %edx, %ebx
movl -0x3c(%rbp), %r15d
je 0x28ba
leaq 0x81f(%rip), %rdi       # 0x30ce
movl %edx, %esi
movl %ebx, %edx
xorl %eax, %eax
callq 0x2050
movl %r14d, %edi
callq 0x22f0
movl -0x30(%rbp), %eax
movl %eax, (%rsp)
leaq -0x100(%rbp), %rdi
leaq -0x70(%rbp), %rsi
movq %r12, %rdx
movl %ebx, %ecx
movq %r13, %rbx
movq %r13, %r8
movq -0x48(%rbp), %r9
callq 0x2997
xorl %r13d, %r13d
movl %r13d, -0x2c(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x100(%rbp)
movaps %xmm0, -0xf0(%rbp)
movaps %xmm0, -0xe0(%rbp)
movaps %xmm0, -0xd0(%rbp)
movaps %xmm0, -0xc0(%rbp)
movaps %xmm0, -0xb0(%rbp)
movaps %xmm0, -0xa0(%rbp)
movaps %xmm0, -0x90(%rbp)
movl %r13d, -0x38(%rbp)
movaps %xmm0, -0x70(%rbp)
movaps %xmm0, -0x60(%rbp)
movb $0x0, -0x50(%rbp)
movl $0x100, %edx            # imm = 0x100
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2140
jmp 0x2728
movl %r14d, %edi
callq 0x22f0
jmp 0x2728
leaq 0x6ce(%rip), %rdi       # 0x302f
jmp 0x297e
leaq 0x6d2(%rip), %rdi       # 0x303c
xorl %ebx, %ebx
jmp 0x297e
leaq 0x6ee(%rip), %rdi       # 0x3063
jmp 0x297e
leaq 0x708(%rip), %rdi       # 0x3086
callq 0x21b0
movl %ebx, %eax
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq