#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 *malloc(int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *misc_canonical_pathname(char *pathname) {
  int l = strlen(pathname);
  char *p;
  if (pathname[l - 1] != '/') {
    if ((p = malloc(l + 2)) == NULL)
      return NULL;
    strcpy(p, pathname);
    strcat(p, "/");
  } else if (pathname[l - 2] == '/') {
    l--;
    while (pathname[l - 2] == '/') {
      pathname[l - 1] = '\0';
      l--;
    }
    if ((p = malloc(l + 1)) == NULL)
      return NULL;
    strncpy(p, pathname, l);
  } else {
    if ((p = strdup(pathname)) == NULL)
      return NULL;
  }
  return p;
}


x86 assembly (clang, -O0):
<misc_canonical_pathname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     11da <misc_canonical_pathname+0x7a>
mov    -0x14(%rbp),%edi
add    $0x2,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11b8 <misc_canonical_pathname+0x58>
movq   $0x0,-0x8(%rbp)
jmp    12a7 <misc_canonical_pathname+0x147>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rdi
lea    0xe30(%rip),%rsi
call   1070 <strcat@plt>
jmp    129f <misc_canonical_pathname+0x13f>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    1271 <misc_canonical_pathname+0x111>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    1236 <misc_canonical_pathname+0xd6>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    11fd <misc_canonical_pathname+0x9d>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    125c <misc_canonical_pathname+0xfc>
movq   $0x0,-0x8(%rbp)
jmp    12a7 <misc_canonical_pathname+0x147>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
jmp    129a <misc_canonical_pathname+0x13a>
mov    -0x10(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1295 <misc_canonical_pathname+0x135>
movq   $0x0,-0x8(%rbp)
jmp    12a7 <misc_canonical_pathname+0x147>
jmp    129a <misc_canonical_pathname+0x13a>
jmp    129f <misc_canonical_pathname+0x13f>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<misc_canonical_pathname>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1050 <strlen@plt>
lea    -0x1(%rax),%ebp
movslq %ebp,%rcx
cmpb   $0x2f,(%rbx,%rcx,1)
jne    11d5 <misc_canonical_pathname+0x75>
movslq %eax,%rdi
cmpb   $0x2f,-0x2(%rdi,%rbx,1)
jne    1203 <misc_canonical_pathname+0xa3>
cmpb   $0x2f,-0x3(%rdi,%rbx,1)
jne    11b9 <misc_canonical_pathname+0x59>
lea    (%rcx,%rbx,1),%rax
add    $0xfffffffffffffffd,%rax
cs nopw 0x0(%rax,%rax,1)
nop
lea    -0x2(%rdi),%ecx
movslq %ecx,%rcx
movb   $0x0,(%rbx,%rcx,1)
add    $0xffffffff,%edi
cmpb   $0x2f,(%rax)
lea    -0x1(%rax),%rax
je     11a0 <misc_canonical_pathname+0x40>
lea    -0x1(%rdi),%ebp
call   1060 <malloc@plt>
test   %rax,%rax
je     1213 <misc_canonical_pathname+0xb3>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
jmp    1216 <misc_canonical_pathname+0xb6>
add    $0x2,%eax
mov    %eax,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1213 <misc_canonical_pathname+0xb3>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
lea    0xe07(%rip),%rsi
mov    %r14,%rdi
call   1070 <strcat@plt>
jmp    1216 <misc_canonical_pathname+0xb6>
mov    %rbx,%rdi
call   1080 <strdup@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1216 <misc_canonical_pathname+0xb6>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
define i8* @misc_canonical_pathname(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @strlen(i8* %6)
store i32 %7, i32* %4
%8 = load i8*, i8** %3
%9 = load i32, i32* %4
%10 = sub nsw i32 %9, 1
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %8, i64 %11
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp ne i32 %14, 47
br i1 %15, label %16, label %28
16: ; preds = %1
%17 = load i32, i32* %4
%18 = add nsw i32 %17, 2
%19 = call i8* @malloc(i32 %18)
store i8* %19, i8** %5
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %22
21: ; preds = %16
store i8* null, i8** %2
br label %77
22: ; preds = %16
%23 = load i8*, i8** %5
%24 = load i8*, i8** %3
%25 = call i32 @strcpy(i8* %23, i8* %24)
%26 = load i8*, i8** %5
%27 = call i32 @strcat(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %75
28: ; preds = %1
%29 = load i8*, i8** %3
%30 = load i32, i32* %4
%31 = sub nsw i32 %30, 2
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %29, i64 %32
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 47
br i1 %36, label %37, label %68
37: ; preds = %28
%38 = load i32, i32* %4
%39 = add nsw i32 %38, -1
store i32 %39, i32* %4
br label %40
40: ; preds = %49, %37
%41 = load i8*, i8** %3
%42 = load i32, i32* %4
%43 = sub nsw i32 %42, 2
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %41, i64 %44
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp eq i32 %47, 47
br i1 %48, label %49, label %57
49: ; preds = %40
%50 = load i8*, i8** %3
%51 = load i32, i32* %4
%52 = sub nsw i32 %51, 1
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %50, i64 %53
store i8 0, i8* %54
%55 = load i32, i32* %4
%56 = add nsw i32 %55, -1
store i32 %56, i32* %4
br label %40
57: ; preds = %40
%58 = load i32, i32* %4
%59 = add nsw i32 %58, 1
%60 = call i8* @malloc(i32 %59)
store i8* %60, i8** %5
%61 = icmp eq i8* %60, null
br i1 %61, label %62, label %63
62: ; preds = %57
store i8* null, i8** %2
br label %77
63: ; preds = %57
%64 = load i8*, i8** %5
%65 = load i8*, i8** %3
%66 = load i32, i32* %4
%67 = call i32 @strncpy(i8* %64, i8* %65, i32 %66)
br label %74
68: ; preds = %28
%69 = load i8*, i8** %3
%70 = call i8* @strdup(i8* %69)
store i8* %70, i8** %5
%71 = icmp eq i8* %70, null
br i1 %71, label %72, label %73
72: ; preds = %68
store i8* null, i8** %2
br label %77
73: ; preds = %68
br label %74
74: ; preds = %73, %63
br label %75
75: ; preds = %74, %22
%76 = load i8*, i8** %5
store i8* %76, i8** %2
br label %77
77: ; preds = %75, %72, %62, %21
%78 = load i8*, i8** %2
ret i8* %78
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
define i8* @misc_canonical_pathname(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add i32 %2, -1
%4 = sext i32 %3 to i64
%5 = getelementptr inbounds i8, i8* %0, i64 %4
%6 = load i8, i8* %5
%7 = icmp eq i8 %6, 47
br i1 %7, label %15, label %8
8: ; preds = %1
%9 = add nsw i32 %2, 2
%10 = tail call i8* @malloc(i32 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %54, label %12
12: ; preds = %8
%13 = tail call i32 @strcpy(i8* %10, i8* %0)
%14 = tail call i32 @strcat(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %52
15: ; preds = %1
%16 = add nsw i32 %2, -2
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %0, i64 %17
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 47
br i1 %20, label %21, label %49
21: ; preds = %15
%22 = add nsw i32 %2, -3
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %0, i64 %23
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 47
br i1 %26, label %27, label %42
27: ; preds = %21, %27
%28 = phi i64 [ %33, %27 ], [ %4, %21 ]
%29 = phi i32 [ %38, %27 ], [ %2, %21 ]
%30 = add nsw i32 %29, -2
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %0, i64 %31
store i8 0, i8* %32
%33 = add i64 %28, -1
%34 = add nsw i64 %28, -3
%35 = getelementptr inbounds i8, i8* %0, i64 %34
%36 = load i8, i8* %35
%37 = icmp eq i8 %36, 47
%38 = trunc i64 %28 to i32
br i1 %37, label %27, label %39
39: ; preds = %27
%40 = trunc i64 %28 to i32
%41 = trunc i64 %33 to i32
br label %42
42: ; preds = %39, %21
%43 = phi i32 [ %2, %21 ], [ %40, %39 ]
%44 = phi i32 [ %3, %21 ], [ %41, %39 ]
%45 = tail call i8* @malloc(i32 %43)
%46 = icmp eq i8* %45, null
br i1 %46, label %54, label %47
47: ; preds = %42
%48 = tail call i32 @strncpy(i8* %45, i8* %0, i32 %44)
br label %52
49: ; preds = %15
%50 = tail call i8* @strdup(i8* %0)
%51 = icmp eq i8* %50, null
br i1 %51, label %54, label %52
52: ; preds = %47, %49, %12
%53 = phi i8* [ %10, %12 ], [ %45, %47 ], [ %50, %49 ]
br label %54
54: ; preds = %49, %42, %8, %52
%55 = phi i8* [ %53, %52 ], [ null, %8 ], [ null, %42 ], [ null, %49 ]
ret i8* %55
}