#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 */
scalar_t__ malloc(size_t const);
int /*<<< orphan*/ memcpy(char *const, char const *, size_t const);
size_t strlen(char const *);
char *SandboxSavedEnvironmentVariable(const char *envvar) {
  const size_t envvar_len = strlen(envvar);
  const size_t kMaxSizeT = (size_t)-1;
  if (envvar_len > kMaxSizeT - 1 - 8)
    return NULL;
  const size_t saved_envvarlen = envvar_len + 1 + 8;
  char *const saved_envvar = (char *)malloc(saved_envvarlen);
  if (!saved_envvar)
    return NULL;
  memcpy(saved_envvar, "SANDBOX_", 8);
  memcpy(saved_envvar + 8, envvar, envvar_len);
  saved_envvar[8 + envvar_len] = 0;
  return saved_envvar;
}


x86 assembly (clang, -O0):
<SandboxSavedEnvironmentVariable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
movq   $0xffffffffffffffff,-0x20(%rbp)
ff
cmpq   $0xfffffffffffffff6,-0x18(%rbp)
jbe    1169 <SandboxSavedEnvironmentVariable+0x39>
movq   $0x0,-0x8(%rbp)
jmp    11e0 <SandboxSavedEnvironmentVariable+0xb0>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
add    $0x8,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    119e <SandboxSavedEnvironmentVariable+0x6e>
movq   $0x0,-0x8(%rbp)
jmp    11e0 <SandboxSavedEnvironmentVariable+0xb0>
mov    -0x30(%rbp),%rdi
lea    0xe57(%rip),%rsi
mov    $0x8,%edx
call   1040 <memcpy@plt>
mov    -0x30(%rbp),%rdi
add    $0x8,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    -0x18(%rbp),%rcx
add    $0x8,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SandboxSavedEnvironmentVariable>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1030 <strlen@plt>
cmp    $0xfffffffffffffff6,%rax
ja     1185 <SandboxSavedEnvironmentVariable+0x55>
mov    %rax,%r15
lea    0x9(%rax),%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     1185 <SandboxSavedEnvironmentVariable+0x55>
mov    %rax,%rbx
lea    0xea2(%rip),%rsi
mov    $0x8,%edx
mov    %rax,%rdi
call   1040 <memcpy@plt>
mov    %rbx,%rdi
add    $0x8,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
movb   $0x0,0x8(%r15,%rbx,1)
jmp    1187 <SandboxSavedEnvironmentVariable+0x57>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [9 x i8] c"SANDBOX_\00"
define i8* @SandboxSavedEnvironmentVariable(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i64 @strlen(i8* %8)
store i64 %9, i64* %4
store i64 -1, i64* %5
%10 = load i64, i64* %4
%11 = icmp ugt i64 %10, -10
br i1 %11, label %12, label %13
12: ; preds = %1
store i8* null, i8** %2
br label %36
13: ; preds = %1
%14 = load i64, i64* %4
%15 = add i64 %14, 1
%16 = add i64 %15, 8
store i64 %16, i64* %6
%17 = load i64, i64* %6
%18 = call i64 @malloc(i64 %17)
%19 = inttoptr i64 %18 to i8*
store i8* %19, i8** %7
%20 = load i8*, i8** %7
%21 = icmp ne i8* %20, null
br i1 %21, label %23, label %22
22: ; preds = %13
store i8* null, i8** %2
br label %36
23: ; preds = %13
%24 = load i8*, i8** %7
%25 = call i32 @memcpy(i8* %24, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i64 8)
%26 = load i8*, i8** %7
%27 = getelementptr inbounds i8, i8* %26, i64 8
%28 = load i8*, i8** %3
%29 = load i64, i64* %4
%30 = call i32 @memcpy(i8* %27, i8* %28, i64 %29)
%31 = load i8*, i8** %7
%32 = load i64, i64* %4
%33 = add i64 8, %32
%34 = getelementptr inbounds i8, i8* %31, i64 %33
store i8 0, i8* %34
%35 = load i8*, i8** %7
store i8* %35, i8** %2
br label %36
36: ; preds = %23, %22, %12
%37 = load i8*, i8** %2
ret i8* %37
}

LLVM IR (-O2):
@.str = [9 x i8] c"SANDBOX_\00"
define i8* @SandboxSavedEnvironmentVariable(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = icmp ugt i64 %2, -10
br i1 %3, label %15, label %4
4: ; preds = %1
%5 = add nuw i64 %2, 9
%6 = tail call i64 @malloc(i64 %5)
%7 = inttoptr i64 %6 to i8*
%8 = icmp eq i64 %6, 0
br i1 %8, label %15, label %9
9: ; preds = %4
%10 = tail call i32 @memcpy(i8* %7, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i64 8)
%11 = getelementptr inbounds i8, i8* %7, i64 8
%12 = tail call i32 @memcpy(i8* %11, i8* %0, i64 %2)
%13 = add nuw i64 %2, 8
%14 = getelementptr inbounds i8, i8* %7, i64 %13
store i8 0, i8* %14
br label %15
15: ; preds = %9, %4, %1
%16 = phi i8* [ null, %1 ], [ %7, %9 ], [ null, %4 ]
ret i8* %16
}