// File: /workspace/github_cpp_makefile_star2_O1/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 %r12
pushq %rbx
subq $0x80, %rsp
movq %r9, %r14
movl %r8d, %ebp
movl %esi, %ebx
xorps %xmm0, %xmm0
leaq 0x74(%rsp), %rax
movaps %xmm0, -0x4(%rax)
movw $0x2, -0x4(%rax)
rolw $0x8, %cx
movw %cx, -0x2(%rax)
movq (%rdx), %rsi
movl $0x2, %edi
movq %rax, %rdx
callq 0x36c0
leaq 0x70(%rsp), %rsi
movl %ebx, %edi
movl $0x10, %edx
callq 0x32d0
testl %eax, %eax
jns 0x5efa
leaq 0x50(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x825f(%rip), %rsi      # 0xe0dd
leaq 0x8294(%rip), %rdx      # 0xe119
leaq 0x40(%rsp), %rdi
callq 0xc5c2
leaq 0x40(%rsp), %rdi
callq 0x57fa
movq 0x40(%rsp), %rdi
cmpq %r15, %rdi
je 0x5eb0
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x3390
movl $0x493e0, %r12d         # imm = 0x493E0
leaq 0x70(%rsp), %r15
callq 0x30f0
movslq %eax, %rdi
imulq $0x6fd91d85, %rdi, %rax # imm = 0x6FD91D85
movq %rax, %rcx
shrq $0x3f, %rcx
sarq $0x31, %rax
addl %ecx, %eax
imull $0x493e0, %eax, %eax    # imm = 0x493E0
subl %eax, %edi
addl %r12d, %edi
callq 0x34d0
movl %ebx, %edi
movq %r15, %rsi
movl $0x10, %edx
callq 0x32d0
testl %eax, %eax
js 0x5ebb
leaq 0x40(%rsp), %r15
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%r15)
movaps %xmm0, (%r15)
movb $0x0, 0x20(%r15)
leaq 0x8203(%rip), %rdx      # 0xe11a
movl $0x21, %esi
movq %r15, %rdi
movl %ebp, %ecx
xorl %eax, %eax
callq 0x35b0
leaq 0x30(%rsp), %r12
movq %r12, -0x10(%r12)
movq %r15, %rdi
callq 0x3180
leaq (%rsp,%rax), %rdx
addq $0x40, %rdx
leaq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0xc5c2
movq (%r14), %rsi
movq 0x8(%r14), %rdx
leaq 0x20(%rsp), %rdi
callq 0x3120
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x31c0
testl %eax, %eax
js 0x5fb2
movl %ebx, %edi
movl $0x1, %esi
callq 0x34b0
movb $0x1, %bl
testl %eax, %eax
jns 0x5ff1
leaq 0x10(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x81b4(%rip), %rsi      # 0xe14d
leaq 0x81e0(%rip), %rdx      # 0xe180
movq %rsp, %rdi
callq 0xc5c2
movq %rsp, %rdi
callq 0x58fc
jmp 0x5fd9
leaq 0x10(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x815c(%rip), %rsi      # 0xe11e
leaq 0x8183(%rip), %rdx      # 0xe14c
movq %rsp, %rdi
callq 0xc5c2
movq %rsp, %rdi
callq 0x58fc
movq (%rsp), %rdi
cmpq %r14, %rdi
je 0x5fef
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3390
xorl %ebx, %ebx
movq 0x20(%rsp), %rdi
cmpq %r12, %rdi
je 0x6008
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x3390
movl %ebx, %eax
addq $0x80, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6034
jmp 0x6053
movq %rax, %rbx
movq 0x40(%rsp), %rdi
cmpq %r15, %rdi
je 0x6072
movq 0x50(%rsp), %rsi
jmp 0x6065
jmp 0x606f
movq %rax, %rbx
movq (%rsp), %rdi
cmpq %r14, %rdi
je 0x6056
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3390
jmp 0x6056
jmp 0x6053
jmp 0x6053
movq %rax, %rbx
movq 0x20(%rsp), %rdi
cmpq %r12, %rdi
je 0x6072
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x3390
jmp 0x6072
movq %rax, %rbx
movq %rbx, %rdi
callq 0x3650