// File: /workspace/github_cpp_makefile_star2_O3/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, %rbx
movq %rdx, %r12
movq %rsi, -0x30(%rbp)
leaq 0x97ea(%rip), %rsi      # 0x102b9
movl $0x2, %ecx
movq %rdx, %rdi
movq $-0x1, %rdx
callq 0x35f0
leaq 0x1(%rax), %rdx
leaq -0x78(%rbp), %rdi
movq %r12, %rsi
movq $-0x1, %rcx
callq 0x3360
movq (%r12), %rdi
leaq -0x310(%rbp), %rsi
callq 0x32f0
testl %eax, %eax
je 0x6b52
movq (%rbx), %rax
movq %rax, (%rsp)
leaq 0x9b19(%rip), %r9       # 0x10635
leaq -0x12e4(%rbp), %rdi
movl $0x7, %esi
movl $0x1, %edx
movl $0x1, %ecx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
callq 0x6607
leaq -0x12e4(%rbp), %rsi
movq -0x30(%rbp), %rdi
callq 0xea3c
jmp 0x6d9f
leaq 0x10(%rbp), %r9
movq %rbx, -0x50(%rbp)
movq (%rbx), %rax
movq %rax, (%rsp)
leaq -0x12e4(%rbp), %rdi
movl $0x3, %esi
movl $0x1, %edx
movl $0x1, %ecx
movl $0xe8, %r8d
callq 0x6607
leaq -0x12e4(%rbp), %rsi
movq -0x30(%rbp), %rdi
callq 0xea3c
leaq -0x280(%rbp), %rdi
callq 0x3380
leaq -0x280(%rbp), %rdi
movq %r12, %rsi
movl $0xc, %edx
callq 0x32d0
leaq -0x280(%rbp), %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x3400
leaq -0x280(%rbp), %rdi
callq 0x3550
movq %rax, %r13
leaq -0x280(%rbp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3400
movq 0xe3e5(%rip), %rdi      # 0x14fd0
leaq 0x96ca(%rip), %rsi      # 0x102bc
movl $0xa, %edx
callq 0x33a0
movq (%r12), %rsi
movq 0x8(%r12), %rdx
movq 0xe3c4(%rip), %rdi      # 0x14fd0
callq 0x33a0
movq %rax, %rbx
leaq 0x96ac(%rip), %rsi      # 0x102c7
movl $0x7, %edx
movq %rax, %rdi
callq 0x33a0
movq %rbx, %rdi
movl %r13d, %esi
callq 0x3540
movq %rax, %rbx
leaq 0x9692(%rip), %rsi      # 0x102cf
movl $0xd, %edx
movq %rax, %rdi
callq 0x33a0
leal -0x1(%r13), %eax
cltq
imulq $0x10624dd3, %rax, %r15 # imm = 0x10624DD3
movq %r15, %r14
shrq $0x3f, %r14
sarq $0x28, %r15
leal (%r15,%r14), %esi
incl %esi
movq %rbx, %rdi
movl %esi, -0x40(%rbp)
callq 0x3540
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3330
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3260
cmpl $0xfffff062, %r13d      # imm = 0xFFFFF062
jl 0x6d87
addl %r14d, %r15d
movslq %r13d, %rax
imulq $0x10624dd3, %rax, %rax # imm = 0x10624DD3
movq %rax, %rcx
shrq $0x3f, %rcx
sarq $0x28, %rax
addl %ecx, %eax
imull $0xfa0, %eax, %eax      # imm = 0xFA0
subl %eax, %r13d
movq %r13, -0x48(%rbp)
xorl %r13d, %r13d
testl %r15d, %r15d
movl $0x0, %eax
cmovgl %r15d, %eax
movl %eax, -0x3c(%rbp)
movl $0xfa0, %ebx            # imm = 0xFA0
leaq -0x280(%rbp), %r14
leaq -0x3260(%rbp), %r12
movq $0x0, -0x38(%rbp)
movq %r14, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x3400
subl $0x1, %r15d
cmovbl -0x48(%rbp), %ebx
movslq %ebx, %rdx
movq %r14, %rdi
movq %r12, %rsi
callq 0x33e0
movq %r12, %r9
movq %r15, -0x58(%rbp)
movq %r14, %r15
movq -0x38(%rbp), %rax
leal 0x1(%rax), %r14d
movq -0x50(%rbp), %rax
movq (%rax), %rax
movq %rax, (%rsp)
leaq -0x22b8(%rbp), %rdi
xorl %esi, %esi
movl %r14d, %edx
movl -0x40(%rbp), %ecx
movl %ebx, %r8d
callq 0x6607
movq -0x30(%rbp), %rdi
leaq -0x22b8(%rbp), %rsi
callq 0xea3c
addq $0xfa0, %r13            # imm = 0xFA0
movq -0x38(%rbp), %rax
cmpl -0x3c(%rbp), %eax
movl %r14d, %eax
movq %rax, -0x38(%rbp)
movq %r15, %r14
movq -0x58(%rbp), %r15
jne 0x6cfe
leaq -0x280(%rbp), %rdi
callq 0x30a0
leaq -0x280(%rbp), %rdi
callq 0x30d0
leaq -0x68(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6db1
callq 0x3320
addq $0x3248, %rsp           # imm = 0x3248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6dd6
jmp 0x6dd6
jmp 0x6dcd
jmp 0x6dd6
jmp 0x6dcd
movq %rax, %rbx
jmp 0x6de5
jmp 0x6dd6
jmp 0x6dd6
movq %rax, %rbx
leaq -0x280(%rbp), %rdi
callq 0x30d0
leaq -0x68(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6df7
callq 0x3320
movq %rbx, %rdi
callq 0x3580
nop