#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 */

/* Variables and functions */
char *calloc(size_t, int);
char *getenv(char const *);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strncat(char *, char const *, size_t);
int /*<<< orphan*/ strncpy(char *, char const *, unsigned int const);
size_t strnlen(char const *, unsigned int const);
char *safe_env(const char *name) {
  if (!name)
    return NULL;
  const char *env = getenv(name);
  if (!env)
    return NULL;
  const unsigned max_envlen = 1024;
  char newenv[max_envlen + 1];
  memset(&newenv[0], '\0', (sizeof((newenv)) / sizeof((newenv)[0])));
  strncpy(&newenv[0], env, max_envlen);
  size_t env_len = strnlen(&newenv[0], max_envlen);
  size_t nme_len = strnlen(name, max_envlen);
  char *ret = calloc(nme_len + env_len + 2, sizeof(char));
  strncat(ret, name, nme_len);
  strcat(ret, "=");
  strncat(ret, env, env_len);
  return ret;
}


x86 assembly (clang, -O0):
<safe_env>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1194 <safe_env+0x24>
movq   $0x0,-0x8(%rbp)
jmp    1275 <safe_env+0x105>
mov    -0x10(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b9 <safe_env+0x49>
movq   $0x0,-0x8(%rbp)
jmp    1275 <safe_env+0x105>
movl   $0x400,-0x1c(%rbp)
mov    %rsp,%rax
mov    %rax,-0x28(%rbp)
mov    %rsp,%rdi
add    $0xfffffffffffffbf0,%rdi
mov    %rdi,-0x48(%rbp)
mov    %rdi,%rsp
xor    %esi,%esi
mov    $0x401,%edx
call   1050 <memset@plt>
mov    -0x48(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x400,%edx
call   1040 <strncpy@plt>
mov    -0x48(%rbp),%rdi
mov    $0x400,%esi
call   1060 <strnlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x400,%esi
call   1060 <strnlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
add    -0x30(%rbp),%rdi
add    $0x2,%rdi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1070 <strncat@plt>
mov    -0x40(%rbp),%rdi
lea    0xdb0(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1070 <strncat@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_env>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     122f <safe_env+0xbf>
mov    %rdi,%r15
call   1030 <getenv@plt>
test   %rax,%rax
je     122f <safe_env+0xbf>
mov    %rax,%r14
mov    %rsp,-0x30(%rbp)
lea    -0x410(%rsp),%rbx
ff
mov    %rbx,%rsp
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x401,%edx
call   1050 <memset@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x400,%edx
call   1040 <strncpy@plt>
mov    %rbx,%rdi
mov    $0x400,%esi
call   1060 <strnlen@plt>
mov    %rax,%r12
mov    %r15,%rdi
mov    $0x400,%esi
call   1060 <strnlen@plt>
mov    %rax,%r13
lea    (%r12,%rax,1),%rdi
add    $0x2,%rdi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
call   1070 <strncat@plt>
lea    0xded(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcat@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1070 <strncat@plt>
mov    -0x30(%rbp),%rsp
jmp    1231 <safe_env+0xc1>
xor    %ebx,%ebx
mov    %rbx,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"=\00"
define i8* @safe_env(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
store i8* %0, i8** %3
%10 = load i8*, i8** %3
%11 = icmp ne i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %1
store i8* null, i8** %2
br label %48
13: ; preds = %1
%14 = load i8*, i8** %3
%15 = call i8* @getenv(i8* %14)
store i8* %15, i8** %4
%16 = load i8*, i8** %4
%17 = icmp ne i8* %16, null
br i1 %17, label %19, label %18
18: ; preds = %13
store i8* null, i8** %2
br label %48
19: ; preds = %13
store i32 1024, i32* %5
%20 = call i8* @llvm.stacksave()
store i8* %20, i8** %6
%21 = alloca i8, i64 1025
%22 = getelementptr inbounds i8, i8* %21, i64 0
%23 = call i32 @memset(i8* %22, i8 signext 0, i32 1025)
%24 = getelementptr inbounds i8, i8* %21, i64 0
%25 = load i8*, i8** %4
%26 = call i32 @strncpy(i8* %24, i8* %25, i32 1024)
%27 = getelementptr inbounds i8, i8* %21, i64 0
%28 = call i64 @strnlen(i8* %27, i32 1024)
store i64 %28, i64* %7
%29 = load i8*, i8** %3
%30 = call i64 @strnlen(i8* %29, i32 1024)
store i64 %30, i64* %8
%31 = load i64, i64* %8
%32 = load i64, i64* %7
%33 = add i64 %31, %32
%34 = add i64 %33, 2
%35 = call i8* @calloc(i64 %34, i32 1)
store i8* %35, i8** %9
%36 = load i8*, i8** %9
%37 = load i8*, i8** %3
%38 = load i64, i64* %8
%39 = call i32 @strncat(i8* %36, i8* %37, i64 %38)
%40 = load i8*, i8** %9
%41 = call i32 @strcat(i8* %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%42 = load i8*, i8** %9
%43 = load i8*, i8** %4
%44 = load i64, i64* %7
%45 = call i32 @strncat(i8* %42, i8* %43, i64 %44)
%46 = load i8*, i8** %9
store i8* %46, i8** %2
%47 = load i8*, i8** %6
br label %48
48: ; preds = %19, %18, %12
%49 = load i8*, i8** %2
ret i8* %49
}

LLVM IR (-O2):
@.str = [2 x i8] c"=\00"
define i8* @safe_env(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %20, label %3
3: ; preds = %1
%4 = tail call i8* @getenv(i8* %0)
%5 = icmp eq i8* %4, null
br i1 %5, label %20, label %6
6: ; preds = %3
%7 = tail call i8* @llvm.stacksave()
%8 = alloca [1025 x i8]
%9 = getelementptr inbounds [1025 x i8], [1025 x i8]* %8, i64 0, i64 0
%10 = call i32 @memset(i8* %9, i8 signext 0, i32 1025)
%11 = call i32 @strncpy(i8* %9, i8* %4, i32 1024)
%12 = call i64 @strnlen(i8* %9, i32 1024)
%13 = call i64 @strnlen(i8* %0, i32 1024)
%14 = add i64 %12, 2
%15 = add i64 %14, %13
%16 = call i8* @calloc(i64 %15, i32 1)
%17 = call i32 @strncat(i8* %16, i8* %0, i64 %13)
%18 = call i32 @strcat(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @strncat(i8* %16, i8* %4, i64 %12)
br label %20
20: ; preds = %6, %3, %1
%21 = phi i8* [ null, %1 ], [ %16, %6 ], [ null, %3 ]
ret i8* %21
}