// File: 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
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xd0(%rbp)
movl $0x3, -0x1c(%rbp)
cmpl $0x0, (%rax)
jge 0xb0de
movq 0x29f0f(%rip), %rdi     # 0x34fc0
leaq 0x194d0(%rip), %rsi     # 0x24588
callq 0x6520
movq %rax, %rdi
movq 0x29ed1(%rip), %rsi     # 0x34f98
callq 0x6580
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xb4ca
movq -0x18(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x20(%rbp)
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
callq 0xb660
movq %rax, %rsi
leaq -0x70(%rbp), %rdi
callq 0xb690
leaq -0x90(%rbp), %rdi
callq 0x6680
movb $0x0, -0x91(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0xd4(%rbp)
testl %eax, %eax
je 0xb143
jmp 0xb122
movl -0xd4(%rbp), %eax
subl $0x1, %eax
je 0xb174
jmp 0xb12f
movl -0xd4(%rbp), %eax
subl $0x2, %eax
je 0xb2fc
jmp 0xb368
leaq 0x19453(%rip), %rsi     # 0x2459d
leaq -0x90(%rbp), %rdi
callq 0x6660
jmp 0xb158
jmp 0xb3af
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0xb4b3
leaq 0x1946e(%rip), %rsi     # 0x245e9
leaq -0x90(%rbp), %rdi
callq 0x6660
jmp 0xb189
movl -0x70(%rbp), %eax
movl %eax, -0xd8(%rbp)
testl %eax, %eax
je 0xb1ac
jmp 0xb198
movl -0xd8(%rbp), %eax
subl $0x1, %eax
je 0xb25f
jmp 0xb2b0
leaq 0x193f7(%rip), %rsi     # 0x245aa
leaq -0x90(%rbp), %rdi
callq 0x6510
jmp 0xb1c1
leaq -0x70(%rbp), %rdi
addq $0x8, %rdi
callq 0x6830
testb $0x1, %al
jne 0xb200
leaq 0x193de(%rip), %rsi     # 0x245b7
leaq -0x90(%rbp), %rdi
callq 0x6510
jmp 0xb1e7
leaq -0x68(%rbp), %rsi
leaq -0x90(%rbp), %rdi
callq 0x65e0
jmp 0xb1f9
movb $0x1, -0x91(%rbp)
leaq -0x70(%rbp), %rdi
addq $0x28, %rdi
callq 0x6830
testb $0x1, %al
jne 0xb25a
testb $0x1, -0x91(%rbp)
je 0xb231
leaq 0x1939e(%rip), %rsi     # 0x245bf
leaq -0x90(%rbp), %rdi
callq 0x6510
jmp 0xb22f
jmp 0xb231
leaq 0x19389(%rip), %rsi     # 0x245c1
leaq -0x90(%rbp), %rdi
callq 0x6510
jmp 0xb246
leaq -0x48(%rbp), %rsi
leaq -0x90(%rbp), %rdi
callq 0x65e0
jmp 0xb258
jmp 0xb25a
jmp 0xb2f7
leaq 0x19360(%rip), %rsi     # 0x245c6
leaq -0x90(%rbp), %rdi
callq 0x6510
jmp 0xb274
leaq -0x70(%rbp), %rdi
addq $0x8, %rdi
callq 0x6830
testb $0x1, %al
jne 0xb2ae
leaq 0x19343(%rip), %rsi     # 0x245cf
leaq -0x90(%rbp), %rdi
callq 0x6510
jmp 0xb29a
leaq -0x68(%rbp), %rsi
leaq -0x90(%rbp), %rdi
callq 0x65e0
jmp 0xb2ac
jmp 0xb2ae
jmp 0xb2f7
movq 0x29d09(%rip), %rdi     # 0x34fc0
leaq 0x19318(%rip), %rsi     # 0x245d6
callq 0x6520
movq %rax, -0xe0(%rbp)
jmp 0xb2cc
movq -0xe0(%rbp), %rdi
movq 0x29cbe(%rip), %rsi     # 0x34f98
callq 0x6580
jmp 0xb2e1
movl $0x2, -0x4(%rbp)
movl $0x1, -0xa8(%rbp)
jmp 0xb49c
jmp 0xb3af
leaq 0x192e4(%rip), %rsi     # 0x245e7
leaq -0x90(%rbp), %rdi
callq 0x6660
jmp 0xb311
movl -0x28(%rbp), %esi
leaq -0xc8(%rbp), %rdi
callq 0xb710
jmp 0xb322
leaq -0x90(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x65e0
jmp 0xb337
leaq -0xc8(%rbp), %rdi
callq 0x69d8
jmp 0xb3af
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x69d8
jmp 0xb4b3
movq 0x29c51(%rip), %rdi     # 0x34fc0
leaq 0x19280(%rip), %rsi     # 0x245f6
callq 0x6520
movq %rax, -0xe8(%rbp)
jmp 0xb384
movq -0xe8(%rbp), %rdi
movq 0x29c06(%rip), %rsi     # 0x34f98
callq 0x6580
jmp 0xb399
movl $0x2, -0x4(%rbp)
movl $0x1, -0xa8(%rbp)
jmp 0xb49c
leaq 0x18e05(%rip), %rsi     # 0x241bb
leaq -0x90(%rbp), %rdi
callq 0x6510
jmp 0xb3c4
movq 0x29bf5(%rip), %rdi     # 0x34fc0
leaq 0x1923b(%rip), %rsi     # 0x2460d
callq 0x6520
movq %rax, -0xf0(%rbp)
jmp 0xb3e0
movq -0xf0(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x64a0
movq %rax, -0xf8(%rbp)
jmp 0xb3fc
movq -0xf8(%rbp), %rdi
movq 0x29b8e(%rip), %rsi     # 0x34f98
callq 0x6580
jmp 0xb411
movq -0xd0(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x10c(%rbp)
leaq -0x90(%rbp), %rdi
movq %rdi, -0x118(%rbp)
callq 0x6220
movq -0x118(%rbp), %rdi
movq %rax, -0x108(%rbp)
callq 0x6740
movl -0x10c(%rbp), %edi
movq -0x108(%rbp), %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x62d0
movq %rax, -0x100(%rbp)
jmp 0xb466
movq -0x100(%rbp), %rax
cmpq $0x0, %rax
jge 0xb48c
movl $0x2, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0xa8(%rbp)
jmp 0xb49c
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0xa8(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x69d8
leaq -0x70(%rbp), %rdi
callq 0xa690
jmp 0xb4ca
leaq -0x90(%rbp), %rdi
callq 0x69d8
leaq -0x70(%rbp), %rdi
callq 0xa690
jmp 0xb4d6
movl -0x4(%rbp), %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
movq -0xa0(%rbp), %rdi
callq 0x68a0
nopw %cs:(%rax,%rax)