#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 last_index(char *, char);
int /*<<< orphan*/ sprintf(char *, char *, ...);
scalar_t__ strcmp(char *, char *);
void next_dir(char *path, char *new_dir) {
  if (!strcmp(new_dir, "..")) {
    int slash_index = last_index(path, '/');
    if (!slash_index)
      sprintf(path, "/");
    else
      path[slash_index] = '\0';
  } else if (strcmp(new_dir, ".")) {
    if (!strcmp(path, "/"))
      path[0] = '\0';
    sprintf(path, "%s/%s", path, new_dir);
  }
}


x86 assembly (clang, -O0):
<next_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb5(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    119d <next_dir+0x6d>
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1030 <last_index@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    118c <next_dir+0x5c>
mov    -0x8(%rbp),%rdi
lea    0xe83(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
jmp    1198 <next_dir+0x68>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11f7 <next_dir+0xc7>
mov    -0x10(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     11f2 <next_dir+0xc2>
mov    -0x8(%rbp),%rdi
lea    0xe41(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    11d8 <next_dir+0xa8>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe1a(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
jmp    11f7 <next_dir+0xc7>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<next_dir>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xebf(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
je     1198 <next_dir+0x68>
lea    0xeac(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11b8 <next_dir+0x88>
lea    0xe9a(%rip),%rsi
mov    %r14,%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
jne    117a <next_dir+0x4a>
movb   $0x0,(%r14)
lea    0xe84(%rip),%rsi
mov    %r14,%rdi
mov    %r14,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <sprintf@plt>
mov    %r14,%rdi
mov    $0x2f,%esi
call   1030 <last_index@plt>
test   %eax,%eax
je     11b2 <next_dir+0x82>
cltq
movb   $0x0,(%r14,%rax,1)
jmp    11b8 <next_dir+0x88>
movw   $0x2f,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [3 x i8] c"..\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [6 x i8] c"%s/%s\00"
define void @next_dir(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%6 = load i8*, i8** %4
%7 = call i64 @strcmp(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%8 = icmp ne i64 %7, 0
br i1 %8, label %23, label %9
9: ; preds = %2
%10 = load i8*, i8** %3
%11 = call i32 @last_index(i8* %10, i8 signext 47)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp ne i32 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %9
%15 = load i8*, i8** %3
%16 = call i32 (i8*, i8*, ...) @sprintf(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %22
17: ; preds = %9
%18 = load i8*, i8** %3
%19 = load i32, i32* %5
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
store i8 0, i8* %21
br label %22
22: ; preds = %17, %14
br label %40
23: ; preds = %2
%24 = load i8*, i8** %4
%25 = call i64 @strcmp(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %39
27: ; preds = %23
%28 = load i8*, i8** %3
%29 = call i64 @strcmp(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%30 = icmp ne i64 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %27
%32 = load i8*, i8** %3
%33 = getelementptr inbounds i8, i8* %32, i64 0
store i8 0, i8* %33
br label %34
34: ; preds = %31, %27
%35 = load i8*, i8** %3
%36 = load i8*, i8** %3
%37 = load i8*, i8** %4
%38 = call i32 (i8*, i8*, ...) @sprintf(i8* %35, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %36, i8* %37)
br label %39
39: ; preds = %34, %23
br label %40
40: ; preds = %39, %22
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"..\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [6 x i8] c"%s/%s\00"
define void @next_dir(i8* %0, i8* %1) {
%3 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %13
5: ; preds = %2
%6 = tail call i32 @last_index(i8* %0, i8 signext 47)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = bitcast i8* %0 to i16*
store i16 47, i16* %9
br label %22
10: ; preds = %5
%11 = sext i32 %6 to i64
%12 = getelementptr inbounds i8, i8* %0, i64 %11
store i8 0, i8* %12
br label %22
13: ; preds = %2
%14 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
br i1 %15, label %22, label %16
16: ; preds = %13
%17 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %16
store i8 0, i8* %0
br label %20
20: ; preds = %19, %16
%21 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %1)
br label %22
22: ; preds = %8, %10, %13, %20
ret void
}