#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_in6 {
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct addrinfo {
  int ai_family;
  scalar_t__ ai_addr;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_socktype;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int AF_UNSPEC;
int /*<<< orphan*/ INET6_ADDRSTRLEN;
int /*<<< orphan*/ INET_ADDRSTRLEN;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char const *, int /*<<< orphan*/ *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ inet_ntop(int, int /*<<< orphan*/ *, char *,
                             int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int get_address(const char *hostname, char *ipv4, char *ipv6) {
  struct addrinfo *result, *res;
  struct addrinfo hints;
  int err;
  if (ipv4)
    *ipv4 = 0;
  if (ipv6)
    *ipv6 = 0;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  err = getaddrinfo(hostname, NULL, &hints, &result);
  if (err)
    return err;
  for (res = result; res; res = res->ai_next)
    switch (res->ai_family) {
    case AF_INET:
      if (ipv4) {
        struct sockaddr_in *sin = (struct sockaddr_in *)res->ai_addr;
        inet_ntop(res->ai_family, &sin->sin_addr, ipv4, INET_ADDRSTRLEN);
      }
      break;
    case AF_INET6:
      if (ipv6) {
        struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)res->ai_addr;
        inet_ntop(res->ai_family, &sin6->sin6_addr, ipv6, INET6_ADDRSTRLEN);
      }
      break;
    }
  freeaddrinfo(result);
  return 0;
}


x86 assembly (clang, -O0):
<get_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1166 <get_address+0x26>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
cmpq   $0x0,-0x20(%rbp)
je     1178 <get_address+0x38>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x50(%rbp),%rdx
lea    -0x28(%rbp),%rcx
call   1050 <getaddrinfo@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
je     11cd <get_address+0x8d>
mov    -0x54(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12a2 <get_address+0x162>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1292 <get_address+0x152>
mov    -0x30(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
sub    $0x80,%eax
je     1244 <get_address+0x104>
jmp    11f9 <get_address+0xb9>
mov    -0x6c(%rbp),%eax
sub    $0x81,%eax
jne    127c <get_address+0x13c>
jmp    120c <get_address+0xcc>
cmpq   $0x0,-0x18(%rbp)
je     123f <get_address+0xff>
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x60(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%edi
mov    -0x60(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    0x2d90(%rip),%rax
mov    (%rax),%ecx
call   1030 <inet_ntop@plt>
jmp    127c <get_address+0x13c>
cmpq   $0x0,-0x20(%rbp)
je     1277 <get_address+0x137>
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x68(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%edi
mov    -0x68(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    0x2d78(%rip),%rax
mov    (%rax),%ecx
call   1030 <inet_ntop@plt>
jmp    127c <get_address+0x13c>
jmp    1281 <get_address+0x141>
mov    -0x30(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    11d5 <get_address+0x95>
mov    -0x28(%rbp),%rdi
call   1060 <freeaddrinfo@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_address>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r14
mov    %rsi,%r13
mov    %rdi,%r15
test   %rsi,%rsi
je     1161 <get_address+0x21>
movb   $0x0,0x0(%r13)
test   %r14,%r14
je     116a <get_address+0x2a>
movb   $0x0,(%r14)
lea    0x8(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    %rsp,%rcx
mov    %r15,%rdi
xor    %esi,%esi
mov    %r12,%rdx
call   1050 <getaddrinfo@plt>
mov    %eax,%r15d
test   %eax,%eax
jne    1234 <get_address+0xf4>
mov    (%rsp),%rbx
xor    %r15d,%r15d
test   %rbx,%rbx
je     122d <get_address+0xed>
mov    0x2e22(%rip),%r12
mov    0x2dfb(%rip),%rbp
jmp    11de <get_address+0x9e>
nop
call   1030 <inet_ntop@plt>
mov    0x10(%rbx),%rbx
test   %rbx,%rbx
je     1227 <get_address+0xe7>
mov    (%rbx),%eax
cmp    $0x80,%eax
je     1210 <get_address+0xd0>
cmp    $0x81,%eax
jne    11d5 <get_address+0x95>
test   %r13,%r13
je     11d5 <get_address+0x95>
mov    0x8(%rbx),%rsi
mov    0x0(%rbp),%ecx
mov    $0x81,%edi
mov    %r13,%rdx
jmp    11d0 <get_address+0x90>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
test   %r14,%r14
je     11d5 <get_address+0x95>
mov    0x8(%rbx),%rsi
mov    (%r12),%ecx
mov    $0x80,%edi
mov    %r14,%rdx
jmp    11d0 <get_address+0x90>
mov    (%rsp),%rdi
jmp    122f <get_address+0xef>
xor    %edi,%edi
call   1060 <freeaddrinfo@plt>
mov    %r15d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i64, %struct.addrinfo*, i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@INET_ADDRSTRLEN = global i32 0
@INET6_ADDRSTRLEN = global i32 0
define i32 @get_address(i8* %0, i8* %1, i8* %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 i8* %1, null
br i1 %8, label %10, label %9
9: ; preds = %3
store i8 0, i8* %1
br label %10
10: ; preds = %9, %3
%11 = icmp eq i8* %2, null
br i1 %11, label %13, label %12
12: ; preds = %10
store i8 0, i8* %2
br label %13
13: ; preds = %12, %10
%14 = call i32 @memset(%struct.addrinfo* %5, i32 0, i32 32)
%15 = load i32, i32* @AF_UNSPEC
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 0
store i32 %15, i32* %16
%17 = load i32, i32* @SOCK_STREAM
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 3
store i32 %17, i32* %18
%19 = call i32 @getaddrinfo(i8* %0, i32* null, %struct.addrinfo* %5, %struct.addrinfo** %4)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %53
21: ; preds = %13
%22 = load %struct.addrinfo*, %struct.addrinfo** %4
%23 = icmp eq %struct.addrinfo* %22, null
br i1 %23, label %50, label %24
24: ; preds = %21, %44
%25 = phi %struct.addrinfo* [ %46, %44 ], [ %22, %21 ]
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 0
%27 = load i32, i32* %26
switch i32 %27, label %44 [
i32 129, label %28
i32 128, label %36
]
28: ; preds = %24
br i1 %8, label %44, label %29
29: ; preds = %28
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 1
%31 = load i64, i64* %30
%32 = inttoptr i64 %31 to %struct.sockaddr_in*
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %32, i64 0, i32 0
%34 = load i32, i32* @INET_ADDRSTRLEN
%35 = call i32 @inet_ntop(i32 129, i32* %33, i8* %1, i32 %34)
br label %44
36: ; preds = %24
br i1 %11, label %44, label %37
37: ; preds = %36
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 1
%39 = load i64, i64* %38
%40 = inttoptr i64 %39 to %struct.sockaddr_in6*
%41 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %40, i64 0, i32 0
%42 = load i32, i32* @INET6_ADDRSTRLEN
%43 = call i32 @inet_ntop(i32 128, i32* %41, i8* %2, i32 %42)
br label %44
44: ; preds = %24, %29, %28, %37, %36
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 2
%46 = load %struct.addrinfo*, %struct.addrinfo** %45
%47 = icmp eq %struct.addrinfo* %46, null
br i1 %47, label %48, label %24
48: ; preds = %44
%49 = load %struct.addrinfo*, %struct.addrinfo** %4
br label %50
50: ; preds = %48, %21
%51 = phi %struct.addrinfo* [ %49, %48 ], [ null, %21 ]
%52 = call i32 @freeaddrinfo(%struct.addrinfo* %51)
br label %53
53: ; preds = %13, %50
%54 = phi i32 [ 0, %50 ], [ %19, %13 ]
ret i32 %54
}