// File: 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
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movq %rdx, -0x130(%rbp)
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl %r8d, -0x28(%rbp)
movq %r9, -0x30(%rbp)
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x4210
movw $0x2, -0x40(%rbp)
movl -0x24(%rbp), %eax
movzwl %ax, %edi
callq 0x4080
movq -0x130(%rbp), %rdi
movw %ax, -0x3e(%rbp)
callq 0x4190
movq %rax, %rsi
leaq -0x40(%rbp), %rdx
addq $0x4, %rdx
movl $0x2, %edi
callq 0x4810
movl $0x10, -0x44(%rbp)
movl -0x14(%rbp), %edi
movl -0x44(%rbp), %edx
leaq -0x40(%rbp), %rsi
callq 0x4330
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
jge 0x7b5e
leaq -0x69(%rbp), %rdi
movq %rdi, -0x138(%rbp)
callq 0x47a0
movq -0x138(%rbp), %rdx
leaq 0x125f2(%rip), %rsi     # 0x1a0c1
leaq -0x68(%rbp), %rdi
callq 0xe8d0
jmp 0x7ada
leaq -0x68(%rbp), %rdi
callq 0x7220
jmp 0x7ae5
leaq -0x68(%rbp), %rdi
callq 0x4878
leaq -0x69(%rbp), %rdi
callq 0x4470
cmpl $0x0, -0x48(%rbp)
jge 0x7b5c
callq 0x4120
movl $0x493e0, %ecx          # imm = 0x493E0
cltd
idivl %ecx
movl %edx, %edi
addl $0x493e0, %edi          # imm = 0x493E0
callq 0x45e0
movl -0x14(%rbp), %edi
movl -0x44(%rbp), %edx
leaq -0x40(%rbp), %rsi
callq 0x4330
movl %eax, -0x48(%rbp)
jmp 0x7af7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x7b4e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x4878
leaq -0x69(%rbp), %rdi
callq 0x4470
jmp 0x7e15
jmp 0x7b5e
xorps %xmm0, %xmm0
movaps %xmm0, -0x90(%rbp)
movaps %xmm0, -0xa0(%rbp)
movb $0x0, -0x80(%rbp)
movl -0x28(%rbp), %ecx
leaq 0x12581(%rip), %rdx     # 0x1a0fe
xorl %eax, %eax
leaq -0xa0(%rbp), %rdi
movq %rdi, -0x148(%rbp)
movl $0x21, %esi
callq 0x46d0
leaq -0xc1(%rbp), %rdi
movq %rdi, -0x140(%rbp)
callq 0x47a0
movq -0x148(%rbp), %rsi
movq -0x140(%rbp), %rdx
leaq -0xc0(%rbp), %rdi
callq 0xe8d0
jmp 0x7bc6
leaq -0xc1(%rbp), %rdi
callq 0x4470
movq -0x30(%rbp), %rsi
leaq -0xc0(%rbp), %rdi
callq 0x4540
jmp 0x7be4
leaq -0xc0(%rbp), %rdi
movq %rdi, -0x168(%rbp)
callq 0x4190
movq -0x168(%rbp), %rdi
movq %rax, -0xd0(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x15c(%rbp)
movq -0xd0(%rbp), %rax
movq %rax, -0x158(%rbp)
callq 0x4650
movl -0x15c(%rbp), %edi
movq -0x158(%rbp), %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x41f0
movq %rax, -0x150(%rbp)
jmp 0x7c41
movq -0x150(%rbp), %rax
movl %eax, -0xd4(%rbp)
cmpl $0x0, -0xd4(%rbp)
jge 0x7d28
leaq -0xf9(%rbp), %rdi
movq %rdi, -0x170(%rbp)
callq 0x47a0
movq -0x170(%rbp), %rdx
leaq 0x12486(%rip), %rsi     # 0x1a102
leaq -0xf8(%rbp), %rdi
callq 0xe8d0
jmp 0x7c8a
leaq -0xf8(%rbp), %rdi
callq 0x7300
jmp 0x7c98
leaq -0xf8(%rbp), %rdi
callq 0x4878
leaq -0xf9(%rbp), %rdi
callq 0x4470
movb $0x0, -0x1(%rbp)
movl $0x1, -0x100(%rbp)
jmp 0x7def
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0xc1(%rbp), %rdi
callq 0x4470
jmp 0x7e15
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x7e09
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x7d17
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x4878
leaq -0xf9(%rbp), %rdi
callq 0x4470
jmp 0x7e09
movl -0x14(%rbp), %edi
movl $0x1, %esi
callq 0x45b0
movl %eax, -0xd4(%rbp)
cmpl $0x0, -0xd4(%rbp)
jge 0x7de1
leaq -0x121(%rbp), %rdi
movq %rdi, -0x178(%rbp)
callq 0x47a0
movq -0x178(%rbp), %rdx
leaq 0x123c8(%rip), %rsi     # 0x1a131
leaq -0x120(%rbp), %rdi
callq 0xe8d0
jmp 0x7d77
leaq -0x120(%rbp), %rdi
callq 0x7300
jmp 0x7d85
leaq -0x120(%rbp), %rdi
callq 0x4878
leaq -0x121(%rbp), %rdi
callq 0x4470
movb $0x0, -0x1(%rbp)
movl $0x1, -0x100(%rbp)
jmp 0x7def
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x7dd3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x4878
leaq -0x121(%rbp), %rdi
callq 0x4470
jmp 0x7e09
movb $0x1, -0x1(%rbp)
movl $0x1, -0x100(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x4878
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
leaq -0xc0(%rbp), %rdi
callq 0x4878
movq -0x78(%rbp), %rdi
callq 0x4780
nop