// File: sjtuwrk[P]SunwayMR/include/Messaging.hpp
// Function: Messaging::sendMessageForReply(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>>&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&)
bool Messaging::sendMessageForReply(string addr, int targetPort, int msgType, string &msg, string &reply)
{
	int empty_reply_time = 0;
	int empty_reply_time_max = 100;
	bool ret = false;
	while(empty_reply_time < empty_reply_time_max) {
		int sockfd = socket(AF_INET, SOCK_STREAM, 0);

		if (sockfd < 0)
		{
			Logging::logError("Messaging::sendMessageForReply: failed to initialize socket");
			return false;
		}

		if(!sendMessageInternal(sockfd, addr, targetPort, msgType, msg)) {
			return false;
		}

		// read replied data
		readSocket(sockfd, reply);

		if(reply == "") { // failed
			empty_reply_time++;
			// release socket
			close(sockfd);
			usleep(300000 + rand()%300000); // sleep & reconnect later
			continue;
		}
		if(reply == EMPTY_MESSAGE) {
			reply = "";
		}
		ret = true;
		// release socket
		close(sockfd);
		break;
	}
	if(!ret) {
		char buffer[256];
		memset(buffer, 0, sizeof(buffer));
		snprintf(buffer,
				sizeof(buffer),
				"Messaging::sendMessageForReply: failed to send after recv %d empty reply",
				empty_reply_time_max);
		string log = buffer;
		Logging::logError(log);
	}

	return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x220, %rsp            # imm = 0x220
movq %rsi, -0x1d8(%rbp)
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x1d0(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x64, -0x38(%rbp)
movb $0x0, -0x39(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x38(%rbp), %eax
jge 0x802e
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x40f0
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
jge 0x7f0f
leaq -0x61(%rbp), %rdi
movq %rdi, -0x1e0(%rbp)
callq 0x47a0
movq -0x1e0(%rbp), %rdx
leaq 0x122b8(%rip), %rsi     # 0x1a165
leaq -0x60(%rbp), %rdi
callq 0xe8d0
jmp 0x7eb8
leaq -0x60(%rbp), %rdi
callq 0x7300
jmp 0x7ec3
leaq -0x60(%rbp), %rdi
callq 0x4878
leaq -0x61(%rbp), %rdi
callq 0x4470
movb $0x0, -0x1(%rbp)
jmp 0x8169
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x7f01
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x4878
leaq -0x61(%rbp), %rdi
callq 0x4470
jmp 0x8177
movq -0x1d8(%rbp), %rsi
movl -0x40(%rbp), %eax
movl %eax, -0x1f4(%rbp)
leaq -0x98(%rbp), %rdi
movq %rdi, -0x1f0(%rbp)
callq 0x41a0
movq -0x1d0(%rbp), %rdi
movl -0x1f4(%rbp), %esi
movq -0x1f0(%rbp), %rdx
movl -0x1c(%rbp), %ecx
movl -0x20(%rbp), %r8d
movq -0x28(%rbp), %r9
callq 0x7a20
movb %al, -0x1e1(%rbp)
jmp 0x7f5e
movb -0x1e1(%rbp), %al
xorb $-0x1, %al
movb %al, -0x1f5(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x4878
movb -0x1f5(%rbp), %al
testb $0x1, %al
jne 0x7f84
jmp 0x7faa
movb $0x0, -0x1(%rbp)
jmp 0x8169
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x4878
jmp 0x8177
movl -0x40(%rbp), %edi
movq -0x30(%rbp), %rsi
callq 0x7750
movq -0x30(%rbp), %rdi
leaq 0x120be(%rip), %rsi     # 0x1a07f
callq 0xef60
testb $0x1, %al
jne 0x7fcc
jmp 0x7ffc
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movl -0x40(%rbp), %edi
callq 0x4840
callq 0x4120
movl $0x493e0, %ecx          # imm = 0x493E0
cltd
idivl %ecx
movl %edx, %edi
addl $0x493e0, %edi          # imm = 0x493E0
callq 0x45e0
jmp 0x7e65
movq -0x30(%rbp), %rdi
leaq 0x1219a(%rip), %rsi     # 0x1a1a1
callq 0xef60
testb $0x1, %al
jne 0x8012
jmp 0x8022
movq -0x30(%rbp), %rdi
leaq 0x12062(%rip), %rsi     # 0x1a07f
callq 0x45a0
movb $0x1, -0x39(%rbp)
movl -0x40(%rbp), %edi
callq 0x4840
testb $0x1, -0x39(%rbp)
jne 0x8161
xorps %xmm0, %xmm0
movaps %xmm0, -0x220(%rbp)
movaps %xmm0, -0xb0(%rbp)
movaps %xmm0, -0xc0(%rbp)
movaps %xmm0, -0xd0(%rbp)
movaps %xmm0, -0xe0(%rbp)
movaps %xmm0, -0xf0(%rbp)
movaps %xmm0, -0x100(%rbp)
movaps %xmm0, -0x110(%rbp)
movaps %xmm0, -0x120(%rbp)
movaps %xmm0, -0x130(%rbp)
movaps %xmm0, -0x140(%rbp)
movaps %xmm0, -0x150(%rbp)
movaps %xmm0, -0x160(%rbp)
movaps %xmm0, -0x170(%rbp)
movaps %xmm0, -0x180(%rbp)
movaps %xmm0, -0x190(%rbp)
movaps %xmm0, -0x1a0(%rbp)
movl -0x38(%rbp), %ecx
leaq 0x120f5(%rip), %rdx     # 0x1a1b1
xorl %eax, %eax
leaq -0x1a0(%rbp), %rdi
movq %rdi, -0x208(%rbp)
movl $0x100, %esi            # imm = 0x100
callq 0x46d0
leaq -0x1c1(%rbp), %rdi
movq %rdi, -0x200(%rbp)
callq 0x47a0
movq -0x208(%rbp), %rsi
movq -0x200(%rbp), %rdx
leaq -0x1c0(%rbp), %rdi
callq 0xe8d0
jmp 0x8105
leaq -0x1c1(%rbp), %rdi
callq 0x4470
leaq -0x1c0(%rbp), %rdi
callq 0x7300
jmp 0x811f
leaq -0x1c0(%rbp), %rdi
callq 0x4878
jmp 0x8161
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x1c1(%rbp), %rdi
callq 0x4470
jmp 0x8177
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x1c0(%rbp), %rdi
callq 0x4878
jmp 0x8177
movb -0x39(%rbp), %al
andb $0x1, %al
movb %al, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x220, %rsp            # imm = 0x220
popq %rbp
retq
movq -0x70(%rbp), %rdi
callq 0x4780