#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 */

/* Type definitions */
struct sockaddr_storage {
  int dummy;
};
struct sockaddr_in6 {
  int dummy;
};
struct sockaddr_in {
  int dummy;
};
struct addrinfo {
  scalar_t__ ai_family;
  int /*<<< orphan*/ ai_addr;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_socktype;
};
typedef scalar_t__ sa_family_t;

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
int EINVAL;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char *, int /*<<< orphan*/ *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memcpy(struct sockaddr_storage *, int /*<<< orphan*/, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int name_to_ip(char *remote_name, struct sockaddr_storage *server_addr,
               sa_family_t ip_family) {
  struct addrinfo hints, *res, *p;
  int rc;
  if (server_addr == NULL)
    return EINVAL;
  if (remote_name == NULL)
    return EINVAL;
  if (ip_family != AF_INET && ip_family != AF_INET6)
    return EINVAL;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = ip_family;
  hints.ai_socktype = SOCK_STREAM;
  if ((rc = getaddrinfo(remote_name, NULL, &hints, &res)) == 0) {
    for (p = res; p != NULL; p = p->ai_next) {
      if (p->ai_family == ip_family) {
        if (ip_family == AF_INET)
          memcpy(server_addr, p->ai_addr, sizeof(struct sockaddr_in));
        else
          memcpy(server_addr, p->ai_addr, sizeof(struct sockaddr_in6));
        break;
      }
    }
    freeaddrinfo(res);
  } else
    return EINVAL;
  return 0;
}


x86 assembly (clang, -O0):
<name_to_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1170 <name_to_ip+0x30>
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12b2 <name_to_ip+0x172>
cmpq   $0x0,-0x10(%rbp)
jne    118c <name_to_ip+0x4c>
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12b2 <name_to_ip+0x172>
mov    -0x20(%rbp),%rax
mov    0x2e51(%rip),%rcx
cmp    (%rcx),%rax
je     11c5 <name_to_ip+0x85>
mov    -0x20(%rbp),%rax
mov    0x2e35(%rip),%rcx
cmp    (%rcx),%rax
je     11c5 <name_to_ip+0x85>
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12b2 <name_to_ip+0x172>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1050 <getaddrinfo@plt>
mov    %eax,-0x54(%rbp)
cmp    $0x0,%eax
jne    129a <name_to_ip+0x15a>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     128c <name_to_ip+0x14c>
mov    -0x50(%rbp),%rax
mov    (%rax),%rax
cmp    -0x20(%rbp),%rax
jne    1276 <name_to_ip+0x136>
mov    -0x20(%rbp),%rax
mov    0x2daf(%rip),%rcx
cmp    (%rcx),%rax
jne    125c <name_to_ip+0x11c>
mov    -0x18(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%esi
mov    $0x4,%edx
call   1040 <memcpy@plt>
jmp    1271 <name_to_ip+0x131>
mov    -0x18(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%esi
mov    $0x4,%edx
call   1040 <memcpy@plt>
jmp    128c <name_to_ip+0x14c>
jmp    127b <name_to_ip+0x13b>
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    1212 <name_to_ip+0xd2>
mov    -0x48(%rbp),%rdi
call   1060 <freeaddrinfo@plt>
jmp    12ab <name_to_ip+0x16b>
mov    0x2d27(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12b2 <name_to_ip+0x172>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<name_to_ip>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
test   %rsi,%rsi
je     11b0 <name_to_ip+0x70>
mov    %rdi,%rbx
test   %rdi,%rdi
je     11b0 <name_to_ip+0x70>
mov    %rdx,%r15
mov    %rsi,%r14
mov    0x2e83(%rip),%rax
cmp    %rdx,(%rax)
je     1176 <name_to_ip+0x36>
mov    0x2e6f(%rip),%rax
cmp    %r15,(%rax)
jne    11b0 <name_to_ip+0x70>
lea    0x8(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    %r15,0x8(%rsp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    %rsp,%rcx
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r12,%rdx
call   1050 <getaddrinfo@plt>
test   %eax,%eax
je     11c5 <name_to_ip+0x85>
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    (%rsp),%rax
test   %rax,%rax
je     11f0 <name_to_ip+0xb0>
xchg   %ax,%ax
cmp    %r15,(%rax)
je     11e0 <name_to_ip+0xa0>
mov    0x10(%rax),%rax
test   %rax,%rax
jne    11d0 <name_to_ip+0x90>
jmp    11f0 <name_to_ip+0xb0>
mov    0x8(%rax),%esi
mov    %r14,%rdi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    (%rsp),%rdi
call   1060 <freeaddrinfo@plt>
xor    %eax,%eax
jmp    11b9 <name_to_ip+0x79>

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

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