// File: amit-mittal[P]FTP/ftpServer.cpp
// Function: ftpServer::transferFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
int ftpServer::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((unsigned long)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);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x4c0, %rsp            # imm = 0x4C0
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 0x20f0
movq %rax, %rdi
leaq 0xb12(%rip), %rsi       # 0x6120
callq 0x2290
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x5640
movq 0x39b3(%rip), %rax      # 0x8fd8
movq (%rax), %rsi
leaq 0xdb7(%rip), %rdi       # 0x63e6
callq 0x2270
movl $0x7, -0x4(%rbp)
jmp 0x58bb
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x2260
movq -0x20(%rbp), %rdi
callq 0x2030
movq %rax, -0x438(%rbp)
movq -0x20(%rbp), %rdi
callq 0x2060
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 0x2350
movq %rax, -0x428(%rbp)
cmpq $0x0, -0x428(%rbp)
jae 0x56c6
movq 0x392d(%rip), %rax      # 0x8fd8
movq (%rax), %rsi
leaq 0xd45(%rip), %rdi       # 0x63fa
callq 0x2270
movl $0x8, -0x4(%rbp)
jmp 0x58bb
movq -0x4a8(%rbp), %rdi
leaq -0x420(%rbp), %rsi
movq -0x428(%rbp), %rax
movl %eax, %edx
callq 0x3300
cltq
movq %rax, -0x430(%rbp)
cmpq $0x0, %rax
jae 0x5713
movq 0x38e0(%rip), %rax      # 0x8fd8
movq (%rax), %rsi
leaq 0xd13(%rip), %rdi       # 0x6415
callq 0x2270
movl $0x9, -0x4(%rbp)
jmp 0x58bb
movq -0x440(%rbp), %rax
cmpq -0x430(%rbp), %rax
jne 0x57cb
leaq -0x461(%rbp), %rdi
movq %rdi, -0x4b8(%rbp)
callq 0x23d0
movq -0x4b8(%rbp), %rdx
leaq 0xcae(%rip), %rsi       # 0x63f6
leaq -0x460(%rbp), %rdi
callq 0x22f0
jmp 0x5756
movq -0x4a8(%rbp), %rdi
leaq -0x460(%rbp), %rsi
callq 0x3340
jmp 0x576b
leaq -0x460(%rbp), %rdi
callq 0x2150
leaq -0x461(%rbp), %rdi
callq 0x2250
jmp 0x5869
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x470(%rbp)
movl %eax, -0x474(%rbp)
jmp 0x57ba
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x470(%rbp)
movl %eax, -0x474(%rbp)
leaq -0x460(%rbp), %rdi
callq 0x2150
leaq -0x461(%rbp), %rdi
callq 0x2250
jmp 0x58c7
leaq -0x499(%rbp), %rdi
movq %rdi, -0x4c0(%rbp)
callq 0x23d0
movq -0x4c0(%rbp), %rdx
leaq 0xc41(%rip), %rsi       # 0x642d
leaq -0x498(%rbp), %rdi
callq 0x22f0
jmp 0x57fa
movq -0x4a8(%rbp), %rdi
leaq -0x498(%rbp), %rsi
callq 0x3340
jmp 0x580f
leaq -0x498(%rbp), %rdi
callq 0x2150
leaq -0x499(%rbp), %rdi
callq 0x2250
jmp 0x5869
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x470(%rbp)
movl %eax, -0x474(%rbp)
jmp 0x585b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x470(%rbp)
movl %eax, -0x474(%rbp)
leaq -0x498(%rbp), %rdi
callq 0x2150
leaq -0x499(%rbp), %rdi
callq 0x2250
jmp 0x58c7
movq -0x20(%rbp), %rdi
movq -0x430(%rbp), %rsi
subq -0x428(%rbp), %rsi
movl $0x1, %edx
callq 0x2260
movq -0x430(%rbp), %rcx
movq -0x440(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x440(%rbp)
cmpq $0x0, -0x440(%rbp)
jg 0x5679
movq -0x20(%rbp), %rdi
callq 0x2210
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x4c0, %rsp            # imm = 0x4C0
popq %rbp
retq
movq -0x470(%rbp), %rdi
callq 0x23c0
nopw %cs:(%rax,%rax)