// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x28a5(%rip), %rdi      # 0x15181
callq 0x55b0
leaq -0x248(%rbp), %rdi
pushq $0x4
popq %rdx
movq %r14, %rsi
callq 0x56e0
leaq 0x27ee(%rip), %rsi      # 0x150e8
leaq -0x48(%rbp), %rdi
movq %r15, %rdx
callq 0x13079
movl 0x14(%rbx), %edi
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
xorl %ecx, %ecx
callq 0x5230
movl $0x400, %edi            # imm = 0x400
callq 0x5030
movq %rax, %r14
movl 0x14(%rbx), %edi
movl $0x400, %edx            # imm = 0x400
movq %rax, %rsi
xorl %ecx, %ecx
callq 0x5150
cmpb $0x31, (%r14)
jne 0x12956
leaq 0x27ae(%rip), %rdi      # 0x150f2
movq %r14, %rsi
xorl %eax, %eax
callq 0x5060
pushq $0x1
popq %rbx
jmp 0x12a35
leaq -0x1d8(%rbp), %r14
leaq 0x2791(%rip), %r15      # 0x150f5
movq %r14, %rdi
callq 0x50d0
testb %al, %al
je 0x12a27
movl $0x400, %edi            # imm = 0x400
callq 0x5030
movq %rax, %r12
movl 0x14(%rbx), %edi
movl $0x400, %edx            # imm = 0x400
movq %rax, %rsi
xorl %ecx, %ecx
callq 0x5150
movq %r12, %rdi
callq 0x51c0
movq %rax, %r13
movq %r15, %rdi
movl %r13d, %esi
xorl %eax, %eax
callq 0x5060
testl %r13d, %r13d
jle 0x129fe
leaq 0x27db(%rip), %rdi      # 0x15192
callq 0x55b0
movl %r13d, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
leaq -0x248(%rbp), %rdi
movq %r12, %rsi
callq 0x54e0
movl $0x400, %edx            # imm = 0x400
movq %r12, %rdi
xorl %esi, %esi
callq 0x5270
cmpl $0x400, %r13d           # imm = 0x400
jae 0x12964
leaq 0x27ab(%rip), %rdi      # 0x151a2
callq 0x55b0
jmp 0x12a1b
callq 0x5050
movl (%rax), %edi
callq 0x5160
leaq 0x26fd(%rip), %rdi      # 0x1510e
movq %rax, %rsi
xorl %eax, %eax
callq 0x5060
leaq -0x248(%rbp), %rdi
callq 0x5550
leaq 0x2789(%rip), %rdi      # 0x151b7
callq 0x55b0
xorl %ebx, %ebx
leaq -0x48(%rbp), %rdi
callq 0x51e0
leaq -0x248(%rbp), %rdi
callq 0x5590
movl %ebx, %eax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x12a6f
jmp 0x12a6f
jmp 0x12a6f
jmp 0x12a6f
movq %rax, %rbx
jmp 0x12a7b
jmp 0x12a6f
jmp 0x12a6f
movq %rax, %rbx
leaq -0x48(%rbp), %rdi
callq 0x51e0
leaq -0x248(%rbp), %rdi
callq 0x5590
movq %rbx, %rdi
callq 0x5660
nop