#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*/ kl_streq(char *, char *);
char *kl_strndup(char const *, int);
int /*<<< orphan*/ strcat(char *, char *);
int strcspn(char const *, char *);
char *strrchr(char *, char);
int /*<<< orphan*/ strspn(char const *, char *);
void append_path(char *path, const char *path_in) {
  int path_nodes = 0;
  while (*path_in) {
    int len = strcspn(path_in, "/");
    char *node = kl_strndup(path_in, len);
    path_in += len;
    path_in += strspn(path_in, "/");
    if (len) {
      if (!kl_streq(node, "..")) {
        strcat(path, "/");
        strcat(path, node);
        path_nodes++;
      } else if (path_nodes) {
        strrchr(path, '/')[0] = '\0';
        path_nodes--;
      }
    }
    free(node);
  }
}


x86 assembly (clang, -O0):
<append_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     1272 <append_path+0x102>
mov    -0x10(%rbp),%rdi
lea    0xe61(%rip),%rsi
call   1070 <strcspn@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1080 <kl_strndup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe2d(%rip),%rsi
call   1060 <strspn@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1264 <append_path+0xf4>
mov    -0x20(%rbp),%rdi
lea    0xe05(%rip),%rsi
call   1040 <kl_streq@plt>
cmp    $0x0,%eax
jne    1236 <append_path+0xc6>
mov    -0x8(%rbp),%rdi
lea    0xdea(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1090 <strcat@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    125f <append_path+0xef>
cmpl   $0x0,-0x14(%rbp)
je     125a <append_path+0xea>
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
movb   $0x0,(%rax)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    125f <append_path+0xef>
jmp    1264 <append_path+0xf4>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    1187 <append_path+0x17>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_path>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,(%rsp)
cmpb   $0x0,(%rsi)
je     1238 <append_path+0xc8>
mov    %rsi,%rbx
xor    %r15d,%r15d
lea    0xe6b(%rip),%r12
jmp    11cb <append_path+0x5b>
nopw   0x0(%rax,%rax,1)
mov    (%rsp),%rbp
mov    %rbp,%rdi
mov    %r12,%rsi
call   1090 <strcat@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
call   1090 <strcat@plt>
add    $0x1,%r15d
mov    %r13,%rdi
call   1030 <free@plt>
cmpb   $0x0,(%rbx)
je     1238 <append_path+0xc8>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1070 <strcspn@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %eax,%esi
call   1080 <kl_strndup@plt>
mov    %rax,%r13
movslq %ebp,%r14
mov    %rbx,%rbp
add    %r14,%rbp
mov    %rbp,%rdi
mov    %r12,%rsi
call   1060 <strspn@plt>
movslq %eax,%rbx
add    %rbp,%rbx
test   %r14d,%r14d
je     11be <append_path+0x4e>
mov    %r13,%rdi
lea    0xdf4(%rip),%rsi
call   1040 <kl_streq@plt>
test   %eax,%eax
je     11a0 <append_path+0x30>
test   %r15d,%r15d
je     1233 <append_path+0xc3>
mov    (%rsp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
movb   $0x0,(%rax)
add    $0xffffffff,%r15d
jmp    11be <append_path+0x4e>
xor    %r15d,%r15d
jmp    11be <append_path+0x4e>
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 = [3 x i8] c"..\00"
define void @append_path(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %5
br label %8
8: ; preds = %52, %2
%9 = load i8*, i8** %4
%10 = load i8, i8* %9
%11 = icmp ne i8 %10, 0
br i1 %11, label %12, label %55
12: ; preds = %8
%13 = load i8*, i8** %4
%14 = call i32 @strcspn(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32 %14, i32* %6
%15 = load i8*, i8** %4
%16 = load i32, i32* %6
%17 = call i8* @kl_strndup(i8* %15, i32 %16)
store i8* %17, i8** %7
%18 = load i32, i32* %6
%19 = load i8*, i8** %4
%20 = sext i32 %18 to i64
%21 = getelementptr inbounds i8, i8* %19, i64 %20
store i8* %21, i8** %4
%22 = load i8*, i8** %4
%23 = call i32 @strspn(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%24 = load i8*, i8** %4
%25 = sext i32 %23 to i64
%26 = getelementptr inbounds i8, i8* %24, i64 %25
store i8* %26, i8** %4
%27 = load i32, i32* %6
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %52
29: ; preds = %12
%30 = load i8*, i8** %7
%31 = call i32 @kl_streq(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%32 = icmp ne i32 %31, 0
br i1 %32, label %41, label %33
33: ; preds = %29
%34 = load i8*, i8** %3
%35 = call i32 @strcat(i8* %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%36 = load i8*, i8** %3
%37 = load i8*, i8** %7
%38 = call i32 @strcat(i8* %36, i8* %37)
%39 = load i32, i32* %5
%40 = add nsw i32 %39, 1
store i32 %40, i32* %5
br label %51
41: ; preds = %29
%42 = load i32, i32* %5
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %41
%45 = load i8*, i8** %3
%46 = call i8* @strrchr(i8* %45, i8 signext 47)
%47 = getelementptr inbounds i8, i8* %46, i64 0
store i8 0, i8* %47
%48 = load i32, i32* %5
%49 = add nsw i32 %48, -1
store i32 %49, i32* %5
br label %50
50: ; preds = %44, %41
br label %51
51: ; preds = %50, %33
br label %52
52: ; preds = %51, %12
%53 = load i8*, i8** %7
%54 = call i32 @free(i8* %53)
br label %8
55: ; preds = %8
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [3 x i8] c"..\00"
define void @append_path(i8* %0, i8* %1) {
%3 = load i8, i8* %1
%4 = icmp eq i8 %3, 0
br i1 %4, label %33, label %5
5: ; preds = %2, %28
%6 = phi i8* [ %14, %28 ], [ %1, %2 ]
%7 = phi i32 [ %29, %28 ], [ 0, %2 ]
%8 = tail call i32 @strcspn(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = tail call i8* @kl_strndup(i8* %6, i32 %8)
%10 = sext i32 %8 to i64
%11 = getelementptr inbounds i8, i8* %6, i64 %10
%12 = tail call i32 @strspn(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %11, i64 %13
%15 = icmp eq i32 %8, 0
br i1 %15, label %28, label %16
16: ; preds = %5
%17 = tail call i32 @kl_streq(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = tail call i32 @strcat(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%21 = tail call i32 @strcat(i8* %0, i8* %9)
%22 = add nsw i32 %7, 1
br label %28
23: ; preds = %16
%24 = icmp eq i32 %7, 0
br i1 %24, label %28, label %25
25: ; preds = %23
%26 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
store i8 0, i8* %26
%27 = add nsw i32 %7, -1
br label %28
28: ; preds = %19, %25, %23, %5
%29 = phi i32 [ %27, %25 ], [ 0, %23 ], [ %22, %19 ], [ %7, %5 ]
%30 = tail call i32 @free(i8* %9)
%31 = load i8, i8* %14
%32 = icmp eq i8 %31, 0
br i1 %32, label %33, label %5
33: ; preds = %28, %2
ret void
}