// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3248, %rsp           # imm = 0x3248
movq %rcx, %r14
movq %rdx, %r12
movq %rsi, -0x40(%rbp)
leaq 0x66a2(%rip), %rsi      # 0xc299
pushq $-0x1
popq %rbx
movq %rdx, %rdi
movq %rbx, %rdx
callq 0x3300
leaq 0x1(%rax), %rdx
leaq -0x78(%rbp), %rdi
movq %r12, %rsi
movq %rbx, %rcx
callq 0x3330
movq (%r12), %rdi
leaq -0x310(%rbp), %rsi
callq 0x32c0
testl %eax, %eax
je 0x5c67
movq (%r14), %rax
movq %rax, (%rsp)
leaq 0x69c3(%rip), %r9       # 0xc5fd
leaq -0x12e4(%rbp), %rdi
pushq $0x7
popq %rsi
pushq $0x1
popq %rcx
pushq $-0x1
popq %r8
movl %ecx, %edx
callq 0x5844
leaq -0x12e4(%rbp), %rsi
movq -0x40(%rbp), %rdi
callq 0xb850
jmp 0x5e2f
leaq 0x10(%rbp), %r9
movq (%r14), %rax
movq %rax, (%rsp)
leaq -0x12e4(%rbp), %rdi
pushq $0x3
popq %rsi
pushq $0x1
popq %r15
movl %r15d, %edx
movl %r15d, %ecx
movl $0xe8, %r8d
callq 0x5844
leaq -0x12e4(%rbp), %rsi
movq -0x40(%rbp), %rdi
callq 0xb850
leaq -0x280(%rbp), %rdi
callq 0x3340
leaq -0x280(%rbp), %rdi
pushq $0xc
popq %rdx
movq %r12, %rsi
callq 0x3290
leaq -0x280(%rbp), %rdi
pushq $0x2
popq %rdx
xorl %esi, %esi
callq 0x33c0
leaq -0x280(%rbp), %rdi
callq 0x3510
movq %rax, %rbx
leaq -0x280(%rbp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x33c0
movq %r14, -0x50(%rbp)
leal -0x1(%rbx), %eax
movl $0xfa0, %ecx            # imm = 0xFA0
cltd
idivl %ecx
movq %rax, -0x38(%rbp)
movl %ebx, %eax
cltd
idivl %ecx
movl %edx, -0x44(%rbp)
movq 0xb2b7(%rip), %rdi      # 0x10fc8
leaq 0x6584(%rip), %rsi      # 0xc29c
callq 0x3310
movq %rax, %rdi
movq %r12, %rsi
callq 0x32b0
leaq 0x6578(%rip), %rsi      # 0xc2a7
movq %rax, %rdi
callq 0x3310
movq %rax, %rdi
movl %ebx, %esi
callq 0x3500
leaq 0x6567(%rip), %rsi      # 0xc2af
movq %rax, %rdi
callq 0x3310
movq -0x38(%rbp), %rcx
leal 0x1(%rcx), %esi
movq %rax, %rdi
movl %esi, -0x2c(%rbp)
callq 0x3500
movq %rax, %rdi
callq 0x31c0
xorl %r14d, %r14d
movl -0x2c(%rbp), %eax
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
negl %ecx
movq %rcx, -0x58(%rbp)
movq -0x38(%rbp), %rax
negl %eax
movq %rax, -0x38(%rbp)
movl $0xfa0, %r13d           # imm = 0xFA0
movq -0x58(%rbp), %rax
addl %r15d, %eax
cmpl $0x1, %eax
je 0x5e17
leaq -0x280(%rbp), %r12
movq %r12, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x33c0
leaq -0x2290(%rbp), %rbx
movq -0x38(%rbp), %rax
addl %r15d, %eax
cmpl $0x1, %eax
cmovel -0x44(%rbp), %r13d
movslq %r13d, %rdx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x3390
movq -0x50(%rbp), %rax
movq (%rax), %rax
movq %rax, (%rsp)
leaq -0x3264(%rbp), %r12
movq %r12, %rdi
xorl %esi, %esi
movl %r15d, %edx
movl -0x2c(%rbp), %ecx
movl %r13d, %r8d
movq %rbx, %r9
callq 0x5844
movq -0x40(%rbp), %rdi
movq %r12, %rsi
callq 0xb850
incl %r15d
addq $0xfa0, %r14            # imm = 0xFA0
jmp 0x5d90
leaq -0x280(%rbp), %rdi
callq 0x3090
leaq -0x280(%rbp), %rdi
callq 0x30d0
leaq -0x78(%rbp), %rdi
callq 0x3170
addq $0x3248, %rsp           # imm = 0x3248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5e5d
jmp 0x5e5d
jmp 0x5e54
jmp 0x5e5d
jmp 0x5e54
movq %rax, %rbx
jmp 0x5e6c
jmp 0x5e5d
jmp 0x5e5d
movq %rax, %rbx
leaq -0x280(%rbp), %rdi
callq 0x30d0
leaq -0x78(%rbp), %rdi
callq 0x3170
movq %rbx, %rdi
callq 0x3540
nop