#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 in6_addr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
size_t INET6_ADDRSTRLEN;
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, struct in6_addr const *,
                             char *, size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int /*<<< orphan*/ putenv(char *);
char *realloc(int /*<<< orphan*/ *, size_t);
size_t strlen(char const *);
void ipv6_to_env(const char *name, const struct in6_addr *addr, size_t cnt) {
  size_t buf_len = strlen(name);
  char *buf = realloc(NULL, cnt * INET6_ADDRSTRLEN + buf_len + 2);
  memcpy(buf, name, buf_len);
  buf[buf_len++] = '=';
  for (size_t i = 0; i < cnt; ++i) {
    inet_ntop(AF_INET6, &addr[i], &buf[buf_len], INET6_ADDRSTRLEN);
    buf_len += strlen(&buf[buf_len]);
    buf[buf_len++] = ' ';
  }
  if (buf[buf_len - 1] == ' ')
    buf_len--;
  buf[buf_len] = '\0';
  putenv(buf);
}


x86 assembly (clang, -O0):
<ipv6_to_env>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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    -0x18(%rbp),%rsi
mov    0x2e6c(%rip),%rax
imul   (%rax),%rsi
add    -0x20(%rbp),%rsi
add    $0x2,%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1070 <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)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    123f <ipv6_to_env+0xef>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x28(%rbp),%rdx
add    -0x20(%rbp),%rdx
mov    0x2dee(%rip),%rax
mov    (%rax),%rcx
call   1030 <inet_ntop@plt>
mov    -0x28(%rbp),%rdi
add    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
movb   $0x20,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    11c5 <ipv6_to_env+0x75>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x20,%eax
jne    1264 <ipv6_to_env+0x114>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   1050 <putenv@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipv6_to_env>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r13
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    0x2e75(%rip),%rbp
mov    0x0(%rbp),%rax
imul   %r14,%rax
lea    (%rbx,%rax,1),%rsi
add    $0x2,%rsi
xor    %edi,%edi
call   1070 <realloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1060 <memcpy@plt>
movb   $0x3d,(%r12,%rbx,1)
add    $0x1,%rbx
test   %r14,%r14
je     11f0 <ipv6_to_env+0xa0>
nopl   0x0(%rax)
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
lea    (%r12,%rbx,1),%r13
mov    0x0(%rbp),%rcx
mov    %r15,%rsi
mov    %r13,%rdx
call   1030 <inet_ntop@plt>
mov    %r13,%rdi
call   1040 <strlen@plt>
lea    (%rax,%rbx,1),%rcx
movb   $0x20,(%r12,%rcx,1)
add    %rax,%rbx
add    $0x1,%rbx
add    $0x4,%r15
add    $0xffffffffffffffff,%r14
jne    11b0 <ipv6_to_env+0x60>
jmp    11f3 <ipv6_to_env+0xa3>
mov    %rbx,%rcx
movb   $0x0,(%r12,%rcx,1)
mov    %r12,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <putenv@plt>

LLVM IR (-O0):
%struct.in6_addr = type { i32 }
@INET6_ADDRSTRLEN = global i64 0
@AF_INET6 = global i32 0
define void @ipv6_to_env(i8* %0, %struct.in6_addr* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca %struct.in6_addr*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
store i8* %0, i8** %4
store %struct.in6_addr* %1, %struct.in6_addr** %5
store i64 %2, i64* %6
%10 = load i8*, i8** %4
%11 = call i64 @strlen(i8* %10)
store i64 %11, i64* %7
%12 = load i64, i64* %6
%13 = load i64, i64* @INET6_ADDRSTRLEN
%14 = mul i64 %12, %13
%15 = load i64, i64* %7
%16 = add i64 %14, %15
%17 = add i64 %16, 2
%18 = call i8* @realloc(i32* null, i64 %17)
store i8* %18, i8** %8
%19 = load i8*, i8** %8
%20 = load i8*, i8** %4
%21 = load i64, i64* %7
%22 = call i32 @memcpy(i8* %19, i8* %20, i64 %21)
%23 = load i8*, i8** %8
%24 = load i64, i64* %7
%25 = add i64 %24, 1
store i64 %25, i64* %7
%26 = getelementptr inbounds i8, i8* %23, i64 %24
store i8 61, i8* %26
store i64 0, i64* %9
br label %27
27: ; preds = %51, %3
%28 = load i64, i64* %9
%29 = load i64, i64* %6
%30 = icmp ult i64 %28, %29
br i1 %30, label %31, label %54
31: ; preds = %27
%32 = load i32, i32* @AF_INET6
%33 = load %struct.in6_addr*, %struct.in6_addr** %5
%34 = load i64, i64* %9
%35 = getelementptr inbounds %struct.in6_addr, %struct.in6_addr* %33, i64 %34
%36 = load i8*, i8** %8
%37 = load i64, i64* %7
%38 = getelementptr inbounds i8, i8* %36, i64 %37
%39 = load i64, i64* @INET6_ADDRSTRLEN
%40 = call i32 @inet_ntop(i32 %32, %struct.in6_addr* %35, i8* %38, i64 %39)
%41 = load i8*, i8** %8
%42 = load i64, i64* %7
%43 = getelementptr inbounds i8, i8* %41, i64 %42
%44 = call i64 @strlen(i8* %43)
%45 = load i64, i64* %7
%46 = add i64 %45, %44
store i64 %46, i64* %7
%47 = load i8*, i8** %8
%48 = load i64, i64* %7
%49 = add i64 %48, 1
store i64 %49, i64* %7
%50 = getelementptr inbounds i8, i8* %47, i64 %48
store i8 32, i8* %50
br label %51
51: ; preds = %31
%52 = load i64, i64* %9
%53 = add i64 %52, 1
store i64 %53, i64* %9
br label %27
54: ; preds = %27
%55 = load i8*, i8** %8
%56 = load i64, i64* %7
%57 = sub i64 %56, 1
%58 = getelementptr inbounds i8, i8* %55, i64 %57
%59 = load i8, i8* %58
%60 = sext i8 %59 to i32
%61 = icmp eq i32 %60, 32
br i1 %61, label %62, label %65
62: ; preds = %54
%63 = load i64, i64* %7
%64 = add i64 %63, -1
store i64 %64, i64* %7
br label %65
65: ; preds = %62, %54
%66 = load i8*, i8** %8
%67 = load i64, i64* %7
%68 = getelementptr inbounds i8, i8* %66, i64 %67
store i8 0, i8* %68
%69 = load i8*, i8** %8
%70 = call i32 @putenv(i8* %69)
ret void
}

LLVM IR (-O2):
%struct.in6_addr = type { i32 }
@INET6_ADDRSTRLEN = global i64 0
@AF_INET6 = global i32 0
define void @ipv6_to_env(i8* %0, %struct.in6_addr* %1, i64 %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = load i64, i64* @INET6_ADDRSTRLEN
%6 = mul i64 %5, %2
%7 = add i64 %4, 2
%8 = add i64 %7, %6
%9 = tail call i8* @realloc(i32* null, i64 %8)
%10 = tail call i32 @memcpy(i8* %9, i8* %0, i64 %4)
%11 = getelementptr inbounds i8, i8* %9, i64 %4
store i8 61, i8* %11
%12 = add i64 %4, 1
%13 = icmp eq i64 %2, 0
br i1 %13, label %14, label %18
14: ; preds = %18, %3
%15 = phi i64 [ %12, %3 ], [ %27, %18 ]
%16 = getelementptr inbounds i8, i8* %9, i64 %15
store i8 0, i8* %16
%17 = tail call i32 @putenv(i8* %9)
ret void
18: ; preds = %3, %18
%19 = phi i64 [ %30, %18 ], [ %12, %3 ]
%20 = phi i64 [ %29, %18 ], [ 0, %3 ]
%21 = load i32, i32* @AF_INET6
%22 = getelementptr inbounds %struct.in6_addr, %struct.in6_addr* %1, i64 %20
%23 = getelementptr inbounds i8, i8* %9, i64 %19
%24 = load i64, i64* @INET6_ADDRSTRLEN
%25 = tail call i32 @inet_ntop(i32 %21, %struct.in6_addr* %22, i8* %23, i64 %24)
%26 = tail call i64 @strlen(i8* dereferenceable(1) %23)
%27 = add i64 %26, %19
%28 = getelementptr inbounds i8, i8* %9, i64 %27
store i8 32, i8* %28
%29 = add nuw i64 %20, 1
%30 = add i64 %27, 1
%31 = icmp eq i64 %29, %2
br i1 %31, label %14, label %18
}