#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*/ setenv(char const *, char *, int);
int /*<<< orphan*/ strlcat(char *, char const *, size_t);
int /*<<< orphan*/ strlcpy(char *, char const *, size_t);
int strlen(char const *);
int prependToPath(const char *name, const char *prepend) {
  const char *old_value = getenv(name);
  char *new_value = NULL;
  if (old_value == NULL) {
    size_t max_length = strlen(prepend) + 1;
    new_value = malloc(max_length);
    strlcpy(new_value, prepend, max_length);
  } else {
    size_t max_length = strlen(old_value) + strlen(prepend) + 2;
    new_value = malloc(max_length);
    strlcpy(new_value, prepend, max_length);
    strlcat(new_value, ":", max_length);
    strlcat(new_value, old_value, max_length);
  }
  setenv(name, new_value, 1);
  free(new_value);
  return 0;
}


x86 assembly (clang, -O0):
<prependToPath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d5 <prependToPath+0x65>
mov    -0x10(%rbp),%rdi
call   1070 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1090 <strlcpy@plt>
jmp    123d <prependToPath+0xcd>
mov    -0x18(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
cltq
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1090 <strlcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rdx
lea    0xdd9(%rip),%rsi
call   1060 <strlcat@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1060 <strlcat@plt>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prependToPath>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
call   1030 <getenv@plt>
test   %rax,%rax
je     11e1 <prependToPath+0x71>
mov    %rax,%r12
mov    %rax,%rdi
call   1070 <strlen@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1070 <strlen@plt>
add    %ebx,%eax
add    $0x2,%eax
movslq %eax,%r13
mov    %r13,%rdi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
call   1090 <strlcpy@plt>
lea    0xe3a(%rip),%rsi
mov    %rbx,%rdi
mov    %r13,%rdx
call   1060 <strlcat@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1060 <strlcat@plt>
jmp    1208 <prependToPath+0x98>
mov    %r15,%rdi
call   1070 <strlen@plt>
add    $0x1,%eax
movslq %eax,%r12
mov    %r12,%rdi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1090 <strlcpy@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    %rbx,%rdi
call   1040 <free@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c":\00"
define i32 @prependToPath(i8* %0, i8* %1) {
%3 = tail call i8* @getenv(i8* %0)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %11
5: ; preds = %2
%6 = tail call i32 @strlen(i8* %1)
%7 = add nsw i32 %6, 1
%8 = sext i32 %7 to i64
%9 = tail call i8* @malloc(i64 %8)
%10 = tail call i32 @strlcpy(i8* %9, i8* %1, i64 %8)
br label %21
11: ; preds = %2
%12 = tail call i32 @strlen(i8* %3)
%13 = tail call i32 @strlen(i8* %1)
%14 = add i32 %12, 2
%15 = add i32 %14, %13
%16 = sext i32 %15 to i64
%17 = tail call i8* @malloc(i64 %16)
%18 = tail call i32 @strlcpy(i8* %17, i8* %1, i64 %16)
%19 = tail call i32 @strlcat(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i64 %16)
%20 = tail call i32 @strlcat(i8* %17, i8* %3, i64 %16)
br label %21
21: ; preds = %11, %5
%22 = phi i8* [ %9, %5 ], [ %17, %11 ]
%23 = tail call i32 @setenv(i8* %0, i8* %22, i32 1)
%24 = tail call i32 @free(i8* %22)
ret i32 0
}