// File: /workspace/github_cpp_makefile_star2_O3/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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
cmpl $0x0, (%rdi)
js 0x83a0
movq %rdi, %rbx
movl (%rsi), %r12d
movq 0x8(%rsi), %r13
movl (%r13), %eax
leaq -0x90(%rbp), %r15
movl %eax, -0x8(%r15)
leaq -0x80(%rbp), %rax
movq %rax, -0x10(%rax)
movq 0x8(%r13), %rsi
movq 0x10(%r13), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0x7fc8
leaq -0x70(%rbp), %r14
leaq -0x60(%rbp), %rax
movq %rax, -0x10(%rax)
movq 0x28(%r13), %rsi
movq 0x30(%r13), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x7fc8
movl 0x48(%r13), %eax
movl %eax, -0x50(%rbp)
leaq -0x38(%rbp), %r13
movq %r13, -0x10(%r13)
movq $0x0, -0x8(%r13)
movb $0x0, (%r13)
cmpl $0x2, %r12d
je 0x847f
cmpl $0x1, %r12d
je 0x83e9
testl %r12d, %r12d
jne 0x84f9
leaq 0xc18f(%rip), %rcx      # 0x14517
leaq -0x48(%rbp), %rdi
movl $0xc, %r8d
xorl %esi, %esi
xorl %edx, %edx
callq 0x56b0
jmp 0x8620
movq 0x13c21(%rip), %rbx     # 0x1bfc8
leaq 0xc154(%rip), %rsi      # 0x14502
movl $0x14, %edx
movq %rbx, %rdi
callq 0x54f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x5470
movsbl %al, %esi
movq %rbx, %rdi
callq 0x5040
movq %rax, %rdi
callq 0x5300
xorl %ebx, %ebx
jmp 0x86d6
leaq 0xc173(%rip), %rcx      # 0x14563
leaq -0x48(%rbp), %rdi
movl $0xc, %r8d
xorl %esi, %esi
xorl %edx, %edx
callq 0x56b0
movl -0x98(%rbp), %eax
cmpl $0x1, %eax
je 0x8507
testl %eax, %eax
jne 0x8544
leaq 0xc103(%rip), %rsi      # 0x14524
leaq -0x48(%rbp), %rdi
callq 0x5860
cmpq $0x0, -0x88(%rbp)
je 0x85f5
leaq 0xc0f2(%rip), %rsi      # 0x14531
leaq -0x48(%rbp), %rdi
callq 0x5860
movq -0x90(%rbp), %rsi
movq -0x88(%rbp), %rdx
leaq -0x48(%rbp), %rdi
callq 0x5160
cmpq $0x0, -0x68(%rbp)
je 0x8620
leaq 0xc0c8(%rip), %rsi      # 0x14539
leaq -0x48(%rbp), %rdi
callq 0x5860
jmp 0x85fc
leaq 0xc0db(%rip), %rcx      # 0x14561
leaq -0x48(%rbp), %rdi
movl $0xe, %r8d
xorl %esi, %esi
xorl %edx, %edx
callq 0x56b0
movl -0x50(%rbp), %r14d
movl $0x1, %eax
cmpl $0xa, %r14d
jb 0x859b
movl $0x4, %eax
movl $0xd1b71759, %ecx       # imm = 0xD1B71759
movl %r14d, %edx
cmpl $0x63, %edx
jbe 0x8594
cmpl $0x3e7, %edx            # imm = 0x3E7
jbe 0x8599
cmpl $0x2710, %edx           # imm = 0x2710
jb 0x859b
movl %edx, %esi
imulq %rcx, %rsi
shrq $0x2d, %rsi
addl $0x4, %eax
cmpl $0x1869f, %edx          # imm = 0x1869F
movl %esi, %edx
ja 0x84b9
addl $-0x3, %eax
jmp 0x859b
movl $0x16, %edx
leaq 0xc06b(%rip), %rsi      # 0x14570
jmp 0x8550
leaq 0xc032(%rip), %rsi      # 0x14540
leaq -0x48(%rbp), %rdi
callq 0x5860
cmpq $0x0, -0x88(%rbp)
je 0x8620
leaq 0xc01d(%rip), %rsi      # 0x14549
leaq -0x48(%rbp), %rdi
callq 0x5860
leaq -0x88(%rbp), %r12
movq %r15, %r14
jmp 0x8610
movl $0x10, %edx
leaq 0xc000(%rip), %rsi      # 0x14550
movq 0x13a71(%rip), %rbx     # 0x1bfc8
movq %rbx, %rdi
callq 0x54f0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x5470
movsbl %al, %esi
movq 0x13a4b(%rip), %rdi     # 0x1bfc8
callq 0x5040
movl $0x2, %ebx
movq %rax, %rdi
callq 0x5300
jmp 0x86a1
addl $-0x2, %eax
jmp 0x859b
decl %eax
movl %eax, %esi
leaq -0xa8(%rbp), %r15
movq %r15, -0x10(%r15)
leaq -0xb8(%rbp), %rdi
xorl %edx, %edx
callq 0x5510
movq -0xb8(%rbp), %rdi
movl -0xb0(%rbp), %esi
movl %r14d, %edx
callq 0x8820
movq -0xb8(%rbp), %rsi
movq -0xb0(%rbp), %rdx
leaq -0x48(%rbp), %rdi
callq 0x5160
movq -0xb8(%rbp), %rdi
cmpq %r15, %rdi
je 0x8620
callq 0x5460
jmp 0x8620
cmpq $0x0, -0x68(%rbp)
je 0x8620
leaq -0x68(%rbp), %r12
leaq 0xbf34(%rip), %rsi      # 0x1453b
leaq -0x48(%rbp), %rdi
callq 0x5860
movq (%r14), %rsi
movq (%r12), %rdx
leaq -0x48(%rbp), %rdi
callq 0x5160
leaq 0xbae4(%rip), %rsi      # 0x1410b
leaq -0x48(%rbp), %rdi
callq 0x5860
movq 0x13991(%rip), %rdi     # 0x1bfc8
leaq 0xbf49(%rip), %rsi      # 0x14587
movl $0x18, %edx
callq 0x54f0
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq 0x13971(%rip), %rdi     # 0x1bfc8
callq 0x54f0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x5470
movsbl %al, %esi
movq %r14, %rdi
callq 0x5040
movq %rax, %rdi
callq 0x5300
movl (%rbx), %edi
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
xorl %ecx, %ecx
callq 0x5240
movq %rax, %rbx
shrq $0x3f, %rbx
xorl $0x3, %ebx
movq -0x48(%rbp), %rdi
cmpq %r13, %rdi
je 0x86af
callq 0x5460
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x86c1
callq 0x5460
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0x86d6
callq 0x5460
movl %ebx, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq -0xb8(%rbp), %rdi
cmpq %r15, %rdi
je 0x871f
callq 0x5460
jmp 0x871f
jmp 0x871c
movq %rax, %rbx
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0x8739
callq 0x5460
jmp 0x8739
movq %rax, %rbx
movq -0x48(%rbp), %rdi
cmpq %r13, %rdi
je 0x872d
callq 0x5460
leaq -0x98(%rbp), %rdi
callq 0x87ea
movq %rbx, %rdi
callq 0x57c0
nop