// File: /workspace/github_cpp_makefile_star2_O2/williewillus[P]panda_scratchpad/src/communication/ClientSocket.cpp
// Function: communication::ClientSocket::SendCommand(communication::SockMessage const&)
SockError ClientSocket::SendCommand(const SockMessage& msg) {
	SockError err = eNone;
	
	// First check if the client is connected
	if ( sockfd < 0 ) {
		cout << "Client not connected" << endl;
		err = eNotConnected;
		return err;
	}
	
	// Get the command and the arguments
	QemuCommand cmd = msg.q_cmd;
	CommandOpts options = *(msg.q_cmd_options);

	// Based on the command, build the actual message now
	string complete_command;
	bool isCustomStart = false;
	switch(cmd) {
		case cListPlugins : 
			complete_command = "list_plugins";
			break;
		case cLoadPlugin : 
			complete_command = "load_plugin ";
			// Let's see how to handle the available plugins
			switch(options.plugin_name) {
				case pWritetracker:
					complete_command += "writetracker";
					if (! options.start.empty()) {
						complete_command += " start=";
						complete_command += options.start;
						isCustomStart = true;
					}

					if (! options.end.empty()) {
						if (isCustomStart)
							complete_command += ",";
						complete_command += "end=";
						complete_command += options.end;
					}
					break;
				case pReplay:
					complete_command += "replayer";
					if (! options.start.empty()) {
						complete_command += " base=";
						complete_command += options.start;
					}
					break;
				default:
					cout << "Undefined Plugin" << endl;
					return eOther;
			}
			break;
		case cUnloadPlugin : 
			complete_command = "unload_plugin ";
			complete_command += to_string(options.id);
			break;
		default :
			cout << "Undefined qemu command" << endl;
			return eOther;
	}

	// The command doesn't execute at the monitor without a newline
	complete_command += "\n";
	cout << "Command being sent is : " << complete_command << endl;
	if ( send( sockfd, complete_command.c_str(), complete_command.length(), 0) < 0) {
		err = eOther;
		return err;
	}
	return err;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0x90, %rsp
cmpl $0x0, (%rdi)
js 0x7822
movq %rdi, %rbx
movl (%rsi), %r14d
movq 0x8(%rsi), %rsi
leaq -0x80(%rbp), %rdi
callq 0x7aa2
leaq -0x20(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
cmpl $0x2, %r14d
je 0x78c0
cmpl $0x1, %r14d
je 0x7844
testl %r14d, %r14d
jne 0x78fd
leaq 0x9cd9(%rip), %rsi      # 0x114ed
leaq -0x30(%rbp), %rdi
callq 0x5820
jmp 0x797a
movq 0x12797(%rip), %rdi     # 0x19fc0
leaq 0x9ca8(%rip), %rsi      # 0x114d8
callq 0x5480
movq %rax, %rdi
callq 0x5240
xorl %ebx, %ebx
jmp 0x79de
leaq 0x9cee(%rip), %rsi      # 0x11539
leaq -0x30(%rbp), %rdi
callq 0x5820
movl -0x80(%rbp), %eax
cmpl $0x1, %eax
je 0x7906
testl %eax, %eax
jne 0x7933
leaq 0x9c8b(%rip), %rsi      # 0x114fa
leaq -0x30(%rbp), %rdi
callq 0x5810
cmpq $0x0, -0x70(%rbp)
je 0x7953
leaq 0x9c7d(%rip), %rsi      # 0x11507
leaq -0x30(%rbp), %rdi
callq 0x5810
leaq -0x78(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0x5850
cmpq $0x0, -0x50(%rbp)
je 0x797a
leaq 0x9c5d(%rip), %rsi      # 0x1150f
leaq -0x30(%rbp), %rdi
callq 0x5810
jmp 0x795a
leaq 0x9c70(%rip), %rsi      # 0x11537
leaq -0x30(%rbp), %rdi
callq 0x5820
movl -0x38(%rbp), %esi
leaq -0xa0(%rbp), %rdi
callq 0x7af8
leaq -0x30(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x5850
leaq -0xa0(%rbp), %rdi
callq 0x58a8
jmp 0x797a
leaq 0x9c42(%rip), %rsi      # 0x11546
jmp 0x793a
leaq 0x9c09(%rip), %rsi      # 0x11516
leaq -0x30(%rbp), %rdi
callq 0x5810
cmpq $0x0, -0x70(%rbp)
je 0x797a
leaq 0x9bfb(%rip), %rsi      # 0x1151f
leaq -0x30(%rbp), %rdi
callq 0x5810
leaq -0x78(%rbp), %r14
jmp 0x796e
leaq 0x9bec(%rip), %rsi      # 0x11526
movq 0x1267f(%rip), %rdi     # 0x19fc0
callq 0x5480
movq %rax, %rdi
callq 0x5240
pushq $0x2
popq %rbx
jmp 0x79cc
cmpq $0x0, -0x50(%rbp)
je 0x797a
leaq -0x58(%rbp), %r14
leaq 0x9bac(%rip), %rsi      # 0x11511
leaq -0x30(%rbp), %rdi
callq 0x5810
leaq -0x30(%rbp), %rdi
movq %r14, %rsi
callq 0x5850
leaq 0x978a(%rip), %rsi      # 0x1110b
leaq -0x30(%rbp), %rdi
callq 0x5810
movq 0x1262f(%rip), %rdi     # 0x19fc0
leaq 0x9bc5(%rip), %rsi      # 0x1155d
callq 0x5480
leaq -0x30(%rbp), %rsi
movq %rax, %rdi
callq 0x5410
movq %rax, %rdi
callq 0x5240
movl (%rbx), %edi
movq -0x30(%rbp), %rsi
movq -0x28(%rbp), %rdx
xorl %ecx, %ecx
callq 0x5260
movq %rax, %rbx
shrq $0x3f, %rbx
xorl $0x3, %ebx
leaq -0x30(%rbp), %rdi
callq 0x58a8
leaq -0x80(%rbp), %rdi
callq 0x75d0
movl %ebx, %eax
addq $0x90, %rsp
popq %rbx
popq %r14
popq %rbp
retq
movq %rax, %rbx
leaq -0xa0(%rbp), %rdi
callq 0x58a8
jmp 0x7a02
jmp 0x79ff
movq %rax, %rbx
leaq -0x30(%rbp), %rdi
callq 0x58a8
leaq -0x80(%rbp), %rdi
callq 0x75d0
movq %rbx, %rdi
callq 0x5780