#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_storage {
  int dummy;
};
struct sockaddr_in6 {
  int dummy;
};
struct sockaddr_in {
  int dummy;
};
struct addrinfo {
  int ai_family;
  scalar_t__ ai_addrlen;
  TYPE_1__ *ai_addr;
};
typedef int /*<<< orphan*/ ai;
struct TYPE_2__ {
  int sa_family;
};

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(char const *, int /*<<< orphan*/ *, struct addrinfo *,
                       struct addrinfo **);
int /*<<< orphan*/ memcpy(struct sockaddr_storage *, TYPE_1__ *, scalar_t__);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int get_addr(const char *hostname, int family, struct sockaddr_storage *addr) {
  struct addrinfo *res;
  size_t len;
  struct addrinfo ai;
  memset(&ai, 0, sizeof(ai));
  ai.ai_family = family;
  if (getaddrinfo(hostname, NULL, &ai, &res) != 0)
    return -1;
  switch (res->ai_addr->sa_family) {
  case AF_INET:
    len = sizeof(struct sockaddr_in);
    break;
  case AF_INET6:
    len = sizeof(struct sockaddr_in6);
    break;
  default:
    goto out_fail;
  }
  if (len < (size_t)res->ai_addrlen)
    goto out_fail;
  memcpy(addr, res->ai_addr, res->ai_addrlen);
  freeaddrinfo(res);
  return 0;
out_fail:
  freeaddrinfo(res);
  return -1;
}


x86 assembly (clang, -O0):
<get_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x48(%rbp),%rdx
lea    -0x28(%rbp),%rcx
call   1050 <getaddrinfo@plt>
cmp    $0x0,%rax
je     1194 <get_addr+0x54>
movl   $0xffffffff,-0x4(%rbp)
jmp    1238 <get_addr+0xf8>
mov    -0x28(%rbp),%rax
mov    0x10(%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
sub    $0x80,%eax
je     11d1 <get_addr+0x91>
jmp    11b1 <get_addr+0x71>
mov    -0x4c(%rbp),%eax
sub    $0x81,%eax
jne    11de <get_addr+0x9e>
jmp    11c4 <get_addr+0x84>
movq   $0x4,-0x30(%rbp)
jmp    11e3 <get_addr+0xa3>
movq   $0x4,-0x30(%rbp)
jmp    11e3 <get_addr+0xa3>
jmp    1228 <get_addr+0xe8>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
cmp    0x8(%rcx),%rax
jae    11fa <get_addr+0xba>
jmp    1228 <get_addr+0xe8>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    0x10(%rax),%rsi
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rdx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rdi
call   1060 <freeaddrinfo@plt>
movl   $0x0,-0x4(%rbp)
jmp    1238 <get_addr+0xf8>
mov    -0x28(%rbp),%rdi
call   1060 <freeaddrinfo@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_addr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
mov    %ebp,0x10(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1050 <getaddrinfo@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    11c0 <get_addr+0x80>
mov    0x8(%rsp),%rdi
mov    0x10(%rdi),%rsi
mov    (%rsi),%eax
and    $0xfffffffe,%eax
cmp    $0x80,%eax
jne    11a5 <get_addr+0x65>
mov    0x8(%rdi),%rdx
cmp    $0x4,%rdx
jbe    11ac <get_addr+0x6c>
call   1060 <freeaddrinfo@plt>
jmp    11c0 <get_addr+0x80>
mov    %r14,%rdi
call   1040 <memcpy@plt>
mov    0x8(%rsp),%rdi
call   1060 <freeaddrinfo@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.addrinfo = type { i32, i64, %struct.TYPE_2__* }
%struct.TYPE_2__ = type { i32 }
define i32 @get_addr(i8* %0, i32 %1, %struct.sockaddr_storage* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.sockaddr_storage*
%8 = alloca %struct.addrinfo*
%9 = alloca i64
%10 = alloca %struct.addrinfo
store i8* %0, i8** %5
store i32 %1, i32* %6
store %struct.sockaddr_storage* %2, %struct.sockaddr_storage** %7
%11 = call i32 @memset(%struct.addrinfo* %10, i32 0, i32 24)
%12 = load i32, i32* %6
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i32 0, i32 0
store i32 %12, i32* %13
%14 = load i8*, i8** %5
%15 = call i64 @getaddrinfo(i8* %14, i32* null, %struct.addrinfo* %10, %struct.addrinfo** %8)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %3
store i32 -1, i32* %4
br label %48
18: ; preds = %3
%19 = load %struct.addrinfo*, %struct.addrinfo** %8
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i32 0, i32 2
%21 = load %struct.TYPE_2__*, %struct.TYPE_2__** %20
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 0
%23 = load i32, i32* %22
switch i32 %23, label %26 [
i32 129, label %24
i32 128, label %25
]
24: ; preds = %18
store i64 4, i64* %9
br label %27
25: ; preds = %18
store i64 4, i64* %9
br label %27
26: ; preds = %18
br label %45
27: ; preds = %25, %24
%28 = load i64, i64* %9
%29 = load %struct.addrinfo*, %struct.addrinfo** %8
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i32 0, i32 1
%31 = load i64, i64* %30
%32 = icmp ult i64 %28, %31
br i1 %32, label %33, label %34
33: ; preds = %27
br label %45
34: ; preds = %27
%35 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %7
%36 = load %struct.addrinfo*, %struct.addrinfo** %8
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i32 0, i32 2
%38 = load %struct.TYPE_2__*, %struct.TYPE_2__** %37
%39 = load %struct.addrinfo*, %struct.addrinfo** %8
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i32 0, i32 1
%41 = load i64, i64* %40
%42 = call i32 @memcpy(%struct.sockaddr_storage* %35, %struct.TYPE_2__* %38, i64 %41)
%43 = load %struct.addrinfo*, %struct.addrinfo** %8
%44 = call i32 @freeaddrinfo(%struct.addrinfo* %43)
store i32 0, i32* %4
br label %48
45: ; preds = %33, %26
%46 = load %struct.addrinfo*, %struct.addrinfo** %8
%47 = call i32 @freeaddrinfo(%struct.addrinfo* %46)
store i32 -1, i32* %4
br label %48
48: ; preds = %45, %34, %17
%49 = load i32, i32* %4
ret i32 %49
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.addrinfo = type { i32, i64, %struct.TYPE_2__* }
%struct.TYPE_2__ = type { i32 }
define i32 @get_addr(i8* %0, i32 %1, %struct.sockaddr_storage* %2) {
%4 = alloca %struct.addrinfo*
%5 = alloca %struct.addrinfo
%6 = bitcast %struct.addrinfo** %4 to i8*
%7 = bitcast %struct.addrinfo* %5 to i8*
%8 = call i32 @memset(%struct.addrinfo* %5, i32 0, i32 24)
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 0
store i32 %1, i32* %9
%10 = call i64 @getaddrinfo(i8* %0, i32* null, %struct.addrinfo* %5, %struct.addrinfo** %4)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %30
12: ; preds = %3
%13 = load %struct.addrinfo*, %struct.addrinfo** %4
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i64 0, i32 2
%15 = load %struct.TYPE_2__*, %struct.TYPE_2__** %14
%16 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %15, i64 0, i32 0
%17 = load i32, i32* %16
%18 = and i32 %17, -2
%19 = icmp eq i32 %18, 128
br i1 %19, label %20, label %28
20: ; preds = %12
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i64 0, i32 1
%22 = load i64, i64* %21
%23 = icmp ugt i64 %22, 4
br i1 %23, label %28, label %24
24: ; preds = %20
%25 = call i32 @memcpy(%struct.sockaddr_storage* %2, %struct.TYPE_2__* %15, i64 %22)
%26 = load %struct.addrinfo*, %struct.addrinfo** %4
%27 = call i32 @freeaddrinfo(%struct.addrinfo* %26)
br label %30
28: ; preds = %12, %20
%29 = call i32 @freeaddrinfo(%struct.addrinfo* %13)
br label %30
30: ; preds = %3, %28, %24
%31 = phi i32 [ -1, %28 ], [ 0, %24 ], [ -1, %3 ]
ret i32 %31
}