#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 /*<<< orphan*/ st_mode;
  int /*<<< orphan*/ st_rdev;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ dev_t;

/* Variables and functions */
scalar_t__ S_ISBLK(int /*<<< orphan*/);
scalar_t__ S_ISCHR(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
unsigned int major(int /*<<< orphan*/);
unsigned int minor(int /*<<< orphan*/);
int readlink(char *, char *, int);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strrchr(char *, char);
char *util_path_sysfs(char *, unsigned int, unsigned int);
scalar_t__ util_sys_get_base_dev(int /*<<< orphan*/, int /*<<< orphan*/ *);
int util_sys_get_dev_addr(const char *dev, char *addr) {
  char device[256], *result;
  unsigned int maj, min;
  struct stat s;
  ssize_t len;
  dev_t base;
  char *path;
  if (stat(dev, &s) != 0)
    return -1;
  if (util_sys_get_base_dev(s.st_rdev, &base))
    return -1;
  maj = major(base);
  min = minor(base);
  if (S_ISBLK(s.st_mode))
    path = util_path_sysfs("dev/block/%u:%u/device", maj, min);
  else if (S_ISCHR(s.st_mode))
    path = util_path_sysfs("dev/char/%u:%u/device", maj, min);
  else
    return -1;
  len = readlink(path, device, 256 - 1);
  free(path);
  if (len != -1)
    device[len] = '\0';
  else
    return -1;
  result = strrchr(device, '/');
  if (result)
    result++;
  else
    result = device;
  strcpy(addr, result);
  return 0;
}


x86 assembly (clang, -O0):
<util_sys_get_dev_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x138(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%rax
je     11e9 <util_sys_get_dev_addr+0x39>
movl   $0xffffffff,-0x4(%rbp)
jmp    136f <util_sys_get_dev_addr+0x1bf>
mov    -0x134(%rbp),%edi
lea    -0x140(%rbp),%rsi
call   10a0 <util_sys_get_base_dev@plt>
cmp    $0x0,%rax
je     1211 <util_sys_get_dev_addr+0x61>
movl   $0xffffffff,-0x4(%rbp)
jmp    136f <util_sys_get_dev_addr+0x1bf>
mov    -0x140(%rbp),%edi
call   1080 <major@plt>
mov    %eax,-0x12c(%rbp)
mov    -0x140(%rbp),%edi
call   1060 <minor@plt>
mov    %eax,-0x130(%rbp)
mov    -0x138(%rbp),%edi
call   10d0 <S_ISBLK@plt>
cmp    $0x0,%rax
je     126c <util_sys_get_dev_addr+0xbc>
mov    -0x12c(%rbp),%esi
mov    -0x130(%rbp),%edx
lea    0xda5(%rip),%rdi
call   10b0 <util_path_sysfs@plt>
mov    %rax,-0x148(%rbp)
jmp    12b6 <util_sys_get_dev_addr+0x106>
mov    -0x138(%rbp),%edi
call   10c0 <S_ISCHR@plt>
cmp    $0x0,%rax
je     12a5 <util_sys_get_dev_addr+0xf5>
mov    -0x12c(%rbp),%esi
mov    -0x130(%rbp),%edx
lea    0xd83(%rip),%rdi
call   10b0 <util_path_sysfs@plt>
mov    %rax,-0x148(%rbp)
jmp    12b1 <util_sys_get_dev_addr+0x101>
movl   $0xffffffff,-0x4(%rbp)
jmp    136f <util_sys_get_dev_addr+0x1bf>
jmp    12b6 <util_sys_get_dev_addr+0x106>
mov    -0x148(%rbp),%rdi
lea    -0x120(%rbp),%rsi
mov    $0xff,%edx
call   1050 <readlink@plt>
mov    %eax,-0x13c(%rbp)
mov    -0x148(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0xffffffff,-0x13c(%rbp)
je     1301 <util_sys_get_dev_addr+0x151>
movslq -0x13c(%rbp),%rax
movb   $0x0,-0x120(%rbp,%rax,1)
jmp    130d <util_sys_get_dev_addr+0x15d>
movl   $0xffffffff,-0x4(%rbp)
jmp    136f <util_sys_get_dev_addr+0x1bf>
lea    -0x120(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
mov    %rax,-0x128(%rbp)
cmpq   $0x0,-0x128(%rbp)
je     134a <util_sys_get_dev_addr+0x19a>
mov    -0x128(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x128(%rbp)
jmp    1358 <util_sys_get_dev_addr+0x1a8>
lea    -0x120(%rbp),%rax
mov    %rax,-0x128(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x128(%rbp),%rsi
call   1040 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_sys_get_dev_addr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x110,%rsp
mov    %rsi,%r12
lea    0x8(%rsp),%rsi
call   1090 <stat@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    1293 <util_sys_get_dev_addr+0xe3>
mov    0xc(%rsp),%edi
lea    0x4(%rsp),%rsi
call   10a0 <util_sys_get_base_dev@plt>
test   %rax,%rax
jne    1293 <util_sys_get_dev_addr+0xe3>
mov    0x4(%rsp),%edi
call   1080 <major@plt>
mov    %eax,%r14d
mov    0x4(%rsp),%edi
call   1060 <minor@plt>
mov    %eax,%r15d
mov    0x8(%rsp),%edi
call   10d0 <S_ISBLK@plt>
test   %rax,%rax
je     1220 <util_sys_get_dev_addr+0x70>
lea    0xde2(%rip),%rdi
jmp    1235 <util_sys_get_dev_addr+0x85>
mov    0x8(%rsp),%edi
call   10c0 <S_ISCHR@plt>
test   %rax,%rax
je     1293 <util_sys_get_dev_addr+0xe3>
lea    0xde2(%rip),%rdi
mov    %r14d,%esi
mov    %r15d,%edx
call   10b0 <util_path_sysfs@plt>
mov    %rax,%rbx
lea    0x10(%rsp),%r14
mov    %rax,%rdi
mov    %r14,%rsi
mov    $0xff,%edx
call   1050 <readlink@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1030 <free@plt>
cmp    $0xffffffff,%r15d
je     1293 <util_sys_get_dev_addr+0xe3>
movslq %r15d,%rax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r14,%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
lea    0x1(%rax),%rsi
test   %rax,%rax
cmove  %r14,%rsi
mov    %r12,%rdi
call   1040 <strcpy@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@.str = [23 x i8] c"dev/block/%u:%u/device\00"
@.str.1 = [22 x i8] c"dev/char/%u:%u/device\00"
define i32 @util_sys_get_dev_addr(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [256 x i8]
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.stat
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%14 = load i8*, i8** %4
%15 = call i64 @stat(i8* %14, %struct.stat* %10)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %2
store i32 -1, i32* %3
br label %75
18: ; preds = %2
%19 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 1
%20 = load i32, i32* %19
%21 = call i64 @util_sys_get_base_dev(i32 %20, i32* %12)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %18
store i32 -1, i32* %3
br label %75
24: ; preds = %18
%25 = load i32, i32* %12
%26 = call i32 @major(i32 %25)
store i32 %26, i32* %8
%27 = load i32, i32* %12
%28 = call i32 @minor(i32 %27)
store i32 %28, i32* %9
%29 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i64 @S_ISBLK(i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %24
%34 = load i32, i32* %8
%35 = load i32, i32* %9
%36 = call i8* @util_path_sysfs(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %34, i32 %35)
store i8* %36, i8** %13
br label %48
37: ; preds = %24
%38 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%39 = load i32, i32* %38
%40 = call i64 @S_ISCHR(i32 %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %37
%43 = load i32, i32* %8
%44 = load i32, i32* %9
%45 = call i8* @util_path_sysfs(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %43, i32 %44)
store i8* %45, i8** %13
br label %47
46: ; preds = %37
store i32 -1, i32* %3
br label %75
47: ; preds = %42
br label %48
48: ; preds = %47, %33
%49 = load i8*, i8** %13
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%51 = call i32 @readlink(i8* %49, i8* %50, i32 255)
store i32 %51, i32* %11
%52 = load i8*, i8** %13
%53 = call i32 @free(i8* %52)
%54 = load i32, i32* %11
%55 = icmp ne i32 %54, -1
br i1 %55, label %56, label %60
56: ; preds = %48
%57 = load i32, i32* %11
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 %58
store i8 0, i8* %59
br label %61
60: ; preds = %48
store i32 -1, i32* %3
br label %75
61: ; preds = %56
%62 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%63 = call i8* @strrchr(i8* %62, i8 signext 47)
store i8* %63, i8** %7
%64 = load i8*, i8** %7
%65 = icmp ne i8* %64, null
br i1 %65, label %66, label %69
66: ; preds = %61
%67 = load i8*, i8** %7
%68 = getelementptr inbounds i8, i8* %67, i32 1
store i8* %68, i8** %7
br label %71
69: ; preds = %61
%70 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
store i8* %70, i8** %7
br label %71
71: ; preds = %69, %66
%72 = load i8*, i8** %5
%73 = load i8*, i8** %7
%74 = call i32 @strcpy(i8* %72, i8* %73)
store i32 0, i32* %3
br label %75
75: ; preds = %71, %60, %46, %23, %17
%76 = load i32, i32* %3
ret i32 %76
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@.str = [23 x i8] c"dev/block/%u:%u/device\00"
@.str.1 = [22 x i8] c"dev/char/%u:%u/device\00"
define i32 @util_sys_get_dev_addr(i8* %0, i8* %1) {
%3 = alloca [256 x i8]
%4 = alloca %struct.stat
%5 = alloca i32
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%7 = bitcast %struct.stat* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = call i64 @stat(i8* %0, %struct.stat* %4)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %43
11: ; preds = %2
%12 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%13 = load i32, i32* %12
%14 = call i64 @util_sys_get_base_dev(i32 %13, i32* %5)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %43
16: ; preds = %11
%17 = load i32, i32* %5
%18 = call i32 @major(i32 %17)
%19 = load i32, i32* %5
%20 = call i32 @minor(i32 %19)
%21 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%22 = load i32, i32* %21
%23 = call i64 @S_ISBLK(i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %16
%26 = load i32, i32* %21
%27 = call i64 @S_ISCHR(i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %43, label %29
29: ; preds = %25, %16
%30 = phi i8* [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), %16 ], [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), %25 ]
%31 = call i8* @util_path_sysfs(i8* %30, i32 %18, i32 %20)
%32 = call i32 @readlink(i8* %31, i8* %6, i32 255)
%33 = call i32 @free(i8* %31)
%34 = icmp eq i32 %32, -1
br i1 %34, label %43, label %35
35: ; preds = %29
%36 = sext i32 %32 to i64
%37 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %36
store i8 0, i8* %37
%38 = call i8* @strrchr(i8* %6, i8 signext 47)
%39 = icmp eq i8* %38, null
%40 = getelementptr inbounds i8, i8* %38, i64 1
%41 = select i1 %39, i8* %6, i8* %40
%42 = call i32 @strcpy(i8* %1, i8* %41)
br label %43
43: ; preds = %29, %25, %11, %2, %35
%44 = phi i32 [ 0, %35 ], [ -1, %2 ], [ -1, %11 ], [ -1, %25 ], [ -1, %29 ]
ret i32 %44
}