#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*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
char *strpbrk(char *, char *);
scalar_t__ strstr(char *, char *);
int relativeUrl(char *this_page, char *dest) {
  int prev_dir = 0;
  char src[1024];
  strcpy(src, this_page);
  char *last_index = dest;
  while (strstr(last_index, "../")) {
    last_index = 3 + strpbrk(last_index, "../");
    prev_dir++;
  }
  while (strstr(last_index, "../")) {
    last_index = 2 + strpbrk(last_index, "./");
  }
  int src_index = (int)strlen(src);
  for (; src_index > 0 && prev_dir > 0; src_index--) {
    if (src[src_index] == '/') {
      prev_dir--;
    }
  }
  src_index += 2;
  if (prev_dir > 0) {
    return -1;
  }
  printf("actual_link %s\n", last_index);
  src[src_index] = '\0';
  printf("folder_link %s\n", src);
  src_index += strlen(last_index);
  strncat(src, last_index, strlen(last_index));
  src[src_index] = '\0';
  memset(dest, 0, 1024);
  strncat(dest, src, src_index);
  printf("final link: %s\n", dest);
  return strlen(dest);
}


x86 assembly (clang, -O0):
<relativeUrl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x428(%rbp)
mov    -0x428(%rbp),%rdi
lea    0xe4d(%rip),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
je     11ee <relativeUrl+0x7e>
mov    -0x428(%rbp),%rdi
lea    0xe30(%rip),%rsi
call   1080 <strpbrk@plt>
add    $0x3,%rax
mov    %rax,-0x428(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11a5 <relativeUrl+0x35>
jmp    11f3 <relativeUrl+0x83>
mov    -0x428(%rbp),%rdi
lea    0xdff(%rip),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
je     1233 <relativeUrl+0xc3>
mov    -0x428(%rbp),%rdi
lea    0xde3(%rip),%rsi
call   1080 <strpbrk@plt>
add    $0x2,%rax
mov    %rax,-0x428(%rbp)
jmp    11f3 <relativeUrl+0x83>
lea    -0x420(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x42c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x42c(%rbp)
mov    %al,-0x42d(%rbp)
jle    1267 <relativeUrl+0xf7>
cmpl   $0x0,-0x1c(%rbp)
setg   %al
mov    %al,-0x42d(%rbp)
mov    -0x42d(%rbp),%al
test   $0x1,%al
jne    127a <relativeUrl+0x10a>
jmp    12b4 <relativeUrl+0x144>
movslq -0x42c(%rbp),%rax
movsbl -0x420(%rbp,%rax,1),%eax
ff
cmp    $0x2f,%eax
jne    129b <relativeUrl+0x12b>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    12a0 <relativeUrl+0x130>
mov    -0x42c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x42c(%rbp)
jmp    1245 <relativeUrl+0xd5>
mov    -0x42c(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x42c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    12d9 <relativeUrl+0x169>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b4 <relativeUrl+0x244>
mov    -0x428(%rbp),%rsi
lea    0xd1d(%rip),%rdi
call   1050 <printf@plt>
movslq -0x42c(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
lea    -0x420(%rbp),%rsi
lea    0xd0b(%rip),%rdi
call   1050 <printf@plt>
mov    -0x428(%rbp),%rdi
call   1040 <strlen@plt>
add    -0x42c(%rbp),%eax
mov    %eax,-0x42c(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x440(%rbp)
mov    -0x428(%rbp),%rax
mov    %rax,-0x438(%rbp)
mov    -0x428(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x440(%rbp),%rdi
mov    -0x438(%rbp),%rsi
mov    %eax,%edx
call   1070 <strncat@plt>
movslq -0x42c(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    -0x18(%rbp),%rdi
lea    -0x420(%rbp),%rsi
mov    -0x42c(%rbp),%edx
call   1070 <strncat@plt>
mov    -0x18(%rbp),%rsi
lea    0xc81(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<relativeUrl>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rsi,%r14
mov    %rdi,%rsi
mov    %rsp,%rdi
call   1030 <strcpy@plt>
lea    0xe6c(%rip),%rsi
mov    %r14,%rdi
call   1090 <strstr@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     11d7 <relativeUrl+0x67>
lea    0xe56(%rip),%r15
mov    %r14,%rbx
nopl   (%rax)
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <strpbrk@plt>
mov    %rax,%rbx
add    $0x3,%rbx
add    $0x1,%ebp
mov    %rbx,%rdi
mov    %r15,%rsi
call   1090 <strstr@plt>
test   %rax,%rax
jne    11b0 <relativeUrl+0x40>
jmp    11da <relativeUrl+0x6a>
mov    %r14,%rbx
lea    0xe1f(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strstr@plt>
test   %rax,%rax
je     1222 <relativeUrl+0xb2>
lea    0xe0c(%rip),%r15
lea    0xe04(%rip),%r12
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <strpbrk@plt>
mov    %rax,%rbx
add    $0x2,%rbx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1090 <strstr@plt>
test   %rax,%rax
jne    1200 <relativeUrl+0x90>
mov    %rsp,%rdi
call   1040 <strlen@plt>
test   %ebp,%ebp
setg   %dl
test   %eax,%eax
jle    125e <relativeUrl+0xee>
test   %ebp,%ebp
jle    125e <relativeUrl+0xee>
nopw   0x0(%rax,%rax,1)
mov    %eax,%ecx
mov    %eax,%eax
xor    %edx,%edx
cmpb   $0x2f,(%rsp,%rax,1)
sete   %dl
lea    -0x1(%rcx),%eax
sub    %edx,%ebp
setne  %dl
cmp    $0x2,%ecx
jb     125e <relativeUrl+0xee>
test   %ebp,%ebp
jne    1240 <relativeUrl+0xd0>
mov    $0xffffffff,%ecx
test   %dl,%dl
jne    12f6 <relativeUrl+0x186>
movslq %eax,%r12
add    $0x2,%r12
lea    0xd8b(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
movb   $0x0,(%rsp,%r12,1)
lea    0xd87(%rip),%rdi
mov    %rsp,%r15
mov    %r15,%rsi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
add    %r12d,%ebp
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1070 <strncat@plt>
movslq %ebp,%rbx
movb   $0x0,(%rsp,%rbx,1)
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1070 <strncat@plt>
lea    0xd40(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    %ecx,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"../\00"
@.str.1 = [3 x i8] c"./\00"
@.str.2 = [16 x i8] c"actual_link %s\0A\00"
@.str.3 = [16 x i8] c"folder_link %s\0A\00"
@.str.4 = [16 x i8] c"final link: %s\0A\00"
define i32 @relativeUrl(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [1024 x i8]
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %6
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%11 = load i8*, i8** %4
%12 = call i32 @strcpy(i8* %10, i8* %11)
%13 = load i8*, i8** %5
store i8* %13, i8** %8
br label %14
14: ; preds = %18, %2
%15 = load i8*, i8** %8
%16 = call i64 @strstr(i8* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %14
%19 = load i8*, i8** %8
%20 = call i8* @strpbrk(i8* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%21 = getelementptr inbounds i8, i8* %20, i64 3
store i8* %21, i8** %8
%22 = load i32, i32* %6
%23 = add nsw i32 %22, 1
store i32 %23, i32* %6
br label %14
24: ; preds = %14
br label %25
25: ; preds = %29, %24
%26 = load i8*, i8** %8
%27 = call i64 @strstr(i8* %26, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = load i8*, i8** %8
%31 = call i8* @strpbrk(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%32 = getelementptr inbounds i8, i8* %31, i64 2
store i8* %32, i8** %8
br label %25
33: ; preds = %25
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%35 = call i32 @strlen(i8* %34)
store i32 %35, i32* %9
br label %36
36: ; preds = %55, %33
%37 = load i32, i32* %9
%38 = icmp sgt i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = load i32, i32* %6
%41 = icmp sgt i32 %40, 0
br label %42
42: ; preds = %39, %36
%43 = phi i1 [ false, %36 ], [ %41, %39 ]
br i1 %43, label %44, label %58
44: ; preds = %42
%45 = load i32, i32* %9
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %46
%48 = load i8, i8* %47
%49 = sext i8 %48 to i32
%50 = icmp eq i32 %49, 47
br i1 %50, label %51, label %54
51: ; preds = %44
%52 = load i32, i32* %6
%53 = add nsw i32 %52, -1
store i32 %53, i32* %6
br label %54
54: ; preds = %51, %44
br label %55
55: ; preds = %54
%56 = load i32, i32* %9
%57 = add nsw i32 %56, -1
store i32 %57, i32* %9
br label %36
58: ; preds = %42
%59 = load i32, i32* %9
%60 = add nsw i32 %59, 2
store i32 %60, i32* %9
%61 = load i32, i32* %6
%62 = icmp sgt i32 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %58
store i32 -1, i32* %3
br label %94
64: ; preds = %58
%65 = load i8*, i8** %8
%66 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %65)
%67 = load i32, i32* %9
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %68
store i8 0, i8* %69
%70 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%71 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %70)
%72 = load i8*, i8** %8
%73 = call i32 @strlen(i8* %72)
%74 = load i32, i32* %9
%75 = add nsw i32 %74, %73
store i32 %75, i32* %9
%76 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%77 = load i8*, i8** %8
%78 = load i8*, i8** %8
%79 = call i32 @strlen(i8* %78)
%80 = call i32 @strncat(i8* %76, i8* %77, i32 %79)
%81 = load i32, i32* %9
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %82
store i8 0, i8* %83
%84 = load i8*, i8** %5
%85 = call i32 @memset(i8* %84, i32 0, i32 1024)
%86 = load i8*, i8** %5
%87 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%88 = load i32, i32* %9
%89 = call i32 @strncat(i8* %86, i8* %87, i32 %88)
%90 = load i8*, i8** %5
%91 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %90)
%92 = load i8*, i8** %5
%93 = call i32 @strlen(i8* %92)
store i32 %93, i32* %3
br label %94
94: ; preds = %64, %63
%95 = load i32, i32* %3
ret i32 %95
}

LLVM IR (-O2):
@.str = [4 x i8] c"../\00"
@.str.1 = [3 x i8] c"./\00"
@.str.2 = [16 x i8] c"actual_link %s\0A\00"
@.str.3 = [16 x i8] c"folder_link %s\0A\00"
@.str.4 = [16 x i8] c"final link: %s\0A\00"
define i32 @relativeUrl(i8* %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = call i32 @strcpy(i8* %4, i8* %0)
%6 = call i64 @strstr(i8* %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %13, %2
%9 = phi i32 [ 0, %2 ], [ %18, %13 ]
%10 = phi i8* [ %1, %2 ], [ %17, %13 ]
%11 = call i64 @strstr(i8* %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
br i1 %12, label %27, label %21
13: ; preds = %2, %13
%14 = phi i8* [ %17, %13 ], [ %1, %2 ]
%15 = phi i32 [ %18, %13 ], [ 0, %2 ]
%16 = call i8* @strpbrk(i8* %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%17 = getelementptr inbounds i8, i8* %16, i64 3
%18 = add nuw nsw i32 %15, 1
%19 = call i64 @strstr(i8* %17, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %8, label %13
21: ; preds = %8, %21
%22 = phi i8* [ %24, %21 ], [ %10, %8 ]
%23 = call i8* @strpbrk(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%24 = getelementptr inbounds i8, i8* %23, i64 2
%25 = call i64 @strstr(i8* %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %21
27: ; preds = %21, %8
%28 = phi i8* [ %10, %8 ], [ %24, %21 ]
%29 = call i32 @strlen(i8* %4)
%30 = icmp sgt i32 %29, 0
%31 = icmp sgt i32 %9, 0
%32 = select i1 %30, i1 %31, i1 false
br i1 %32, label %33, label %46
33: ; preds = %27, %33
%34 = phi i32 [ %42, %33 ], [ %29, %27 ]
%35 = phi i32 [ %41, %33 ], [ %9, %27 ]
%36 = zext i32 %34 to i64
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %36
%38 = load i8, i8* %37
%39 = icmp eq i8 %38, 47
%40 = sext i1 %39 to i32
%41 = add nsw i32 %35, %40
%42 = add nsw i32 %34, -1
%43 = icmp ugt i32 %34, 1
%44 = icmp ne i32 %41, 0
%45 = select i1 %43, i1 %44, i1 false
br i1 %45, label %33, label %46
46: ; preds = %33, %27
%47 = phi i32 [ %29, %27 ], [ %42, %33 ]
%48 = phi i1 [ %31, %27 ], [ %44, %33 ]
%49 = add nsw i32 %47, 2
br i1 %48, label %65, label %50
50: ; preds = %46
%51 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %28)
%52 = sext i32 %49 to i64
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %52
store i8 0, i8* %53
%54 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%55 = call i32 @strlen(i8* %28)
%56 = add nsw i32 %55, %49
%57 = call i32 @strlen(i8* %28)
%58 = call i32 @strncat(i8* %4, i8* %28, i32 %57)
%59 = sext i32 %56 to i64
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %59
store i8 0, i8* %60
%61 = call i32 @memset(i8* %1, i32 0, i32 1024)
%62 = call i32 @strncat(i8* %1, i8* %4, i32 %56)
%63 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %1)
%64 = call i32 @strlen(i8* %1)
br label %65
65: ; preds = %46, %50
%66 = phi i32 [ %64, %50 ], [ -1, %46 ]
ret i32 %66
}