// File: /workspace/github_cpp_makefile_star2_O2/sjtuwrk[P]SunwayMR/include/Messaging.hpp
// Function: messageHandler(void*)
void* messageHandler(void *data)
{
	// parse data
	struct xyz_messaging_listen_thread_data_ *td = (struct xyz_messaging_listen_thread_data_ *)data;
	Messaging *m = td->mess;

	string msg;
	readSocket(td->client_sockfd, msg);

	string::size_type pos;
	pos=msg.find('$', 0);
	if (pos !=  string::npos) {
		string typeStr = msg.substr(0, pos);
		int msgType = atoi(typeStr.c_str());
		string msgContent=msg.substr(pos+1);

		if(msgType == FILE_BLOCK_REQUEST) { // reply file block
			vector<string> vs;
			splitString(msgContent, vs, FILE_BLOCK_REQUEST_DELIMITATION);
			if(vs.size() >= 4) {
				string ret;
				string path = vs[0];
				int offset = atoi(vs[1].c_str());
				int length = atoi(vs[2].c_str());
				FileSourceFormat format = static_cast<FileSourceFormat>(atoi(vs[3].c_str()));
				if (format == FILE_SOURCE_FORMAT_BYTE) {

					pthread_mutex_lock(&m->mutex_check_file_cache);
					if (m->file_cache_bytes.find(path) == m->file_cache_bytes.end()) { // check file cache
						string *file_content = new string();
						readFile(path, *file_content);
						m->file_cache_bytes[path] = file_content;
					} 
					pthread_mutex_unlock(&m->mutex_check_file_cache);

					ret = m->file_cache_bytes[path]->substr(offset, length);
				} else {
					pthread_mutex_lock(&m->mutex_check_file_cache);
					if (m->file_cache_lines.find(path) == m->file_cache_lines.end()) { // check file cache
						vector<string> *file_content = new vector<string>();
						readFileToLines(path, *file_content);
						m->file_cache_lines[path] = file_content;
					} 
					pthread_mutex_unlock(&m->mutex_check_file_cache);

					unsigned end_line = offset+length;
					for (unsigned int i=offset; i<end_line && i<m->file_cache_lines[path]->size(); i++) {
						ret += (*m->file_cache_lines[path])[i];
						ret += "\n";
					}
				}
				send(td->client_sockfd, ret);
				close(td->client_sockfd);
			}
		} else if(msgType == FETCH_REQUEST) {

			//cout << "CONTEXT ID: " << SUNWAYMR_CONTEXT_ID << endl;

			vector<string> paras;
			splitString(msgContent, paras, ",");
			if(paras.size() == 2)
			{
				long shuffleID = atol(paras[0].c_str());
				int partitionID = atoi(paras[1].c_str());

				// send back data
				string base_dir = "cache/shuffle/";
				string app_id = num2string(SUNWAYMR_CONTEXT_ID);
			    string shuffle_id = num2string(shuffleID);

			    string dir = app_id.append("/shuffle-") + shuffle_id.append("/");
			    dir = base_dir + dir;
			    vector<string> allFileNames;
				listAllFileNamesContain(dir, allFileNames, "shuffleTaskFile");

				pthread_mutex_lock(&m->mutex_check_file_cache);
				//map< long, vector< vector<string>* > > fetch_content_local; // !global shuffle cache data map(fetch_content) will cause segmentation fault!
				map< long, vector< vector<string>* > >::iterator it = m->fetch_content_local.find(shuffleID);
				if(it == m->fetch_content_local.end())
				{
					// this shuffle has not  been cached, read it
					vector< vector<string>* > vvs;
					for(unsigned int i=0; i<allFileNames.size(); i++)
					{
							string content;
							readFile(dir+allFileNames[i], content);
							vector<string> *lines = new vector<string>();
							splitString(content, *lines, SHUFFLETASK_PARTITION_DELIMITATION);
							vvs.push_back(lines);
					}
					m->fetch_content_local[shuffleID] = vvs;
				}
				pthread_mutex_unlock(&m->mutex_check_file_cache);

				//organize send message
				string senMsg;
				for(unsigned int i=0; i<m->fetch_content_local[shuffleID].size()-1; i++)
					senMsg += (*(m->fetch_content_local[shuffleID][i]))[partitionID] + string(SHUFFLETASK_KV_DELIMITATION);
				senMsg += (*(m->fetch_content_local[shuffleID].back()))[partitionID];

				send(td->client_sockfd, senMsg);
			}
			close(td->client_sockfd);
		} else if(msgType == FILE_INFO || msgType > 999999) { // sunwaymrhelper file sending
			m->messageReceived(td->local_port, td->ip, msgType, msgContent);
			string reply = "0";
			send(td->client_sockfd, reply);
			close(td->client_sockfd);
		} else {
			string reply = "";
			send(td->client_sockfd, reply);
			close(td->client_sockfd);

			m->messageReceived(td->local_port, td->ip, msgType, msgContent);
		}
	} else {
		string reply = "";
		send(td->client_sockfd, reply);
		close(td->client_sockfd);
	}

	pthread_exit(NULL);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x298, %rsp            # imm = 0x298
movq %rdi, %r12
movq (%rdi), %r14
leaq 0x188(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movl 0x30(%rdi), %edi
leaq 0x178(%rsp), %rsi
callq 0x51b5
leaq 0x178(%rsp), %rdi
pushq $0x24
popq %rsi
xorl %edx, %edx
callq 0x3470
cmpq $-0x1, %rax
jne 0x5b19
leaq 0x65c7(%rip), %rsi      # 0xc09b
leaq 0x110(%rsp), %rdi
leaq 0xf0(%rsp), %rdx
callq 0x915a
movl 0x30(%r12), %edi
leaq 0x110(%rsp), %rsi
callq 0x65a7
movl 0x30(%r12), %edi
callq 0x36c0
leaq 0x110(%rsp), %rdi
callq 0x36e8
xorl %edi, %edi
callq 0x3620
movq %rax, %r15
leaq 0x110(%rsp), %rdi
leaq 0x178(%rsp), %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x3390
movq 0x110(%rsp), %rdi
callq 0x3540
movl %eax, %ebp
incq %r15
leaq 0xf0(%rsp), %rdi
leaq 0x178(%rsp), %rsi
pushq $-0x1
popq %rcx
movq %r15, %rdx
callq 0x3390
cmpl $0x6, %ebp
je 0x5d3a
cmpl $0x5, %ebp
jne 0x6019
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
andq $0x0, 0x90(%rsp)
leaq 0x6778(%rip), %rsi      # 0xc308
leaq 0x10(%rsp), %rdi
leaq 0x40(%rsp), %rdx
callq 0x915a
leaq 0xf0(%rsp), %rdi
leaq 0x80(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x38cc
leaq 0x10(%rsp), %rdi
callq 0x36e8
movq 0x80(%rsp), %rsi
movq 0x88(%rsp), %rax
subq %rsi, %rax
cmpq $0x61, %rax
jb 0x5d2d
leaq 0x20(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x40(%rsp), %rdi
callq 0x3490
movq %r12, 0x38(%rsp)
movq 0x80(%rsp), %rbx
movq 0x20(%rbx), %rdi
callq 0x3540
movl %eax, %ebp
movq 0x40(%rbx), %rdi
callq 0x3540
movq %rax, (%rsp)
movq 0x60(%rbx), %rdi
callq 0x3540
movl %eax, %r13d
leaq 0x30(%r14), %r12
movq %r12, %rdi
callq 0x34d0
testl %r13d, %r13d
jne 0x6121
leaq 0x58(%r14), %r13
leaq 0x40(%rsp), %rsi
movq %r13, %rdi
callq 0x9e04
addq $0x60, %r14
cmpq %r14, %rax
jne 0x5cb6
pushq $0x20
popq %rdi
callq 0x3370
movq %rax, %r14
addq $0x10, %rax
movq %rax, (%r14)
andq $0x0, 0x8(%r14)
movb $0x0, 0x10(%r14)
leaq 0x258(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x3490
leaq 0x258(%rsp), %rdi
movq %r14, %rsi
callq 0x4295
leaq 0x258(%rsp), %rdi
callq 0x36e8
leaq 0x40(%rsp), %rsi
movq %r13, %rdi
callq 0x91e6
movq %r14, (%rax)
movq %r12, %rdi
callq 0x3280
leaq 0x40(%rsp), %rsi
movq %r13, %rdi
callq 0x91e6
movq (%rax), %rsi
movslq %ebp, %rdx
movslq (%rsp), %rcx
leaq 0x158(%rsp), %rdi
callq 0x3390
leaq 0x10(%rsp), %rdi
leaq 0x158(%rsp), %r14
movq %r14, %rsi
callq 0x3410
movq %r14, %rdi
callq 0x36e8
movq 0x38(%rsp), %rbx
movl 0x30(%rbx), %edi
leaq 0x10(%rsp), %rsi
callq 0x65a7
movl 0x30(%rbx), %edi
callq 0x36c0
leaq 0x40(%rsp), %rdi
callq 0x36e8
leaq 0x10(%rsp), %rdi
callq 0x36e8
leaq 0x80(%rsp), %rdi
jmp 0x63af
xorps %xmm0, %xmm0
movaps %xmm0, 0xd0(%rsp)
andq $0x0, 0xe0(%rsp)
leaq 0x65ca(%rip), %rsi      # 0xc31f
leaq 0x10(%rsp), %rdi
leaq 0x40(%rsp), %rdx
callq 0x915a
leaq 0xf0(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x38cc
leaq 0x10(%rsp), %rdi
callq 0x36e8
movq 0xd0(%rsp), %rbx
movq 0xd8(%rsp), %rax
subq %rbx, %rax
cmpq $0x40, %rax
jne 0x639d
movq (%rbx), %rdi
callq 0x33f0
movq %rax, 0x30(%rsp)
movq 0x20(%rbx), %rdi
callq 0x3540
movl %eax, %ebx
leaq 0x655d(%rip), %rsi      # 0xc321
leaq 0x10(%rsp), %rdi
leaq 0x40(%rsp), %rdx
callq 0x915a
movq 0xc60e(%rip), %rsi      # 0x123e8
leaq 0x40(%rsp), %rdi
callq 0x481f
movq 0x30(%rsp), %rsi
leaq 0x158(%rsp), %rdi
callq 0x481f
leaq 0x6533(%rip), %rsi      # 0xc330
leaq 0x40(%rsp), %rdi
callq 0x3690
movq %rax, %r15
leaq 0x651d(%rip), %rsi      # 0xc32e
leaq 0x158(%rsp), %rdi
callq 0x3690
leaq 0x80(%rsp), %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x9195
movl %ebx, (%rsp)
movq %r12, 0x38(%rsp)
leaq 0x60(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x80(%rsp), %rdx
callq 0x9195
leaq 0x80(%rsp), %r15
leaq 0x60(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x3410
movq %r12, %rdi
callq 0x36e8
xorps %xmm0, %xmm0
movaps %xmm0, 0xb0(%rsp)
andq $0x0, 0xc0(%rsp)
leaq 0x218(%rsp), %rdi
movq %r15, %rsi
callq 0x3490
leaq 0x649f(%rip), %rsi      # 0xc33a
leaq 0x1f8(%rsp), %rdi
leaq 0x60(%rsp), %rdx
callq 0x915a
leaq 0x218(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
leaq 0x1f8(%rsp), %rdx
callq 0x4b84
leaq 0x1f8(%rsp), %rdi
callq 0x36e8
leaq 0x218(%rsp), %rdi
callq 0x36e8
leaq 0x30(%r14), %rdi
movq %rdi, 0xe8(%rsp)
callq 0x34d0
leaq 0xb8(%r14), %rdi
leaq 0x30(%rsp), %rsi
movq %rdi, 0xa0(%rsp)
callq 0xa5e0
addq $0xc0, %r14
cmpq %r14, %rax
jne 0x624d
leaq 0x130(%rsp), %rax
andq $0x0, 0x10(%rax)
xorps %xmm0, %xmm0
movq %rax, %rbx
movaps %xmm0, (%rax)
xorl %r15d, %r15d
leaq 0x278(%rsp), %r13
leaq 0x60(%rsp), %rbp
leaq 0x1d8(%rsp), %r14
movl %r15d, %r15d
movq 0xb0(%rsp), %rdx
movq 0xb8(%rsp), %rax
subq %rdx, %rax
sarq $0x5, %rax
cmpq %r15, %rax
jbe 0x621e
leaq 0x70(%rsp), %rax
movq %rax, 0x60(%rsp)
andq $0x0, 0x68(%rsp)
movb $0x0, 0x70(%rsp)
movq %r15, %rax
shlq $0x5, %rax
addq %rax, %rdx
movq %r13, %rdi
leaq 0x80(%rsp), %rsi
callq 0x9195
movq %r13, %rdi
movq %rbp, %rsi
callq 0x4295
movq %r13, %rdi
callq 0x36e8
pushq $0x18
popq %rdi
callq 0x3370
movq %rax, %r12
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq %rax, 0xa8(%rsp)
movq %r14, %rdi
leaq 0x6371(%rip), %rsi      # 0xc34a
leaq 0xf(%rsp), %rdx
callq 0x915a
movq %rbp, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x38cc
movq %r14, %rdi
callq 0x36e8
movq %rbx, %rdi
leaq 0xa8(%rsp), %rsi
callq 0x92b2
movq %rbp, %rdi
callq 0x36e8
incl %r15d
jmp 0x5f4c
cmpl $0x1, %ebp
setne %al
cmpl $0xf4240, %ebp          # imm = 0xF4240
setl %cl
testb %cl, %al
jne 0x609f
movl 0x8(%r12), %r15d
leaq 0x10(%r12), %rsi
leaq 0x1b8(%rsp), %rdi
callq 0x3490
movq (%r14), %rax
leaq 0x1b8(%rsp), %rdx
leaq 0xf0(%rsp), %r8
movq %r14, %rdi
movl %r15d, %esi
movl %ebp, %ecx
callq *0x10(%rax)
leaq 0x1b8(%rsp), %rdi
callq 0x36e8
leaq 0x62e0(%rip), %rsi      # 0xc355
leaq 0x10(%rsp), %rdi
leaq 0x40(%rsp), %rdx
callq 0x915a
movl 0x30(%r12), %edi
leaq 0x10(%rsp), %rsi
callq 0x65a7
movl 0x30(%r12), %edi
callq 0x36c0
jmp 0x6112
leaq 0x5ff5(%rip), %rsi      # 0xc09b
leaq 0x10(%rsp), %rdi
leaq 0x40(%rsp), %rdx
callq 0x915a
movl 0x30(%r12), %edi
leaq 0x10(%rsp), %rsi
callq 0x65a7
movl 0x30(%r12), %edi
callq 0x36c0
movl 0x8(%r12), %r15d
addq $0x10, %r12
leaq 0x198(%rsp), %rdi
movq %r12, %rsi
callq 0x3490
movq (%r14), %rax
leaq 0x198(%rsp), %rdx
leaq 0xf0(%rsp), %r8
movq %r14, %rdi
movl %r15d, %esi
movl %ebp, %ecx
callq *0x10(%rax)
leaq 0x198(%rsp), %rdi
callq 0x36e8
leaq 0x10(%rsp), %rdi
callq 0x36e8
jmp 0x63b4
leaq 0x88(%r14), %r13
leaq 0x40(%rsp), %rsi
movq %r13, %rdi
callq 0xa1fc
addq $0x90, %r14
cmpq %r14, %rax
jne 0x6196
pushq $0x18
popq %rdi
callq 0x3370
movq %rax, %r14
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
leaq 0x238(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x3490
leaq 0x238(%rsp), %rdi
movq %r14, %rsi
callq 0x4479
leaq 0x238(%rsp), %rdi
callq 0x36e8
leaq 0x40(%rsp), %rsi
movq %r13, %rdi
callq 0x924c
movq %r14, (%rax)
movq %r12, %rdi
callq 0x3280
movq (%rsp), %rax
addl %ebp, %eax
movq %rax, (%rsp)
movl %ebp, %ebx
movq %rbx, %r15
shlq $0x5, %r15
leaq 0x40(%rsp), %r14
leaq 0x10(%rsp), %r12
leaq 0x615b(%rip), %rbp      # 0xc31d
cmpq (%rsp), %rbx
jae 0x5cff
movq %r13, %rdi
movq %r14, %rsi
callq 0x924c
movq (%rax), %rax
movq 0x8(%rax), %rcx
subq (%rax), %rcx
sarq $0x5, %rcx
cmpq %rbx, %rcx
jbe 0x5cff
movq %r13, %rdi
movq %r14, %rsi
callq 0x924c
movq (%rax), %rax
movq (%rax), %rsi
addq %r15, %rsi
movq %r12, %rdi
callq 0x36b0
movq %r12, %rdi
movq %rbp, %rsi
callq 0x3690
incq %rbx
addq $0x20, %r15
jmp 0x61c2
leaq 0x30(%rsp), %rsi
movq 0xa0(%rsp), %rdi
callq 0x92d0
leaq 0x130(%rsp), %rsi
movq %rax, %rdi
callq 0x9320
leaq 0x130(%rsp), %rdi
callq 0xa636
movq 0xe8(%rsp), %rdi
callq 0x3280
leaq 0x70(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movslq (%rsp), %rax
xorl %r14d, %r14d
leaq 0x30(%rsp), %r12
leaq 0x130(%rsp), %rbp
shlq $0x5, %rax
movq %rax, (%rsp)
leaq 0x1d8(%rsp), %r13
movq 0xa0(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x92d0
movq 0x8(%rax), %rcx
subq (%rax), %rcx
movl %r14d, %r14d
sarq $0x3, %rcx
decq %rcx
cmpq %r14, %rcx
jbe 0x631b
movq %rbx, %rdi
movq %r12, %rsi
callq 0x92d0
movq (%rax), %rax
movq (%rax,%r14,8), %rax
movq (%rax), %r15
movq %rbp, %rdi
leaq 0x6078(%rip), %rsi      # 0xc34f
leaq 0xa8(%rsp), %rdx
callq 0x915a
addq (%rsp), %r15
movq %r13, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x93ea
leaq 0x60(%rsp), %rdi
movq %r13, %rsi
callq 0x36b0
movq %r13, %rdi
callq 0x36e8
movq %rbp, %rdi
callq 0x36e8
incl %r14d
jmp 0x6297
leaq 0x30(%rsp), %rsi
movq %rbx, %rdi
callq 0x92d0
movq 0x8(%rax), %rax
movq -0x8(%rax), %rax
movq (%rsp), %rsi
addq (%rax), %rsi
leaq 0x60(%rsp), %rdi
callq 0x36b0
movq 0x38(%rsp), %rax
movl 0x30(%rax), %edi
leaq 0x60(%rsp), %rsi
callq 0x65a7
leaq 0x60(%rsp), %rdi
callq 0x36e8
leaq 0xb0(%rsp), %rdi
callq 0x904e
leaq 0x80(%rsp), %rdi
callq 0x36e8
leaq 0x158(%rsp), %rdi
callq 0x36e8
leaq 0x40(%rsp), %rdi
callq 0x36e8
leaq 0x10(%rsp), %rdi
callq 0x36e8
movq 0x38(%rsp), %r12
movl 0x30(%r12), %edi
callq 0x36c0
leaq 0xd0(%rsp), %rdi
callq 0x904e
leaq 0xf0(%rsp), %rdi
callq 0x36e8
jmp 0x5b05
movq %rax, %rbx
jmp 0x642f
movq %rax, %rbx
leaq 0x1d8(%rsp), %rdi
jmp 0x63e5
jmp 0x6422
movq %rax, %rbx
leaq 0x278(%rsp), %rdi
callq 0x36e8
jmp 0x6425
jmp 0x6422
jmp 0x6418
movq %rax, %rbx
leaq 0x1d8(%rsp), %rdi
callq 0x36e8
jmp 0x6405
movq %rax, %rbx
leaq 0x130(%rsp), %rdi
callq 0x36e8
jmp 0x641b
jmp 0x6418
jmp 0x6418
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
jmp 0x645d
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x36e8
leaq 0x130(%rsp), %rdi
callq 0xa636
jmp 0x6467
jmp 0x6464
movq %rax, %rbx
leaq 0x1f8(%rsp), %rdi
callq 0x36e8
jmp 0x6455
movq %rax, %rbx
leaq 0x218(%rsp), %rdi
callq 0x36e8
jmp 0x6467
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x904e
jmp 0x6479
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x36e8
jmp 0x64b4
movq %rax, %rbx
leaq 0x238(%rsp), %rdi
jmp 0x64a0
movq %rax, %rbx
leaq 0x258(%rsp), %rdi
callq 0x36e8
jmp 0x64e9
jmp 0x64e6
jmp 0x64e6
jmp 0x64e6
jmp 0x64e6
jmp 0x64e6
movq %rax, %rbx
leaq 0x158(%rsp), %rdi
callq 0x36e8
jmp 0x64d8
movq %rax, %rbx
leaq 0x198(%rsp), %rdi
callq 0x36e8
jmp 0x6511
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x36e8
jmp 0x6527
jmp 0x64e6
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x36e8
jmp 0x6536
jmp 0x6524
jmp 0x650e
jmp 0x651f
jmp 0x6542
jmp 0x6533
jmp 0x6542
movq %rax, %rbx
leaq 0x1b8(%rsp), %rdi
jmp 0x6516
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x36e8
jmp 0x655f
jmp 0x651f
movq %rax, %rbx
jmp 0x655f
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x36e8
jmp 0x6545
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x36e8
jmp 0x6552
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
jmp 0x655a
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x904e
leaq 0xf0(%rsp), %rdi
callq 0x36e8
jmp 0x6575
jmp 0x6588
jmp 0x6572
movq %rax, %rbx
leaq 0x110(%rsp), %rdi
callq 0x36e8
jmp 0x658b
jmp 0x6588
jmp 0x6588
movq %rax, %rbx
leaq 0x178(%rsp), %rdi
callq 0x36e8
movq %rbx, %rdi
callq 0x3610