// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %r9, %r12
movq %r8, 0x18(%rsp)
movl %ecx, 0xc(%rsp)
movl %edx, 0x8(%rsp)
movq %rsi, 0x10(%rsp)
movl $0x64, %r13d
leaq 0x50(%rsp), %r15
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x30d0
testl %eax, %eax
js 0x61f0
movl %eax, %r14d
movq %r15, 0x40(%rsp)
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
callq 0xc556
movl %r14d, %esi
movq %rbx, %rdx
movl 0x8(%rsp), %ecx
movl 0xc(%rsp), %r8d
movq 0x18(%rsp), %r9
callq 0x5e00
movl %eax, %ebp
movq 0x40(%rsp), %rdi
movq %r15, %rbx
cmpq %r15, %rdi
je 0x6111
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x3390
testb %bpl, %bpl
je 0x6232
movl %r14d, %edi
movq %r12, %rsi
callq 0x5c25
movq %r12, %rdi
leaq 0x7f9c(%rip), %rsi      # 0xe0cb
callq 0x3110
testl %eax, %eax
jne 0x6236
movl %r14d, %edi
callq 0x36d0
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
movl $0x493e0, %eax          # imm = 0x493E0
addl %eax, %edi
callq 0x34c0
decl %r13d
movq %rbx, %r15
jne 0x609f
leaq 0x60(%rsp), %rbx
movl $0x100, %edx            # imm = 0x100
movq %rbx, %rdi
xorl %esi, %esi
callq 0x31f0
leaq 0x8062(%rip), %rdx      # 0xe1fd
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdi
movl $0x64, %ecx
xorl %eax, %eax
callq 0x35a0
leaq 0x30(%rsp), %r14
movq %r14, -0x10(%r14)
movq %rbx, %rdi
callq 0x3180
leaq (%rsp,%rax), %rdx
addq $0x60, %rdx
leaq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0xc5ec
leaq 0x20(%rsp), %rdi
callq 0x58ec
movq 0x20(%rsp), %rdi
cmpq %r14, %rdi
je 0x6232
movq 0x30(%rsp), %rsi
jmp 0x622a
leaq 0x70(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x7fb1(%rip), %rsi      # 0xe1b1
leaq 0x7fe5(%rip), %rdx      # 0xe1ec
leaq 0x60(%rsp), %rdi
callq 0xc5ec
leaq 0x60(%rsp), %rdi
callq 0x58ec
movq 0x60(%rsp), %rdi
cmpq %r14, %rdi
je 0x6232
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x3390
xorl %eax, %eax
jmp 0x626c
leaq 0x7fb0(%rip), %rsi      # 0xe1ed
movq %r12, %rdi
callq 0x3110
testl %eax, %eax
jne 0x6262
movq 0x8(%r12), %rdx
leaq 0x7e76(%rip), %rcx      # 0xe0cb
movq %r12, %rdi
xorl %esi, %esi
xorl %r8d, %r8d
callq 0x3560
movl %r14d, %edi
callq 0x36d0
movb $0x1, %al
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x20(%rsp), %rdi
cmpq %r14, %rdi
je 0x62c7
movq 0x30(%rsp), %rsi
jmp 0x62bf
jmp 0x62a8
movq %rax, %rbx
movq 0x60(%rsp), %rdi
cmpq %r14, %rdi
je 0x62c7
movq 0x70(%rsp), %rsi
jmp 0x62bf
movq %rax, %rbx
jmp 0x62c7
movq %rax, %rbx
movq 0x40(%rsp), %rdi
cmpq %r15, %rdi
je 0x62c7
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x3390
movq %rbx, %rdi
callq 0x3640
nop