#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(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
char *stpcpy(char *, char const *);
size_t strcspn(char const *, char *);
int strlen(char const *);
scalar_t__ strncmp(char const *, char *, int);
char *strrchr(char *, char);
char *getNewLogicalPwd(const char *oldPwd, const char *dir) {
  if (*dir == '/') {
    oldPwd = "/";
  }
  size_t newSize = strlen(oldPwd) + strlen(dir) + 2;
  char *newPwd = malloc(newSize);
  if (!newPwd)
    return NULL;
  char *pwdEnd = stpcpy(newPwd, oldPwd);
  const char *component = dir;
  size_t componentLength = strcspn(component, "/");
  while (*component) {
    if (componentLength == 0 ||
        (componentLength == 1 && strncmp(component, ".", 1) == 0)) {
    } else if (componentLength == 2 && strncmp(component, "..", 2) == 0) {
      char *lastSlash = strrchr(newPwd, '/');
      if (lastSlash == newPwd) {
        pwdEnd = newPwd + 1;
      } else if (lastSlash) {
        pwdEnd = lastSlash;
      }
    } else {
      if (pwdEnd != newPwd + 1) {
        *pwdEnd++ = '/';
      }
      memcpy(pwdEnd, component, componentLength);
      pwdEnd += componentLength;
    }
    component += componentLength;
    if (*component)
      component++;
    componentLength = strcspn(component, "/");
    *pwdEnd = '\0';
  }
  return newPwd;
}


x86 assembly (clang, -O0):
<getNewLogicalPwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    119b <getNewLogicalPwd+0x2b>
lea    0xe69(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x4c(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e5 <getNewLogicalPwd+0x75>
movq   $0x0,-0x8(%rbp)
jmp    1367 <getNewLogicalPwd+0x1f7>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <stpcpy@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xdf7(%rip),%rsi
call   1070 <strcspn@plt>
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rax
cmpb   $0x0,(%rax)
je     135f <getNewLogicalPwd+0x1ef>
cmpq   $0x0,-0x40(%rbp)
je     1254 <getNewLogicalPwd+0xe4>
cmpq   $0x1,-0x40(%rbp)
jne    1259 <getNewLogicalPwd+0xe9>
mov    -0x38(%rbp),%rdi
lea    0xdc3(%rip),%rsi
mov    $0x1,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1259 <getNewLogicalPwd+0xe9>
jmp    131a <getNewLogicalPwd+0x1aa>
cmpq   $0x2,-0x40(%rbp)
jne    12d1 <getNewLogicalPwd+0x161>
mov    -0x38(%rbp),%rdi
lea    0xd93(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    12d1 <getNewLogicalPwd+0x161>
mov    -0x28(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x28(%rbp),%rax
jne    12b4 <getNewLogicalPwd+0x144>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    12cc <getNewLogicalPwd+0x15c>
cmpq   $0x0,-0x48(%rbp)
je     12c7 <getNewLogicalPwd+0x157>
mov    -0x48(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    12cc <getNewLogicalPwd+0x15c>
jmp    1315 <getNewLogicalPwd+0x1a5>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
add    $0x1,%rcx
cmp    %rcx,%rax
je     12f8 <getNewLogicalPwd+0x188>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movb   $0x2f,(%rax)
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%rdx
call   1080 <memcpy@plt>
mov    -0x40(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    131a <getNewLogicalPwd+0x1aa>
mov    -0x40(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmpb   $0x0,(%rax)
je     133f <getNewLogicalPwd+0x1cf>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xcb6(%rip),%rsi
call   1070 <strcspn@plt>
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
jmp    1212 <getNewLogicalPwd+0xa2>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getNewLogicalPwd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
cmpb   $0x2f,(%rsi)
lea    0xe78(%rip),%rbp
cmovne %rdi,%rbp
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %eax,%r14d
mov    %r15,%rdi
call   1050 <strlen@plt>
add    %r14d,%eax
add    $0x2,%eax
movslq %eax,%rdi
call   1090 <malloc@plt>
mov    %rax,(%rsp)
test   %rax,%rax
je     12cc <getNewLogicalPwd+0x15c>
mov    (%rsp),%rdi
mov    %rbp,%rsi
call   1040 <stpcpy@plt>
cmpb   $0x0,(%r15)
je     12cc <getNewLogicalPwd+0x15c>
mov    %rax,%r13
lea    0xe26(%rip),%rsi
mov    %r15,%rdi
call   1070 <strcspn@plt>
mov    %rax,%rbp
mov    (%rsp),%rax
lea    0x1(%rax),%r14
jmp    1226 <getNewLogicalPwd+0xb6>
nop
mov    %r13,%r12
lea    (%r15,%rbp,1),%rbx
cmpb   $0x1,(%r15,%rbp,1)
sbb    $0xffffffffffffffff,%rbx
mov    %rbx,%rdi
lea    0xdf6(%rip),%rsi
call   1070 <strcspn@plt>
mov    %rax,%rbp
movb   $0x0,(%r12)
cmpb   $0x0,(%rbx)
mov    %rbx,%r15
mov    %r12,%r13
je     12cc <getNewLogicalPwd+0x15c>
test   %rbp,%rbp
je     11f0 <getNewLogicalPwd+0x80>
cmp    $0x2,%rbp
je     1260 <getNewLogicalPwd+0xf0>
cmp    $0x1,%rbp
jne    1279 <getNewLogicalPwd+0x109>
mov    %r15,%rdi
lea    0xdc2(%rip),%rsi
mov    $0x1,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1279 <getNewLogicalPwd+0x109>
jmp    11f0 <getNewLogicalPwd+0x80>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15,%rdi
lea    0xd98(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     12a3 <getNewLogicalPwd+0x133>
mov    %r14,%r12
cmp    %r14,%r13
je     128d <getNewLogicalPwd+0x11d>
movb   $0x2f,0x0(%r13)
add    $0x1,%r13
mov    %r13,%r12
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rbp,%rdx
call   1080 <memcpy@plt>
add    %rbp,%r12
jmp    11f3 <getNewLogicalPwd+0x83>
mov    (%rsp),%rbx
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %r14,%r12
cmp    %rbx,%rax
je     11f3 <getNewLogicalPwd+0x83>
test   %rax,%rax
cmovne %rax,%r13
jmp    11f0 <getNewLogicalPwd+0x80>
mov    (%rsp),%rax
add    $0x8,%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 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
define i8* @getNewLogicalPwd(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%12 = load i8*, i8** %5
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp eq i32 %14, 47
br i1 %15, label %16, label %17
16: ; preds = %2
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %4
br label %17
17: ; preds = %16, %2
%18 = load i8*, i8** %4
%19 = call i32 @strlen(i8* %18)
%20 = load i8*, i8** %5
%21 = call i32 @strlen(i8* %20)
%22 = add nsw i32 %19, %21
%23 = add nsw i32 %22, 2
%24 = sext i32 %23 to i64
store i64 %24, i64* %6
%25 = load i64, i64* %6
%26 = call i8* @malloc(i64 %25)
store i8* %26, i8** %7
%27 = load i8*, i8** %7
%28 = icmp ne i8* %27, null
br i1 %28, label %30, label %29
29: ; preds = %17
store i8* null, i8** %3
br label %108
30: ; preds = %17
%31 = load i8*, i8** %7
%32 = load i8*, i8** %4
%33 = call i8* @stpcpy(i8* %31, i8* %32)
store i8* %33, i8** %8
%34 = load i8*, i8** %5
store i8* %34, i8** %9
%35 = load i8*, i8** %9
%36 = call i64 @strcspn(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i64 %36, i64* %10
br label %37
37: ; preds = %102, %30
%38 = load i8*, i8** %9
%39 = load i8, i8* %38
%40 = icmp ne i8 %39, 0
br i1 %40, label %41, label %106
41: ; preds = %37
%42 = load i64, i64* %10
%43 = icmp eq i64 %42, 0
br i1 %43, label %51, label %44
44: ; preds = %41
%45 = load i64, i64* %10
%46 = icmp eq i64 %45, 1
br i1 %46, label %47, label %52
47: ; preds = %44
%48 = load i8*, i8** %9
%49 = call i64 @strncmp(i8* %48, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %47, %41
br label %92
52: ; preds = %47, %44
%53 = load i64, i64* %10
%54 = icmp eq i64 %53, 2
br i1 %54, label %55, label %75
55: ; preds = %52
%56 = load i8*, i8** %9
%57 = call i64 @strncmp(i8* %56, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%58 = icmp eq i64 %57, 0
br i1 %58, label %59, label %75
59: ; preds = %55
%60 = load i8*, i8** %7
%61 = call i8* @strrchr(i8* %60, i8 signext 47)
store i8* %61, i8** %11
%62 = load i8*, i8** %11
%63 = load i8*, i8** %7
%64 = icmp eq i8* %62, %63
br i1 %64, label %65, label %68
65: ; preds = %59
%66 = load i8*, i8** %7
%67 = getelementptr inbounds i8, i8* %66, i64 1
store i8* %67, i8** %8
br label %74
68: ; preds = %59
%69 = load i8*, i8** %11
%70 = icmp ne i8* %69, null
br i1 %70, label %71, label %73
71: ; preds = %68
%72 = load i8*, i8** %11
store i8* %72, i8** %8
br label %73
73: ; preds = %71, %68
br label %74
74: ; preds = %73, %65
br label %91
75: ; preds = %55, %52
%76 = load i8*, i8** %8
%77 = load i8*, i8** %7
%78 = getelementptr inbounds i8, i8* %77, i64 1
%79 = icmp ne i8* %76, %78
br i1 %79, label %80, label %83
80: ; preds = %75
%81 = load i8*, i8** %8
%82 = getelementptr inbounds i8, i8* %81, i32 1
store i8* %82, i8** %8
store i8 47, i8* %81
br label %83
83: ; preds = %80, %75
%84 = load i8*, i8** %8
%85 = load i8*, i8** %9
%86 = load i64, i64* %10
%87 = call i32 @memcpy(i8* %84, i8* %85, i64 %86)
%88 = load i64, i64* %10
%89 = load i8*, i8** %8
%90 = getelementptr inbounds i8, i8* %89, i64 %88
store i8* %90, i8** %8
br label %91
91: ; preds = %83, %74
br label %92
92: ; preds = %91, %51
%93 = load i64, i64* %10
%94 = load i8*, i8** %9
%95 = getelementptr inbounds i8, i8* %94, i64 %93
store i8* %95, i8** %9
%96 = load i8*, i8** %9
%97 = load i8, i8* %96
%98 = icmp ne i8 %97, 0
br i1 %98, label %99, label %102
99: ; preds = %92
%100 = load i8*, i8** %9
%101 = getelementptr inbounds i8, i8* %100, i32 1
store i8* %101, i8** %9
br label %102
102: ; preds = %99, %92
%103 = load i8*, i8** %9
%104 = call i64 @strcspn(i8* %103, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i64 %104, i64* %10
%105 = load i8*, i8** %8
store i8 0, i8* %105
br label %37
106: ; preds = %37
%107 = load i8*, i8** %7
store i8* %107, i8** %3
br label %108
108: ; preds = %106, %29
%109 = load i8*, i8** %3
ret i8* %109
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
define i8* @getNewLogicalPwd(i8* %0, i8* %1) {
%3 = load i8, i8* %1
%4 = icmp eq i8 %3, 47
%5 = select i1 %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %0
%6 = tail call i32 @strlen(i8* %5)
%7 = tail call i32 @strlen(i8* %1)
%8 = add i32 %6, 2
%9 = add i32 %8, %7
%10 = sext i32 %9 to i64
%11 = tail call i8* @malloc(i64 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %54, label %13
13: ; preds = %2
%14 = tail call i8* @stpcpy(i8* %11, i8* %5)
%15 = load i8, i8* %1
%16 = icmp eq i8 %15, 0
br i1 %16, label %54, label %17
17: ; preds = %13
%18 = tail call i64 @strcspn(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%19 = getelementptr inbounds i8, i8* %11, i64 1
br label %20
20: ; preds = %17, %44
%21 = phi i64 [ %18, %17 ], [ %51, %44 ]
%22 = phi i8* [ %1, %17 ], [ %50, %44 ]
%23 = phi i8* [ %14, %17 ], [ %45, %44 ]
switch i64 %21, label %36 [
i64 0, label %44
i64 1, label %24
i64 2, label %27
]
24: ; preds = %20
%25 = tail call i64 @strncmp(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%26 = icmp eq i64 %25, 0
br i1 %26, label %44, label %36
27: ; preds = %20
%28 = tail call i64 @strncmp(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %36
30: ; preds = %27
%31 = tail call i8* @strrchr(i8* %11, i8 signext 47)
%32 = icmp eq i8* %31, %11
br i1 %32, label %44, label %33
33: ; preds = %30
%34 = icmp eq i8* %31, null
%35 = select i1 %34, i8* %23, i8* %31
br label %44
36: ; preds = %24, %20, %27
%37 = icmp eq i8* %23, %19
br i1 %37, label %40, label %38
38: ; preds = %36
%39 = getelementptr inbounds i8, i8* %23, i64 1
store i8 47, i8* %23
br label %40
40: ; preds = %38, %36
%41 = phi i8* [ %39, %38 ], [ %19, %36 ]
%42 = tail call i32 @memcpy(i8* %41, i8* %22, i64 %21)
%43 = getelementptr inbounds i8, i8* %41, i64 %21
br label %44
44: ; preds = %30, %33, %20, %40, %24
%45 = phi i8* [ %23, %20 ], [ %23, %24 ], [ %43, %40 ], [ %35, %33 ], [ %19, %30 ]
%46 = getelementptr inbounds i8, i8* %22, i64 %21
%47 = load i8, i8* %46
%48 = icmp ne i8 %47, 0
%49 = zext i1 %48 to i64
%50 = getelementptr i8, i8* %46, i64 %49
%51 = tail call i64 @strcspn(i8* %50, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8 0, i8* %45
%52 = load i8, i8* %50
%53 = icmp eq i8 %52, 0
br i1 %53, label %54, label %20
54: ; preds = %44, %13, %2
ret i8* %11
}