// File: sjtuwrk[P]SunwayMR/tools/SunwayMRHelper.hpp
// Function: SunwayMRHelper::SunwayMRHelper(bool)
SunwayMRHelper::SunwayMRHelper(bool forUserApp) {
	masterListenPort = 0;
	threads = 0;
	memory = 0;
	listenPort = 0;
	listening = false;
	sendResourceInfoThread = pthread_t();

	// -a mode
	if (forUserApp)
	{
		localAddr = getLocalHost();
		if (localAddr == "") {
			Logging::logError("SunwayMRHelper: failed to obtain local IP address.");
			exit(1);
		} else {
			stringstream ipinfo;
			ipinfo << "SunwayMRHelper: local IP is " << localAddr;
			Logging::logInfo(ipinfo.str());
		}

		// find master in host resource file
		masterAddr = localAddr;
		stringstream hostFilePath;
		hostFilePath << fileSaveDir << allHostsFileName;
		//read resource file
		string fileContent;
		bool rd = readFile(hostFilePath.str(), fileContent);
		if (!rd) {
			stringstream error;
			error << "SunwayMRHelper: unable to read host resource file: " << hostFilePath.str();
			Logging::logError(error.str());
			exit(1);
		}

		stringstream fileContentStream(fileContent);
		string line;
		while(std::getline(fileContentStream,line,'\n')){
			vector<string> temp = splitString(line, ' ');
			if(temp.size() < 5) continue;
			if(temp[4] == "master") {
				masterAddr = temp[0];
				break;
			}
		}
	}

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x7f0, %rsp            # imm = 0x7F0
movb %sil, %al
movq %rdi, -0x8(%rbp)
andb $0x1, %al
movb %al, -0x9(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x778(%rbp)
callq 0x73e0
movq -0x778(%rbp), %rdi
leaq 0x1aca3(%rip), %rax     # 0x24d58
addq $0x10, %rax
movq %rax, (%rdi)
addq $0xf0, %rdi
movq %rdi, -0x798(%rbp)
callq 0x45c0
movq -0x778(%rbp), %rdi
addq $0x110, %rdi            # imm = 0x110
movq %rdi, -0x790(%rbp)
callq 0x45c0
movq -0x778(%rbp), %rdi
addq $0x140, %rdi            # imm = 0x140
movq %rdi, -0x788(%rbp)
callq 0xf780
movq -0x778(%rbp), %rdi
addq $0x158, %rdi            # imm = 0x158
movq %rdi, -0x780(%rbp)
callq 0xf7a0
movq -0x778(%rbp), %rax
movl $0x0, 0x130(%rax)
movl $0x0, 0x134(%rax)
movl $0x0, 0x138(%rax)
movl $0x0, 0x13c(%rax)
movb $0x0, 0x188(%rax)
movq $0x0, 0x190(%rax)
testb $0x1, -0x9(%rbp)
je 0xa702
leaq -0x30(%rbp), %rdi
callq 0x5850
jmp 0xa173
movq -0x778(%rbp), %rdi
addq $0x110, %rdi            # imm = 0x110
movq %rdi, -0x7a8(%rbp)
leaq -0x30(%rbp), %rsi
movq %rsi, -0x7b0(%rbp)
callq 0x44f0
movq -0x7b0(%rbp), %rdi
callq 0x4878
movq -0x7a8(%rbp), %rdi
leaq 0xfecd(%rip), %rsi      # 0x1a07f
callq 0xef60
movb %al, -0x799(%rbp)
jmp 0xa1bf
movb -0x799(%rbp), %al
testb $0x1, %al
jne 0xa1ce
jmp 0xa260
leaq -0x61(%rbp), %rdi
movq %rdi, -0x7b8(%rbp)
callq 0x47a0
movq -0x7b8(%rbp), %rdx
leaq 0x10175(%rip), %rsi     # 0x1a361
leaq -0x60(%rbp), %rdi
callq 0xe8d0
jmp 0xa1f7
leaq -0x60(%rbp), %rdi
callq 0x7300
jmp 0xa202
leaq -0x60(%rbp), %rdi
callq 0x4878
leaq -0x61(%rbp), %rdi
callq 0x4470
movl $0x1, %edi
callq 0x44e0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0xa70b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0xa252
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x4878
leaq -0x61(%rbp), %rdi
callq 0x4470
jmp 0xa70b
leaq -0x1f0(%rbp), %rdi
callq 0x4280
jmp 0xa26e
leaq -0x1e0(%rbp), %rdi
leaq 0x10118(%rip), %rsi     # 0x1a394
callq 0x43f0
movq %rax, -0x7c0(%rbp)
jmp 0xa28a
movq -0x7c0(%rbp), %rdi
movq -0x778(%rbp), %rsi
addq $0x110, %rsi            # imm = 0x110
callq 0x4380
jmp 0xa2a6
leaq -0x210(%rbp), %rdi
leaq -0x1f0(%rbp), %rsi
callq 0x43e0
jmp 0xa2bb
leaq -0x210(%rbp), %rdi
callq 0x7140
jmp 0xa2c9
leaq -0x210(%rbp), %rdi
callq 0x4878
leaq -0x1f0(%rbp), %rdi
callq 0x42b0
jmp 0xa31a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0xa309
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x210(%rbp), %rdi
callq 0x4878
leaq -0x1f0(%rbp), %rdi
callq 0x42b0
jmp 0xa70b
movq -0x778(%rbp), %rdi
movq %rdi, %rsi
addq $0x110, %rsi            # imm = 0x110
addq $0xf0, %rdi
callq 0x4050
jmp 0xa339
leaq -0x398(%rbp), %rdi
callq 0x4280
jmp 0xa347
leaq -0x388(%rbp), %rdi
leaq 0x1b163(%rip), %rsi     # 0x254b8
callq 0x4380
movq %rax, -0x7c8(%rbp)
jmp 0xa363
movq -0x7c8(%rbp), %rdi
leaq 0x1b187(%rip), %rsi     # 0x254f8
callq 0x4380
jmp 0xa378
leaq -0x3b8(%rbp), %rdi
callq 0x45c0
leaq -0x3e0(%rbp), %rdi
leaq -0x398(%rbp), %rsi
callq 0x43e0
jmp 0xa399
leaq -0x3e0(%rbp), %rdi
leaq -0x3b8(%rbp), %rsi
callq 0x5dc0
movb %al, -0x7c9(%rbp)
jmp 0xa3b4
leaq -0x3e0(%rbp), %rdi
callq 0x4878
movb -0x7c9(%rbp), %al
andb $0x1, %al
movb %al, -0x3b9(%rbp)
testb $0x1, -0x3b9(%rbp)
jne 0xa504
leaq -0x568(%rbp), %rdi
callq 0x4280
jmp 0xa3e9
leaq -0x558(%rbp), %rdi
leaq 0xffba(%rip), %rsi      # 0x1a3b1
callq 0x43f0
movq %rax, -0x7d8(%rbp)
jmp 0xa405
leaq -0x588(%rbp), %rdi
leaq -0x398(%rbp), %rsi
callq 0x43e0
jmp 0xa41a
movq -0x7d8(%rbp), %rdi
leaq -0x588(%rbp), %rsi
callq 0x4380
jmp 0xa42f
leaq -0x588(%rbp), %rdi
callq 0x4878
leaq -0x5a8(%rbp), %rdi
leaq -0x568(%rbp), %rsi
callq 0x43e0
jmp 0xa450
leaq -0x5a8(%rbp), %rdi
callq 0x7300
jmp 0xa45e
leaq -0x5a8(%rbp), %rdi
callq 0x4878
movl $0x1, %edi
callq 0x44e0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0xa6f4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0xa6e8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x3e0(%rbp), %rdi
callq 0x4878
jmp 0xa6e8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0xa4f3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x588(%rbp), %rdi
callq 0x4878
jmp 0xa4f3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x5a8(%rbp), %rdi
callq 0x4878
leaq -0x568(%rbp), %rdi
callq 0x42b0
jmp 0xa6e8
movl $0x10, %edi
movl $0x8, %esi
callq 0xea00
movl %eax, -0x7dc(%rbp)
jmp 0xa51b
movl -0x7dc(%rbp), %edx
leaq -0x730(%rbp), %rdi
leaq -0x3b8(%rbp), %rsi
callq 0x46b0
jmp 0xa536
leaq -0x750(%rbp), %rdi
callq 0x45c0
leaq -0x730(%rbp), %rdi
leaq -0x750(%rbp), %rsi
movl $0xa, %edx
callq 0x47f0
movq %rax, -0x7e8(%rbp)
jmp 0xa563
movq -0x7e8(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x4510
movb %al, -0x7e9(%rbp)
jmp 0xa581
movb -0x7e9(%rbp), %al
testb $0x1, %al
jne 0xa590
jmp 0xa69e
leaq -0x768(%rbp), %rdi
leaq -0x750(%rbp), %rsi
movl $0x20, %edx
callq 0x4e00
jmp 0xa5aa
leaq -0x768(%rbp), %rdi
callq 0xe3f0
cmpq $0x5, %rax
jae 0xa5dc
movl $0x2, -0x76c(%rbp)
jmp 0xa668
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0xa6d0
leaq -0x768(%rbp), %rdi
movl $0x4, %esi
callq 0xef40
movq %rax, %rdi
leaq 0x10235(%rip), %rsi     # 0x1a82c
callq 0xef60
movb %al, -0x7ea(%rbp)
jmp 0xa604
movb -0x7ea(%rbp), %al
testb $0x1, %al
jne 0xa610
jmp 0xa65e
xorl %eax, %eax
movl %eax, %esi
leaq -0x768(%rbp), %rdi
callq 0xef40
movq -0x778(%rbp), %rdi
movq %rax, %rsi
addq $0xf0, %rdi
callq 0x4050
jmp 0xa638
movl $0x3, -0x76c(%rbp)
jmp 0xa668
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x768(%rbp), %rdi
callq 0xe390
jmp 0xa6d0
movl $0x0, -0x76c(%rbp)
leaq -0x768(%rbp), %rdi
callq 0xe390
movl -0x76c(%rbp), %eax
movl %eax, -0x7f0(%rbp)
testl %eax, %eax
je 0xa699
jmp 0xa686
movl -0x7f0(%rbp), %eax
subl $0x2, %eax
je 0xa542
jmp 0xa697
jmp 0xa69e
jmp 0xa542
leaq -0x750(%rbp), %rdi
callq 0x4878
leaq -0x730(%rbp), %rdi
callq 0x42b0
leaq -0x3b8(%rbp), %rdi
callq 0x4878
leaq -0x398(%rbp), %rdi
callq 0x42b0
jmp 0xa702
leaq -0x750(%rbp), %rdi
callq 0x4878
leaq -0x730(%rbp), %rdi
callq 0x42b0
leaq -0x3b8(%rbp), %rdi
callq 0x4878
leaq -0x398(%rbp), %rdi
callq 0x42b0
jmp 0xa70b
addq $0x7f0, %rsp            # imm = 0x7F0
popq %rbp
retq
movq -0x780(%rbp), %rdi
callq 0xf7c0
movq -0x788(%rbp), %rdi
callq 0xf7e0
movq -0x790(%rbp), %rdi
callq 0x4878
movq -0x798(%rbp), %rdi
callq 0x4878
movq -0x778(%rbp), %rdi
callq 0x7450
movq -0x38(%rbp), %rdi
callq 0x4780