#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*/ 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[256], name[256], *res = 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);
    res = strdup(path);
  }
  fclose(f);
  return res;
}


x86 assembly (clang, -O0):
<canonicalize_dm_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x228(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x100,%esi
lea    0xe6f(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x120(%rbp),%rdi
lea    0xe72(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11c4 <canonicalize_dm_name+0x64>
movq   $0x0,-0x8(%rbp)
jmp    1253 <canonicalize_dm_name+0xf3>
lea    -0x220(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x100,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     123f <canonicalize_dm_name+0xdf>
lea    -0x220(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x1,%rax
jbe    123f <canonicalize_dm_name+0xdf>
mov    -0x20(%rbp),%rax
sub    $0x1,%rax
movb   $0x0,-0x220(%rbp,%rax,1)
lea    -0x120(%rbp),%rdi
lea    -0x220(%rbp),%rcx
mov    $0x100,%esi
lea    0xdf1(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x120(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x228(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x228(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<canonicalize_dm_name>:
push   %r15
push   %r14
push   %rbx
sub    $0x200,%rsp
mov    %rdi,%rcx
lea    0xe8a(%rip),%rdx
lea    0x100(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1050 <snprintf@plt>
lea    0xe84(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1205 <canonicalize_dm_name+0xa5>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     11f9 <canonicalize_dm_name+0x99>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmp    $0x2,%rax
jb     11f9 <canonicalize_dm_name+0x99>
movb   $0x0,-0x1(%rax,%rbx,1)
lea    0xe44(%rip),%rdx
lea    0x100(%rsp),%r15
mov    %r15,%rdi
mov    $0x100,%esi
mov    %rbx,%rcx
call   1050 <snprintf@plt>
mov    %r15,%rdi
call   1080 <strdup@plt>
mov    %rax,%rbx
jmp    11fb <canonicalize_dm_name+0x9b>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1030 <fclose@plt>
jmp    1207 <canonicalize_dm_name+0xa7>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.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"
define i8* @canonicalize_dm_name(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca [256 x i8]
%7 = alloca [256 x i8]
%8 = alloca i8*
store i8* %0, i8** %3
store i8* null, i8** %8
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%10 = load i8*, i8** %3
%11 = call i32 @snprintf(i8* %9, i32 256, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %4
%14 = icmp ne i32* %13, null
br i1 %14, label %16, label %15
15: ; preds = %1
store i8* null, i8** %2
br label %38
16: ; preds = %1
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%18 = load i32*, i32** %4
%19 = call i64 @fgets(i8* %17, i32 256, i32* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %34
21: ; preds = %16
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%23 = call i64 @strlen(i8* %22)
store i64 %23, i64* %5
%24 = icmp ugt i64 %23, 1
br i1 %24, label %25, label %34
25: ; preds = %21
%26 = load i64, i64* %5
%27 = sub i64 %26, 1
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 %27
store i8 0, i8* %28
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%31 = call i32 @snprintf(i8* %29, i32 256, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %30)
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%33 = call i8* @strdup(i8* %32)
store i8* %33, i8** %8
br label %34
34: ; preds = %25, %21, %16
%35 = load i32*, i32** %4
%36 = call i32 @fclose(i32* %35)
%37 = load i8*, i8** %8
store i8* %37, i8** %2
br label %38
38: ; preds = %34, %15
%39 = load i8*, i8** %2
ret i8* %39
}

LLVM IR (-O2):
@.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"
define noalias i8* @canonicalize_dm_name(i8* %0) {
%2 = alloca [256 x i8]
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = call i32 @snprintf(i8* %4, i32 256, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %0)
%7 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %23, label %9
9: ; preds = %1
%10 = call i64 @fgets(i8* %5, i32 256, i32* %7)
%11 = icmp eq i64 %10, 0
br i1 %11, label %20, label %12
12: ; preds = %9
%13 = call i64 @strlen(i8* %5)
%14 = icmp ugt i64 %13, 1
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = add i64 %13, -1
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %16
store i8 0, i8* %17
%18 = call i32 @snprintf(i8* %4, i32 256, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %5)
%19 = call i8* @strdup(i8* %4)
br label %20
20: ; preds = %15, %12, %9
%21 = phi i8* [ %19, %15 ], [ null, %12 ], [ null, %9 ]
%22 = call i32 @fclose(i32* %7)
br label %23
23: ; preds = %1, %20
%24 = phi i8* [ %21, %20 ], [ null, %1 ]
ret i8* %24
}