#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 *calloc(int, int);
int /*<<< orphan*/ clean_path(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char *);
int strlen(char *);
char *canonize_path(char *prefix, char *path) {
  char *p;
  int len;
  if (path[0] == '/') {
    p = strdup(path);
  } else {
    if (prefix[0] != '/') {
      printf("Error - prefix should start with /\n");
      return NULL;
    }
    len = strlen(prefix) + strlen(path) + 1;
    p = calloc(1, len);
    strcpy(p, prefix);
    strcat(p, "/");
    strcat(p, path);
  }
  if (!(clean_path(p))) {
    printf("Cannot clean path: %s\n", p);
    return NULL;
  }
  printf("[%s]\n", p);
  return p;
}


x86 assembly (clang, -O0):
<canonize_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11a2 <canonize_path+0x32>
mov    -0x18(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x20(%rbp)
jmp    122a <canonize_path+0xba>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     11cd <canonize_path+0x5d>
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1275 <canonize_path+0x105>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
mov    $0x1,%edi
call   1070 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rdi
lea    0xe0c(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x20(%rbp),%rdi
call   1060 <clean_path@plt>
cmp    $0x0,%eax
jne    125b <canonize_path+0xeb>
mov    -0x20(%rbp),%rsi
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1275 <canonize_path+0x105>
mov    -0x20(%rbp),%rsi
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<canonize_path>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
cmpb   $0x2f,(%rsi)
jne    119a <canonize_path+0x1a>
mov    %r14,%rdi
call   10a0 <strdup@plt>
mov    %rax,%rbx
jmp    11ec <canonize_path+0x6c>
mov    %rdi,%r15
cmpb   $0x2f,(%rdi)
jne    121e <canonize_path+0x9e>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    (%rbx,%rax,1),%esi
add    $0x1,%esi
mov    $0x1,%edi
call   1080 <calloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
lea    0xe65(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcat@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1090 <strcat@plt>
mov    %rbx,%rdi
call   1070 <clean_path@plt>
test   %eax,%eax
je     120b <canonize_path+0x8b>
lea    0xe18(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    122c <canonize_path+0xac>
lea    0xdee(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    122a <canonize_path+0xaa>
lea    0xdf8(%rip),%rdi
call   1040 <puts@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [36 x i8] c"Error - prefix should start with /\0A\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [23 x i8] c"Cannot clean path: %s\0A\00"
@.str.3 = [6 x i8] c"[%s]\0A\00"
define i8* @canonize_path(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = getelementptr inbounds i8, i8* %8, i64 0
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp eq i32 %11, 47
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i8*, i8** %5
%15 = call i8* @strdup(i8* %14)
store i8* %15, i8** %6
br label %41
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = getelementptr inbounds i8, i8* %17, i64 0
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp ne i32 %20, 47
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %3
br label %52
24: ; preds = %16
%25 = load i8*, i8** %4
%26 = call i32 @strlen(i8* %25)
%27 = load i8*, i8** %5
%28 = call i32 @strlen(i8* %27)
%29 = add nsw i32 %26, %28
%30 = add nsw i32 %29, 1
store i32 %30, i32* %7
%31 = load i32, i32* %7
%32 = call i8* @calloc(i32 1, i32 %31)
store i8* %32, i8** %6
%33 = load i8*, i8** %6
%34 = load i8*, i8** %4
%35 = call i32 @strcpy(i8* %33, i8* %34)
%36 = load i8*, i8** %6
%37 = call i32 @strcat(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%38 = load i8*, i8** %6
%39 = load i8*, i8** %5
%40 = call i32 @strcat(i8* %38, i8* %39)
br label %41
41: ; preds = %24, %13
%42 = load i8*, i8** %6
%43 = call i32 @clean_path(i8* %42)
%44 = icmp ne i32 %43, 0
br i1 %44, label %48, label %45
45: ; preds = %41
%46 = load i8*, i8** %6
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %46)
store i8* null, i8** %3
br label %52
48: ; preds = %41
%49 = load i8*, i8** %6
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %49)
%51 = load i8*, i8** %6
store i8* %51, i8** %3
br label %52
52: ; preds = %48, %45, %22
%53 = load i8*, i8** %3
ret i8* %53
}

LLVM IR (-O2):
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [23 x i8] c"Cannot clean path: %s\0A\00"
@.str.3 = [6 x i8] c"[%s]\0A\00"
@str = [35 x i8] c"Error - prefix should start with /\00"
define i8* @canonize_path(i8* %0, i8* %1) {
%3 = load i8, i8* %1
%4 = icmp eq i8 %3, 47
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i8* @strdup(i8* %1)
br label %21
7: ; preds = %2
%8 = load i8, i8* %0
%9 = icmp eq i8 %8, 47
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
br label %29
12: ; preds = %7
%13 = tail call i32 @strlen(i8* %0)
%14 = tail call i32 @strlen(i8* %1)
%15 = add i32 %13, 1
%16 = add i32 %15, %14
%17 = tail call i8* @calloc(i32 1, i32 %16)
%18 = tail call i32 @strcpy(i8* %17, i8* %0)
%19 = tail call i32 @strcat(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%20 = tail call i32 @strcat(i8* %17, i8* %1)
br label %21
21: ; preds = %12, %5
%22 = phi i8* [ %6, %5 ], [ %17, %12 ]
%23 = tail call i32 @clean_path(i8* %22)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %22)
br label %29
27: ; preds = %21
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %22)
br label %29
29: ; preds = %27, %25, %10
%30 = phi i8* [ %22, %27 ], [ null, %25 ], [ null, %10 ]
ret i8* %30
}