// File: /workspace/github_cpp_makefile_star2_O3/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
leaq 0x74(%rsp), %rax
movl $0x0, 0x8(%rax)
movq $0x0, (%rax)
movw $0x2, -0x4(%rax)
rolw $0x8, %cx
movw %cx, -0x2(%rax)
movq (%rdx), %rsi
movl $0x2, %edi
movq %rax, %rdx
callq 0x36b0
leaq 0x70(%rsp), %rsi
movl %ebx, %edi
movl $0x10, %edx
callq 0x32d0
testl %eax, %eax
jns 0x5eed
leaq 0x50(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x829c(%rip), %rsi      # 0xe10d
leaq 0x82d1(%rip), %rdx      # 0xe149
leaq 0x40(%rsp), %rdi
callq 0xc5ec
leaq 0x40(%rsp), %rdi
callq 0x57ea
movq 0x40(%rsp), %rdi
cmpq %r15, %rdi
je 0x5ea3
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 0x34c0
movl %ebx, %edi
movq %r15, %rsi
movl $0x10, %edx
callq 0x32d0
testl %eax, %eax
js 0x5eae
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %r15
movaps %xmm0, 0x10(%r15)
movaps %xmm0, (%r15)
movb $0x0, 0x20(%r15)
leaq 0x8240(%rip), %rdx      # 0xe14a
movl $0x21, %esi
movq %r15, %rdi
movl %ebp, %ecx
xorl %eax, %eax
callq 0x35a0
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 0xc5ec
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 0x5fa5
movl %ebx, %edi
movl $0x1, %esi
callq 0x34b0
movb $0x1, %bl
testl %eax, %eax
jns 0x5fe4
leaq 0x10(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x81f1(%rip), %rsi      # 0xe17d
leaq 0x821d(%rip), %rdx      # 0xe1b0
movq %rsp, %rdi
callq 0xc5ec
movq %rsp, %rdi
callq 0x58ec
jmp 0x5fcc
leaq 0x10(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x8199(%rip), %rsi      # 0xe14e
leaq 0x81c0(%rip), %rdx      # 0xe17c
movq %rsp, %rdi
callq 0xc5ec
movq %rsp, %rdi
callq 0x58ec
movq (%rsp), %rdi
cmpq %r14, %rdi
je 0x5fe2
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3390
xorl %ebx, %ebx
movq 0x20(%rsp), %rdi
cmpq %r12, %rdi
je 0x5ffb
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 0x6027
jmp 0x6046
movq %rax, %rbx
movq 0x40(%rsp), %rdi
cmpq %r15, %rdi
je 0x6065
movq 0x50(%rsp), %rsi
jmp 0x6058
jmp 0x6062
movq %rax, %rbx
movq (%rsp), %rdi
cmpq %r14, %rdi
je 0x6049
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3390
jmp 0x6049
jmp 0x6046
jmp 0x6046
movq %rax, %rbx
movq 0x20(%rsp), %rdi
cmpq %r12, %rdi
je 0x6065
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x3390
jmp 0x6065
movq %rax, %rbx
movq %rbx, %rdi
callq 0x3640
nop