#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 */
typedef int /*<<< orphan*/ uint8_t;
struct sockaddr_in6 {
  int /*<<< orphan*/ sin6_addr;
};
struct addrinfo {
  scalar_t__ ai_addr;
  int /*<<< orphan*/ ai_family;
};
typedef int /*<<< orphan*/ namebuf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
scalar_t__ INET6_ADDRSTRLEN;
scalar_t__ dn_expand(int /*<<< orphan*/ const *, int /*<<< orphan*/ const *,
                     int /*<<< orphan*/ const *, char *, int);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(char *, int /*<<< orphan*/ *, struct addrinfo *,
                       struct addrinfo **);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/ *, char *,
                             scalar_t__);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int /*<<< orphan*/ putenv(char *);
char *realloc(int /*<<< orphan*/ *, scalar_t__);
size_t strlen(char const *);
void fqdn_to_ip_env(const char *name, const uint8_t *fqdn, size_t len) {
  size_t buf_len = strlen(name);
  char *buf = realloc(NULL, INET6_ADDRSTRLEN + buf_len + 3);
  memcpy(buf, name, buf_len);
  buf[buf_len++] = '=';
  char namebuf[256];
  if (dn_expand(fqdn, fqdn + len, fqdn, namebuf, sizeof(namebuf)) <= 0)
    return;
  struct addrinfo hints = {.ai_family = AF_INET6}, *r;
  if (getaddrinfo(namebuf, NULL, &hints, &r))
    return;
  struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)r->ai_addr;
  inet_ntop(AF_INET6, &sin6->sin6_addr, &buf[buf_len], INET6_ADDRSTRLEN);
  freeaddrinfo(r);
  putenv(buf);
}


x86 assembly (clang, -O0):
<fqdn_to_ip_env>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%rsi
add    -0x20(%rbp),%rsi
add    $0x3,%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1080 <realloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
movb   $0x3d,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x10(%rbp),%rdx
lea    -0x130(%rbp),%rcx
mov    $0x100,%r8d
call   1070 <dn_expand@plt>
cmp    $0x0,%rax
jg     1223 <fqdn_to_ip_env+0xa3>
jmp    12b7 <fqdn_to_ip_env+0x137>
movq   $0x0,-0x140(%rbp)
mov    0x2dab(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x138(%rbp)
lea    -0x130(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x140(%rbp),%rdx
lea    -0x148(%rbp),%rcx
call   1090 <getaddrinfo@plt>
cmp    $0x0,%rax
je     126a <fqdn_to_ip_env+0xea>
jmp    12b7 <fqdn_to_ip_env+0x137>
mov    -0x148(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x150(%rbp)
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
mov    -0x150(%rbp),%rsi
mov    -0x28(%rbp),%rdx
add    -0x20(%rbp),%rdx
mov    0x2d4e(%rip),%rax
mov    (%rax),%rcx
call   1030 <inet_ntop@plt>
mov    -0x148(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    -0x28(%rbp),%rdi
call   1050 <putenv@plt>
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fqdn_to_ip_env>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x128,%rsp
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%r13
call   1040 <strlen@plt>
mov    %rax,%r15
mov    0x2e3f(%rip),%rbp
mov    0x0(%rbp),%rax
lea    (%r15,%rax,1),%rsi
add    $0x3,%rsi
xor    %edi,%edi
call   1080 <realloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r13,%rsi
mov    %r15,%rdx
call   1060 <memcpy@plt>
movb   $0x3d,(%r14,%r15,1)
lea    (%rbx,%r12,4),%rsi
lea    0x20(%rsp),%rcx
mov    %rbx,%rdi
mov    %rbx,%rdx
mov    $0x100,%r8d
call   1070 <dn_expand@plt>
test   %rax,%rax
jle    1251 <fqdn_to_ip_env+0xd1>
movq   $0x0,0x10(%rsp)
mov    0x2ddf(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
lea    0x20(%rsp),%rdi
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rcx
xor    %esi,%esi
call   1090 <getaddrinfo@plt>
test   %rax,%rax
jne    1251 <fqdn_to_ip_env+0xd1>
add    $0x1,%r15
mov    0x8(%rsp),%rax
mov    (%rax),%rsi
mov    (%rbx),%edi
add    %r14,%r15
mov    0x0(%rbp),%rcx
mov    %r15,%rdx
call   1030 <inet_ntop@plt>
mov    0x8(%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    %r14,%rdi
call   1050 <putenv@plt>
add    $0x128,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i64, i32 }
%struct.sockaddr_in6 = type { i32 }
@INET6_ADDRSTRLEN = global i64 0
@AF_INET6 = global i32 0
define void @fqdn_to_ip_env(i8* %0, i32* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
%9 = alloca [256 x i8]
%10 = alloca %struct.addrinfo
%11 = alloca %struct.addrinfo*
%12 = alloca %struct.sockaddr_in6*
store i8* %0, i8** %4
store i32* %1, i32** %5
store i64 %2, i64* %6
%13 = load i8*, i8** %4
%14 = call i64 @strlen(i8* %13)
store i64 %14, i64* %7
%15 = load i64, i64* @INET6_ADDRSTRLEN
%16 = load i64, i64* %7
%17 = add i64 %15, %16
%18 = add i64 %17, 3
%19 = call i8* @realloc(i32* null, i64 %18)
store i8* %19, i8** %8
%20 = load i8*, i8** %8
%21 = load i8*, i8** %4
%22 = load i64, i64* %7
%23 = call i32 @memcpy(i8* %20, i8* %21, i64 %22)
%24 = load i8*, i8** %8
%25 = load i64, i64* %7
%26 = add i64 %25, 1
store i64 %26, i64* %7
%27 = getelementptr inbounds i8, i8* %24, i64 %25
store i8 61, i8* %27
%28 = load i32*, i32** %5
%29 = load i32*, i32** %5
%30 = load i64, i64* %6
%31 = getelementptr inbounds i32, i32* %29, i64 %30
%32 = load i32*, i32** %5
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%34 = call i64 @dn_expand(i32* %28, i32* %31, i32* %32, i8* %33, i32 256)
%35 = icmp sle i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %3
br label %62
37: ; preds = %3
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i32 0, i32 0
store i64 0, i64* %38
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i32 0, i32 1
%40 = load i32, i32* @AF_INET6
store i32 %40, i32* %39
%41 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%42 = call i64 @getaddrinfo(i8* %41, i32* null, %struct.addrinfo* %10, %struct.addrinfo** %11)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %37
br label %62
45: ; preds = %37
%46 = load %struct.addrinfo*, %struct.addrinfo** %11
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %46, i32 0, i32 0
%48 = load i64, i64* %47
%49 = inttoptr i64 %48 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %49, %struct.sockaddr_in6** %12
%50 = load i32, i32* @AF_INET6
%51 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %12
%52 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %51, i32 0, i32 0
%53 = load i8*, i8** %8
%54 = load i64, i64* %7
%55 = getelementptr inbounds i8, i8* %53, i64 %54
%56 = load i64, i64* @INET6_ADDRSTRLEN
%57 = call i32 @inet_ntop(i32 %50, i32* %52, i8* %55, i64 %56)
%58 = load %struct.addrinfo*, %struct.addrinfo** %11
%59 = call i32 @freeaddrinfo(%struct.addrinfo* %58)
%60 = load i8*, i8** %8
%61 = call i32 @putenv(i8* %60)
br label %62
62: ; preds = %45, %44, %36
ret void
}

LLVM IR (-O2):
%struct.addrinfo = type { i64, i32 }
%struct.sockaddr_in6 = type { i32 }
@INET6_ADDRSTRLEN = global i64 0
@AF_INET6 = global i32 0
define void @fqdn_to_ip_env(i8* %0, i32* %1, i64 %2) {
%4 = alloca [256 x i8]
%5 = alloca %struct.addrinfo
%6 = alloca %struct.addrinfo*
%7 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%8 = load i64, i64* @INET6_ADDRSTRLEN
%9 = add i64 %7, 3
%10 = add i64 %9, %8
%11 = tail call i8* @realloc(i32* null, i64 %10)
%12 = tail call i32 @memcpy(i8* %11, i8* %0, i64 %7)
%13 = add i64 %7, 1
%14 = getelementptr inbounds i8, i8* %11, i64 %7
store i8 61, i8* %14
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%16 = getelementptr inbounds i32, i32* %1, i64 %2
%17 = call i64 @dn_expand(i32* %1, i32* %16, i32* %1, i8* %15, i32 256)
%18 = icmp slt i64 %17, 1
br i1 %18, label %41, label %19
19: ; preds = %3
%20 = bitcast %struct.addrinfo* %5 to i8*
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 0
store i64 0, i64* %21
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 1
%23 = load i32, i32* @AF_INET6
store i32 %23, i32* %22
%24 = bitcast %struct.addrinfo** %6 to i8*
%25 = call i64 @getaddrinfo(i8* %15, i32* null, %struct.addrinfo* %5, %struct.addrinfo** %6)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %40
27: ; preds = %19
%28 = load %struct.addrinfo*, %struct.addrinfo** %6
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 0
%30 = load i64, i64* %29
%31 = inttoptr i64 %30 to %struct.sockaddr_in6*
%32 = load i32, i32* @AF_INET6
%33 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %31, i64 0, i32 0
%34 = getelementptr inbounds i8, i8* %11, i64 %13
%35 = load i64, i64* @INET6_ADDRSTRLEN
%36 = call i32 @inet_ntop(i32 %32, i32* %33, i8* %34, i64 %35)
%37 = load %struct.addrinfo*, %struct.addrinfo** %6
%38 = call i32 @freeaddrinfo(%struct.addrinfo* %37)
%39 = call i32 @putenv(i8* %11)
br label %40
40: ; preds = %19, %27
br label %41
41: ; preds = %3, %40
ret void
}