// File: /workspace/github_cpp_makefile_star2_O3/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, -0x34(%rbp)
movq %rsi, -0x48(%rbp)
movl %edi, %ebx
movl $0x10, -0x40(%rbp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x20a0
cmpl $-0x1, %eax
je 0x28f6
movl %eax, %r15d
leaq -0x58(%rbp), %rsi
movq $0x0, 0x8(%rsi)
movw $0x2, (%rsi)
movl $0x0, 0x4(%rsi)
rolw $0x8, %bx
movw %bx, 0x2(%rsi)
movl %eax, %edi
movl $0x10, %edx
callq 0x2060
cmpl $-0x1, %eax
je 0x28ff
movl -0x54(%rbp), %edi
callq 0x2110
movzwl -0x56(%rbp), %ecx
rolw $0x8, %cx
movzwl %cx, %edx
leaq 0x97c(%rip), %rdi       # 0x3047
movq %rax, %rsi
xorl %eax, %eax
callq 0x2050
movl %r15d, %edi
movl $0x3, %esi
callq 0x2210
cmpl $-0x1, %eax
je 0x290f
leaq 0x97e(%rip), %rdi       # 0x3070
movl -0x34(%rbp), %ebx
movl %ebx, %esi
xorl %eax, %eax
callq 0x2050
movslq %ebx, %rdi
callq 0x2270
testq %rax, %rax
je 0x2918
movq %rax, %r12
leaq -0x210(%rbp), %rbx
leaq 0xb74(%rip), %r13       # 0x3294
movl %r15d, -0x30(%rbp)
movl $0x0, -0x3c(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
movaps %xmm0, -0x80(%rbp)
movb $0x0, -0x60(%rbp)
movl $0x100, %edx            # imm = 0x100
movq %rbx, %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 %r13, %rdi
callq 0x22a0
movl %r15d, %edi
leaq -0x90(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq 0x20b0
cmpl $-0x1, %eax
je 0x2724
movl %eax, %r14d
movl -0x8c(%rbp), %edi
callq 0x2110
leaq 0x8db(%rip), %rdi       # 0x3094
movq %rax, %rsi
xorl %eax, %eax
callq 0x2050
xorl %eax, %eax
movl %eax, -0x2c(%rbp)
movl %eax, -0x38(%rbp)
movl $0x4, %edx
movl %r14d, %edi
leaq -0x38(%rbp), %rsi
xorl %ecx, %ecx
callq 0x20d0
cmpl $0xf0f0f0f0, -0x38(%rbp) # imm = 0xF0F0F0F0
jne 0x28e9
movl %r15d, %r13d
movq %rbx, %r15
xorl %ebx, %ebx
leaq 0x8a7(%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 -0x110(%rbp), %rsi
xorl %ecx, %ecx
callq 0x20d0
movl $0x4, %edx
movl %r14d, %edi
leaq -0x3c(%rbp), %rsi
xorl %ecx, %ecx
callq 0x20d0
movl $0x20, %edx
movl %r14d, %edi
leaq -0x80(%rbp), %rsi
xorl %ecx, %ecx
callq 0x20d0
movl $0x100, %edx            # imm = 0x100
movl %r14d, %edi
movq %r15, %rsi
movl %r13d, %r15d
xorl %ecx, %ecx
callq 0x20d0
movl -0x2c(%rbp), %edx
testl %edx, %edx
je 0x28ac
xorl %ebx, %ebx
movl %ebx, %r15d
movl %ebx, %esi
addq %r12, %rsi
subl %ebx, %edx
movl %r14d, %edi
xorl %ecx, %ecx
callq 0x20d0
movq %rax, %rbx
addl %r15d, %ebx
movl -0x2c(%rbp), %edx
cmpl %edx, %ebx
jb 0x2873
je 0x28a8
leaq 0x831(%rip), %rdi       # 0x30ce
movl %edx, %esi
movl %ebx, %edx
xorl %eax, %eax
callq 0x2050
movl -0x30(%rbp), %r15d
movl %r14d, %edi
callq 0x22f0
movl -0x34(%rbp), %eax
movl %eax, (%rsp)
leaq -0x110(%rbp), %rdi
leaq -0x80(%rbp), %rsi
movq %r12, %rdx
movl %ebx, %ecx
leaq -0x210(%rbp), %rbx
movq %rbx, %r8
movq -0x48(%rbp), %r9
callq 0x293b
leaq 0x9b0(%rip), %r13       # 0x3294
jmp 0x2724
movl %r14d, %edi
callq 0x22f0
jmp 0x2724
leaq 0x732(%rip), %rdi       # 0x302f
jmp 0x291f
leaq 0x736(%rip), %rdi       # 0x303c
callq 0x21b0
xorl %eax, %eax
jmp 0x2929
leaq 0x74d(%rip), %rdi       # 0x3063
jmp 0x291f
leaq 0x767(%rip), %rdi       # 0x3086
callq 0x21b0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq