// File: /workspace/github_cpp_makefile_star2_O1/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)
leaq 0x50(%rsp), %rbp
xorl %r13d, %r13d
leaq 0x60(%rsp), %r15
movl $0x0, 0x4(%rsp)
cmpl $0x63, %r13d
jg 0x624f
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x30d0
testl %eax, %eax
js 0x61ac
movl %eax, %r14d
movq %rbp, 0x40(%rsp)
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
callq 0xc532
movl %r14d, %esi
movq %rbx, %rdx
movl 0x8(%rsp), %ecx
movl 0xc(%rsp), %r8d
movq 0x18(%rsp), %r9
callq 0x5e14
movl %eax, %ebx
movq 0x40(%rsp), %rdi
movq %rbp, %r15
cmpq %rbp, %rdi
je 0x6131
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x3390
movl $0x1, %eax
testb %bl, %bl
je 0x61f7
movl %r14d, %edi
movq %r12, %rsi
callq 0x5c35
movq %r12, %rdi
leaq 0x7f48(%rip), %rsi      # 0xe09b
callq 0x3110
testl %eax, %eax
movq %r15, %rbp
je 0x61fc
movq %r12, %rdi
leaq 0x8050(%rip), %rsi      # 0xe1bd
callq 0x3110
testl %eax, %eax
leaq 0x60(%rsp), %r15
jne 0x6194
movq 0x8(%r12), %rdx
movq %r12, %rdi
xorl %esi, %esi
leaq 0x7f0f(%rip), %rcx      # 0xe09b
xorl %r8d, %r8d
callq 0x3570
movl %r14d, %edi
callq 0x36e0
movl $0x3, %eax
movb $0x1, %cl
movl %ecx, 0x4(%rsp)
jmp 0x6241
leaq 0x70(%rsp), %rax
movq %rax, 0x60(%rsp)
movq %r15, %rdi
leaq 0x7fc1(%rip), %rsi      # 0xe181
leaq 0x7ff5(%rip), %rdx      # 0xe1bc
callq 0xc5c2
movq %r15, %rdi
callq 0x58fc
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x61f0
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x3390
movl $0x1, %eax
jmp 0x6241
movq %r15, %rbp
jmp 0x623c
incl %r13d
movl %r14d, %edi
callq 0x36e0
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 0x34d0
movl $0x2, %eax
leaq 0x60(%rsp), %r15
cmpl $0x2, %eax
je 0x60b5
cmpl $0x3, %eax
jne 0x62cd
movl 0x4(%rsp), %eax
testb $0x1, %al
jne 0x62cf
leaq 0x60(%rsp), %rbx
movl $0x100, %edx            # imm = 0x100
movq %rbx, %rdi
xorl %esi, %esi
callq 0x31f0
leaq 0x7f5b(%rip), %rdx      # 0xe1cd
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdi
movl $0x64, %ecx
xorl %eax, %eax
callq 0x35b0
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 0xc5c2
leaq 0x20(%rsp), %rdi
callq 0x58fc
movq 0x20(%rsp), %rdi
cmpq %r14, %rdi
je 0x62cd
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x3390
xorl %eax, %eax
andb $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 0x6331
movq 0x30(%rsp), %rsi
jmp 0x6329
jmp 0x6312
movq %rax, %rbx
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x6331
movq 0x70(%rsp), %rsi
jmp 0x6329
movq %rax, %rbx
jmp 0x6331
movq %rax, %rbx
movq 0x40(%rsp), %rdi
cmpq %rbp, %rdi
je 0x6331
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x3390
movq %rbx, %rdi
callq 0x3650
nop