#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 *malloc(scalar_t__);
int /*<<< orphan*/ memcpy(char **, char **, scalar_t__);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ *strstr(char *, char *);
void set_env(char *key, char *value, char **environ) {
  int index = 0;
  char *new_value = malloc(strlen(key) + strlen(value) + 2);
  strcpy(new_value, key);
  strcat(new_value, "=");
  strcat(new_value, value);
  while (environ[index]) {
    if (strstr(environ[index], key) != NULL && environ[index][0] == key[0]) {
      memcpy(&environ[index], &new_value, strlen(new_value));
      break;
    }
    index++;
  }
}


x86 assembly (clang, -O0):
<set_env>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rdi
lea    0xe43(%rip),%rsi
call   1070 <strcat@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strcat@plt>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     125e <set_env+0xfe>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x8(%rbp),%rsi
call   1080 <strstr@plt>
cmp    $0x0,%rax
je     1250 <set_env+0xf0>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movsbl (%rax),%eax
mov    -0x8(%rbp),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1250 <set_env+0xf0>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    %rax,%rdx
lea    -0x28(%rbp),%rsi
call   1050 <memcpy@plt>
jmp    125e <set_env+0xfe>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11cf <set_env+0x6f>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_env>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r13
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x2,%rdi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %rax,0x8(%rsp)
mov    %rax,%rdi
mov    %r13,%rsi
call   1030 <strcpy@plt>
lea    0xe53(%rip),%rsi
mov    %r15,%rdi
call   1070 <strcat@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1070 <strcat@plt>
mov    (%r14),%r12
test   %r12,%r12
je     1217 <set_env+0xb7>
xor    %ebx,%ebx
jmp    11de <set_env+0x7e>
nopl   0x0(%rax)
mov    0x8(%r14,%rbx,8),%r12
add    $0x1,%rbx
test   %r12,%r12
je     1217 <set_env+0xb7>
mov    %r12,%rdi
mov    %r13,%rsi
call   1080 <strstr@plt>
test   %rax,%rax
je     11d0 <set_env+0x70>
movzbl (%r12),%eax
cmp    0x0(%r13),%al
jne    11d0 <set_env+0x70>
mov    %ebx,%eax
lea    (%r14,%rax,8),%rbx
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    %rax,%rdx
call   1050 <memcpy@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"=\00"
define void @set_env(i8* %0, i8* %1, i8** %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
%6 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%7 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%8 = add i64 %6, 2
%9 = add i64 %8, %7
%10 = tail call i8* @malloc(i64 %9)
store i8* %10, i8** %4
%11 = tail call i32 @strcpy(i8* %10, i8* %0)
%12 = tail call i32 @strcat(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @strcat(i8* %10, i8* %1)
%14 = load i8*, i8** %2
%15 = icmp eq i8* %14, null
br i1 %15, label %35, label %16
16: ; preds = %3, %30
%17 = phi i64 [ %31, %30 ], [ 0, %3 ]
%18 = phi i8* [ %33, %30 ], [ %14, %3 ]
%19 = tail call i32* @strstr(i8* %18, i8* dereferenceable(1) %0)
%20 = icmp eq i32* %19, null
br i1 %20, label %30, label %21
21: ; preds = %16
%22 = load i8, i8* %18
%23 = load i8, i8* %0
%24 = icmp eq i8 %22, %23
br i1 %24, label %25, label %30
25: ; preds = %21
%26 = and i64 %17, 4294967295
%27 = getelementptr inbounds i8*, i8** %2, i64 %26
%28 = tail call i64 @strlen(i8* dereferenceable(1) %10)
%29 = call i32 @memcpy(i8** %27, i8** %4, i64 %28)
br label %35
30: ; preds = %21, %16
%31 = add nuw i64 %17, 1
%32 = getelementptr inbounds i8*, i8** %2, i64 %31
%33 = load i8*, i8** %32
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %16
35: ; preds = %30, %3, %25
ret void
}