#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*/ clean_path(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ panic(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char *);
scalar_t__ strlen(char *);
char *canonize_path(char *prefix, char *path) {
  char *p;
  if (path[0] == '/') {
    p = strdup(path);
  } else {
    if (prefix[0] != '/') {
      panic("canonize_path: prefix should start with /\n");
      return NULL;
    }
    p = malloc(strlen(prefix) + strlen(path) + 2);
    strcpy(p, prefix);
    strcat(p, "/");
    strcat(p, path);
  }
  if (!(clean_path(p))) {
    printf("Cannot clean path: %s\n", p);
    return NULL;
  }
  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    11b2 <canonize_path+0x32>
mov    -0x18(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x20(%rbp)
jmp    122f <canonize_path+0xaf>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     11db <canonize_path+0x5b>
lea    0xe37(%rip),%rdi
call   1060 <panic@plt>
movq   $0x0,-0x8(%rbp)
jmp    1266 <canonize_path+0xe6>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x28(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rdi
lea    0xe0e(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <strcat@plt>
mov    -0x20(%rbp),%rdi
call   1070 <clean_path@plt>
cmp    $0x0,%eax
jne    125e <canonize_path+0xde>
mov    -0x20(%rbp),%rsi
lea    0xde1(%rip),%rdi
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1266 <canonize_path+0xe6>
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    11ea <canonize_path+0x6a>
mov    %rdi,%r15
cmpb   $0x2f,(%rdi)
jne    1207 <canonize_path+0x87>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x2,%rdi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
lea    0xe54(%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
jne    1215 <canonize_path+0x95>
lea    0xe30(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
jmp    1213 <canonize_path+0x93>
lea    0xdf2(%rip),%rdi
call   1060 <panic@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [43 x i8] c"canonize_path: 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"
define i8* @canonize_path(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%7 = load i8*, i8** %5
%8 = getelementptr inbounds i8, i8* %7, i64 0
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp eq i32 %10, 47
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i8*, i8** %5
%14 = call i8* @strdup(i8* %13)
store i8* %14, i8** %6
br label %39
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = getelementptr inbounds i8, i8* %16, i64 0
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp ne i32 %19, 47
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = call i32 @panic(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %3
br label %48
23: ; preds = %15
%24 = load i8*, i8** %4
%25 = call i64 @strlen(i8* %24)
%26 = load i8*, i8** %5
%27 = call i64 @strlen(i8* %26)
%28 = add nsw i64 %25, %27
%29 = add nsw i64 %28, 2
%30 = call i8* @malloc(i64 %29)
store i8* %30, i8** %6
%31 = load i8*, i8** %6
%32 = load i8*, i8** %4
%33 = call i32 @strcpy(i8* %31, i8* %32)
%34 = load i8*, i8** %6
%35 = call i32 @strcat(i8* %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%36 = load i8*, i8** %6
%37 = load i8*, i8** %5
%38 = call i32 @strcat(i8* %36, i8* %37)
br label %39
39: ; preds = %23, %12
%40 = load i8*, i8** %6
%41 = call i32 @clean_path(i8* %40)
%42 = icmp ne i32 %41, 0
br i1 %42, label %46, label %43
43: ; preds = %39
%44 = load i8*, i8** %6
%45 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %44)
store i8* null, i8** %3
br label %48
46: ; preds = %39
%47 = load i8*, i8** %6
store i8* %47, i8** %3
br label %48
48: ; preds = %46, %43, %21
%49 = load i8*, i8** %3
ret i8* %49
}

LLVM IR (-O2):
@.str = [43 x i8] c"canonize_path: 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"
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 @panic(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
br label %27
12: ; preds = %7
%13 = tail call i64 @strlen(i8* %0)
%14 = tail call i64 @strlen(i8* %1)
%15 = add i64 %13, 2
%16 = add i64 %15, %14
%17 = tail call i8* @malloc(i64 %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 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %22)
br label %27
27: ; preds = %21, %25, %10
%28 = phi i8* [ null, %25 ], [ null, %10 ], [ %22, %21 ]
ret i8* %28
}