#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 */
scalar_t__ PATH_MAX;
char *canonicalize_dm_name(char *);
int /*<<< orphan*/ free(char *);
scalar_t__ isdigit(char);
char *malloc(scalar_t__);
int /*<<< orphan*/ myrealpath(char const *, char *, scalar_t__);
char *strdup(char const *);
scalar_t__ strncmp(char *, char *, int);
char *strrchr(char *, char);
char *canonicalize_path(const char *path) {
  char *canonical;
  char *p;
  if (path == NULL)
    return NULL;
  canonical = malloc(PATH_MAX + 2);
  if (!myrealpath(path, canonical, PATH_MAX + 1)) {
    free(canonical);
    return strdup(path);
  }
  p = strrchr(canonical, '/');
  if (p && strncmp(p, "/dm-", 4) == 0 && isdigit(*(p + 4))) {
    p = canonicalize_dm_name(p + 1);
    if (p) {
      free(canonical);
      return p;
    }
  }
  return canonical;
}


x86 assembly (clang, -O0):
<canonicalize_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a4 <canonicalize_path+0x24>
movq   $0x0,-0x8(%rbp)
jmp    128c <canonicalize_path+0x10c>
mov    0x2e45(%rip),%rax
mov    (%rax),%rdi
add    $0x2,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2e26(%rip),%rax
mov    (%rax),%rdx
add    $0x1,%rdx
call   1070 <myrealpath@plt>
cmp    $0x0,%eax
jne    11fa <canonicalize_path+0x7a>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    128c <canonicalize_path+0x10c>
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1284 <canonicalize_path+0x104>
mov    -0x20(%rbp),%rdi
lea    0xdde(%rip),%rsi
mov    $0x4,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1284 <canonicalize_path+0x104>
mov    -0x20(%rbp),%rax
movsbl 0x4(%rax),%edi
call   1050 <isdigit@plt>
cmp    $0x0,%rax
je     1284 <canonicalize_path+0x104>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1090 <canonicalize_dm_name@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     127f <canonicalize_path+0xff>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    128c <canonicalize_path+0x10c>
jmp    1284 <canonicalize_path+0x104>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<canonicalize_path>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     121a <canonicalize_path+0x9a>
mov    %rdi,%r15
mov    0x2e58(%rip),%rax
mov    (%rax),%rbx
lea    0x2(%rbx),%rdi
call   1080 <malloc@plt>
mov    %rax,%r14
add    $0x1,%rbx
mov    %r15,%rdi
mov    %rax,%rsi
mov    %rbx,%rdx
call   1070 <myrealpath@plt>
test   %eax,%eax
je     1226 <canonicalize_path+0xa6>
mov    %r14,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     121d <canonicalize_path+0x9d>
mov    %rax,%rbx
lea    0xe27(%rip),%rsi
mov    %rax,%rdi
mov    $0x4,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
jne    121d <canonicalize_path+0x9d>
movsbl 0x4(%rbx),%edi
call   1050 <isdigit@plt>
test   %rax,%rax
je     121d <canonicalize_path+0x9d>
add    $0x1,%rbx
mov    %rbx,%rdi
call   1090 <canonicalize_dm_name@plt>
test   %rax,%rax
je     121d <canonicalize_path+0x9d>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%r14
jmp    121d <canonicalize_path+0x9d>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    10a0 <strdup@plt>

LLVM IR (-O0):
@PATH_MAX = global i64 0
@.str = [5 x i8] c"/dm-\00"
define i8* @canonicalize_path(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %9
8: ; preds = %1
store i8* null, i8** %2
br label %52
9: ; preds = %1
%10 = load i64, i64* @PATH_MAX
%11 = add nsw i64 %10, 2
%12 = call i8* @malloc(i64 %11)
store i8* %12, i8** %4
%13 = load i8*, i8** %3
%14 = load i8*, i8** %4
%15 = load i64, i64* @PATH_MAX
%16 = add nsw i64 %15, 1
%17 = call i32 @myrealpath(i8* %13, i8* %14, i64 %16)
%18 = icmp ne i32 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %9
%20 = load i8*, i8** %4
%21 = call i32 @free(i8* %20)
%22 = load i8*, i8** %3
%23 = call i8* @strdup(i8* %22)
store i8* %23, i8** %2
br label %52
24: ; preds = %9
%25 = load i8*, i8** %4
%26 = call i8* @strrchr(i8* %25, i8 signext 47)
store i8* %26, i8** %5
%27 = load i8*, i8** %5
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %50
29: ; preds = %24
%30 = load i8*, i8** %5
%31 = call i64 @strncmp(i8* %30, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %50
33: ; preds = %29
%34 = load i8*, i8** %5
%35 = getelementptr inbounds i8, i8* %34, i64 4
%36 = load i8, i8* %35
%37 = call i64 @isdigit(i8 signext %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %50
39: ; preds = %33
%40 = load i8*, i8** %5
%41 = getelementptr inbounds i8, i8* %40, i64 1
%42 = call i8* @canonicalize_dm_name(i8* %41)
store i8* %42, i8** %5
%43 = load i8*, i8** %5
%44 = icmp ne i8* %43, null
br i1 %44, label %45, label %49
45: ; preds = %39
%46 = load i8*, i8** %4
%47 = call i32 @free(i8* %46)
%48 = load i8*, i8** %5
store i8* %48, i8** %2
br label %52
49: ; preds = %39
br label %50
50: ; preds = %49, %33, %29, %24
%51 = load i8*, i8** %4
store i8* %51, i8** %2
br label %52
52: ; preds = %50, %45, %19, %8
%53 = load i8*, i8** %2
ret i8* %53
}

LLVM IR (-O2):
@PATH_MAX = global i64 0
@.str = [5 x i8] c"/dm-\00"
define i8* @canonicalize_path(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %30, label %3
3: ; preds = %1
%4 = load i64, i64* @PATH_MAX
%5 = add nsw i64 %4, 2
%6 = tail call i8* @malloc(i64 %5)
%7 = add nsw i64 %4, 1
%8 = tail call i32 @myrealpath(i8* %0, i8* %6, i64 %7)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %3
%11 = tail call i32 @free(i8* %6)
%12 = tail call i8* @strdup(i8* %0)
br label %30
13: ; preds = %3
%14 = tail call i8* @strrchr(i8* dereferenceable(1) %6, i8 signext 47)
%15 = icmp eq i8* %14, null
br i1 %15, label %30, label %16
16: ; preds = %13
%17 = tail call i64 @strncmp(i8* %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %30
19: ; preds = %16
%20 = getelementptr inbounds i8, i8* %14, i64 4
%21 = load i8, i8* %20
%22 = tail call i64 @isdigit(i8 signext %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %30, label %24
24: ; preds = %19
%25 = getelementptr inbounds i8, i8* %14, i64 1
%26 = tail call i8* @canonicalize_dm_name(i8* %25)
%27 = icmp eq i8* %26, null
br i1 %27, label %30, label %28
28: ; preds = %24
%29 = tail call i32 @free(i8* %6)
br label %30
30: ; preds = %13, %16, %19, %24, %1, %28, %10
%31 = phi i8* [ %26, %28 ], [ %12, %10 ], [ null, %1 ], [ %6, %24 ], [ %6, %19 ], [ %6, %16 ], [ %6, %13 ]
ret i8* %31
}