#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 */
struct stat {
  int st_rdev;
};
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
char *transform_devmapname(unsigned int major, unsigned int minor) {
  DIR *dm_dir;
  struct dirent *dp;
  char filen[256];
  char *dm_name = NULL;
  struct stat aux;
  unsigned int dm_major, dm_minor;
  if ((dm_dir = opendir("/dev/mapper")) == NULL) {
    fprintf(stderr, ("Cannot open %s: %s\n"), "/dev/mapper", strerror(errno));
    exit(4);
  }
  while ((dp = readdir(dm_dir)) != NULL) {
    snprintf(filen, 256, "%s/%s", "/dev/mapper", dp->d_name);
    filen[256 - 1] = '\0';
    if (stat(filen, &aux) == 0) {
      dm_major = ((aux.st_rdev >> 8) & 0xff);
      dm_minor = (aux.st_rdev & 0xff);
      if ((dm_minor == minor) && (dm_major == major)) {
        dm_name = dp->d_name;
        break;
      }
    }
  }
  closedir(dm_dir);
  return dm_name;
}


x86 assembly (clang, -O0):
<transform_devmapname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movq   $0x0,-0x128(%rbp)
lea    0xe5d(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11f9 <transform_devmapname+0x79>
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x13c(%rbp)
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x13c(%rbp),%edi
mov    %rax,%rcx
lea    0xe29(%rip),%rsi
lea    0xe16(%rip),%rdx
call   1060 <fprintf@plt>
mov    $0x4,%edi
call   1090 <exit@plt>
jmp    11fe <transform_devmapname+0x7e>
mov    -0x10(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     12bc <transform_devmapname+0x13c>
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%r8
mov    $0x100,%esi
lea    0xdf1(%rip),%rdx
lea    0xdca(%rip),%rcx
call   1040 <snprintf@plt>
movb   $0x0,-0x21(%rbp)
lea    -0x120(%rbp),%rdi
lea    -0x130(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jne    12b7 <transform_devmapname+0x137>
mov    -0x130(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %eax,-0x134(%rbp)
mov    -0x130(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x138(%rbp)
mov    -0x138(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    12b2 <transform_devmapname+0x132>
mov    -0x134(%rbp),%eax
cmp    -0x4(%rbp),%eax
jne    12b2 <transform_devmapname+0x132>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x128(%rbp)
jmp    12bc <transform_devmapname+0x13c>
jmp    12b7 <transform_devmapname+0x137>
jmp    11fe <transform_devmapname+0x7e>
mov    -0x10(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x128(%rbp),%rax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<transform_devmapname>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %esi,0xc(%rsp)
mov    %edi,0x8(%rsp)
lea    0xe60(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     1259 <transform_devmapname+0xd9>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     123a <transform_devmapname+0xba>
mov    %rax,%rbp
lea    0xe54(%rip),%r13
lea    0xe2d(%rip),%r15
lea    0x10(%rsp),%r14
mov    %rsp,%r12
jmp    11f0 <transform_devmapname+0x70>
nopl   (%rax)
mov    %rbx,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     123a <transform_devmapname+0xba>
mov    0x0(%rbp),%r8
mov    %r14,%rdi
mov    $0x100,%esi
mov    %r13,%rdx
mov    %r15,%rcx
call   1040 <snprintf@plt>
movb   $0x0,0x10f(%rsp)
mov    %r14,%rdi
mov    %r12,%rsi
call   1070 <stat@plt>
test   %rax,%rax
jne    11e0 <transform_devmapname+0x60>
mov    (%rsp),%eax
movzbl %al,%ecx
cmp    0xc(%rsp),%ecx
jne    11e0 <transform_devmapname+0x60>
movzbl %ah,%eax
cmp    0x8(%rsp),%eax
jne    11e0 <transform_devmapname+0x60>
mov    0x0(%rbp),%rbp
jmp    123c <transform_devmapname+0xbc>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1050 <closedir@plt>
mov    %rbp,%rax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d80(%rip),%rax
mov    (%rax),%ebx
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd95(%rip),%rsi
lea    0xd82(%rip),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
call   1060 <fprintf@plt>
mov    $0x4,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@.str = [12 x i8] c"/dev/mapper\00"
@stderr = global i32 0
@.str.1 = [20 x i8] c"Cannot open %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [6 x i8] c"%s/%s\00"
define i8* @transform_devmapname(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca [256 x i8]
%8 = alloca i8*
%9 = alloca %struct.stat
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i8* null, i8** %8
%12 = call i32* @opendir(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %5
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 4)
unreachable
20: ; preds = %2
br label %21
21: ; preds = %55, %20
%22 = load i32*, i32** %5
%23 = call %struct.dirent* @readdir(i32* %22)
store %struct.dirent* %23, %struct.dirent** %6
%24 = icmp ne %struct.dirent* %23, null
br i1 %24, label %25, label %56
25: ; preds = %21
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%27 = load %struct.dirent*, %struct.dirent** %6
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i32 0, i32 0
%29 = load i8*, i8** %28
%30 = call i32 @snprintf(i8* %26, i32 256, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %29)
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 255
store i8 0, i8* %31
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%33 = call i64 @stat(i8* %32, %struct.stat* %9)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %55
35: ; preds = %25
%36 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%37 = load i32, i32* %36
%38 = ashr i32 %37, 8
%39 = and i32 %38, 255
store i32 %39, i32* %10
%40 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%41 = load i32, i32* %40
%42 = and i32 %41, 255
store i32 %42, i32* %11
%43 = load i32, i32* %11
%44 = load i32, i32* %4
%45 = icmp eq i32 %43, %44
br i1 %45, label %46, label %54
46: ; preds = %35
%47 = load i32, i32* %10
%48 = load i32, i32* %3
%49 = icmp eq i32 %47, %48
br i1 %49, label %50, label %54
50: ; preds = %46
%51 = load %struct.dirent*, %struct.dirent** %6
%52 = getelementptr inbounds %struct.dirent, %struct.dirent* %51, i32 0, i32 0
%53 = load i8*, i8** %52
store i8* %53, i8** %8
br label %56
54: ; preds = %46, %35
br label %55
55: ; preds = %54, %25
br label %21
56: ; preds = %50, %21
%57 = load i32*, i32** %5
%58 = call i32 @closedir(i32* %57)
%59 = load i8*, i8** %8
ret i8* %59
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [12 x i8] c"/dev/mapper\00"
@stderr = global i32 0
@.str.1 = [20 x i8] c"Cannot open %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [6 x i8] c"%s/%s\00"
define i8* @transform_devmapname(i32 %0, i32 %1) {
%3 = alloca [256 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.stat* %4 to i8*
%7 = tail call i32* @opendir(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %15, label %9
9: ; preds = %2
%10 = call %struct.dirent* @readdir(i32* %7)
%11 = icmp eq %struct.dirent* %10, null
br i1 %11, label %42, label %12
12: ; preds = %9
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 255
%14 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
br label %21
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i32, i32* @errno
%18 = tail call i8* @strerror(i32 %17)
%19 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = tail call i32 @exit(i32 4)
unreachable
21: ; preds = %12, %39
%22 = phi %struct.dirent* [ %10, %12 ], [ %40, %39 ]
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %22, i64 0, i32 0
%24 = load i8*, i8** %23
%25 = call i32 @snprintf(i8* %5, i32 256, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %24)
store i8 0, i8* %13
%26 = call i64 @stat(i8* %5, %struct.stat* %4)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %39
28: ; preds = %21
%29 = load i32, i32* %14
%30 = and i32 %29, 255
%31 = icmp eq i32 %30, %1
br i1 %31, label %32, label %39
32: ; preds = %28
%33 = lshr i32 %29, 8
%34 = and i32 %33, 255
%35 = icmp eq i32 %34, %0
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = getelementptr inbounds %struct.dirent, %struct.dirent* %22, i64 0, i32 0
%38 = load i8*, i8** %37
br label %42
39: ; preds = %28, %32, %21
%40 = call %struct.dirent* @readdir(i32* %7)
%41 = icmp eq %struct.dirent* %40, null
br i1 %41, label %42, label %21
42: ; preds = %39, %9, %36
%43 = phi i8* [ %38, %36 ], [ null, %9 ], [ null, %39 ]
%44 = call i32 @closedir(i32* %7)
ret i8* %43
}