#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 addrinfo {
  int ai_addrlen;
  struct addrinfo *ai_addr;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *get_sockname(struct addrinfo *);
scalar_t__ getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
void *malloc(int);
int /*<<< orphan*/ memcpy(struct addrinfo *, struct addrinfo *, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
char *get_ipv6_addr() {
  struct addrinfo hints;
  struct addrinfo *result, *resp;
  static struct addrinfo *cached = NULL;
  if (cached != NULL)
    return get_sockname(cached);
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_INET6;
  if (getaddrinfo("k.root-servers.net", "domain", &hints, &result) != 0) {
    return NULL;
  }
  for (resp = result; resp != NULL; resp = resp->ai_next) {
    char *addr_string = get_sockname(resp);
    if (!addr_string)
      continue;
    if ((cached = malloc(sizeof(struct addrinfo))) == NULL)
      return NULL;
    memcpy(cached, resp, sizeof(struct addrinfo));
    if ((cached->ai_addr = malloc(resp->ai_addrlen)) == NULL) {
      cached = NULL;
      return NULL;
    }
    memcpy(cached->ai_addr, resp->ai_addr, resp->ai_addrlen);
    freeaddrinfo(result);
    return addr_string;
  }
  freeaddrinfo(result);
  return NULL;
}


x86 assembly (clang, -O0):
<get_ipv6_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
cmpq   $0x0,0x2ee8(%rip)
je     118b <get_ipv6_addr+0x2b>
mov    0x2edb(%rip),%rdi
call   1030 <get_sockname@plt>
mov    %rax,-0x8(%rbp)
jmp    12d4 <get_ipv6_addr+0x174>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
lea    0xe52(%rip),%rdi
lea    0xe5e(%rip),%rsi
lea    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1070 <getaddrinfo@plt>
cmp    $0x0,%rax
je     11d9 <get_ipv6_addr+0x79>
movq   $0x0,-0x8(%rbp)
jmp    12d4 <get_ipv6_addr+0x174>
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     12c3 <get_ipv6_addr+0x163>
mov    -0x38(%rbp),%rdi
call   1030 <get_sockname@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1209 <get_ipv6_addr+0xa9>
jmp    12b2 <get_ipv6_addr+0x152>
mov    $0x20,%edi
call   1060 <malloc@plt>
mov    %rax,0x2e3e(%rip)
cmp    $0x0,%rax
jne    1231 <get_ipv6_addr+0xd1>
movq   $0x0,-0x8(%rbp)
jmp    12d4 <get_ipv6_addr+0x174>
mov    0x2e20(%rip),%rdi
mov    -0x38(%rbp),%rsi
mov    $0x20,%edx
call   1050 <memcpy@plt>
mov    -0x38(%rbp),%rax
mov    (%rax),%edi
call   1060 <malloc@plt>
mov    0x2e00(%rip),%rcx
mov    %rax,0x8(%rcx)
cmp    $0x0,%rax
jne    127e <get_ipv6_addr+0x11e>
movq   $0x0,0x2de7(%rip)
movq   $0x0,-0x8(%rbp)
jmp    12d4 <get_ipv6_addr+0x174>
mov    0x2dd3(%rip),%rax
mov    0x8(%rax),%rdi
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
call   1050 <memcpy@plt>
mov    -0x30(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12d4 <get_ipv6_addr+0x174>
mov    -0x38(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x38(%rbp)
jmp    11e1 <get_ipv6_addr+0x81>
mov    -0x30(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ipv6_addr>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    0x2ee9(%rip),%rdi
test   %rdi,%rdi
je     1181 <get_ipv6_addr+0x21>
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <get_sockname@plt>
lea    0x10(%rsp),%rbx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
lea    0xe54(%rip),%rdi
lea    0xe60(%rip),%rsi
lea    0x8(%rsp),%rcx
mov    %rbx,%rdx
call   1070 <getaddrinfo@plt>
test   %rax,%rax
jne    11f7 <get_ipv6_addr+0x97>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     11ed <get_ipv6_addr+0x8d>
nop
mov    %rbx,%rdi
call   1030 <get_sockname@plt>
test   %rax,%rax
jne    1203 <get_ipv6_addr+0xa3>
mov    0x10(%rbx),%rbx
test   %rbx,%rbx
jne    11d0 <get_ipv6_addr+0x70>
mov    0x8(%rsp),%rdi
jmp    11ef <get_ipv6_addr+0x8f>
xor    %edi,%edi
call   1080 <freeaddrinfo@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %rax,%r14
mov    $0x20,%edi
call   1060 <malloc@plt>
mov    %rax,0x2e41(%rip)
test   %rax,%rax
je     11f4 <get_ipv6_addr+0x94>
mov    %rax,%rdi
mov    %rbx,%rsi
mov    $0x20,%edx
call   1050 <memcpy@plt>
mov    (%rbx),%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
mov    0x2e1c(%rip),%rcx
mov    %rax,0x8(%rcx)
test   %rax,%rax
je     125f <get_ipv6_addr+0xff>
mov    0x8(%rbx),%rsi
mov    %rax,%rdi
mov    %ebp,%edx
call   1050 <memcpy@plt>
mov    0x8(%rsp),%rdi
call   1080 <freeaddrinfo@plt>
jmp    11f7 <get_ipv6_addr+0x97>
movq   $0x0,0x2dee(%rip)
jmp    11f4 <get_ipv6_addr+0x94>

LLVM IR (-O0):
%struct.addrinfo = type { i32, %struct.addrinfo*, %struct.addrinfo*, i32 }
@get_ipv6_addr.cached = internal global %struct.addrinfo* null
@AF_INET6 = global i32 0
@.str = [19 x i8] c"k.root-servers.net\00"
@.str.1 = [7 x i8] c"domain\00"
define i8* @get_ipv6_addr() {
%1 = alloca i8*
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = alloca %struct.addrinfo*
%5 = alloca i8*
%6 = load %struct.addrinfo*, %struct.addrinfo** @get_ipv6_addr.cached
%7 = icmp ne %struct.addrinfo* %6, null
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = load %struct.addrinfo*, %struct.addrinfo** @get_ipv6_addr.cached
%10 = call i8* @get_sockname(%struct.addrinfo* %9)
store i8* %10, i8** %1
br label %68
11: ; preds = %0
%12 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 32)
%13 = load i32, i32* @AF_INET6
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i32 0, i32 3
store i32 %13, i32* %14
%15 = call i64 @getaddrinfo(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* %2, %struct.addrinfo** %3)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %11
store i8* null, i8** %1
br label %68
18: ; preds = %11
%19 = load %struct.addrinfo*, %struct.addrinfo** %3
store %struct.addrinfo* %19, %struct.addrinfo** %4
br label %20
20: ; preds = %61, %18
%21 = load %struct.addrinfo*, %struct.addrinfo** %4
%22 = icmp ne %struct.addrinfo* %21, null
br i1 %22, label %23, label %65
23: ; preds = %20
%24 = load %struct.addrinfo*, %struct.addrinfo** %4
%25 = call i8* @get_sockname(%struct.addrinfo* %24)
store i8* %25, i8** %5
%26 = load i8*, i8** %5
%27 = icmp ne i8* %26, null
br i1 %27, label %29, label %28
28: ; preds = %23
br label %61
29: ; preds = %23
%30 = call i8* @malloc(i32 32)
%31 = bitcast i8* %30 to %struct.addrinfo*
store %struct.addrinfo* %31, %struct.addrinfo** @get_ipv6_addr.cached
%32 = icmp eq %struct.addrinfo* %31, null
br i1 %32, label %33, label %34
33: ; preds = %29
store i8* null, i8** %1
br label %68
34: ; preds = %29
%35 = load %struct.addrinfo*, %struct.addrinfo** @get_ipv6_addr.cached
%36 = load %struct.addrinfo*, %struct.addrinfo** %4
%37 = call i32 @memcpy(%struct.addrinfo* %35, %struct.addrinfo* %36, i32 32)
%38 = load %struct.addrinfo*, %struct.addrinfo** %4
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 0
%40 = load i32, i32* %39
%41 = call i8* @malloc(i32 %40)
%42 = bitcast i8* %41 to %struct.addrinfo*
%43 = load %struct.addrinfo*, %struct.addrinfo** @get_ipv6_addr.cached
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i32 0, i32 1
store %struct.addrinfo* %42, %struct.addrinfo** %44
%45 = icmp eq %struct.addrinfo* %42, null
br i1 %45, label %46, label %47
46: ; preds = %34
store %struct.addrinfo* null, %struct.addrinfo** @get_ipv6_addr.cached
store i8* null, i8** %1
br label %68
47: ; preds = %34
%48 = load %struct.addrinfo*, %struct.addrinfo** @get_ipv6_addr.cached
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i32 0, i32 1
%50 = load %struct.addrinfo*, %struct.addrinfo** %49
%51 = load %struct.addrinfo*, %struct.addrinfo** %4
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i32 0, i32 1
%53 = load %struct.addrinfo*, %struct.addrinfo** %52
%54 = load %struct.addrinfo*, %struct.addrinfo** %4
%55 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %54, i32 0, i32 0
%56 = load i32, i32* %55
%57 = call i32 @memcpy(%struct.addrinfo* %50, %struct.addrinfo* %53, i32 %56)
%58 = load %struct.addrinfo*, %struct.addrinfo** %3
%59 = call i32 @freeaddrinfo(%struct.addrinfo* %58)
%60 = load i8*, i8** %5
store i8* %60, i8** %1
br label %68
61: ; preds = %28
%62 = load %struct.addrinfo*, %struct.addrinfo** %4
%63 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %62, i32 0, i32 2
%64 = load %struct.addrinfo*, %struct.addrinfo** %63
store %struct.addrinfo* %64, %struct.addrinfo** %4
br label %20
65: ; preds = %20
%66 = load %struct.addrinfo*, %struct.addrinfo** %3
%67 = call i32 @freeaddrinfo(%struct.addrinfo* %66)
store i8* null, i8** %1
br label %68
68: ; preds = %65, %47, %46, %33, %17, %8
%69 = load i8*, i8** %1
ret i8* %69
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, %struct.addrinfo*, %struct.addrinfo*, i32 }
@get_ipv6_addr.cached = internal unnamed_addr global %struct.addrinfo* null
@AF_INET6 = global i32 0
@.str = [19 x i8] c"k.root-servers.net\00"
@.str.1 = [7 x i8] c"domain\00"
define i8* @get_ipv6_addr() {
%1 = alloca %struct.addrinfo
%2 = alloca %struct.addrinfo*
%3 = bitcast %struct.addrinfo* %1 to i8*
%4 = bitcast %struct.addrinfo** %2 to i8*
%5 = load %struct.addrinfo*, %struct.addrinfo** @get_ipv6_addr.cached
%6 = icmp eq %struct.addrinfo* %5, null
br i1 %6, label %9, label %7
7: ; preds = %0
%8 = tail call i8* @get_sockname(%struct.addrinfo* %5)
br label %52
9: ; preds = %0
%10 = call i32 @memset(%struct.addrinfo* %1, i32 0, i32 32)
%11 = load i32, i32* @AF_INET6
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 3
store i32 %11, i32* %12
%13 = call i64 @getaddrinfo(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* %1, %struct.addrinfo** %2)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %52
15: ; preds = %9
%16 = load %struct.addrinfo*, %struct.addrinfo** %2
%17 = icmp eq %struct.addrinfo* %16, null
br i1 %17, label %49, label %18
18: ; preds = %15, %43
%19 = phi %struct.addrinfo* [ %45, %43 ], [ %16, %15 ]
%20 = call i8* @get_sockname(%struct.addrinfo* %19)
%21 = icmp eq i8* %20, null
br i1 %21, label %43, label %22
22: ; preds = %18
%23 = call dereferenceable_or_null(32) i8* @malloc(i32 32)
store i8* %23, i8** bitcast (%struct.addrinfo** @get_ipv6_addr.cached to i8**)
%24 = icmp eq i8* %23, null
br i1 %24, label %52, label %25
25: ; preds = %22
%26 = bitcast i8* %23 to %struct.addrinfo*
%27 = call i32 @memcpy(%struct.addrinfo* %26, %struct.addrinfo* %19, i32 32)
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i64 0, i32 0
%29 = load i32, i32* %28
%30 = call i8* @malloc(i32 %29)
%31 = load %struct.addrinfo*, %struct.addrinfo** @get_ipv6_addr.cached
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i64 0, i32 1
%33 = bitcast %struct.addrinfo** %32 to i8**
store i8* %30, i8** %33
%34 = icmp eq i8* %30, null
br i1 %34, label %35, label %36
35: ; preds = %25
store %struct.addrinfo* null, %struct.addrinfo** @get_ipv6_addr.cached
br label %52
36: ; preds = %25
%37 = bitcast i8* %30 to %struct.addrinfo*
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i64 0, i32 1
%39 = load %struct.addrinfo*, %struct.addrinfo** %38
%40 = call i32 @memcpy(%struct.addrinfo* %37, %struct.addrinfo* %39, i32 %29)
%41 = load %struct.addrinfo*, %struct.addrinfo** %2
%42 = call i32 @freeaddrinfo(%struct.addrinfo* %41)
br label %52
43: ; preds = %18
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i64 0, i32 2
%45 = load %struct.addrinfo*, %struct.addrinfo** %44
%46 = icmp eq %struct.addrinfo* %45, null
br i1 %46, label %47, label %18
47: ; preds = %43
%48 = load %struct.addrinfo*, %struct.addrinfo** %2
br label %49
49: ; preds = %47, %15
%50 = phi %struct.addrinfo* [ %48, %47 ], [ null, %15 ]
%51 = call i32 @freeaddrinfo(%struct.addrinfo* %50)
br label %52
52: ; preds = %22, %35, %36, %9, %49, %7
%53 = phi i8* [ %8, %7 ], [ null, %49 ], [ null, %9 ], [ null, %22 ], [ null, %35 ], [ %20, %36 ]
ret i8* %53
}