// File: /workspace/github_cpp_makefile_star2_O1/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 0x64f3
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 0x651a
movl %r14d, %edi
movl $0x24, %esi
callq 0x33e0
leaq 0x8(%rbx), %rdi
testl %eax, %eax
jns 0x6542
movl $0x2, 0xe8(%rbx)
jmp 0x652b
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 0x7cd0(%rip), %rsi      # 0xe23d
movl $0x1e, %edx
movq %r15, %rdi
callq 0x3400
movq %r15, %rdi
movl %ebp, %esi
callq 0x3610
leaq 0x8349(%rip), %rsi      # 0xe8d4
movl $0x1, %edx
movq %rax, %rdi
callq 0x3400
leaq 0x218(%rsp), %rsi
leaq 0x78(%rsp), %rdi
callq 0x3590
leaq 0x78(%rsp), %rdi
callq 0x56f8
leaq 0x88(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x65d5
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, %r13d
testl %eax, %eax
js 0x6725
movl 0x5c(%rsp), %edi
callq 0x3190
leaq 0x78(%rsp), %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0xbe6e
movl $0x38, %edi
callq 0x3380
movq %rax, %r15
leaq 0x40(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x78(%rsp), %rsi
movq 0x80(%rsp), %rdx
addq %rsi, %rdx
leaq 0x30(%rsp), %rdi
callq 0xc532
movq %rbx, (%r15)
movl %ebp, 0x8(%r15)
leaq 0x10(%r15), %rdi
movq %r15, %rax
addq $0x20, %rax
movq %rax, 0x10(%r15)
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
addq %rsi, %rdx
callq 0xc532
movl %r13d, 0x30(%r15)
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x6694
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x3390
leaq 0x50(%rsp), %rdi
xorl %esi, %esi
leaq 0x21b(%rip), %rdx       # 0x68bd
movq %r15, %rcx
callq 0x3680
testl %eax, %eax
je 0x66fa
leaq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %r12, %rdi
leaq 0x7bcb(%rip), %rsi      # 0xe28d
leaq 0x7bfa(%rip), %rdx      # 0xe2c3
callq 0xc5c2
movq %r12, %rdi
callq 0x58fc
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x66f2
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3390
movl %r13d, %edi
callq 0x36e0
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
cmpq %rax, %rdi
je 0x65e5
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x3390
jmp 0x65e5
leaq 0x78(%rsp), %rdi
callq 0x3240
movl $0x30, %edx
leaq 0x88(%rsp), %r15
movq %r15, %rdi
leaq 0x7b16(%rip), %rsi      # 0xe25c
callq 0x3400
callq 0x3060
movl (%rax), %esi
movq %r15, %rdi
callq 0x3610
movq %r12, %rdi
leaq 0x90(%rsp), %rsi
callq 0x3590
movq %r12, %rdi
callq 0x57fa
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x678e
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3390
leaq 0x78(%rsp), %rdi
movq 0xc816(%rip), %rsi      # 0x12fb0
callq 0x3260
leaq 0xf8(%rsp), %rdi
callq 0x3150
jmp 0x65e5
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x6847
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3390
jmp 0x6847
jmp 0x6844
jmp 0x6891
jmp 0x6830
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x6833
movq 0x20(%rsp), %rsi
incq %rsi
jmp 0x6829
jmp 0x6830
movq %rax, %rbx
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x6821
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x3390
jmp 0x6821
movq %rax, %rbx
movl $0x38, %esi
movq %r15, %rdi
callq 0x3390
jmp 0x6833
movq %rax, %rbx
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
jmp 0x6878
jmp 0x6891
movq %rax, %rbx
movq 0xc762(%rip), %rsi      # 0x12fb0
leaq 0x78(%rsp), %rdi
callq 0x3260
leaq 0xf8(%rsp), %rdi
callq 0x3150
jmp 0x6894
jmp 0x6891
movq %rax, %rbx
leaq 0x88(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6894
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x3390
jmp 0x6894
jmp 0x6891
movq %rax, %rbx
movq 0xc715(%rip), %rsi      # 0x12fb0
leaq 0x200(%rsp), %rdi
callq 0x3260
leaq 0x280(%rsp), %rdi
callq 0x3150
movq %rbx, %rdi
callq 0x3650