#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(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *, ...);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ trim(char *);
void setPath(char *envp[], char *newval) {
  printf("In setpath %s   %s\n", newval, *envp);
  trim(*envp);
  int len = strlen(*envp) + strlen(newval) + 1;
  char *str = malloc(len);
  memset(str, 0, len);
  strcpy(str, *envp);
  printf("str 1 %s\n", str);
  memset(*envp, 0, 1024);
  strcat(str, ":");
  strcat(str, newval);
  printf("str 2 %s\n", str);
  strcpy(*envp, str);
  printf("str 3 %s\n", str);
  printf("new envp %s \n", *envp);
  return;
}


x86 assembly (clang, -O0):
<setPath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%rax
mov    (%rax),%rdx
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1060 <trim@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rsi
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    -0x20(%rbp),%rdi
lea    0xdfa(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strcat@plt>
mov    -0x20(%rbp),%rsi
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rsi
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setPath>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    (%rdi),%rdx
lea    0xe79(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%r15),%rdi
call   1060 <trim@plt>
mov    (%r15),%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%ebp
add    $0x1,%ebp
mov    %ebp,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1070 <memset@plt>
mov    (%r15),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0xe3e(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%r15),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
lea    0xe28(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcat@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1090 <strcat@plt>
lea    0xe10(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%r15),%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
lea    0xdfe(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%r15),%rsi
lea    0xdf4(%rip),%rdi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <printf@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"In setpath %s %s\0A\00"
@.str.1 = [10 x i8] c"str 1 %s\0A\00"
@.str.2 = [2 x i8] c":\00"
@.str.3 = [10 x i8] c"str 2 %s\0A\00"
@.str.4 = [10 x i8] c"str 3 %s\0A\00"
@.str.5 = [14 x i8] c"new envp %s \0A\00"
define void @setPath(i8** %0, i8* %1) {
%3 = alloca i8**
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store i8** %0, i8*** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %4
%8 = load i8**, i8*** %3
%9 = load i8*, i8** %8
%10 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %7, i8* %9)
%11 = load i8**, i8*** %3
%12 = load i8*, i8** %11
%13 = call i32 @trim(i8* %12)
%14 = load i8**, i8*** %3
%15 = load i8*, i8** %14
%16 = call i32 @strlen(i8* %15)
%17 = load i8*, i8** %4
%18 = call i32 @strlen(i8* %17)
%19 = add nsw i32 %16, %18
%20 = add nsw i32 %19, 1
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = call i8* @malloc(i32 %21)
store i8* %22, i8** %6
%23 = load i8*, i8** %6
%24 = load i32, i32* %5
%25 = call i32 @memset(i8* %23, i32 0, i32 %24)
%26 = load i8*, i8** %6
%27 = load i8**, i8*** %3
%28 = load i8*, i8** %27
%29 = call i32 @strcpy(i8* %26, i8* %28)
%30 = load i8*, i8** %6
%31 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %30)
%32 = load i8**, i8*** %3
%33 = load i8*, i8** %32
%34 = call i32 @memset(i8* %33, i32 0, i32 1024)
%35 = load i8*, i8** %6
%36 = call i32 @strcat(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%37 = load i8*, i8** %6
%38 = load i8*, i8** %4
%39 = call i32 @strcat(i8* %37, i8* %38)
%40 = load i8*, i8** %6
%41 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %40)
%42 = load i8**, i8*** %3
%43 = load i8*, i8** %42
%44 = load i8*, i8** %6
%45 = call i32 @strcpy(i8* %43, i8* %44)
%46 = load i8*, i8** %6
%47 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %46)
%48 = load i8**, i8*** %3
%49 = load i8*, i8** %48
%50 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* %49)
ret void
}

LLVM IR (-O2):
@.str = [20 x i8] c"In setpath %s %s\0A\00"
@.str.1 = [10 x i8] c"str 1 %s\0A\00"
@.str.2 = [2 x i8] c":\00"
@.str.3 = [10 x i8] c"str 2 %s\0A\00"
@.str.4 = [10 x i8] c"str 3 %s\0A\00"
@.str.5 = [14 x i8] c"new envp %s \0A\00"
define void @setPath(i8** nocapture %0, i8* %1) {
%3 = load i8*, i8** %0
%4 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %3)
%5 = load i8*, i8** %0
%6 = tail call i32 @trim(i8* %5)
%7 = load i8*, i8** %0
%8 = tail call i32 @strlen(i8* %7)
%9 = tail call i32 @strlen(i8* %1)
%10 = add i32 %8, 1
%11 = add i32 %10, %9
%12 = tail call i8* @malloc(i32 %11)
%13 = tail call i32 @memset(i8* %12, i32 0, i32 %11)
%14 = load i8*, i8** %0
%15 = tail call i32 @strcpy(i8* %12, i8* %14)
%16 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%17 = load i8*, i8** %0
%18 = tail call i32 @memset(i8* %17, i32 0, i32 1024)
%19 = tail call i32 @strcat(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%20 = tail call i32 @strcat(i8* %12, i8* %1)
%21 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %12)
%22 = load i8*, i8** %0
%23 = tail call i32 @strcpy(i8* %22, i8* %12)
%24 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %12)
%25 = load i8*, i8** %0
%26 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* %25)
ret void
}