#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 *);
int /*<<< orphan*/ free_array(char **, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char **strtoken(char *, char *, int *);
int /*<<< orphan*/ trim(char *);
void cmd_set_path(char *cmdpath, char **envp, int path_index, char *envpath) {
  int token_len = 0, i = 0;
  char *sbushPath = (char *)malloc(2000 * sizeof(char));
  sbushPath[0] = '\0';
  char **tokens;
  trim(cmdpath);
  tokens = strtoken(cmdpath, ":", &token_len);
  for (i = 0; i < token_len; i++) {
    if (i > 0)
      strcat(sbushPath, ":");
    if (strcmp(tokens[i], "$PATH") != 0)
      strcat(sbushPath, tokens[i]);
    else
      strcat(sbushPath, envpath);
  }
  strcpy(envp[path_index], "PATH=");
  strcat(envp[path_index], sbushPath);
  free(sbushPath);
  free_array(tokens, token_len);
}


x86 assembly (clang, -O0):
<cmd_set_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    $0x7d0,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
call   1050 <trim@plt>
mov    -0x8(%rbp),%rdi
lea    0xe32(%rip),%rsi
lea    -0x24(%rbp),%rdx
call   1090 <strtoken@plt>
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1264 <cmd_set_path+0xe4>
cmpl   $0x0,-0x28(%rbp)
jle    1208 <cmd_set_path+0x88>
mov    -0x30(%rbp),%rdi
lea    0xdfd(%rip),%rsi
call   10a0 <strcat@plt>
mov    -0x38(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xde7(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1244 <cmd_set_path+0xc4>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   10a0 <strcat@plt>
jmp    1251 <cmd_set_path+0xd1>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   10a0 <strcat@plt>
jmp    1256 <cmd_set_path+0xd6>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11e2 <cmd_set_path+0x62>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xd91(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x30(%rbp),%rsi
call   10a0 <strcat@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1060 <free_array@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_set_path>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r12
mov    %edx,0xc(%rsp)
mov    %rsi,0x10(%rsp)
mov    %rdi,%rbp
movl   $0x0,0x8(%rsp)
mov    $0x7d0,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
movb   $0x0,(%rax)
mov    %rbp,%rdi
call   1050 <trim@plt>
lea    0xe3c(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rbp,%rdi
call   1090 <strtoken@plt>
mov    %rax,%rbp
cmpl   $0x0,0x8(%rsp)
jle    125d <cmd_set_path+0xdd>
mov    0x0(%rbp),%rdi
lea    0xe18(%rip),%rsi
call   1070 <strcmp@plt>
mov    %r12,%rsi
test   %rax,%rax
je     11fb <cmd_set_path+0x7b>
mov    0x0(%rbp),%rsi
mov    %rbx,%rdi
call   10a0 <strcat@plt>
cmpl   $0x2,0x8(%rsp)
jl     125d <cmd_set_path+0xdd>
mov    $0x1,%r14d
lea    0xde9(%rip),%r13
lea    0xde4(%rip),%r15
jmp    1236 <cmd_set_path+0xb6>
mov    %rbx,%rdi
call   10a0 <strcat@plt>
add    $0x1,%r14
movslq 0x8(%rsp),%rax
cmp    %rax,%r14
jge    125d <cmd_set_path+0xdd>
mov    %rbx,%rdi
mov    %r13,%rsi
call   10a0 <strcat@plt>
mov    0x0(%rbp,%r14,8),%rdi
mov    %r15,%rsi
call   1070 <strcmp@plt>
mov    %r12,%rsi
test   %rax,%rax
je     1220 <cmd_set_path+0xa0>
mov    0x0(%rbp,%r14,8),%rsi
jmp    1220 <cmd_set_path+0xa0>
movslq 0xc(%rsp),%r14
mov    0x10(%rsp),%r15
mov    (%r15,%r14,8),%rdi
lea    0xd96(%rip),%rsi
call   1040 <strcpy@plt>
mov    (%r15,%r14,8),%rdi
mov    %rbx,%rsi
call   10a0 <strcat@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%esi
mov    %rbp,%rdi
call   1060 <free_array@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c":\00"
@.str.1 = [6 x i8] c"$PATH\00"
@.str.2 = [6 x i8] c"PATH=\00"
define void @cmd_set_path(i8* %0, i8** nocapture %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%7 = tail call i64 @malloc(i32 2000)
%8 = inttoptr i64 %7 to i8*
store i8 0, i8* %8
%9 = tail call i32 @trim(i8* %0)
%10 = call i8** @strtoken(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32* %5)
%11 = load i32, i32* %5
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %40
13: ; preds = %4
%14 = load i8*, i8** %10
%15 = call i64 @strcmp(i8* %14, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %13
%18 = load i8*, i8** %10
br label %19
19: ; preds = %13, %17
%20 = phi i8* [ %18, %17 ], [ %3, %13 ]
%21 = call i32 @strcat(i8* %8, i8* %20)
%22 = load i32, i32* %5
%23 = icmp sgt i32 %22, 1
br i1 %23, label %24, label %40
24: ; preds = %19, %33
%25 = phi i64 [ %36, %33 ], [ 1, %19 ]
%26 = call i32 @strcat(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%27 = getelementptr inbounds i8*, i8** %10, i64 %25
%28 = load i8*, i8** %27
%29 = call i64 @strcmp(i8* %28, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %24
%32 = load i8*, i8** %27
br label %33
33: ; preds = %24, %31
%34 = phi i8* [ %32, %31 ], [ %3, %24 ]
%35 = call i32 @strcat(i8* %8, i8* %34)
%36 = add nuw nsw i64 %25, 1
%37 = load i32, i32* %5
%38 = sext i32 %37 to i64
%39 = icmp slt i64 %36, %38
br i1 %39, label %24, label %40
40: ; preds = %33, %19, %4
%41 = sext i32 %2 to i64
%42 = getelementptr inbounds i8*, i8** %1, i64 %41
%43 = load i8*, i8** %42
%44 = call i32 @strcpy(i8* %43, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%45 = load i8*, i8** %42
%46 = call i32 @strcat(i8* %45, i8* %8)
%47 = call i32 @free(i8* %8)
%48 = load i32, i32* %5
%49 = call i32 @free_array(i8** %10, i32 %48)
ret void
}