#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*/ env__cat(char *, char *, char *);
int env__find(char **, char *);
unsigned int env_length(char **);
char *malloc(unsigned int);
char **realloc(char **, unsigned int);
unsigned int strlen(char *);
char **env_set(char **env, char *name, char *value) {
  int index;
  char *pair;
  unsigned int name_length, value_length, length;
  name_length = strlen(name);
  value_length = strlen(value);
  if ((index = env__find(env, name)) == -1) {
    length = env_length(env);
    env = realloc(env, (length + 2) * sizeof(char *));
    pair = malloc(name_length + 1 + value_length + 1);
    env__cat(pair, name, value);
    env[length] = pair;
    env[length + 1] = NULL;
    return env;
  }
  pair = malloc(name_length + 1 + value_length + 1);
  env__cat(pair, name, value);
  env[index] = pair;
  return env;
}


x86 assembly (clang, -O0):
<env_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <env__find@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    1222 <env_set+0xc2>
mov    -0x10(%rbp),%rdi
call   1080 <env_length@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x3c(%rbp),%eax
add    $0x2,%eax
mov    %eax,%eax
shl    $0x3,%rax
mov    %eax,%esi
call   1070 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x34(%rbp),%edi
add    $0x1,%edi
add    -0x38(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <env__cat@plt>
mov    -0x30(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    -0x3c(%rbp),%ecx
mov    %rdx,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
mov    -0x3c(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,%ecx
movq   $0x0,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1260 <env_set+0x100>
mov    -0x34(%rbp),%edi
add    $0x1,%edi
add    -0x38(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <env__cat@plt>
mov    -0x30(%rbp),%rdx
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<env_set>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
mov    %rsi,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %eax,%r13d
mov    %r12,%rdi
mov    %r15,%rsi
call   1030 <env__find@plt>
cmp    $0xffffffff,%eax
je     11c1 <env_set+0x61>
mov    %eax,%ebp
lea    (%rbx,%r13,1),%edi
add    $0x2,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <env__cat@plt>
movslq %ebp,%rax
mov    %rbx,(%r12,%rax,8)
jmp    120b <env_set+0xab>
mov    %r12,%rdi
call   1080 <env_length@plt>
mov    %eax,%ebp
lea    0x10(,%rbp,8),%esi
mov    %r12,%rdi
call   1070 <realloc@plt>
mov    %rax,%r12
lea    (%rbx,%r13,1),%edi
add    $0x2,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <env__cat@plt>
mov    %ebp,%eax
mov    %rbx,(%r12,%rax,8)
add    $0x1,%ebp
movq   $0x0,(%r12,%rbp,8)
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8** @env_set(i8** %0, i8* %1, i8* %2) {
%4 = tail call i32 @strlen(i8* %1)
%5 = tail call i32 @strlen(i8* %2)
%6 = tail call i32 @env__find(i8** %0, i8* %1)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %22
8: ; preds = %3
%9 = tail call i32 @env_length(i8** %0)
%10 = shl i32 %9, 3
%11 = add i32 %10, 16
%12 = tail call i8** @realloc(i8** %0, i32 %11)
%13 = add i32 %5, %4
%14 = add i32 %13, 2
%15 = tail call i8* @malloc(i32 %14)
%16 = tail call i32 @env__cat(i8* %15, i8* %1, i8* %2)
%17 = zext i32 %9 to i64
%18 = getelementptr inbounds i8*, i8** %12, i64 %17
store i8* %15, i8** %18
%19 = add i32 %9, 1
%20 = zext i32 %19 to i64
%21 = getelementptr inbounds i8*, i8** %12, i64 %20
store i8* null, i8** %21
br label %29
22: ; preds = %3
%23 = add i32 %5, %4
%24 = add i32 %23, 2
%25 = tail call i8* @malloc(i32 %24)
%26 = tail call i32 @env__cat(i8* %25, i8* %1, i8* %2)
%27 = sext i32 %6 to i64
%28 = getelementptr inbounds i8*, i8** %0, i64 %27
store i8* %25, i8** %28
br label %29
29: ; preds = %22, %8
%30 = phi i8** [ %12, %8 ], [ %0, %22 ]
ret i8** %30
}