// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x458, %rsp            # imm = 0x458
movq %rdi, %rbx
movq (%rsi), %rdi
leaq 0x3696(%rip), %rsi      # 0x7657
callq 0x32c0
testq %rax, %rax
je 0x4154
movq %rax, %r14
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x3270
movq %r14, %rdi
callq 0x3030
movq %rax, %r12
movq %r14, %rdi
callq 0x3060
leaq -0x70(%rbp), %r13
movq %r12, -0x38(%rbp)
movl $0x1, %esi
movl $0x3ff, %edx            # imm = 0x3FF
leaq -0x480(%rbp), %r15
movq %r15, %rdi
movq %r14, %rcx
callq 0x3360
movl 0xf0(%rbx), %edi
movq %rax, -0x40(%rbp)
movslq %eax, %rdx
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x3140
movslq %eax, %r15
subq %r15, %r12
movq %r12, -0x30(%rbp)
jne 0x406d
movq %r13, -0x80(%rbp)
leaq -0x80(%rbp), %r12
movq %r12, %rdi
leaq 0x304a(%rip), %rsi      # 0x7097
leaq 0x3046(%rip), %rdx      # 0x709a
callq 0x62b6
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3ed2
movq -0x80(%rbp), %rdi
cmpq %r13, %rdi
jmp 0x40b1
movq %r13, %r12
movq %r14, %r13
movq %rbx, %r14
leaq -0x50(%rbp), %rbx
movq %rbx, -0x60(%rbp)
leaq -0x60(%rbp), %rdi
leaq 0x3012(%rip), %rsi      # 0x709b
leaq 0x300e(%rip), %rdx      # 0x709e
callq 0x62b6
movq %r14, %rdi
leaq -0x60(%rbp), %rsi
callq 0x3ed2
movq -0x60(%rbp), %rdi
cmpq %rbx, %rdi
movq %r14, %rbx
movq %r13, %r14
movq %r12, %r13
movq -0x30(%rbp), %r12
je 0x40bc
callq 0x3210
subq -0x40(%rbp), %r15
movq %r14, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x3270
testq %r12, %r12
jg 0x3ffc
movq %r14, %rdi
callq 0x3240
movq 0x5ee8(%rip), %rbx      # 0x9fd0
leaq 0x2fb0(%rip), %rsi      # 0x709f
movl $0x1c, %edx
movq %rbx, %rdi
callq 0x32b0
movq -0x38(%rbp), %rsi
callq 0x4188
testq %rax, %rax
je 0x4129
movq %rax, %r14
movq %rax, %rdi
callq 0x3120
movq 0x5eb4(%rip), %rdi      # 0x9fd0
movq %r14, %rsi
movq %rax, %rdx
callq 0x32b0
jmp 0x4140
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x3410
xorl %eax, %eax
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x35a9
movl $0x7, %eax
jmp 0x4142
movq %rax, %r14
movq -0x60(%rbp), %rdi
cmpq %rbx, %rdi
jne 0x417a
jmp 0x417f
movq %rax, %r14
movq -0x80(%rbp), %rdi
cmpq %r13, %rdi
je 0x417f
callq 0x3210
movq %r14, %rdi
callq 0x3450
nop