#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;

/* Variables and functions */
int dn_expand(int /*<<< orphan*/ const *, int /*<<< orphan*/ const *,
              int /*<<< orphan*/ 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 fqdn_to_env(const char *name, const uint8_t *fqdn, size_t len) {
  size_t buf_len = strlen(name);
  size_t buf_size = len + buf_len + 2;
  const uint8_t *fqdn_end = fqdn + len;
  char *buf = realloc(NULL, len + buf_len + 2);
  memcpy(buf, name, buf_len);
  buf[buf_len++] = '=';
  int l = 1;
  while (l > 0 && fqdn < fqdn_end) {
    l = dn_expand(fqdn, fqdn_end, fqdn, &buf[buf_len], buf_size - buf_len);
    fqdn += l;
    buf_len += strlen(&buf[buf_len]);
    buf[buf_len++] = ' ';
  }
  buf[buf_len - 1] = '\0';
  putenv(buf);
}


x86 assembly (clang, -O0):
<fqdn_to_env>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rsi
add    -0x20(%rbp),%rsi
add    $0x2,%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1070 <realloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x38(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
movb   $0x3d,(%rax,%rcx,1)
movl   $0x1,-0x3c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x3c(%rbp)
mov    %al,-0x3d(%rbp)
jle    11f9 <fqdn_to_env+0xa9>
mov    -0x10(%rbp),%rax
cmp    -0x30(%rbp),%rax
setb   %al
mov    %al,-0x3d(%rbp)
mov    -0x3d(%rbp),%al
test   $0x1,%al
jne    1209 <fqdn_to_env+0xb9>
jmp    1273 <fqdn_to_env+0x123>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x38(%rbp),%rcx
add    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
sub    -0x20(%rbp),%r8
call   1060 <dn_expand@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x38(%rbp),%rdi
add    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
movb   $0x20,(%rax,%rcx,1)
jmp    11dc <fqdn_to_env+0x8c>
mov    -0x38(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rdi
call   1040 <putenv@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fqdn_to_env>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r13
mov    %rsi,%r14
mov    %rdi,%rbp
call   1030 <strlen@plt>
mov    %rax,%rbx
lea    (%rax,%r13,1),%rsi
add    $0x2,%rsi
xor    %edi,%edi
mov    %rsi,(%rsp)
call   1070 <realloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
movb   $0x3d,(%r15,%rbx,1)
test   %r13,%r13
jle    11ea <fqdn_to_env+0x9a>
lea    (%r14,%r13,4),%r13
xchg   %ax,%ax
lea    0x1(%rbx),%rax
lea    (%r15,%rbx,1),%r12
add    $0x1,%r12
mov    (%rsp),%r8
sub    %rax,%r8
mov    %r14,%rdi
mov    %r13,%rsi
mov    %r14,%rdx
mov    %r12,%rcx
call   1060 <dn_expand@plt>
mov    %eax,%ebp
mov    %r12,%rdi
call   1030 <strlen@plt>
add    %rax,%rbx
add    $0x1,%rbx
movb   $0x20,(%r15,%rbx,1)
test   %ebp,%ebp
jle    11ea <fqdn_to_env+0x9a>
movslq %ebp,%rax
lea    (%r14,%rax,4),%r14
cmp    %r13,%r14
jb     11a0 <fqdn_to_env+0x50>
movb   $0x0,(%r15,%rbx,1)
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <putenv@plt>

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

LLVM IR (-O2):
define void @fqdn_to_env(i8* %0, i32* %1, i64 %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = add i64 %2, 2
%6 = add i64 %5, %4
%7 = getelementptr inbounds i32, i32* %1, i64 %2
%8 = tail call i8* @realloc(i32* null, i64 %6)
%9 = tail call i32 @memcpy(i8* %8, i8* %0, i64 %4)
%10 = getelementptr inbounds i8, i8* %8, i64 %4
store i8 61, i8* %10
%11 = icmp sgt i64 %2, 0
br i1 %11, label %12, label %27
12: ; preds = %3, %12
%13 = phi i64 [ %22, %12 ], [ %4, %3 ]
%14 = phi i32* [ %20, %12 ], [ %1, %3 ]
%15 = add i64 %13, 1
%16 = getelementptr inbounds i8, i8* %8, i64 %15
%17 = sub i64 %6, %15
%18 = tail call i32 @dn_expand(i32* %14, i32* %7, i32* %14, i8* %16, i64 %17)
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i32, i32* %14, i64 %19
%21 = tail call i64 @strlen(i8* dereferenceable(1) %16)
%22 = add i64 %21, %15
%23 = getelementptr inbounds i8, i8* %8, i64 %22
store i8 32, i8* %23
%24 = icmp sgt i32 %18, 0
%25 = icmp ult i32* %20, %7
%26 = select i1 %24, i1 %25, i1 false
br i1 %26, label %12, label %27
27: ; preds = %12, %3
%28 = phi i64 [ %4, %3 ], [ %22, %12 ]
%29 = getelementptr inbounds i8, i8* %8, i64 %28
store i8 0, i8* %29
%30 = tail call i32 @putenv(i8* %8)
ret void
}