// File: /workspace/github_cpp_makefile_star2_O2/sjtuwrk[P]SunwayMR/include/Messaging.hpp
// Function: Messaging::sendMessageInternal(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&)
bool Messaging::sendMessageInternal(int socket_fd, string addr, int targetPort, int msgType, string &msg) {
	struct sockaddr_in address;
	bzero(&address, sizeof(address));
	address.sin_family = AF_INET;
	address.sin_port = htons(targetPort);  // set port no.

	inet_pton(AF_INET, addr.c_str(), &address.sin_addr);  // translate IP Address
	int len = sizeof(address);

	// connect
	int conn = connect(socket_fd, (struct sockaddr *)&address, len);
	if (conn < 0)
	{
		Logging::logWarning("Messaging: sendMessageInternal: connect fail! will try again");
		while (conn < 0) {
			usleep(300000 + rand()%300000); // sleep & reconnect later
			conn = connect(socket_fd, (struct sockaddr *)&address, len);
		}
	}

	// reorganize packet
	char buffer [33];
	memset(buffer, 0, sizeof(buffer));
	snprintf(buffer, sizeof(buffer), "%d$", msgType);
	string content = buffer;
	content += msg;

	// send data
	const char* ch1 = content.c_str();
	int ret = send(socket_fd, ch1, content.length(), 0);
	if (ret < 0)
	{
		Logging::logError("Messaging: sendMessageInternal: failed to send");
		return false;
	}
	ret = shutdown(socket_fd, SHUT_WR);
	if (ret < 0)
	{
		Logging::logError("Messaging: sendMessageInternal: failed to shutdown!");
		return false;
	}
	return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r9, 0x28(%rsp)
movl %r8d, %ebp
movl %esi, %ebx
leaq 0x54(%rsp), %rax
andl $0x0, 0x8(%rax)
andq $0x0, (%rax)
movw $0x2, -0x4(%rax)
rolw $0x8, %cx
movw %cx, -0x2(%rax)
movq (%rdx), %rsi
pushq $0x2
popq %rdi
movq %rax, %rdx
callq 0x3680
leaq 0x50(%rsp), %rsi
pushq $0x10
popq %rdx
movl %ebx, %edi
callq 0x32c0
testl %eax, %eax
js 0x5463
xorps %xmm0, %xmm0
leaq 0x60(%rsp), %r15
movaps %xmm0, 0x10(%r15)
movaps %xmm0, (%r15)
movb $0x0, 0x20(%r15)
leaq 0x6d7a(%rip), %rdx      # 0xc11a
pushq $0x21
popq %rsi
movq %r15, %rdi
movl %ebp, %ecx
xorl %eax, %eax
callq 0x3580
leaq 0x30(%rsp), %rdi
leaq 0x8(%rsp), %rdx
movq %r15, %rsi
callq 0x915a
leaq 0x30(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x36b0
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x31c0
testl %eax, %eax
js 0x5419
pushq $0x1
popq %rsi
movl %ebx, %edi
callq 0x3480
movb $0x1, %bl
testl %eax, %eax
jns 0x5445
leaq 0x6d4f(%rip), %rsi      # 0xc14d
leaq 0x8(%rsp), %rdi
leaq 0x7(%rsp), %rdx
callq 0x915a
leaq 0x8(%rsp), %rdi
callq 0x4efc
jmp 0x5439
leaq 0x6cfe(%rip), %rsi      # 0xc11e
leaq 0x8(%rsp), %rdi
leaq 0x7(%rsp), %rdx
callq 0x915a
leaq 0x8(%rsp), %rdi
callq 0x4efc
leaq 0x8(%rsp), %rdi
callq 0x36e8
xorl %ebx, %ebx
leaq 0x30(%rsp), %rdi
callq 0x36e8
movl %ebx, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %r15d
leaq 0x6c70(%rip), %rsi      # 0xc0dd
leaq 0x60(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x915a
leaq 0x60(%rsp), %rdi
callq 0x4e5c
leaq 0x60(%rsp), %rdi
callq 0x36e8
movl $0x493e0, %r14d         # imm = 0x493E0
leaq 0x50(%rsp), %r12
pushq $0x10
popq %r13
testl %r15d, %r15d
jns 0x5383
callq 0x3100
cltd
idivl %r14d
leal (%rdx,%r14), %edi
callq 0x34a0
movl %ebx, %edi
movq %r12, %rsi
movl %r13d, %edx
callq 0x32c0
movl %eax, %r15d
jmp 0x549f
jmp 0x54dc
jmp 0x54ef
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
jmp 0x54f7
jmp 0x54fe
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x36e8
jmp 0x54f2
jmp 0x54ef
jmp 0x54ef
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x36e8
jmp 0x5501
movq %rax, %rbx
movq %rbx, %rdi
callq 0x3610
nop