#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct sockaddr_in6 {
  int dummy;
};
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
struct addrinfo {
  TYPE_1__ *ai_addr;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};
typedef int /*<<< orphan*/ hints;
struct TYPE_2__ {
  scalar_t__ sa_family;
};

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char const *, int /*<<< orphan*/ *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memcpy(struct sockaddr *, TYPE_1__ *, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
const char *hostToIP(const char *host, struct sockaddr *ip) {
  struct addrinfo hints;
  struct addrinfo *addrlist;
  struct addrinfo *p;
  const char *errorMsg = NULL;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  {
    int result = getaddrinfo(host, NULL, &hints, &addrlist);
    if (result) {
      return "getaddrinfo failed";
    }
  }
  errorMsg = "no ipv4 or ipv6 address found";
  for (p = addrlist; p != NULL; p = p->ai_next) {
    if (p->ai_addr->sa_family == AF_INET) {
      memcpy(ip, p->ai_addr, sizeof(struct sockaddr_in));
      errorMsg = NULL;
      break;
    }
  }
  if (errorMsg) {
    for (p = addrlist; p != NULL; p = p->ai_next) {
      if (p->ai_addr->sa_family == AF_INET6) {
        memcpy(ip, p->ai_addr, sizeof(struct sockaddr_in6));
        errorMsg = NULL;
        break;
      }
    }
  }
  freeaddrinfo(addrlist);
  return errorMsg;
}


x86 assembly (clang, -O0):
<hostToIP>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x48(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1050 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
je     11b2 <hostToIP+0x72>
lea    0xe57(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    12a8 <hostToIP+0x168>
lea    0xe5a(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1222 <hostToIP+0xe2>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    0x2e07(%rip),%rcx
cmp    (%rcx),%rax
jne    120c <hostToIP+0xcc>
mov    -0x18(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    (%rax),%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
movq   $0x0,-0x48(%rbp)
jmp    1222 <hostToIP+0xe2>
jmp    1211 <hostToIP+0xd1>
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    11c5 <hostToIP+0x85>
cmpq   $0x0,-0x48(%rbp)
je     1297 <hostToIP+0x157>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1292 <hostToIP+0x152>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    0x2d87(%rip),%rcx
cmp    (%rcx),%rax
jne    127c <hostToIP+0x13c>
mov    -0x18(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    (%rax),%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
movq   $0x0,-0x48(%rbp)
jmp    1292 <hostToIP+0x152>
jmp    1281 <hostToIP+0x141>
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    1235 <hostToIP+0xf5>
jmp    1297 <hostToIP+0x157>
mov    -0x38(%rbp),%rdi
call   1060 <freeaddrinfo@plt>
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hostToIP>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
mov    0x2e76(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    %rsp,%rcx
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1050 <getaddrinfo@plt>
test   %eax,%eax
je     119a <hostToIP+0x5a>
lea    0xe68(%rip),%rbx
jmp    120b <hostToIP+0xcb>
mov    (%rsp),%rax
lea    0xe6e(%rip),%rbx
test   %rax,%rax
je     1202 <hostToIP+0xc2>
mov    0x2e37(%rip),%rcx
mov    (%rcx),%rcx
mov    %rax,%rdx
nopw   0x0(%rax,%rax,1)
mov    (%rdx),%rsi
cmp    %rcx,(%rsi)
je     11f3 <hostToIP+0xb3>
mov    0x8(%rdx),%rdx
test   %rdx,%rdx
jne    11c0 <hostToIP+0x80>
test   %rax,%rax
je     1202 <hostToIP+0xc2>
mov    0x2dfb(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rax),%rsi
cmp    %rcx,(%rsi)
je     11f3 <hostToIP+0xb3>
mov    0x8(%rax),%rax
test   %rax,%rax
jne    11e0 <hostToIP+0xa0>
jmp    1202 <hostToIP+0xc2>
mov    %r14,%rdi
mov    $0x4,%edx
call   1040 <memcpy@plt>
xor    %ebx,%ebx
mov    (%rsp),%rdi
call   1060 <freeaddrinfo@plt>
mov    %rbx,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.addrinfo = type { %struct.TYPE_2__*, %struct.addrinfo*, i32, i32 }
%struct.TYPE_2__ = type { i64 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [19 x i8] c"getaddrinfo failed\00"
@.str.1 = [30 x i8] c"no ipv4 or ipv6 address found\00"
@AF_INET = global i64 0
@AF_INET6 = global i64 0
define i8* @hostToIP(i8* %0, %struct.sockaddr* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.sockaddr*
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = alloca %struct.addrinfo*
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %4
store %struct.sockaddr* %1, %struct.sockaddr** %5
store i8* null, i8** %9
%11 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 24)
%12 = load i32, i32* @AF_UNSPEC
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 3
store i32 %12, i32* %13
%14 = load i32, i32* @SOCK_STREAM
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 2
store i32 %14, i32* %15
%16 = load i8*, i8** %4
%17 = call i32 @getaddrinfo(i8* %16, i32* null, %struct.addrinfo* %6, %struct.addrinfo** %7)
store i32 %17, i32* %10
%18 = load i32, i32* %10
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %2
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8** %3
br label %77
21: ; preds = %2
store i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8** %9
%22 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %22, %struct.addrinfo** %8
br label %23
23: ; preds = %41, %21
%24 = load %struct.addrinfo*, %struct.addrinfo** %8
%25 = icmp ne %struct.addrinfo* %24, null
br i1 %25, label %26, label %45
26: ; preds = %23
%27 = load %struct.addrinfo*, %struct.addrinfo** %8
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i32 0, i32 0
%29 = load %struct.TYPE_2__*, %struct.TYPE_2__** %28
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
%31 = load i64, i64* %30
%32 = load i64, i64* @AF_INET
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %40
34: ; preds = %26
%35 = load %struct.sockaddr*, %struct.sockaddr** %5
%36 = load %struct.addrinfo*, %struct.addrinfo** %8
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i32 0, i32 0
%38 = load %struct.TYPE_2__*, %struct.TYPE_2__** %37
%39 = call i32 @memcpy(%struct.sockaddr* %35, %struct.TYPE_2__* %38, i32 4)
store i8* null, i8** %9
br label %45
40: ; preds = %26
br label %41
41: ; preds = %40
%42 = load %struct.addrinfo*, %struct.addrinfo** %8
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %42, i32 0, i32 1
%44 = load %struct.addrinfo*, %struct.addrinfo** %43
store %struct.addrinfo* %44, %struct.addrinfo** %8
br label %23
45: ; preds = %34, %23
%46 = load i8*, i8** %9
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %73
48: ; preds = %45
%49 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %49, %struct.addrinfo** %8
br label %50
50: ; preds = %68, %48
%51 = load %struct.addrinfo*, %struct.addrinfo** %8
%52 = icmp ne %struct.addrinfo* %51, null
br i1 %52, label %53, label %72
53: ; preds = %50
%54 = load %struct.addrinfo*, %struct.addrinfo** %8
%55 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %54, i32 0, i32 0
%56 = load %struct.TYPE_2__*, %struct.TYPE_2__** %55
%57 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %56, i32 0, i32 0
%58 = load i64, i64* %57
%59 = load i64, i64* @AF_INET6
%60 = icmp eq i64 %58, %59
br i1 %60, label %61, label %67
61: ; preds = %53
%62 = load %struct.sockaddr*, %struct.sockaddr** %5
%63 = load %struct.addrinfo*, %struct.addrinfo** %8
%64 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %63, i32 0, i32 0
%65 = load %struct.TYPE_2__*, %struct.TYPE_2__** %64
%66 = call i32 @memcpy(%struct.sockaddr* %62, %struct.TYPE_2__* %65, i32 4)
store i8* null, i8** %9
br label %72
67: ; preds = %53
br label %68
68: ; preds = %67
%69 = load %struct.addrinfo*, %struct.addrinfo** %8
%70 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %69, i32 0, i32 1
%71 = load %struct.addrinfo*, %struct.addrinfo** %70
store %struct.addrinfo* %71, %struct.addrinfo** %8
br label %50
72: ; preds = %61, %50
br label %73
73: ; preds = %72, %45
%74 = load %struct.addrinfo*, %struct.addrinfo** %7
%75 = call i32 @freeaddrinfo(%struct.addrinfo* %74)
%76 = load i8*, i8** %9
store i8* %76, i8** %3
br label %77
77: ; preds = %73, %20
%78 = load i8*, i8** %3
ret i8* %78
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.addrinfo = type { %struct.TYPE_2__*, %struct.addrinfo*, i32, i32 }
%struct.TYPE_2__ = type { i64 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [19 x i8] c"getaddrinfo failed\00"
@.str.1 = [30 x i8] c"no ipv4 or ipv6 address found\00"
@AF_INET = global i64 0
@AF_INET6 = global i64 0
define i8* @hostToIP(i8* %0, %struct.sockaddr* %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = bitcast %struct.addrinfo* %3 to i8*
%6 = bitcast %struct.addrinfo** %4 to i8*
%7 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 24)
%8 = load i32, i32* @AF_UNSPEC
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %8, i32* %9
%10 = load i32, i32* @SOCK_STREAM
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 2
store i32 %10, i32* %11
%12 = call i32 @getaddrinfo(i8* %0, i32* null, %struct.addrinfo* %3, %struct.addrinfo** %4)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %51
14: ; preds = %2
%15 = load %struct.addrinfo*, %struct.addrinfo** %4
%16 = icmp eq %struct.addrinfo* %15, null
br i1 %16, label %47, label %17
17: ; preds = %14
%18 = load i64, i64* @AF_INET
br label %26
19: ; preds = %26
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 1
%21 = load %struct.addrinfo*, %struct.addrinfo** %20
%22 = icmp eq %struct.addrinfo* %21, null
br i1 %22, label %23, label %26
23: ; preds = %19
br i1 %16, label %47, label %24
24: ; preds = %23
%25 = load i64, i64* @AF_INET6
br label %37
26: ; preds = %17, %19
%27 = phi %struct.addrinfo* [ %15, %17 ], [ %21, %19 ]
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 0
%29 = load %struct.TYPE_2__*, %struct.TYPE_2__** %28
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i64 0, i32 0
%31 = load i64, i64* %30
%32 = icmp eq i64 %31, %18
br i1 %32, label %44, label %19
33: ; preds = %37
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i64 0, i32 1
%35 = load %struct.addrinfo*, %struct.addrinfo** %34
%36 = icmp eq %struct.addrinfo* %35, null
br i1 %36, label %47, label %37
37: ; preds = %24, %33
%38 = phi %struct.addrinfo* [ %15, %24 ], [ %35, %33 ]
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i64 0, i32 0
%40 = load %struct.TYPE_2__*, %struct.TYPE_2__** %39
%41 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %40, i64 0, i32 0
%42 = load i64, i64* %41
%43 = icmp eq i64 %42, %25
br i1 %43, label %44, label %33
44: ; preds = %26, %37
%45 = phi %struct.TYPE_2__* [ %40, %37 ], [ %29, %26 ]
%46 = call i32 @memcpy(%struct.sockaddr* %1, %struct.TYPE_2__* %45, i32 4)
br label %47
47: ; preds = %33, %44, %14, %23
%48 = phi i8* [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), %23 ], [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), %14 ], [ null, %44 ], [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), %33 ]
%49 = load %struct.addrinfo*, %struct.addrinfo** %4
%50 = call i32 @freeaddrinfo(%struct.addrinfo* %49)
br label %51
51: ; preds = %2, %47
%52 = phi i8* [ %48, %47 ], [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), %2 ]
ret i8* %52
}