// File: akw0088[P]openssl/download.cpp
// Function: main
int main(int argc, char *argv[])
{
	unsigned short port = 65535;
	unsigned int size = 0;

	if (argc < 4)
	{
		printf("Usage: download ip port max_size\r\n");
		return 0;
	}

	port = atoi(argv[2]);
	size = atoi(argv[3]);

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

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

	printf("Attempting to download file from ip %s port %d\r\n", argv[1], (int)port);
	int ret = file_download(argv[1], port, response, size, &download_size, file_name);
	if (ret != 0)
	{
		printf("Download failed\r\n");
		return 0;
	}

	printf("Download complete\r\n");
	printf("Got %d bytes file name %s\r\n", download_size, file_name);

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

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

	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:
pushq %rbp
movq %rsp, %rbp
subq $0x2c0, %rsp            # imm = 0x2C0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movw $0xffff, -0x12(%rbp)    # imm = 0xFFFF
movl $0x0, -0x18(%rbp)
cmpl $0x4, -0x8(%rbp)
jge 0x1a56
leaq 0x7d2(%rip), %rdi       # 0x2215
movb $0x0, %al
callq 0x1060
movl $0x0, -0x4(%rbp)
jmp 0x1bdd
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x11f0
movw %ax, -0x12(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x11f0
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %esi
leaq 0x7b7(%rip), %rdi       # 0x2238
movb $0x0, %al
callq 0x1060
movl -0x18(%rbp), %eax
movl %eax, %edi
callq 0x11d0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x1aa9
leaq 0x7aa(%rip), %rdi       # 0x224e
callq 0x1150
movl $0x0, -0x24(%rbp)
leaq -0xb0(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x1110
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movzwl -0x12(%rbp), %edx
leaq 0x786(%rip), %rdi       # 0x225c
movb $0x0, %al
callq 0x1060
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movw -0x12(%rbp), %ax
movq -0x20(%rbp), %rdx
movl -0x18(%rbp), %ecx
leaq -0xb0(%rbp), %r9
leaq -0x24(%rbp), %r8
movzwl %ax, %esi
callq 0x14d0
movl %eax, -0xb4(%rbp)
cmpl $0x0, -0xb4(%rbp)
je 0x1b2c
leaq 0x774(%rip), %rdi       # 0x228d
movb $0x0, %al
callq 0x1060
movl $0x0, -0x4(%rbp)
jmp 0x1bdd
leaq 0x76c(%rip), %rdi       # 0x229f
movb $0x0, %al
callq 0x1060
movl -0x24(%rbp), %esi
leaq -0xb0(%rbp), %rdx
leaq 0x768(%rip), %rdi       # 0x22b3
movb $0x0, %al
callq 0x1060
leaq -0x1c0(%rbp), %rdi
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x1110
leaq -0x2c0(%rbp), %rdi
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x1110
leaq -0xb0(%rbp), %rdi
leaq -0x2c0(%rbp), %rsi
leaq 0x742(%rip), %rdx       # 0x22cf
callq 0x1950
leaq -0x1c0(%rbp), %rdi
leaq -0x2c0(%rbp), %rdx
leaq 0x734(%rip), %rsi       # 0x22db
movb $0x0, %al
callq 0x1090
leaq -0x1c0(%rbp), %rsi
leaq 0x72d(%rip), %rdi       # 0x22e9
movb $0x0, %al
callq 0x1060
leaq -0x1c0(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl -0x24(%rbp), %edx
callq 0x1430
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x2c0, %rsp            # imm = 0x2C0
popq %rbp
retq