// File: /workspace/github_cpp_makefile_star2_O3/sjtuwrk[P]SunwayMR/include/Messaging.hpp
// Function: Messaging::listenMessage(int)
void Messaging::listenMessage(int listenPort)
{
	listenStatus = NA;

	// init socket
	int server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if (server_sockfd < 0) {
		listenStatus = FAILURE;
		pthread_mutex_unlock(&mutex_listen_status);
		return;
	}

	struct sockaddr_in server_address;

	server_address.sin_family = AF_INET;
	server_address.sin_addr.s_addr =  htonl(INADDR_ANY);
	server_address.sin_port = htons(listenPort);
	int server_len = sizeof(server_address);

	// bind
	int rb = bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
	if (rb < 0)
	{
		listenStatus = FAILURE;
		pthread_mutex_unlock(&mutex_listen_status);
		return;
	}

	//listen
	int n = listen(server_sockfd, 36);
	if (n < 0)
	{
		listenStatus = FAILURE;
		pthread_mutex_unlock(&mutex_listen_status);
		return;
	}

	listenStatus = SUCCESS;
	pthread_mutex_unlock(&mutex_listen_status);

	stringstream listenInfo;
	listenInfo << "Messaging: listening on port [" << listenPort << "]";
	Logging::logInfo(listenInfo.str());

	struct sockaddr_in client_address;
	int client_len = sizeof(sockaddr_in);
	while (1) {
		int client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, (socklen_t *)&client_len);
		if (client_sockfd < 0) {
			stringstream ss;
			ss << "Messaging::listenMessage: accept failed, errno: " << errno;
			Logging::logWarning(ss.str());
			continue;
		}
		string ip = inet_ntoa(client_address.sin_addr);
		// thread data encapsulation
		struct xyz_messaging_listen_thread_data_ *td =
				new xyz_messaging_listen_thread_data_(
						this, listenPort, ip, client_sockfd);
		// create a thread
		pthread_t worker;
		if (pthread_create(&worker, NULL, messageHandler, (void *)td) != 0) {
			Logging::logError("Messaging: failed to create new thread while listening");
			close(client_sockfd);
		}
	}

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x388, %rsp            # imm = 0x388
movl %esi, %ebp
movq %rdi, %rbx
movl $0x0, 0xe8(%rdi)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x30d0
testl %eax, %eax
js 0x6489
movl %eax, %r14d
leaq 0x68(%rsp), %rsi
movw $0x2, (%rsi)
movl $0x0, 0x4(%rsi)
movl %ebp, %eax
rolw $0x8, %ax
movw %ax, 0x2(%rsi)
movl %r14d, %edi
movl $0x10, %edx
callq 0x3080
testl %eax, %eax
js 0x64b0
movl %r14d, %edi
movl $0x24, %esi
callq 0x33e0
leaq 0x8(%rbx), %rdi
testl %eax, %eax
jns 0x64d8
movl $0x2, 0xe8(%rbx)
jmp 0x64c1
movl $0x2, 0xe8(%rbx)
addq $0x8, %rbx
movq %rbx, %rdi
addq $0x388, %rsp            # imm = 0x388
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3290
movl $0x2, 0xe8(%rbx)
addq $0x8, %rbx
movq %rbx, %rdi
callq 0x3290
addq $0x388, %rsp            # imm = 0x388
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, 0xe8(%rbx)
callq 0x3290
leaq 0x200(%rsp), %rdi
callq 0x3240
leaq 0x210(%rsp), %r15
leaq 0x7d6a(%rip), %rsi      # 0xe26d
movl $0x1e, %edx
movq %r15, %rdi
callq 0x3400
movq %r15, %rdi
movl %ebp, %esi
callq 0x3600
leaq 0x83e3(%rip), %rsi      # 0xe904
movl $0x1, %edx
movq %rax, %rdi
callq 0x3400
leaq 0x218(%rsp), %rsi
leaq 0x78(%rsp), %rdi
callq 0x3580
leaq 0x78(%rsp), %rdi
callq 0x56e8
leaq 0x88(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x656b
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x3390
leaq 0xc(%rsp), %rax
movl $0x10, (%rax)
leaq 0x10(%rsp), %r12
movl %r14d, %edi
leaq 0x58(%rsp), %rsi
leaq 0xc(%rsp), %rdx
callq 0x3130
movl %eax, %r15d
testl %eax, %eax
js 0x66bc
movl 0x5c(%rsp), %edi
callq 0x3190
leaq 0x78(%rsp), %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0xbe80
movl $0x38, %edi
callq 0x3380
movq %rax, %r13
leaq 0x40(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x78(%rsp), %rsi
movq 0x80(%rsp), %rdx
addq %rsi, %rdx
leaq 0x30(%rsp), %rdi
callq 0xc556
movq %rbx, (%r13)
movl %ebp, 0x8(%r13)
leaq 0x10(%r13), %rdi
movq %r13, %rax
addq $0x20, %rax
movq %rax, 0x10(%r13)
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
addq %rsi, %rdx
callq 0xc556
movl %r15d, 0x30(%r13)
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x662b
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x3390
leaq 0x50(%rsp), %rdi
xorl %esi, %esi
leaq 0x21b(%rip), %rdx       # 0x6854
movq %r13, %rcx
callq 0x3670
testl %eax, %eax
je 0x6691
leaq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %r12, %rdi
leaq 0x7c64(%rip), %rsi      # 0xe2bd
leaq 0x7c93(%rip), %rdx      # 0xe2f3
callq 0xc5ec
movq %r12, %rdi
callq 0x58ec
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x6689
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3390
movl %r15d, %edi
callq 0x36d0
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
cmpq %rax, %rdi
je 0x657b
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x3390
jmp 0x657b
leaq 0x78(%rsp), %rdi
callq 0x3240
movl $0x30, %edx
leaq 0x88(%rsp), %r15
movq %r15, %rdi
leaq 0x7baf(%rip), %rsi      # 0xe28c
callq 0x3400
callq 0x3060
movl (%rax), %esi
movq %r15, %rdi
callq 0x3600
movq %r12, %rdi
leaq 0x90(%rsp), %rsi
callq 0x3580
movq %r12, %rdi
callq 0x57ea
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x6725
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3390
leaq 0x78(%rsp), %rdi
movq 0xc87f(%rip), %rsi      # 0x12fb0
callq 0x3260
leaq 0xf8(%rsp), %rdi
callq 0x3150
jmp 0x657b
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x67de
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3390
jmp 0x67de
jmp 0x67db
jmp 0x6828
jmp 0x67c7
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x67ca
movq 0x20(%rsp), %rsi
incq %rsi
jmp 0x67c0
jmp 0x67c7
movq %rax, %rbx
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x67b8
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x3390
jmp 0x67b8
movq %rax, %rbx
movl $0x38, %esi
movq %r13, %rdi
callq 0x3390
jmp 0x67ca
movq %rax, %rbx
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
jmp 0x680f
jmp 0x6828
movq %rax, %rbx
movq 0xc7cb(%rip), %rsi      # 0x12fb0
leaq 0x78(%rsp), %rdi
callq 0x3260
leaq 0xf8(%rsp), %rdi
callq 0x3150
jmp 0x682b
jmp 0x6828
movq %rax, %rbx
leaq 0x88(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x682b
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x3390
jmp 0x682b
jmp 0x6828
movq %rax, %rbx
movq 0xc77e(%rip), %rsi      # 0x12fb0
leaq 0x200(%rsp), %rdi
callq 0x3260
leaq 0x280(%rsp), %rdi
callq 0x3150
movq %rbx, %rdi
callq 0x3640