#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*/ getcwd(char *, int);
char *malloc(int);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ parsePATH(char **, char *, char *, char *, char *);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
void setPS1(char *new_ps, char *envp[], char *new_path) {
  int i = 0, j = 0, esc_flag = 2;
  char *curr_path;
  char home[128];
  char path[256];
  char user[22], hostname[22];
  curr_path = malloc(128 * sizeof(char));
  getcwd(curr_path, 128);
  memset(new_path, '\0', 128);
  parsePATH(envp, home, path, user, hostname);
  while (new_ps[i] != '\0') {
    if (new_ps[i] == '\\') {
      esc_flag = 1;
    } else if (new_ps[i] == 'h' && esc_flag == 1) {
      strcat(new_path, hostname);
      esc_flag = 0;
      j = j + strlen(hostname);
    } else if (new_ps[i] == 'u' && esc_flag == 1) {
      strcat(new_path, user);
      esc_flag = 0;
      j = j + strlen(user);
    } else if (new_ps[i] == 'w' && esc_flag == 1) {
      strcat(new_path, curr_path);
      esc_flag = 0;
      j = j + strlen(curr_path);
    } else {
      new_path[j] = new_ps[i];
      j++;
    }
    i++;
  }
}


x86 assembly (clang, -O0):
<setPS1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x200,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x2,-0x24(%rbp)
mov    $0x80,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    $0x80,%esi
call   1060 <getcwd@plt>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    -0xb0(%rbp),%rsi
lea    -0x1b0(%rbp),%rdx
lea    -0x1d0(%rbp),%rcx
lea    -0x1f0(%rbp),%r8
call   1030 <parsePATH@plt>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     1360 <setPS1+0x200>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x5c,%eax
jne    1213 <setPS1+0xb3>
movl   $0x1,-0x24(%rbp)
jmp    1352 <setPS1+0x1f2>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x68,%eax
jne    1270 <setPS1+0x110>
cmpl   $0x1,-0x24(%rbp)
jne    1270 <setPS1+0x110>
mov    -0x18(%rbp),%rdi
lea    -0x1f0(%rbp),%rsi
call   1080 <strcat@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x1f4(%rbp)
lea    -0x1f0(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x1f4(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x20(%rbp)
jmp    134d <setPS1+0x1ed>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x75,%eax
jne    12cd <setPS1+0x16d>
cmpl   $0x1,-0x24(%rbp)
jne    12cd <setPS1+0x16d>
mov    -0x18(%rbp),%rdi
lea    -0x1d0(%rbp),%rsi
call   1080 <strcat@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x1f8(%rbp)
lea    -0x1d0(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x1f8(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x20(%rbp)
jmp    1348 <setPS1+0x1e8>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x77,%eax
jne    1324 <setPS1+0x1c4>
cmpl   $0x1,-0x24(%rbp)
jne    1324 <setPS1+0x1c4>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1080 <strcat@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x1fc(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x1fc(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x20(%rbp)
jmp    1343 <setPS1+0x1e3>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1348 <setPS1+0x1e8>
jmp    134d <setPS1+0x1ed>
jmp    1352 <setPS1+0x1f2>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11dd <setPS1+0x7d>
add    $0x200,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setPS1>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1d8,%rsp
mov    %rdx,%r14
mov    %rsi,0x8(%rsp)
mov    %rdi,%rbx
mov    $0x80,%edi
call   1070 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x80,%esi
call   1060 <getcwd@plt>
xor    %ebp,%ebp
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
lea    0x50(%rsp),%rsi
lea    0xd0(%rsp),%rdx
lea    0x30(%rsp),%r12
lea    0x10(%rsp),%r13
mov    0x8(%rsp),%rdi
mov    %r12,%rcx
mov    %r13,%r8
call   1030 <parsePATH@plt>
mov    $0x2,%ecx
jmp    11e9 <setPS1+0x89>
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0x1,%ecx
add    $0x1,%rbx
movzbl (%rbx),%eax
cmp    $0x5c,%al
je     11e0 <setPS1+0x80>
test   %al,%al
je     1261 <setPS1+0x101>
cmp    $0x68,%al
jne    1210 <setPS1+0xb0>
cmp    $0x1,%ecx
jne    1210 <setPS1+0xb0>
mov    %r14,%rdi
mov    %r13,%rsi
call   1080 <strcat@plt>
mov    %r13,%rdi
jmp    1240 <setPS1+0xe0>
nopl   (%rax)
cmp    $0x75,%al
jne    1229 <setPS1+0xc9>
cmp    $0x1,%ecx
jne    1229 <setPS1+0xc9>
mov    %r14,%rdi
mov    %r12,%rsi
call   1080 <strcat@plt>
mov    %r12,%rdi
jmp    1240 <setPS1+0xe0>
cmp    $0x77,%al
jne    1251 <setPS1+0xf1>
cmp    $0x1,%ecx
jne    1251 <setPS1+0xf1>
mov    %r14,%rdi
mov    %r15,%rsi
call   1080 <strcat@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
add    %ebp,%eax
xor    %ecx,%ecx
mov    %eax,%ebp
add    $0x1,%rbx
jmp    11e9 <setPS1+0x89>
movslq %ebp,%rbp
mov    %al,(%r14,%rbp,1)
add    $0x1,%ebp
add    $0x1,%rbx
jmp    11e9 <setPS1+0x89>
add    $0x1d8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @setPS1(i8* %0, i8** %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca [128 x i8]
%12 = alloca [256 x i8]
%13 = alloca [22 x i8]
%14 = alloca [22 x i8]
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i8* %2, i8** %6
store i32 0, i32* %7
store i32 0, i32* %8
store i32 2, i32* %9
%15 = call i8* @malloc(i32 128)
store i8* %15, i8** %10
%16 = load i8*, i8** %10
%17 = call i32 @getcwd(i8* %16, i32 128)
%18 = load i8*, i8** %6
%19 = call i32 @memset(i8* %18, i8 signext 0, i32 128)
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%23 = getelementptr inbounds [22 x i8], [22 x i8]* %13, i64 0, i64 0
%24 = getelementptr inbounds [22 x i8], [22 x i8]* %14, i64 0, i64 0
%25 = call i32 @parsePATH(i8** %20, i8* %21, i8* %22, i8* %23, i8* %24)
br label %26
26: ; preds = %115, %3
%27 = load i8*, i8** %4
%28 = load i32, i32* %7
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %118
34: ; preds = %26
%35 = load i8*, i8** %4
%36 = load i32, i32* %7
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, 92
br i1 %41, label %42, label %43
42: ; preds = %34
store i32 1, i32* %9
br label %115
43: ; preds = %34
%44 = load i8*, i8** %4
%45 = load i32, i32* %7
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
%48 = load i8, i8* %47
%49 = sext i8 %48 to i32
%50 = icmp eq i32 %49, 104
br i1 %50, label %51, label %62
51: ; preds = %43
%52 = load i32, i32* %9
%53 = icmp eq i32 %52, 1
br i1 %53, label %54, label %62
54: ; preds = %51
%55 = load i8*, i8** %6
%56 = getelementptr inbounds [22 x i8], [22 x i8]* %14, i64 0, i64 0
%57 = call i32 @strcat(i8* %55, i8* %56)
store i32 0, i32* %9
%58 = load i32, i32* %8
%59 = getelementptr inbounds [22 x i8], [22 x i8]* %14, i64 0, i64 0
%60 = call i32 @strlen(i8* %59)
%61 = add nsw i32 %58, %60
store i32 %61, i32* %8
br label %114
62: ; preds = %51, %43
%63 = load i8*, i8** %4
%64 = load i32, i32* %7
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %63, i64 %65
%67 = load i8, i8* %66
%68 = sext i8 %67 to i32
%69 = icmp eq i32 %68, 117
br i1 %69, label %70, label %81
70: ; preds = %62
%71 = load i32, i32* %9
%72 = icmp eq i32 %71, 1
br i1 %72, label %73, label %81
73: ; preds = %70
%74 = load i8*, i8** %6
%75 = getelementptr inbounds [22 x i8], [22 x i8]* %13, i64 0, i64 0
%76 = call i32 @strcat(i8* %74, i8* %75)
store i32 0, i32* %9
%77 = load i32, i32* %8
%78 = getelementptr inbounds [22 x i8], [22 x i8]* %13, i64 0, i64 0
%79 = call i32 @strlen(i8* %78)
%80 = add nsw i32 %77, %79
store i32 %80, i32* %8
br label %113
81: ; preds = %70, %62
%82 = load i8*, i8** %4
%83 = load i32, i32* %7
%84 = sext i32 %83 to i64
%85 = getelementptr inbounds i8, i8* %82, i64 %84
%86 = load i8, i8* %85
%87 = sext i8 %86 to i32
%88 = icmp eq i32 %87, 119
br i1 %88, label %89, label %100
89: ; preds = %81
%90 = load i32, i32* %9
%91 = icmp eq i32 %90, 1
br i1 %91, label %92, label %100
92: ; preds = %89
%93 = load i8*, i8** %6
%94 = load i8*, i8** %10
%95 = call i32 @strcat(i8* %93, i8* %94)
store i32 0, i32* %9
%96 = load i32, i32* %8
%97 = load i8*, i8** %10
%98 = call i32 @strlen(i8* %97)
%99 = add nsw i32 %96, %98
store i32 %99, i32* %8
br label %112
100: ; preds = %89, %81
%101 = load i8*, i8** %4
%102 = load i32, i32* %7
%103 = sext i32 %102 to i64
%104 = getelementptr inbounds i8, i8* %101, i64 %103
%105 = load i8, i8* %104
%106 = load i8*, i8** %6
%107 = load i32, i32* %8
%108 = sext i32 %107 to i64
%109 = getelementptr inbounds i8, i8* %106, i64 %108
store i8 %105, i8* %109
%110 = load i32, i32* %8
%111 = add nsw i32 %110, 1
store i32 %111, i32* %8
br label %112
112: ; preds = %100, %92
br label %113
113: ; preds = %112, %73
br label %114
114: ; preds = %113, %54
br label %115
115: ; preds = %114, %42
%116 = load i32, i32* %7
%117 = add nsw i32 %116, 1
store i32 %117, i32* %7
br label %26
118: ; preds = %26
ret void
}

LLVM IR (-O2):
define void @setPS1(i8* nocapture %0, i8** %1, i8* %2) {
%4 = alloca [128 x i8]
%5 = alloca [256 x i8]
%6 = alloca [22 x i8]
%7 = alloca [22 x i8]
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%10 = getelementptr inbounds [22 x i8], [22 x i8]* %6, i64 0, i64 0
%11 = getelementptr inbounds [22 x i8], [22 x i8]* %7, i64 0, i64 0
%12 = tail call dereferenceable_or_null(128) i8* @malloc(i32 128)
%13 = tail call i32 @getcwd(i8* %12, i32 128)
%14 = tail call i32 @memset(i8* %2, i8 signext 0, i32 128)
%15 = call i32 @parsePATH(i8** %1, i8* %8, i8* %9, i8* %10, i8* %11)
br label %16
16: ; preds = %48, %3
%17 = phi i64 [ %51, %48 ], [ 0, %3 ]
%18 = phi i32 [ %49, %48 ], [ 0, %3 ]
%19 = phi i32 [ %50, %48 ], [ 2, %3 ]
%20 = getelementptr inbounds i8, i8* %0, i64 %17
%21 = load i8, i8* %20
switch i8 %21, label %22 [
i8 0, label %52
i8 92, label %48
]
22: ; preds = %16
%23 = icmp eq i8 %21, 104
%24 = icmp eq i32 %19, 1
%25 = select i1 %23, i1 %24, i1 false
br i1 %25, label %26, label %30
26: ; preds = %22
%27 = call i32 @strcat(i8* %2, i8* %11)
%28 = call i32 @strlen(i8* %11)
%29 = add nsw i32 %28, %18
br label %48
30: ; preds = %22
%31 = icmp eq i8 %21, 117
%32 = select i1 %31, i1 %24, i1 false
br i1 %32, label %33, label %37
33: ; preds = %30
%34 = call i32 @strcat(i8* %2, i8* %10)
%35 = call i32 @strlen(i8* %10)
%36 = add nsw i32 %35, %18
br label %48
37: ; preds = %30
%38 = icmp eq i8 %21, 119
%39 = select i1 %38, i1 %24, i1 false
br i1 %39, label %40, label %44
40: ; preds = %37
%41 = call i32 @strcat(i8* %2, i8* %12)
%42 = call i32 @strlen(i8* %12)
%43 = add nsw i32 %42, %18
br label %48
44: ; preds = %37
%45 = sext i32 %18 to i64
%46 = getelementptr inbounds i8, i8* %2, i64 %45
store i8 %21, i8* %46
%47 = add nsw i32 %18, 1
br label %48
48: ; preds = %16, %26, %40, %44, %33
%49 = phi i32 [ %29, %26 ], [ %36, %33 ], [ %43, %40 ], [ %47, %44 ], [ %18, %16 ]
%50 = phi i32 [ 0, %26 ], [ 0, %33 ], [ 0, %40 ], [ %19, %44 ], [ 1, %16 ]
%51 = add nuw i64 %17, 1
br label %16
52: ; preds = %16
ret void
}