// File: sjtuwrk[P]SunwayMR/include/Utils.hpp
// Function: getLocalHost[abi:cxx11]()
string getLocalHost() {
	string ret = "";

    struct ifaddrs * ifAddrStruct=NULL;
    struct ifaddrs * ifa=NULL;
    void * tmpAddrPtr=NULL;

    getifaddrs(&ifAddrStruct);

    for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
        if (!ifa->ifa_addr) {
            continue;
        }
        if (ifa->ifa_addr->sa_family == AF_INET) { // check it is IP4
            // is a valid IP4 Address
            tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
            char addressBuffer[INET_ADDRSTRLEN];
            inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
            //printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);

            string temp = string(addressBuffer);
            string lo = "127.0.0.1";
            if (temp.compare(0, lo.size(), lo)!=0) {
            	ret = temp;
            	break;
            }

        } else if (ifa->ifa_addr->sa_family == AF_INET6) { // check it is IP6
            // is a valid IP6 Address
            tmpAddrPtr=&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
            char addressBuffer[INET6_ADDRSTRLEN];
            inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
            //printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);
        }
    }
    if (ifAddrStruct!=NULL) freeifaddrs(ifAddrStruct);

    return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0xe0(%rbp)
movq %rdi, %rax
movq %rax, -0xe8(%rbp)
movq %rdi, -0x8(%rbp)
movb $0x0, -0x9(%rbp)
leaq -0xa(%rbp), %rdi
movq %rdi, -0xd8(%rbp)
callq 0x47a0
movq -0xe0(%rbp), %rdi
movq -0xd8(%rbp), %rdx
leaq 0x147e6(%rip), %rsi     # 0x1a07f
callq 0xe8d0
jmp 0x58a0
leaq -0xa(%rbp), %rdi
callq 0x4470
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x4800
movq -0x28(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x5aea
movq -0x30(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x5907
jmp 0x5ada
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0xa(%rbp), %rdi
callq 0x4470
jmp 0x5b36
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movzwl (%rax), %eax
cmpl $0x2, %eax
jne 0x5a9c
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
addq $0x4, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rsi
movl $0x2, %edi
leaq -0x50(%rbp), %rdx
movq %rdx, -0xf8(%rbp)
movl $0x10, %ecx
callq 0x4460
leaq -0x71(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
callq 0x47a0
movq -0xf8(%rbp), %rsi
movq -0xf0(%rbp), %rdx
leaq -0x70(%rbp), %rdi
callq 0xe8d0
jmp 0x5972
leaq -0x71(%rbp), %rdi
callq 0x4470
leaq -0x99(%rbp), %rdi
movq %rdi, -0x100(%rbp)
callq 0x47a0
movq -0x100(%rbp), %rdx
leaq 0x14677(%rip), %rsi     # 0x1a013
leaq -0x98(%rbp), %rdi
callq 0xe8d0
jmp 0x59aa
leaq -0x99(%rbp), %rdi
callq 0x4470
leaq -0x98(%rbp), %rdi
movq %rdi, -0x110(%rbp)
callq 0x4270
movq -0x110(%rbp), %rcx
movq %rax, %rdx
xorl %eax, %eax
movl %eax, %esi
leaq -0x70(%rbp), %rdi
callq 0x4570
movl %eax, -0x104(%rbp)
jmp 0x59e8
movl -0x104(%rbp), %eax
cmpl $0x0, %eax
je 0x5a5f
movq -0xe0(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x4050
jmp 0x5a05
movl $0x2, -0xa0(%rbp)
jmp 0x5a69
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0x71(%rbp), %rdi
callq 0x4470
jmp 0x5b2a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0x99(%rbp), %rdi
callq 0x4470
jmp 0x5a8e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x4878
jmp 0x5a8e
movl $0x0, -0xa0(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x4878
leaq -0x70(%rbp), %rdi
callq 0x4878
movl -0xa0(%rbp), %eax
testl %eax, %eax
je 0x5a8c
jmp 0x5a8a
jmp 0x5aea
jmp 0x5ad8
leaq -0x70(%rbp), %rdi
callq 0x4878
jmp 0x5b2a
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movzwl (%rax), %eax
cmpl $0xa, %eax
jne 0x5ad6
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
addq $0x8, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rsi
leaq -0xd0(%rbp), %rdx
movl $0xa, %edi
movl $0x2e, %ecx
callq 0x4460
jmp 0x5ad8
jmp 0x5ada
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x58d2
cmpq $0x0, -0x28(%rbp)
je 0x5afa
movq -0x28(%rbp), %rdi
callq 0x4770
movb $0x1, -0x9(%rbp)
movl $0x1, -0xa0(%rbp)
testb $0x1, -0x9(%rbp)
jne 0x5b1a
movq -0xe0(%rbp), %rdi
callq 0x4878
movq -0xe8(%rbp), %rax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
movq -0xe0(%rbp), %rdi
callq 0x4878
movq -0x18(%rbp), %rdi
callq 0x4780
nop