#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 */
typedef int /*<<< orphan*/ path;
typedef int /*<<< orphan*/ name;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ F_OK;
int PATH_MAX;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
char *strdup(char *);
size_t strlen(char *);
char *canonicalize_dm_name(const char *ptname) {
  FILE *f;
  size_t sz;
  char path[PATH_MAX], name[PATH_MAX], *res = NULL;
  if (!ptname || !*ptname)
    return NULL;
  snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptname);
  if (!(f = fopen(path, "r")))
    return NULL;
  if (fgets(name, sizeof(name), f) && (sz = strlen(name)) > 1) {
    name[sz - 1] = '\0';
    snprintf(path, sizeof(path), "/dev/mapper/%s", name);
    if (access(path, F_OK) == 0)
      res = strdup(path);
  }
  fclose(f);
  return res;
}


x86 assembly (clang, -O0):
<canonicalize_dm_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%ecx
mov    %rcx,-0x68(%rbp)
mov    %rsp,%rdx
mov    %rdx,-0x28(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x60(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x30(%rbp)
mov    (%rax),%eax
mov    %rax,-0x58(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11ee <canonicalize_dm_name+0x7e>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    1202 <canonicalize_dm_name+0x92>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    12ee <canonicalize_dm_name+0x17e>
mov    -0x60(%rbp),%rdi
mov    -0x68(%rbp),%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xde9(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x60(%rbp),%rdi
lea    0xdef(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    124e <canonicalize_dm_name+0xde>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    12ee <canonicalize_dm_name+0x17e>
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rax
mov    %eax,%esi
mov    -0x18(%rbp),%rdx
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     12d6 <canonicalize_dm_name+0x166>
mov    -0x50(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x1,%rax
jbe    12d6 <canonicalize_dm_name+0x166>
mov    -0x60(%rbp),%rdi
mov    -0x50(%rbp),%rcx
mov    -0x68(%rbp),%rax
mov    -0x20(%rbp),%rdx
sub    $0x1,%rdx
movb   $0x0,(%rcx,%rdx,1)
mov    %eax,%esi
lea    0xd75(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x60(%rbp),%rdi
mov    0x2d1d(%rip),%rax
mov    (%rax),%esi
call   1070 <access@plt>
cmp    $0x0,%rax
jne    12d1 <canonicalize_dm_name+0x161>
mov    -0x60(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x40(%rbp)
jmp    12d6 <canonicalize_dm_name+0x166>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<canonicalize_dm_name>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e6e(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%r12
lea    0xf(%r15),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    %rsp,%rbx
sub    %rax,%rbx
mov    %rbx,%rsp
test   %rdi,%rdi
je     123a <canonicalize_dm_name+0xca>
mov    %rdi,%rcx
cmpb   $0x0,(%rdi)
je     123a <canonicalize_dm_name+0xca>
lea    0xe45(%rip),%rdx
mov    %r12,%rdi
mov    %r15d,%esi
call   1050 <snprintf@plt>
lea    0xe49(%rip),%rsi
mov    %r12,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     123a <canonicalize_dm_name+0xca>
mov    %rax,%r14
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     122e <canonicalize_dm_name+0xbe>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmp    $0x2,%rax
jb     122e <canonicalize_dm_name+0xbe>
movb   $0x0,-0x1(%rax,%rbx,1)
lea    0xe0e(%rip),%rdx
mov    %r12,%rdi
mov    %r15d,%esi
mov    %rbx,%rcx
call   1050 <snprintf@plt>
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   1070 <access@plt>
test   %rax,%rax
je     124c <canonicalize_dm_name+0xdc>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1030 <fclose@plt>
jmp    123c <canonicalize_dm_name+0xcc>
xor    %ebx,%ebx
mov    %rbx,%rax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12,%rdi
call   1090 <strdup@plt>
mov    %rax,%rbx
jmp    1230 <canonicalize_dm_name+0xc0>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [22 x i8] c"/sys/block/%s/dm/name\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [15 x i8] c"/dev/mapper/%s\00"
@F_OK = global i32 0
define i8* @canonicalize_dm_name(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %3
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %6
%14 = alloca i8, i64 %12
store i64 %12, i64* %7
%15 = load i32, i32* @PATH_MAX
%16 = zext i32 %15 to i64
%17 = alloca i8, i64 %16
store i64 %16, i64* %8
store i8* null, i8** %9
%18 = load i8*, i8** %3
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %24
20: ; preds = %1
%21 = load i8*, i8** %3
%22 = load i8, i8* %21
%23 = icmp ne i8 %22, 0
br i1 %23, label %25, label %24
24: ; preds = %20, %1
store i8* null, i8** %2
store i32 1, i32* %10
br label %56
25: ; preds = %20
%26 = trunc i64 %12 to i32
%27 = load i8*, i8** %3
%28 = call i32 @snprintf(i8* %14, i32 %26, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %27)
%29 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %29, i32** %4
%30 = icmp ne i32* %29, null
br i1 %30, label %32, label %31
31: ; preds = %25
store i8* null, i8** %2
store i32 1, i32* %10
br label %56
32: ; preds = %25
%33 = trunc i64 %16 to i32
%34 = load i32*, i32** %4
%35 = call i64 @fgets(i8* %17, i32 %33, i32* %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %52
37: ; preds = %32
%38 = call i64 @strlen(i8* %17)
store i64 %38, i64* %5
%39 = icmp ugt i64 %38, 1
br i1 %39, label %40, label %52
40: ; preds = %37
%41 = load i64, i64* %5
%42 = sub i64 %41, 1
%43 = getelementptr inbounds i8, i8* %17, i64 %42
store i8 0, i8* %43
%44 = trunc i64 %12 to i32
%45 = call i32 @snprintf(i8* %14, i32 %44, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %17)
%46 = load i32, i32* @F_OK
%47 = call i64 @access(i8* %14, i32 %46)
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %40
%50 = call i8* @strdup(i8* %14)
store i8* %50, i8** %9
br label %51
51: ; preds = %49, %40
br label %52
52: ; preds = %51, %37, %32
%53 = load i32*, i32** %4
%54 = call i32 @fclose(i32* %53)
%55 = load i8*, i8** %9
store i8* %55, i8** %2
store i32 1, i32* %10
br label %56
56: ; preds = %52, %31, %24
%57 = load i8*, i8** %6
%58 = load i8*, i8** %2
ret i8* %58
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [22 x i8] c"/sys/block/%s/dm/name\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [15 x i8] c"/dev/mapper/%s\00"
@F_OK = global i32 0
define noalias i8* @canonicalize_dm_name(i8* %0) {
%2 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = alloca i8, i64 %3
%6 = icmp eq i8* %0, null
br i1 %6, label %32, label %7
7: ; preds = %1
%8 = load i8, i8* %0
%9 = icmp eq i8 %8, 0
br i1 %9, label %32, label %10
10: ; preds = %7
%11 = call i32 @snprintf(i8* %4, i32 %2, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %0)
%12 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %32, label %14
14: ; preds = %10
%15 = call i64 @fgets(i8* %5, i32 %2, i32* %12)
%16 = icmp eq i64 %15, 0
br i1 %16, label %29, label %17
17: ; preds = %14
%18 = call i64 @strlen(i8* %5)
%19 = icmp ugt i64 %18, 1
br i1 %19, label %20, label %29
20: ; preds = %17
%21 = add i64 %18, -1
%22 = getelementptr inbounds i8, i8* %5, i64 %21
store i8 0, i8* %22
%23 = call i32 @snprintf(i8* %4, i32 %2, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %5)
%24 = load i32, i32* @F_OK
%25 = call i64 @access(i8* %4, i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %20
%28 = call i8* @strdup(i8* %4)
br label %29
29: ; preds = %20, %27, %17, %14
%30 = phi i8* [ %28, %27 ], [ null, %20 ], [ null, %17 ], [ null, %14 ]
%31 = call i32 @fclose(i32* %12)
br label %32
32: ; preds = %10, %1, %7, %29
%33 = phi i8* [ %30, %29 ], [ null, %7 ], [ null, %1 ], [ null, %10 ]
ret i8* %33
}