// File: /workspace/github_cpp_makefile_star2_O1/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 0x2e57(%rip), %rdi      # 0x17181
callq 0x55a0
leaq -0x248(%rbp), %rdi
movq %r14, %rsi
movl $0x4, %edx
callq 0x56d0
leaq 0x2d9e(%rip), %rsi      # 0x170e8
leaq -0x48(%rbp), %rdi
movq %r15, %rdx
callq 0x14ddb
movl 0x14(%rbx), %edi
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
xorl %ecx, %ecx
callq 0x51f0
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 0x5140
cmpb $0x31, (%r14)
jne 0x143a8
leaq 0x2d5e(%rip), %rdi      # 0x170f2
movq %r14, %rsi
xorl %eax, %eax
callq 0x5070
movl $0x1, %ebx
jmp 0x14487
leaq -0x1d8(%rbp), %r14
leaq 0x2d3f(%rip), %r15      # 0x170f5
movq %r14, %rdi
callq 0x50c0
testb %al, %al
je 0x14479
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 0x5140
movq %r12, %rdi
callq 0x51c0
movq %rax, %r13
movq %r15, %rdi
movl %r13d, %esi
xorl %eax, %eax
callq 0x5070
testl %r13d, %r13d
jle 0x14450
leaq 0x2d89(%rip), %rdi      # 0x17192
callq 0x55a0
movl %r13d, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
leaq -0x248(%rbp), %rdi
movq %r12, %rsi
callq 0x54c0
movl $0x400, %edx            # imm = 0x400
movq %r12, %rdi
xorl %esi, %esi
callq 0x5230
cmpl $0x400, %r13d           # imm = 0x400
jge 0x143b6
leaq 0x2d59(%rip), %rdi      # 0x171a2
callq 0x55a0
jmp 0x1446d
callq 0x5060
movl (%rax), %edi
callq 0x5160
leaq 0x2cab(%rip), %rdi      # 0x1710e
movq %rax, %rsi
xorl %eax, %eax
callq 0x5070
leaq -0x248(%rbp), %rdi
callq 0x5530
leaq 0x2d37(%rip), %rdi      # 0x171b7
callq 0x55a0
xorl %ebx, %ebx
leaq -0x38(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x144a0
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x5370
movq 0xaad9(%rip), %rax      # 0x1ef80
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq -0x240(%rbp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, -0x248(%rbp,%rcx)
callq 0x5300
leaq -0x150(%rbp), %rdi
callq 0x5150
movl %ebx, %eax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x144fb
jmp 0x144fb
jmp 0x144fb
jmp 0x144fb
movq %rax, %rbx
jmp 0x14517
jmp 0x144fb
jmp 0x144fb
movq %rax, %rbx
leaq -0x38(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x14517
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x5370
leaq -0x248(%rbp), %rdi
callq 0x5580
movq %rbx, %rdi
callq 0x5630
nop