#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 {
  scalar_t__ st_rdev;
  int /*<<< orphan*/ st_mode;
};
typedef size_t ssize_t;
typedef int /*<<< orphan*/ path;
typedef int /*<<< orphan*/ link;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ S_ISBLK(int /*<<< orphan*/);
char *dm_device_path(char *, int, int);
scalar_t__ dm_is_dm_kernel_name(char *);
int /*<<< orphan*/ free(char *);
char *lookup_dev_old(int, int);
scalar_t__ makedev(int, int);
size_t readlink(char *, char *, int);
scalar_t__ snprintf(char *, int, char *, char const *);
int sscanf(char const *, char *, int *, int *);
scalar_t__ stat(char *, struct stat *);
char *strdup(char *);
char *strrchr(char *, char);
char *crypt_lookup_dev(const char *dev_id) {
  int major, minor;
  char link[PATH_MAX], path[PATH_MAX], *devname, *devpath = NULL;
  struct stat st;
  ssize_t len;
  if (sscanf(dev_id, "%d:%d", &major, &minor) != 2)
    return NULL;
  if (snprintf(path, sizeof(path), "/sys/dev/block/%s", dev_id) < 0)
    return NULL;
  len = readlink(path, link, sizeof(link) - 1);
  if (len < 0) {
    if (stat("/sys/dev/block", &st) < 0)
      return lookup_dev_old(major, minor);
    return NULL;
  }
  link[len] = '\0';
  devname = strrchr(link, '/');
  if (!devname)
    return NULL;
  devname++;
  if (dm_is_dm_kernel_name(devname))
    devpath = dm_device_path("/dev/mapper/", major, minor);
  else if (snprintf(path, sizeof(path), "/dev/%s", devname) > 0)
    devpath = strdup(path);
  if (devpath && ((stat(devpath, &st) < 0) || !S_ISBLK(st.st_mode) ||
                  (st.st_rdev != makedev(major, minor)))) {
    free(devpath);
    return lookup_dev_old(major, minor);
  }
  return devpath;
}


x86 assembly (clang, -O0):
<crypt_lookup_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e1a(%rip),%rax
mov    (%rax),%ecx
mov    %rcx,-0x80(%rbp)
mov    %rsp,%rdx
mov    %rdx,-0x20(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x78(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x28(%rbp)
mov    (%rax),%eax
mov    %rax,-0x70(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x68(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xdcc(%rip),%rsi
lea    -0x14(%rbp),%rdx
lea    -0x18(%rbp),%rcx
call   10b0 <sscanf@plt>
cmp    $0x2,%eax
je     125e <crypt_lookup_dev+0x9e>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x5c(%rbp)
jmp    143d <crypt_lookup_dev+0x27d>
mov    -0x68(%rbp),%rdi
mov    -0x70(%rbp),%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xd93(%rip),%rdx
call   1060 <snprintf@plt>
cmp    $0x0,%rax
jge    1296 <crypt_lookup_dev+0xd6>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x5c(%rbp)
jmp    143d <crypt_lookup_dev+0x27d>
mov    -0x78(%rbp),%rsi
mov    -0x68(%rbp),%rdi
mov    -0x80(%rbp),%rax
sub    $0x1,%rax
mov    %eax,%edx
call   1040 <readlink@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jae    1305 <crypt_lookup_dev+0x145>
lea    0xd55(%rip),%rdi
lea    -0x50(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%rax
jge    12f1 <crypt_lookup_dev+0x131>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1080 <lookup_dev_old@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x5c(%rbp)
jmp    143d <crypt_lookup_dev+0x27d>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x5c(%rbp)
jmp    143d <crypt_lookup_dev+0x27d>
mov    -0x78(%rbp),%rdi
mov    -0x58(%rbp),%rax
movb   $0x0,(%rdi,%rax,1)
mov    $0x2f,%esi
call   1070 <strrchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    133e <crypt_lookup_dev+0x17e>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x5c(%rbp)
jmp    143d <crypt_lookup_dev+0x27d>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1050 <dm_is_dm_kernel_name@plt>
cmp    $0x0,%rax
je     1378 <crypt_lookup_dev+0x1b8>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xcbd(%rip),%rdi
call   10a0 <dm_device_path@plt>
mov    %rax,-0x40(%rbp)
jmp    13ae <crypt_lookup_dev+0x1ee>
mov    -0x68(%rbp),%rdi
mov    -0x70(%rbp),%rax
mov    %eax,%esi
mov    -0x38(%rbp),%rcx
lea    0xca7(%rip),%rdx
call   1060 <snprintf@plt>
cmp    $0x0,%rax
jle    13a9 <crypt_lookup_dev+0x1e9>
mov    -0x68(%rbp),%rdi
call   10e0 <strdup@plt>
mov    %rax,-0x40(%rbp)
jmp    13ae <crypt_lookup_dev+0x1ee>
cmpq   $0x0,-0x40(%rbp)
je     142e <crypt_lookup_dev+0x26e>
mov    -0x40(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%rax
jl     140a <crypt_lookup_dev+0x24a>
mov    -0x48(%rbp),%edi
call   10c0 <S_ISBLK@plt>
cmp    $0x0,%eax
je     140a <crypt_lookup_dev+0x24a>
mov    -0x50(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   10d0 <makedev@plt>
mov    %rax,%rcx
mov    -0x88(%rbp),%rax
cmp    %rcx,%rax
je     142e <crypt_lookup_dev+0x26e>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1080 <lookup_dev_old@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x5c(%rbp)
jmp    143d <crypt_lookup_dev+0x27d>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x5c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<crypt_lookup_dev>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r13
mov    %rsp,-0x38(%rbp)
mov    0x2e11(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
lea    0xf(%r12),%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rbx
mov    %rbx,%rsp
mov    (%rax),%r14d
mov    %rsp,%r15
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
lea    0xdf1(%rip),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x2c(%rbp),%rcx
call   10b0 <sscanf@plt>
cmp    $0x2,%eax
jne    130e <crypt_lookup_dev+0x14e>
lea    0xdda(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
mov    %r13,%rcx
call   1060 <snprintf@plt>
test   %rax,%rax
js     130e <crypt_lookup_dev+0x14e>
add    $0xffffffff,%r12d
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1040 <readlink@plt>
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
test   %rax,%rax
je     130e <crypt_lookup_dev+0x14e>
mov    %rax,%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
call   1050 <dm_is_dm_kernel_name@plt>
test   %rax,%rax
je     1297 <crypt_lookup_dev+0xd7>
mov    -0x30(%rbp),%esi
mov    -0x2c(%rbp),%edx
lea    0xd88(%rip),%rdi
call   10a0 <dm_device_path@plt>
jmp    12b9 <crypt_lookup_dev+0xf9>
lea    0xd87(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
mov    %rbx,%rcx
call   1060 <snprintf@plt>
test   %rax,%rax
jle    130e <crypt_lookup_dev+0x14e>
mov    %r15,%rdi
call   10e0 <strdup@plt>
mov    %rax,%rbx
mov    -0x38(%rbp),%r15
test   %rax,%rax
je     1329 <crypt_lookup_dev+0x169>
lea    -0x48(%rbp),%rsi
mov    %rbx,%rdi
call   1090 <stat@plt>
test   %rax,%rax
js     12f6 <crypt_lookup_dev+0x136>
mov    -0x40(%rbp),%edi
call   10c0 <S_ISBLK@plt>
test   %eax,%eax
je     12f6 <crypt_lookup_dev+0x136>
mov    -0x48(%rbp),%r14
mov    -0x30(%rbp),%edi
mov    -0x2c(%rbp),%esi
call   10d0 <makedev@plt>
cmp    %rax,%r14
je     1314 <crypt_lookup_dev+0x154>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%edi
mov    -0x2c(%rbp),%esi
call   1080 <lookup_dev_old@plt>
mov    %rax,%rbx
jmp    1314 <crypt_lookup_dev+0x154>
xor    %ebx,%ebx
mov    -0x38(%rbp),%r15
mov    %r15,%rsp
mov    %rbx,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebx,%ebx
jmp    1314 <crypt_lookup_dev+0x154>

LLVM IR (-O0):
%struct.stat = type { i64, i32 }
@PATH_MAX = global i32 0
@.str = [6 x i8] c"%d:%d\00"
@.str.1 = [18 x i8] c"/sys/dev/block/%s\00"
@.str.2 = [15 x i8] c"/sys/dev/block\00"
@.str.3 = [13 x i8] c"/dev/mapper/\00"
@.str.4 = [8 x i8] c"/dev/%s\00"
define i8* @crypt_lookup_dev(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca %struct.stat
%12 = alloca i64
%13 = alloca i32
store i8* %0, i8** %3
%14 = load i32, i32* @PATH_MAX
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %6
%17 = alloca i8, i64 %15
store i64 %15, i64* %7
%18 = load i32, i32* @PATH_MAX
%19 = zext i32 %18 to i64
%20 = alloca i8, i64 %19
store i64 %19, i64* %8
store i8* null, i8** %10
%21 = load i8*, i8** %3
%22 = call i32 @sscanf(i8* %21, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32* %4, i32* %5)
%23 = icmp ne i32 %22, 2
br i1 %23, label %24, label %25
24: ; preds = %1
store i8* null, i8** %2
store i32 1, i32* %13
br label %97
25: ; preds = %1
%26 = trunc i64 %19 to i32
%27 = load i8*, i8** %3
%28 = call i64 @snprintf(i8* %20, i32 %26, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %25
store i8* null, i8** %2
store i32 1, i32* %13
br label %97
31: ; preds = %25
%32 = sub i64 %15, 1
%33 = trunc i64 %32 to i32
%34 = call i64 @readlink(i8* %20, i8* %17, i32 %33)
store i64 %34, i64* %12
%35 = load i64, i64* %12
%36 = icmp ult i64 %35, 0
br i1 %36, label %37, label %45
37: ; preds = %31
%38 = call i64 @stat(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), %struct.stat* %11)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %37
%41 = load i32, i32* %4
%42 = load i32, i32* %5
%43 = call i8* @lookup_dev_old(i32 %41, i32 %42)
store i8* %43, i8** %2
store i32 1, i32* %13
br label %97
44: ; preds = %37
store i8* null, i8** %2
store i32 1, i32* %13
br label %97
45: ; preds = %31
%46 = load i64, i64* %12
%47 = getelementptr inbounds i8, i8* %17, i64 %46
store i8 0, i8* %47
%48 = call i8* @strrchr(i8* %17, i8 signext 47)
store i8* %48, i8** %9
%49 = load i8*, i8** %9
%50 = icmp ne i8* %49, null
br i1 %50, label %52, label %51
51: ; preds = %45
store i8* null, i8** %2
store i32 1, i32* %13
br label %97
52: ; preds = %45
%53 = load i8*, i8** %9
%54 = getelementptr inbounds i8, i8* %53, i32 1
store i8* %54, i8** %9
%55 = load i8*, i8** %9
%56 = call i64 @dm_is_dm_kernel_name(i8* %55)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %62
58: ; preds = %52
%59 = load i32, i32* %4
%60 = load i32, i32* %5
%61 = call i8* @dm_device_path(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %59, i32 %60)
store i8* %61, i8** %10
br label %70
62: ; preds = %52
%63 = trunc i64 %19 to i32
%64 = load i8*, i8** %9
%65 = call i64 @snprintf(i8* %20, i32 %63, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %64)
%66 = icmp sgt i64 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %62
%68 = call i8* @strdup(i8* %20)
store i8* %68, i8** %10
br label %69
69: ; preds = %67, %62
br label %70
70: ; preds = %69, %58
%71 = load i8*, i8** %10
%72 = icmp ne i8* %71, null
br i1 %72, label %73, label %95
73: ; preds = %70
%74 = load i8*, i8** %10
%75 = call i64 @stat(i8* %74, %struct.stat* %11)
%76 = icmp slt i64 %75, 0
br i1 %76, label %89, label %77
77: ; preds = %73
%78 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 1
%79 = load i32, i32* %78
%80 = call i32 @S_ISBLK(i32 %79)
%81 = icmp ne i32 %80, 0
br i1 %81, label %82, label %89
82: ; preds = %77
%83 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%84 = load i64, i64* %83
%85 = load i32, i32* %4
%86 = load i32, i32* %5
%87 = call i64 @makedev(i32 %85, i32 %86)
%88 = icmp ne i64 %84, %87
br i1 %88, label %89, label %95
89: ; preds = %82, %77, %73
%90 = load i8*, i8** %10
%91 = call i32 @free(i8* %90)
%92 = load i32, i32* %4
%93 = load i32, i32* %5
%94 = call i8* @lookup_dev_old(i32 %92, i32 %93)
store i8* %94, i8** %2
store i32 1, i32* %13
br label %97
95: ; preds = %82, %70
%96 = load i8*, i8** %10
store i8* %96, i8** %2
store i32 1, i32* %13
br label %97
97: ; preds = %95, %89, %51, %44, %40, %30, %24
%98 = load i8*, i8** %6
%99 = load i8*, i8** %2
ret i8* %99
}

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
@PATH_MAX = global i32 0
@.str = [6 x i8] c"%d:%d\00"
@.str.1 = [18 x i8] c"/sys/dev/block/%s\00"
@.str.3 = [13 x i8] c"/dev/mapper/\00"
@.str.4 = [8 x i8] c"/dev/%s\00"
define i8* @crypt_lookup_dev(i8* %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.stat
%5 = bitcast i32* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = load i32, i32* @PATH_MAX
%8 = zext i32 %7 to i64
%9 = tail call i8* @llvm.stacksave()
%10 = alloca i8, i64 %8
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = alloca i8, i64 %12
%14 = bitcast %struct.stat* %4 to i8*
%15 = call i32 @sscanf(i8* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32* %2, i32* %3)
%16 = icmp eq i32 %15, 2
br i1 %16, label %17, label %62
17: ; preds = %1
%18 = call i64 @snprintf(i8* %13, i32 %11, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%19 = icmp slt i64 %18, 0
br i1 %19, label %62, label %20
20: ; preds = %17
%21 = add i32 %7, -1
%22 = call i64 @readlink(i8* %13, i8* %10, i32 %21)
%23 = getelementptr inbounds i8, i8* %10, i64 %22
store i8 0, i8* %23
%24 = call i8* @strrchr(i8* %10, i8 signext 47)
%25 = icmp eq i8* %24, null
br i1 %25, label %62, label %26
26: ; preds = %20
%27 = getelementptr inbounds i8, i8* %24, i64 1
%28 = call i64 @dm_is_dm_kernel_name(i8* %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %34, label %30
30: ; preds = %26
%31 = load i32, i32* %2
%32 = load i32, i32* %3
%33 = call i8* @dm_device_path(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %31, i32 %32)
br label %39
34: ; preds = %26
%35 = call i64 @snprintf(i8* %13, i32 %11, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %27)
%36 = icmp sgt i64 %35, 0
br i1 %36, label %37, label %62
37: ; preds = %34
%38 = call i8* @strdup(i8* %13)
br label %39
39: ; preds = %37, %30
%40 = phi i8* [ %33, %30 ], [ %38, %37 ]
%41 = icmp eq i8* %40, null
br i1 %41, label %62, label %42
42: ; preds = %39
%43 = call i64 @stat(i8* %40, %struct.stat* %4)
%44 = icmp slt i64 %43, 0
br i1 %44, label %57, label %45
45: ; preds = %42
%46 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%47 = load i32, i32* %46
%48 = call i32 @S_ISBLK(i32 %47)
%49 = icmp eq i32 %48, 0
br i1 %49, label %57, label %50
50: ; preds = %45
%51 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%52 = load i64, i64* %51
%53 = load i32, i32* %2
%54 = load i32, i32* %3
%55 = call i64 @makedev(i32 %53, i32 %54)
%56 = icmp eq i64 %52, %55
br i1 %56, label %62, label %57
57: ; preds = %50, %45, %42
%58 = call i32 @free(i8* %40)
%59 = load i32, i32* %2
%60 = load i32, i32* %3
%61 = call i8* @lookup_dev_old(i32 %59, i32 %60)
br label %62
62: ; preds = %34, %39, %50, %20, %17, %1, %57
%63 = phi i8* [ %61, %57 ], [ null, %1 ], [ null, %17 ], [ null, %20 ], [ %40, %50 ], [ null, %39 ], [ null, %34 ]
ret i8* %63
}