// File: amit-mittal[P]FTP/ftpClient.cpp
// Function: ftpClient::transferFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
int ftpClient::transferFile(string filename){
	
	FILE* fp;
	char data[MAXBUFFSIZE];
	size_t bytes_read, bytes_sent;
	long fSize, remaining;

	fp = fopen(filename.c_str(), "r");

	if (fp == NULL) {
		fputs("File not found\n", stderr); 
		return 7;
	}

	// obtain file size:
  	fseek (fp, 0, SEEK_END);
	fSize = ftell(fp);
	rewind(fp);

	remaining = fSize;
	do {
		bytes_read = fread(data, 1, MAXBUFFSIZE-1, fp);
	
		if (bytes_read < 0) {
			fputs("Error in reading the file\n", stderr);
			return 8;
		}

		if ((bytes_sent = sendData(data, bytes_read)) < 0) {
			fputs("Error in send function\n", stderr);
			return 9;
		}
		
		if(remaining == bytes_sent)
			sendControl("EOF");

		else	sendControl("FSR");

		fseek(fp, bytes_sent-bytes_read, SEEK_CUR);	//As send() function may not send bytes_read amount of data
		remaining -= bytes_sent;		
		
	} while (remaining > 0);

	fclose(fp);
	cout << "\nSize of file transferred : " << calculateSize(fSize);

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x4d0, %rsp            # imm = 0x4D0
movq %rsi, -0x4b0(%rbp)
movq %rdi, %rax
movq -0x4b0(%rbp), %rdi
movq %rax, -0x10(%rbp)
movq %rdi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x4a8(%rbp)
callq 0x3100
movq %rax, %rdi
leaq 0x43ad(%rip), %rsi      # 0x875b
callq 0x32e0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x43e0
movq 0x6c13(%rip), %rax      # 0xafd8
movq (%rax), %rsi
leaq 0x3cb8(%rip), %rdi      # 0x8087
callq 0x32c0
movl $0x7, -0x4(%rbp)
jmp 0x469a
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x32b0
movq -0x20(%rbp), %rdi
callq 0x3030
movq %rax, -0x438(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3060
movq -0x438(%rbp), %rax
movq %rax, -0x440(%rbp)
leaq -0x420(%rbp), %rdi
movq -0x20(%rbp), %rcx
movl $0x1, %esi
movl $0x3ff, %edx            # imm = 0x3FF
callq 0x33b0
movq %rax, -0x428(%rbp)
cmpq $0x0, -0x428(%rbp)
jae 0x4466
movq 0x6b8d(%rip), %rax      # 0xafd8
movq (%rax), %rsi
leaq 0x3c42(%rip), %rdi      # 0x8097
callq 0x32c0
movl $0x8, -0x4(%rbp)
jmp 0x469a
movq -0x4a8(%rbp), %rdi
leaq -0x420(%rbp), %rsi
movq -0x428(%rbp), %rax
movl %eax, %edx
callq 0x41d0
cltq
movq %rax, -0x430(%rbp)
cmpq $0x0, %rax
jae 0x44b3
movq 0x6b40(%rip), %rax      # 0xafd8
movq (%rax), %rsi
leaq 0x3c10(%rip), %rdi      # 0x80b2
callq 0x32c0
movl $0x9, -0x4(%rbp)
jmp 0x469a
movq -0x440(%rbp), %rax
cmpq -0x430(%rbp), %rax
jne 0x456b
leaq -0x461(%rbp), %rdi
movq %rdi, -0x4b8(%rbp)
callq 0x3450
movq -0x4b8(%rbp), %rdx
leaq 0x3be2(%rip), %rsi      # 0x80ca
leaq -0x460(%rbp), %rdi
callq 0x3350
jmp 0x44f6
movq -0x4a8(%rbp), %rdi
leaq -0x460(%rbp), %rsi
callq 0x4210
jmp 0x450b
leaq -0x460(%rbp), %rdi
callq 0x3160
leaq -0x461(%rbp), %rdi
callq 0x32a0
jmp 0x460c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x470(%rbp)
movl %eax, -0x474(%rbp)
jmp 0x455a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x470(%rbp)
movl %eax, -0x474(%rbp)
leaq -0x460(%rbp), %rdi
callq 0x3160
leaq -0x461(%rbp), %rdi
callq 0x32a0
jmp 0x46a6
leaq -0x499(%rbp), %rdi
movq %rdi, -0x4c0(%rbp)
callq 0x3450
movq -0x4c0(%rbp), %rdx
leaq 0x3b42(%rip), %rsi      # 0x80ce
leaq -0x498(%rbp), %rdi
callq 0x3350
jmp 0x459a
movq -0x4a8(%rbp), %rdi
leaq -0x498(%rbp), %rsi
callq 0x4210
jmp 0x45af
leaq -0x498(%rbp), %rdi
callq 0x3160
leaq -0x499(%rbp), %rdi
callq 0x32a0
jmp 0x460c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x470(%rbp)
movl %eax, -0x474(%rbp)
jmp 0x45fb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x470(%rbp)
movl %eax, -0x474(%rbp)
leaq -0x498(%rbp), %rdi
callq 0x3160
leaq -0x499(%rbp), %rdi
callq 0x32a0
jmp 0x46a6
movq -0x20(%rbp), %rdi
movq -0x430(%rbp), %rsi
subq -0x428(%rbp), %rsi
movl $0x1, %edx
callq 0x32b0
movq -0x430(%rbp), %rcx
movq -0x440(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x440(%rbp)
cmpq $0x0, -0x440(%rbp)
jg 0x4419
movq -0x20(%rbp), %rdi
callq 0x3260
movq 0x6972(%rip), %rdi      # 0xafd0
leaq 0x3a6d(%rip), %rsi      # 0x80d2
callq 0x3250
movq -0x4a8(%rbp), %rdi
movq %rax, -0x4c8(%rbp)
movq -0x438(%rbp), %rsi
callq 0x46c0
movq -0x4c8(%rbp), %rdi
movq %rax, %rsi
callq 0x3250
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x4d0, %rsp            # imm = 0x4D0
popq %rbp
retq
movq -0x470(%rbp), %rdi
callq 0x3440
nopw %cs:(%rax,%rax)