#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 */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
char **realloc(char **, int);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int strlen(char *);
char **addEnv(char **envp, char *name, char *value, int *length) {
  envp = realloc(envp, sizeof(char *) * (*length + 2));
  if (envp == NULL) {
    perror("realloc");
    exit(EXIT_FAILURE);
  }
  char *vptr = malloc((2 + strlen(name) + strlen(value)) * sizeof(char));
  if (vptr == NULL) {
    perror("malloc");
    exit(EXIT_FAILURE);
  }
  sprintf(vptr, "%s=%s", name, value);
  envp[*length] = vptr;
  *length += 1;
  return envp;
}


x86 assembly (clang, -O0):
<addEnv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
add    $0x2,%eax
cltq
shl    $0x3,%rax
mov    %eax,%esi
call   1050 <realloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11bb <addEnv+0x5b>
lea    0xe58(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x2,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
add    %ecx,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1210 <addEnv+0xb0>
lea    0xe0b(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xdec(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x28(%rbp),%rdx
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movslq (%rcx),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<addEnv>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%r12
mov    %rdx,%r14
mov    %rsi,%r15
mov    (%rcx),%eax
lea    0x10(,%rax,8),%esi
call   1050 <realloc@plt>
test   %rax,%rax
je     11df <addEnv+0x7f>
mov    %rax,%r13
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x2,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     11e8 <addEnv+0x88>
mov    %rax,%rbx
lea    0xe5b(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
call   1070 <sprintf@plt>
movslq (%r12),%rax
mov    %rbx,0x0(%r13,%rax,8)
add    $0x1,%eax
mov    %eax,(%r12)
mov    %r13,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xe1a(%rip),%rdi
jmp    11ef <addEnv+0x8f>
lea    0xe19(%rip),%rdi
call   1060 <perror@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"realloc\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [7 x i8] c"malloc\00"
@.str.2 = [6 x i8] c"%s=%s\00"
define i8** @addEnv(i8** %0, i8* %1, i8* %2, i32* %3) {
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i8*
store i8** %0, i8*** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32* %3, i32** %8
%10 = load i8**, i8*** %5
%11 = load i32*, i32** %8
%12 = load i32, i32* %11
%13 = add nsw i32 %12, 2
%14 = sext i32 %13 to i64
%15 = mul i64 8, %14
%16 = trunc i64 %15 to i32
%17 = call i8** @realloc(i8** %10, i32 %16)
store i8** %17, i8*** %5
%18 = load i8**, i8*** %5
%19 = icmp eq i8** %18, null
br i1 %19, label %20, label %24
20: ; preds = %4
%21 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %4
%25 = load i8*, i8** %6
%26 = call i32 @strlen(i8* %25)
%27 = add nsw i32 2, %26
%28 = load i8*, i8** %7
%29 = call i32 @strlen(i8* %28)
%30 = add nsw i32 %27, %29
%31 = sext i32 %30 to i64
%32 = mul i64 %31, 1
%33 = trunc i64 %32 to i32
%34 = call i8* @malloc(i32 %33)
store i8* %34, i8** %9
%35 = load i8*, i8** %9
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %41
37: ; preds = %24
%38 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %24
%42 = load i8*, i8** %9
%43 = load i8*, i8** %6
%44 = load i8*, i8** %7
%45 = call i32 @sprintf(i8* %42, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %43, i8* %44)
%46 = load i8*, i8** %9
%47 = load i8**, i8*** %5
%48 = load i32*, i32** %8
%49 = load i32, i32* %48
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8*, i8** %47, i64 %50
store i8* %46, i8** %51
%52 = load i32*, i32** %8
%53 = load i32, i32* %52
%54 = add nsw i32 %53, 1
store i32 %54, i32* %52
%55 = load i8**, i8*** %5
ret i8** %55
}

LLVM IR (-O2):
@.str = [8 x i8] c"realloc\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [7 x i8] c"malloc\00"
@.str.2 = [6 x i8] c"%s=%s\00"
define i8** @addEnv(i8** %0, i8* %1, i8* %2, i32* nocapture %3) {
%5 = load i32, i32* %3
%6 = shl i32 %5, 3
%7 = add i32 %6, 16
%8 = tail call i8** @realloc(i8** %0, i32 %7)
%9 = icmp eq i8** %8, null
br i1 %9, label %10, label %14
10: ; preds = %4
%11 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %4
%15 = tail call i32 @strlen(i8* %1)
%16 = add nsw i32 %15, 2
%17 = tail call i32 @strlen(i8* %2)
%18 = add nsw i32 %16, %17
%19 = tail call i8* @malloc(i32 %18)
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %25
21: ; preds = %14
%22 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
%24 = tail call i32 @exit(i32 %23)
unreachable
25: ; preds = %14
%26 = tail call i32 @sprintf(i8* %19, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %1, i8* %2)
%27 = load i32, i32* %3
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8*, i8** %8, i64 %28
store i8* %19, i8** %29
%30 = add nsw i32 %27, 1
store i32 %30, i32* %3
ret i8** %8
}