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

/* Variables and functions */
int ENOMEM;
int argz_add(char **, size_t *, char const *);
int /*<<< orphan*/ envz_remove(char **, size_t *, char const *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int strlen(char const *);
error_t envz_add(char **envz, size_t *envz_len, const char *name,
                 const char *value) {
  char *concat = NULL;
  int name_len = 0;
  int val_len = 0;
  int retval = 0;
  envz_remove(envz, envz_len, name);
  if (value) {
    name_len = strlen(name);
    val_len = strlen(value);
    if (!(concat = (char *)malloc(name_len + val_len + 2)))
      return ENOMEM;
    memcpy(concat, name, name_len);
    concat[name_len] = '=';
    memcpy(concat + name_len + 1, value, val_len + 1);
    retval = argz_add(envz, envz_len, concat);
    free(concat);
  } else {
    retval = argz_add(envz, envz_len, name);
  }
  return retval;
}


x86 assembly (clang, -O0):
<envz_add>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <envz_remove@plt>
cmpq   $0x0,-0x28(%rbp)
je     1252 <envz_add+0xf2>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x38(%rbp)
mov    -0x34(%rbp),%edi
add    -0x38(%rbp),%edi
add    $0x2,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11f6 <envz_add+0x96>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    126c <envz_add+0x10c>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1070 <memcpy@plt>
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
movb   $0x3d,(%rax,%rcx,1)
mov    -0x30(%rbp),%rdi
movslq -0x34(%rbp),%rax
add    %rax,%rdi
add    $0x1,%rdi
mov    -0x28(%rbp),%rsi
mov    -0x38(%rbp),%edx
add    $0x1,%edx
call   1070 <memcpy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1060 <argz_add@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
jmp    1266 <envz_add+0x106>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1060 <argz_add@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<envz_add>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r12
mov    %rdx,%r13
mov    %rsi,%rbx
mov    %rdi,%r15
call   1050 <envz_remove@plt>
test   %r12,%r12
je     11f5 <envz_add+0x95>
mov    %rbx,(%rsp)
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    %eax,%r14d
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
lea    (%r14,%rbp,1),%edi
add    $0x2,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1211 <envz_add+0xb1>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r13,%rsi
mov    %r14d,%edx
call   1070 <memcpy@plt>
movslq %r14d,%rax
movb   $0x3d,(%rbx,%rax,1)
lea    (%rbx,%rax,1),%rdi
add    $0x1,%rdi
add    $0x1,%ebp
mov    %r12,%rsi
mov    %ebp,%edx
call   1070 <memcpy@plt>
mov    %r15,%rdi
mov    (%rsp),%rsi
mov    %rbx,%rdx
call   1060 <argz_add@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
jmp    121a <envz_add+0xba>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <argz_add@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ENOMEM = global i32 0
define i32 @envz_add(i8** %0, i64* %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i64*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8** %0, i8*** %6
store i64* %1, i64** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i8* null, i8** %10
store i32 0, i32* %11
store i32 0, i32* %12
store i32 0, i32* %13
%14 = load i8**, i8*** %6
%15 = load i64*, i64** %7
%16 = load i8*, i8** %8
%17 = call i32 @envz_remove(i8** %14, i64* %15, i8* %16)
%18 = load i8*, i8** %9
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %58
20: ; preds = %4
%21 = load i8*, i8** %8
%22 = call i32 @strlen(i8* %21)
store i32 %22, i32* %11
%23 = load i8*, i8** %9
%24 = call i32 @strlen(i8* %23)
store i32 %24, i32* %12
%25 = load i32, i32* %11
%26 = load i32, i32* %12
%27 = add nsw i32 %25, %26
%28 = add nsw i32 %27, 2
%29 = call i64 @malloc(i32 %28)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %10
%31 = icmp ne i8* %30, null
br i1 %31, label %34, label %32
32: ; preds = %20
%33 = load i32, i32* @ENOMEM
store i32 %33, i32* %5
br label %65
34: ; preds = %20
%35 = load i8*, i8** %10
%36 = load i8*, i8** %8
%37 = load i32, i32* %11
%38 = call i32 @memcpy(i8* %35, i8* %36, i32 %37)
%39 = load i8*, i8** %10
%40 = load i32, i32* %11
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
store i8 61, i8* %42
%43 = load i8*, i8** %10
%44 = load i32, i32* %11
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
%47 = getelementptr inbounds i8, i8* %46, i64 1
%48 = load i8*, i8** %9
%49 = load i32, i32* %12
%50 = add nsw i32 %49, 1
%51 = call i32 @memcpy(i8* %47, i8* %48, i32 %50)
%52 = load i8**, i8*** %6
%53 = load i64*, i64** %7
%54 = load i8*, i8** %10
%55 = call i32 @argz_add(i8** %52, i64* %53, i8* %54)
store i32 %55, i32* %13
%56 = load i8*, i8** %10
%57 = call i32 @free(i8* %56)
br label %63
58: ; preds = %4
%59 = load i8**, i8*** %6
%60 = load i64*, i64** %7
%61 = load i8*, i8** %8
%62 = call i32 @argz_add(i8** %59, i64* %60, i8* %61)
store i32 %62, i32* %13
br label %63
63: ; preds = %58, %34
%64 = load i32, i32* %13
store i32 %64, i32* %5
br label %65
65: ; preds = %63, %32
%66 = load i32, i32* %5
ret i32 %66
}

LLVM IR (-O2):
@ENOMEM = global i32 0
define i32 @envz_add(i8** %0, i64* %1, i8* %2, i8* %3) {
%5 = tail call i32 @envz_remove(i8** %0, i64* %1, i8* %2)
%6 = icmp eq i8* %3, null
br i1 %6, label %26, label %7
7: ; preds = %4
%8 = tail call i32 @strlen(i8* %2)
%9 = tail call i32 @strlen(i8* %3)
%10 = add i32 %8, 2
%11 = add i32 %10, %9
%12 = tail call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8*
%14 = icmp eq i64 %12, 0
br i1 %14, label %15, label %17
15: ; preds = %7
%16 = load i32, i32* @ENOMEM
br label %28
17: ; preds = %7
%18 = tail call i32 @memcpy(i8* %13, i8* %2, i32 %8)
%19 = sext i32 %8 to i64
%20 = getelementptr inbounds i8, i8* %13, i64 %19
store i8 61, i8* %20
%21 = getelementptr inbounds i8, i8* %20, i64 1
%22 = add nsw i32 %9, 1
%23 = tail call i32 @memcpy(i8* %21, i8* %3, i32 %22)
%24 = tail call i32 @argz_add(i8** %0, i64* %1, i8* %13)
%25 = tail call i32 @free(i8* %13)
br label %28
26: ; preds = %4
%27 = tail call i32 @argz_add(i8** %0, i64* %1, i8* %2)
br label %28
28: ; preds = %17, %26, %15
%29 = phi i32 [ %16, %15 ], [ %24, %17 ], [ %27, %26 ]
ret i32 %29
}