#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*/ free(char *);
char *getenv(char const *);
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int /*<<< orphan*/ setenv(char const *, char *, int);
int strlen(char const *);
int env_append(const char *env, const char *val, char separator) {
  size_t env_len;
  const char *old_env;
  char *new_env;
  old_env = getenv(env);
  if (old_env != NULL) {
    env_len = strlen(old_env) + strlen(val) + 2;
    new_env = malloc(env_len);
    memcpy(new_env, old_env, strlen(old_env));
    memcpy(&new_env[strlen(old_env) + 1], val, strlen(val) + 1);
    new_env[strlen(old_env)] = separator;
  } else {
    env_len = strlen(val) + 1;
    new_env = malloc(env_len);
    memcpy(new_env, val, env_len);
  }
  setenv(env, new_env, 1);
  free(new_env);
  return 0;
}


x86 assembly (clang, -O0):
<env_append>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %dl,%al
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     125b <env_append+0xfb>
mov    -0x28(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x6c(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x6c(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x68(%rbp),%rdi
mov    -0x60(%rbp),%rsi
movslq %eax,%rdx
call   1070 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x58(%rbp),%rax
add    $0x1,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rsi
add    $0x1,%eax
movslq %eax,%rdx
call   1070 <memcpy@plt>
mov    -0x11(%rbp),%al
mov    %al,-0x39(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x39(%rbp),%dl
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    128b <env_append+0x12b>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <memcpy@plt>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<env_append>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r12
mov    %rdi,%r14
call   1030 <getenv@plt>
test   %rax,%rax
je     11f5 <env_append+0x95>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebp
mov    %r12,%rdi
call   1060 <strlen@plt>
add    %ebp,%eax
add    $0x2,%eax
movslq %eax,%rdi
call   1080 <malloc@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1060 <strlen@plt>
movslq %eax,%rdx
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1070 <memcpy@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
lea    (%rax,%rbp,1),%r13
add    $0x1,%r13
mov    %r12,%rdi
call   1060 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rdx
mov    %r13,%rdi
mov    %r12,%rsi
call   1070 <memcpy@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
mov    %r15b,0x0(%rbp,%rax,1)
jmp    121c <env_append+0xbc>
mov    %r12,%rdi
call   1060 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rbx
mov    %rbx,%rdi
call   1080 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1070 <memcpy@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    %rbp,%rdi
call   1040 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @env_append(i8* %0, i8* %1, i8 signext %2) {
%4 = tail call i8* @getenv(i8* %0)
%5 = icmp eq i8* %4, null
br i1 %5, label %27, label %6
6: ; preds = %3
%7 = tail call i32 @strlen(i8* %4)
%8 = tail call i32 @strlen(i8* %1)
%9 = add i32 %7, 2
%10 = add i32 %9, %8
%11 = sext i32 %10 to i64
%12 = tail call i8* @malloc(i64 %11)
%13 = tail call i32 @strlen(i8* %4)
%14 = sext i32 %13 to i64
%15 = tail call i32 @memcpy(i8* %12, i8* %4, i64 %14)
%16 = tail call i32 @strlen(i8* %4)
%17 = add nsw i32 %16, 1
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %12, i64 %18
%20 = tail call i32 @strlen(i8* %1)
%21 = add nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = tail call i32 @memcpy(i8* %19, i8* %1, i64 %22)
%24 = tail call i32 @strlen(i8* %4)
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %12, i64 %25
store i8 %2, i8* %26
br label %33
27: ; preds = %3
%28 = tail call i32 @strlen(i8* %1)
%29 = add nsw i32 %28, 1
%30 = sext i32 %29 to i64
%31 = tail call i8* @malloc(i64 %30)
%32 = tail call i32 @memcpy(i8* %31, i8* %1, i64 %30)
br label %33
33: ; preds = %27, %6
%34 = phi i8* [ %12, %6 ], [ %31, %27 ]
%35 = tail call i32 @setenv(i8* %0, i8* %34, i32 1)
%36 = tail call i32 @free(i8* %34)
ret i32 0
}