// File: vianna274[P]dropbox/src/Operations.cpp
// Function: Dropbox::Operations::sendFile(Dropbox::WrapperSocket*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, Dropbox::_FileRecord, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void Operations::sendFile(WrapperSocket *socket, string filePath, FileRecord fileRec, string username){

	string filename = filePath.substr(filePath.find_last_of("/\\") + 1);
	struct stat buffer;   
  	if(stat(filePath.c_str(), &buffer) != 0){
		  MessageData failed = make_packet(TYPE_NOTHING_TO_SEND, 1, 1, -1, "", username.c_str());
		  socket->send(&failed);
		  return;
	}
	MessageData packet = make_packet(TYPE_SEND_FILE, 1, 1, sizeof(FileRecord), (char *)&fileRec, username.c_str());
	socket->send(&packet);

	ifstream file;
	file.open(filePath, ifstream::in | ifstream::binary);
	file.seekg(0, file.end);
	int fileSize = file.tellg();
	file.seekg(0, file.beg);
	int totalPackets = 1 + ((fileSize - 1) / MESSAGE_LEN); // ceil(x/y)
	int lastPacketSize = fileSize % MESSAGE_LEN;
	int packetsSent = 0;
	int filePointer = 0;
	int packetSize = MESSAGE_LEN;
	char payload[MESSAGE_LEN];
	cout << "Uploading " << filePath << " Size: " << fileSize << " NumPackets: " << totalPackets << endl;

	while (packetsSent < totalPackets){
		file.seekg(filePointer, file.beg);
		filePointer += MESSAGE_LEN;
		if(packetsSent == totalPackets - 1) packetSize = lastPacketSize;
		file.read(payload, packetSize);
		MessageData packet = make_packet(TYPE_DATA, packetsSent + 1, totalPackets, packetSize, payload, username.c_str());
		socket->send(&packet);
		packetsSent++;
	}
	file.close();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x42d0, %rsp           # imm = 0x42D0
movq %rcx, -0x4260(%rbp)
movq %rdx, -0x4250(%rbp)
movq %rdi, %rax
movq -0x4250(%rbp), %rdi
leaq 0x10(%rbp), %rdx
movq %rdx, -0x4258(%rbp)
movq %rax, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdi, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
leaq 0xf374(%rip), %rsi      # 0x19299
movq $-0x1, %rdx
callq 0x3380
movq -0x4250(%rbp), %rsi
movq %rax, %rdx
addq $0x1, %rdx
leaq -0x40(%rbp), %rdi
movq $-0x1, %rcx
callq 0x33c0
movq -0x4250(%rbp), %rdi
callq 0x3180
movq %rax, %rdi
leaq -0xd0(%rbp), %rsi
callq 0x3340
cmpl $0x0, %eax
je 0x9fe3
movq -0x4260(%rbp), %rdi
callq 0x3180
movq %rax, %rcx
movq %rsp, %rax
movq %rcx, (%rax)
leaq 0xf6c7(%rip), %r9       # 0x19652
leaq -0x10a4(%rbp), %rdi
movl $0x7, %esi
movl $0x1, %ecx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movl %ecx, %edx
callq 0x9830
jmp 0x9fab
movq -0x10(%rbp), %rdi
leaq -0x10a4(%rbp), %rsi
callq 0x17d90
jmp 0x9fbd
movl $0x1, -0x10b8(%rbp)
jmp 0xa36f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x10b0(%rbp)
movl %eax, -0x10b4(%rbp)
jmp 0xa381
movq -0x4260(%rbp), %rdi
callq 0x3180
movq -0x4258(%rbp), %r9
movq %rax, %rcx
movq %rsp, %rax
movq %rcx, (%rax)
leaq -0x208c(%rbp), %rdi
movl $0x3, %esi
movl $0x1, %ecx
movl $0xe8, %r8d
movl %ecx, %edx
callq 0x9830
jmp 0xa01f
movq -0x10(%rbp), %rdi
leaq -0x208c(%rbp), %rsi
callq 0x17d90
jmp 0xa031
leaq -0x2298(%rbp), %rdi
callq 0x33f0
jmp 0xa03f
movl $0x8, %edi
movl $0x4, %esi
callq 0xbc40
movl %eax, -0x4264(%rbp)
jmp 0xa056
movl -0x4264(%rbp), %edx
movq -0x4250(%rbp), %rsi
leaq -0x2298(%rbp), %rdi
callq 0x3310
jmp 0xa071
xorl %eax, %eax
movl %eax, %esi
leaq -0x2298(%rbp), %rdi
movl $0x2, %edx
callq 0x3470
jmp 0xa088
leaq -0x2298(%rbp), %rdi
callq 0x35e0
movq %rdx, -0x4278(%rbp)
movq %rax, -0x4270(%rbp)
jmp 0xa0a4
movq -0x4278(%rbp), %rax
movq -0x4270(%rbp), %rcx
movq %rcx, -0x22b0(%rbp)
movq %rax, -0x22a8(%rbp)
leaq -0x22b0(%rbp), %rdi
callq 0xbc60
movq %rax, -0x4280(%rbp)
jmp 0xa0d5
movq -0x4280(%rbp), %rax
movl %eax, -0x229c(%rbp)
xorl %edx, %edx
movl %edx, %esi
leaq -0x2298(%rbp), %rdi
callq 0x3470
jmp 0xa0f4
movl -0x229c(%rbp), %eax
decl %eax
cltq
imulq $0x10624dd3, %rax, %rax # imm = 0x10624DD3
movq %rax, %rcx
sarq $0x28, %rcx
movl %ecx, %edx
shrq $0x3f, %rax
movl %eax, %ecx
movl %edx, %eax
leal 0x1(%rax,%rcx), %eax
movl %eax, -0x22b4(%rbp)
movslq -0x229c(%rbp), %rcx
movl %ecx, %eax
imulq $0x10624dd3, %rcx, %rcx # imm = 0x10624DD3
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x28, %rcx
addl %edx, %ecx
imull $0xfa0, %ecx, %ecx      # imm = 0xFA0
subl %ecx, %eax
movl %eax, -0x22b8(%rbp)
movl $0x0, -0x22bc(%rbp)
movl $0x0, -0x22c0(%rbp)
movl $0xfa0, -0x22c4(%rbp)   # imm = 0xFA0
movq 0x18e58(%rip), %rdi     # 0x22fc8
leaq 0xf125(%rip), %rsi      # 0x1929c
callq 0x33a0
movq %rax, -0x4288(%rbp)
jmp 0xa185
movq -0x4250(%rbp), %rsi
movq -0x4288(%rbp), %rdi
callq 0x3330
movq %rax, -0x4290(%rbp)
jmp 0xa1a1
movq -0x4290(%rbp), %rdi
leaq 0xf0f8(%rip), %rsi      # 0x192a7
callq 0x33a0
movq %rax, -0x4298(%rbp)
jmp 0xa1bd
movq -0x4298(%rbp), %rdi
movl -0x229c(%rbp), %esi
callq 0x35d0
movq %rax, -0x42a0(%rbp)
jmp 0xa1d8
movq -0x42a0(%rbp), %rdi
leaq 0xf0c9(%rip), %rsi      # 0x192af
callq 0x33a0
movq %rax, -0x42a8(%rbp)
jmp 0xa1f4
movq -0x42a8(%rbp), %rdi
movl -0x22b4(%rbp), %esi
callq 0x35d0
movq %rax, -0x42b0(%rbp)
jmp 0xa20f
movq -0x42b0(%rbp), %rdi
movq 0x18d93(%rip), %rsi     # 0x22fb0
callq 0x33d0
jmp 0xa224
jmp 0xa226
movl -0x22bc(%rbp), %eax
cmpl -0x22b4(%rbp), %eax
jge 0xa34b
movslq -0x22c0(%rbp), %rsi
leaq -0x2298(%rbp), %rdi
xorl %edx, %edx
callq 0x3470
jmp 0xa24f
movl -0x22c0(%rbp), %eax
addl $0xfa0, %eax            # imm = 0xFA0
movl %eax, -0x22c0(%rbp)
movl -0x22bc(%rbp), %eax
movl -0x22b4(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0xa2a4
movl -0x22b8(%rbp), %eax
movl %eax, -0x22c4(%rbp)
jmp 0xa2a4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x10b0(%rbp)
movl %eax, -0x10b4(%rbp)
leaq -0x2298(%rbp), %rdi
callq 0x3100
jmp 0xa381
movslq -0x22c4(%rbp), %rdx
leaq -0x2298(%rbp), %rdi
leaq -0x3270(%rbp), %rsi
callq 0x3440
jmp 0xa2c0
movq -0x4260(%rbp), %rdi
movl -0x22bc(%rbp), %eax
incl %eax
movl %eax, -0x42bc(%rbp)
movl -0x22b4(%rbp), %eax
movl %eax, -0x42b8(%rbp)
movl -0x22c4(%rbp), %eax
movl %eax, -0x42b4(%rbp)
callq 0x3180
movl -0x42bc(%rbp), %edx
movl -0x42b8(%rbp), %ecx
movl -0x42b4(%rbp), %r8d
movq %rax, %rsi
movq %rsp, %rax
movq %rsi, (%rax)
leaq -0x4244(%rbp), %rdi
xorl %esi, %esi
leaq -0x3270(%rbp), %r9
callq 0x9830
jmp 0xa325
movq -0x10(%rbp), %rdi
leaq -0x4244(%rbp), %rsi
callq 0x17d90
jmp 0xa337
movl -0x22bc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x22bc(%rbp)
jmp 0xa226
leaq -0x2298(%rbp), %rdi
callq 0x30d0
jmp 0xa359
leaq -0x2298(%rbp), %rdi
callq 0x3100
movl $0x0, -0x10b8(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x3280
addq $0x42d0, %rsp           # imm = 0x42D0
popq %rbp
retq
leaq -0x40(%rbp), %rdi
callq 0x3280
movq -0x10b0(%rbp), %rdi
callq 0x3610
nopw %cs:(%rax,%rax)