#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*/ dev_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
int major(int /*<<< orphan*/);
int /*<<< orphan*/ makedev(int, int);
int minor(int /*<<< orphan*/);
int sscanf(char *, char *, int *, int *);
scalar_t__ util_file_read_line(char *, int, char *);
char *util_path_sysfs(char *, int, int);
int /*<<< orphan*/ util_sys_dev_is_partition(int /*<<< orphan*/);
int /*<<< orphan*/ warn(char *, char *);
int /*<<< orphan*/ warnx(char *, char *);
int util_sys_get_base_dev(dev_t dev, dev_t *base_dev) {
  int base_major, base_minor;
  char buf[4096];
  char *path;
  if (!util_sys_dev_is_partition(dev)) {
    *base_dev = makedev(major(dev), minor(dev));
    return 0;
  }
  path = util_path_sysfs("dev/block/%d:%d/../dev", major(dev), minor(dev));
  if (util_file_read_line(buf, sizeof(buf), path)) {
    warnx("Could not read from path '%s'", path);
    free(path);
    return -1;
  }
  free(path);
  if (sscanf(buf, "%i:%i", &base_major, &base_minor) != 2) {
    warn("Could not parse major:minor from string '%s'", buf);
    return -1;
  }
  *base_dev = makedev(base_major, base_minor);
  return 0;
}


x86 assembly (clang, -O0):
<util_sys_get_base_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   10b0 <util_sys_dev_is_partition@plt>
cmp    $0x0,%eax
jne    11fa <util_sys_get_base_dev+0x5a>
mov    -0x8(%rbp),%edi
call   1070 <major@plt>
mov    %eax,-0x102c(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <minor@plt>
mov    -0x102c(%rbp),%edi
mov    %eax,%esi
call   10c0 <makedev@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    12e1 <util_sys_get_base_dev+0x141>
mov    -0x8(%rbp),%edi
call   1070 <major@plt>
mov    %eax,-0x1030(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <minor@plt>
mov    -0x1030(%rbp),%esi
mov    %eax,%edx
lea    0xde1(%rip),%rdi
call   1080 <util_path_sysfs@plt>
mov    %rax,-0x1028(%rbp)
lea    -0x1020(%rbp),%rdi
mov    -0x1028(%rbp),%rdx
mov    $0x1000,%esi
call   10a0 <util_file_read_line@plt>
cmp    $0x0,%rax
je     1278 <util_sys_get_base_dev+0xd8>
mov    -0x1028(%rbp),%rsi
lea    0xdbc(%rip),%rdi
call   1060 <warnx@plt>
mov    -0x1028(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e1 <util_sys_get_base_dev+0x141>
mov    -0x1028(%rbp),%rdi
call   1030 <free@plt>
lea    -0x1020(%rbp),%rdi
lea    0xda3(%rip),%rsi
lea    -0x14(%rbp),%rdx
lea    -0x18(%rbp),%rcx
call   1090 <sscanf@plt>
cmp    $0x2,%eax
je     12c7 <util_sys_get_base_dev+0x127>
lea    -0x1020(%rbp),%rsi
lea    0xd85(%rip),%rdi
call   1040 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e1 <util_sys_get_base_dev+0x141>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   10c0 <makedev@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_sys_get_base_dev>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1018,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
call   10b0 <util_sys_dev_is_partition@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1070 <major@plt>
mov    %eax,%r15d
mov    %ebp,%edi
call   1050 <minor@plt>
test   %ebx,%ebx
je     1212 <util_sys_get_base_dev+0x72>
lea    0xe2b(%rip),%rdi
mov    %r15d,%esi
mov    %eax,%edx
call   1080 <util_path_sysfs@plt>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0x1000,%esi
mov    %rax,%rdx
call   10a0 <util_file_read_line@plt>
test   %rax,%rax
je     1219 <util_sys_get_base_dev+0x79>
lea    0xe17(%rip),%rdi
mov    %rbx,%rsi
call   1060 <warnx@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    1266 <util_sys_get_base_dev+0xc6>
mov    %r15d,%edi
mov    %eax,%esi
jmp    1249 <util_sys_get_base_dev+0xa9>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xe0d(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0xc(%rsp),%rdx
lea    0x8(%rsp),%rcx
call   1090 <sscanf@plt>
cmp    $0x2,%eax
jne    1255 <util_sys_get_base_dev+0xb5>
mov    0xc(%rsp),%edi
mov    0x8(%rsp),%esi
call   10c0 <makedev@plt>
mov    %eax,(%r14)
xor    %eax,%eax
jmp    126b <util_sys_get_base_dev+0xcb>
lea    0xddf(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1040 <warn@plt>
mov    $0xffffffff,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"dev/block/%d:%d/../dev\00"
@.str.1 = [30 x i8] c"Could not read from path '%s'\00"
@.str.2 = [6 x i8] c"%i:%i\00"
@.str.3 = [45 x i8] c"Could not parse major:minor from string '%s'\00"
define i32 @util_sys_get_base_dev(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [4096 x i8]
%9 = alloca i8*
store i32 %0, i32* %4
store i32* %1, i32** %5
%10 = load i32, i32* %4
%11 = call i32 @util_sys_dev_is_partition(i32 %10)
%12 = icmp ne i32 %11, 0
br i1 %12, label %20, label %13
13: ; preds = %2
%14 = load i32, i32* %4
%15 = call i32 @major(i32 %14)
%16 = load i32, i32* %4
%17 = call i32 @minor(i32 %16)
%18 = call i32 @makedev(i32 %15, i32 %17)
%19 = load i32*, i32** %5
store i32 %18, i32* %19
store i32 0, i32* %3
br label %49
20: ; preds = %2
%21 = load i32, i32* %4
%22 = call i32 @major(i32 %21)
%23 = load i32, i32* %4
%24 = call i32 @minor(i32 %23)
%25 = call i8* @util_path_sysfs(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %22, i32 %24)
store i8* %25, i8** %9
%26 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%27 = load i8*, i8** %9
%28 = call i64 @util_file_read_line(i8* %26, i32 4096, i8* %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %20
%31 = load i8*, i8** %9
%32 = call i32 @warnx(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %31)
%33 = load i8*, i8** %9
%34 = call i32 @free(i8* %33)
store i32 -1, i32* %3
br label %49
35: ; preds = %20
%36 = load i8*, i8** %9
%37 = call i32 @free(i8* %36)
%38 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%39 = call i32 @sscanf(i8* %38, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32* %6, i32* %7)
%40 = icmp ne i32 %39, 2
br i1 %40, label %41, label %44
41: ; preds = %35
%42 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%43 = call i32 @warn(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0), i8* %42)
store i32 -1, i32* %3
br label %49
44: ; preds = %35
%45 = load i32, i32* %6
%46 = load i32, i32* %7
%47 = call i32 @makedev(i32 %45, i32 %46)
%48 = load i32*, i32** %5
store i32 %47, i32* %48
store i32 0, i32* %3
br label %49
49: ; preds = %44, %41, %30, %13
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
@.str = [23 x i8] c"dev/block/%d:%d/../dev\00"
@.str.1 = [30 x i8] c"Could not read from path '%s'\00"
@.str.2 = [6 x i8] c"%i:%i\00"
@.str.3 = [45 x i8] c"Could not parse major:minor from string '%s'\00"
define i32 @util_sys_get_base_dev(i32 %0, i32* nocapture writeonly %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [4096 x i8]
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%9 = tail call i32 @util_sys_dev_is_partition(i32 %0)
%10 = icmp eq i32 %9, 0
%11 = tail call i32 @major(i32 %0)
%12 = tail call i32 @minor(i32 %0)
br i1 %10, label %13, label %15
13: ; preds = %2
%14 = tail call i32 @makedev(i32 %11, i32 %12)
store i32 %14, i32* %1
br label %32
15: ; preds = %2
%16 = tail call i8* @util_path_sysfs(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %12)
%17 = call i64 @util_file_read_line(i8* %8, i32 4096, i8* %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %15
%20 = call i32 @warnx(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%21 = call i32 @free(i8* %16)
br label %32
22: ; preds = %15
%23 = call i32 @free(i8* %16)
%24 = call i32 @sscanf(i8* %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32* %3, i32* %4)
%25 = icmp eq i32 %24, 2
br i1 %25, label %28, label %26
26: ; preds = %22
%27 = call i32 @warn(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0), i8* %8)
br label %32
28: ; preds = %22
%29 = load i32, i32* %3
%30 = load i32, i32* %4
%31 = call i32 @makedev(i32 %29, i32 %30)
store i32 %31, i32* %1
br label %32
32: ; preds = %28, %26, %19, %13
%33 = phi i32 [ -1, %19 ], [ -1, %26 ], [ 0, %28 ], [ 0, %13 ]
ret i32 %33
}