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


x86 assembly (clang, -O0):
<path_canonicalize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1194 <path_canonicalize+0x24>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    11a1 <path_canonicalize+0x31>
movq   $0x0,-0x8(%rbp)
jmp    1261 <path_canonicalize+0xf1>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <realpath@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11cf <path_canonicalize+0x5f>
mov    -0x10(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1261 <path_canonicalize+0xf1>
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1259 <path_canonicalize+0xe9>
mov    -0x20(%rbp),%rdi
lea    0xe09(%rip),%rsi
mov    $0x4,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1259 <path_canonicalize+0xe9>
mov    -0x20(%rbp),%rax
movsbl 0x4(%rax),%edi
call   1050 <isdigit@plt>
cmp    $0x0,%rax
je     1259 <path_canonicalize+0xe9>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1080 <path_canonicalize_dm_name@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1254 <path_canonicalize+0xe4>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1261 <path_canonicalize+0xf1>
jmp    1259 <path_canonicalize+0xe9>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<path_canonicalize>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     11f2 <path_canonicalize+0x82>
mov    %rdi,%rbx
cmpb   $0x0,(%rdi)
je     11f2 <path_canonicalize+0x82>
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <realpath@plt>
test   %rax,%rax
je     11fe <path_canonicalize+0x8e>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     11f5 <path_canonicalize+0x85>
mov    %rax,%r14
lea    0xe50(%rip),%rsi
mov    %rax,%rdi
mov    $0x4,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
jne    11f5 <path_canonicalize+0x85>
movsbl 0x4(%r14),%edi
call   1050 <isdigit@plt>
test   %rax,%rax
je     11f5 <path_canonicalize+0x85>
add    $0x1,%r14
mov    %r14,%rdi
call   1080 <path_canonicalize_dm_name@plt>
test   %rax,%rax
je     11f5 <path_canonicalize+0x85>
mov    %rax,%rbx
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%r15
jmp    11f5 <path_canonicalize+0x85>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1090 <strdup@plt>

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

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