// File: akw0088[P]openssl/rsync_download.c
// Function: main
int main(int argc, char *argv[])
{
	if (argc < 5)
	{
		printf("Usage: rsync_download ip port max_size block_size\r\n");
		return 0;
	}

#ifdef WIN32
	WSADATA		WSAData;

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

	unsigned short port = atoi(argv[2]);
	unsigned int max_malloc_size = atoi(argv[3]);
	unsigned int block_size = atoi(argv[4]);

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

	unsigned int download_size = 0;
	char file_name[PATH_SIZE] = {0};



	printf("Attempting to download file from ip %s port %d using block size %d\r\n", argv[1], (int)port, block_size);
	int ret = rsync_file_download(argv[1], port, response, max_malloc_size, &download_size, file_name, block_size);
	if (ret < 0)
	{
		printf("Download failed\r\n");
		return 0;
	}
	else if (ret == 0 && download_size == 0)
	{
		printf("File did not change\r\n");
	}
	else
	{
		printf("Download complete\r\n");
		printf("Got %d bytes file name %s\r\n", download_size, file_name);

		char new_filename[PATH_SIZE] = {0};
		char strip_filename[PATH_SIZE] = {0};

		StripChars(file_name, strip_filename, ".\\/;:*?\"<>|");

		sprintf(new_filename, "downloaded_%s", strip_filename);
		printf("Saving as file name %s\r\n", new_filename);
		write_file(new_filename, response, download_size);
	}
	return 0;
}

// Assembly:
subq $0x1d8, %rsp            # imm = 0x1D8
movl $0x0, 0x1d4(%rsp)
movl %edi, 0x1d0(%rsp)
movq %rsi, 0x1c8(%rsp)
cmpl $0x5, 0x1d0(%rsp)
jge 0x3369
leaq 0x2291(%rip), %rdi      # 0x55e3
movb $0x0, %al
callq 0x10e0
movl $0x0, 0x1d4(%rsp)
jmp 0x359a
movq 0x1c8(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x11f0
movw %ax, 0x1c6(%rsp)
movq 0x1c8(%rsp), %rax
movq 0x18(%rax), %rdi
callq 0x11f0
movl %eax, 0x1c0(%rsp)
movq 0x1c8(%rsp), %rax
movq 0x20(%rax), %rdi
callq 0x11f0
movl %eax, 0x1bc(%rsp)
movl 0x1c0(%rsp), %esi
leaq 0x2257(%rip), %rdi      # 0x5617
movb $0x0, %al
callq 0x10e0
movl 0x1c0(%rsp), %eax
movl %eax, %edi
callq 0x1180
movq %rax, 0x1b0(%rsp)
cmpq $0x0, 0x1b0(%rsp)
jne 0x33f4
leaq 0x1edd(%rip), %rdi      # 0x52cc
callq 0x11d0
movl $0x0, 0x1ac(%rsp)
leaq 0x120(%rsp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x1110
movq 0x1c8(%rsp), %rax
movq 0x8(%rax), %rsi
movzwl 0x1c6(%rsp), %edx
movl 0x1bc(%rsp), %ecx
leaq 0x21f8(%rip), %rdi      # 0x562d
movb $0x0, %al
callq 0x10e0
movq 0x1c8(%rsp), %rax
movq 0x8(%rax), %rdi
movw 0x1c6(%rsp), %si
movq 0x1b0(%rsp), %rdx
movl 0x1c0(%rsp), %ecx
leaq 0x120(%rsp), %r9
movl 0x1bc(%rsp), %eax
leaq 0x1ac(%rsp), %r8
movzwl %si, %esi
movl %eax, (%rsp)
callq 0x1cf0
movl %eax, 0x11c(%rsp)
cmpl $0x0, 0x11c(%rsp)
jge 0x34b0
leaq 0x21d9(%rip), %rdi      # 0x5672
movb $0x0, %al
callq 0x10e0
movl $0x0, 0x1d4(%rsp)
jmp 0x359a
cmpl $0x0, 0x11c(%rsp)
jne 0x34d7
cmpl $0x0, 0x1ac(%rsp)
jne 0x34d7
leaq 0x21b9(%rip), %rdi      # 0x5684
movb $0x0, %al
callq 0x10e0
jmp 0x358d
leaq 0x21bc(%rip), %rdi      # 0x569a
movb $0x0, %al
callq 0x10e0
movl 0x1ac(%rsp), %esi
leaq 0x120(%rsp), %rdx
leaq 0x21b3(%rip), %rdi      # 0x56ae
movb $0x0, %al
callq 0x10e0
leaq 0x90(%rsp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x1110
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x1110
leaq 0x120(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x1cac(%rip), %rdx      # 0x51e7
callq 0x2800
leaq 0x90(%rsp), %rdi
leaq 0x10(%rsp), %rdx
leaq 0x2176(%rip), %rsi      # 0x56ca
movb $0x0, %al
callq 0x1200
leaq 0x90(%rsp), %rsi
leaq 0x216e(%rip), %rdi      # 0x56d8
movb $0x0, %al
callq 0x10e0
leaq 0x90(%rsp), %rdi
movq 0x1b0(%rsp), %rsi
movl 0x1ac(%rsp), %edx
callq 0x1ae0
jmp 0x358f
movl $0x0, 0x1d4(%rsp)
movl 0x1d4(%rsp), %eax
addq $0x1d8, %rsp            # imm = 0x1D8
retq
nopl (%rax)