#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*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ free(char *);
scalar_t__ getenv(char const *);
char *malloc(int);
int /*<<< orphan*/ setenv(char const *, char const *, int);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char const *, char const *);
int /*<<< orphan*/ stderr;
int strlen(char const *);
int /*<<< orphan*/ *strstr(char const *, char const *);
void addPathToEnvVariable(const char *variableName, const char *path) {
  const char *value = (const char *)getenv(variableName);
  if (value == NULL)
    value = "";
  if (strstr(value, path) == NULL) {
    size_t valueLen = strlen(path) + strlen(value) + 2;
    const char *newValue = malloc(sizeof(char) * (valueLen));
    if (newValue == NULL) {
      fprintf(stderr,
              "fatal: "
              "Not enough memory to change '%s'!"
              "\n",
              variableName);
      return;
    }
    snprintf((char *)newValue, valueLen, "%s:%s", path, value);
    setenv(variableName, newValue, 1);
    free((char *)newValue);
  }
}


x86 assembly (clang, -O0):
<addPathToEnvVariable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b3 <addPathToEnvVariable+0x33>
lea    0xe7a(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10a0 <strstr@plt>
cmp    $0x0,%rax
jne    1262 <addPathToEnvVariable+0xe2>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    122b <addPathToEnvVariable+0xab>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xddf(%rip),%rsi
call   1080 <fprintf@plt>
jmp    1262 <addPathToEnvVariable+0xe2>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0xde8(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<addPathToEnvVariable>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe8c(%rip),%rbx
cmovne %rax,%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
call   10a0 <strstr@plt>
test   %rax,%rax
je     11ba <addPathToEnvVariable+0x3a>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1060 <strlen@plt>
add    %eax,%ebp
add    $0x2,%ebp
mov    %ebp,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     1218 <addPathToEnvVariable+0x98>
mov    %rax,%r12
movslq %ebp,%rsi
lea    0xe40(%rip),%rdx
mov    %rax,%rdi
mov    %r15,%rcx
mov    %rbx,%r8
call   1070 <snprintf@plt>
mov    %r14,%rdi
mov    %r12,%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    %r12,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <free@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
lea    0xdd8(%rip),%rsi
mov    %r14,%rdx
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <fprintf@plt>

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@stderr = global i32 0
@.str.1 = [42 x i8] c"fatal: Not enough memory to change '%s'!\0A\00"
@.str.2 = [6 x i8] c"%s:%s\00"
define void @addPathToEnvVariable(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = load i8*, i8** %3
%9 = call i64 @getenv(i8* %8)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %5
%11 = load i8*, i8** %5
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %14
13: ; preds = %2
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %5
br label %14
14: ; preds = %13, %2
%15 = load i8*, i8** %5
%16 = load i8*, i8** %4
%17 = call i32* @strstr(i8* %15, i8* %16)
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %48
19: ; preds = %14
%20 = load i8*, i8** %4
%21 = call i32 @strlen(i8* %20)
%22 = load i8*, i8** %5
%23 = call i32 @strlen(i8* %22)
%24 = add nsw i32 %21, %23
%25 = add nsw i32 %24, 2
%26 = sext i32 %25 to i64
store i64 %26, i64* %6
%27 = load i64, i64* %6
%28 = mul i64 1, %27
%29 = trunc i64 %28 to i32
%30 = call i8* @malloc(i32 %29)
store i8* %30, i8** %7
%31 = load i8*, i8** %7
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %37
33: ; preds = %19
%34 = load i32, i32* @stderr
%35 = load i8*, i8** %3
%36 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %35)
br label %48
37: ; preds = %19
%38 = load i8*, i8** %7
%39 = load i64, i64* %6
%40 = load i8*, i8** %4
%41 = load i8*, i8** %5
%42 = call i32 @snprintf(i8* %38, i64 %39, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %40, i8* %41)
%43 = load i8*, i8** %3
%44 = load i8*, i8** %7
%45 = call i32 @setenv(i8* %43, i8* %44, i32 1)
%46 = load i8*, i8** %7
%47 = call i32 @free(i8* %46)
br label %48
48: ; preds = %33, %37, %14
ret void
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@stderr = global i32 0
@.str.1 = [42 x i8] c"fatal: Not enough memory to change '%s'!\0A\00"
@.str.2 = [6 x i8] c"%s:%s\00"
define void @addPathToEnvVariable(i8* %0, i8* %1) {
%3 = tail call i64 @getenv(i8* %0)
%4 = inttoptr i64 %3 to i8*
%5 = icmp eq i64 %3, 0
%6 = select i1 %5, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* %4
%7 = tail call i32* @strstr(i8* dereferenceable(1) %6, i8* dereferenceable(1) %1)
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %24
9: ; preds = %2
%10 = tail call i32 @strlen(i8* %1)
%11 = tail call i32 @strlen(i8* %6)
%12 = add i32 %10, 2
%13 = add i32 %12, %11
%14 = tail call i8* @malloc(i32 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %9
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %24
19: ; preds = %9
%20 = sext i32 %13 to i64
%21 = tail call i32 @snprintf(i8* %14, i64 %20, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %1, i8* %6)
%22 = tail call i32 @setenv(i8* %0, i8* %14, i32 1)
%23 = tail call i32 @free(i8* %14)
br label %24
24: ; preds = %16, %19, %2
ret void
}