#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;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ major(int /*<<< orphan*/);
int /*<<< orphan*/ minor(int /*<<< orphan*/);
scalar_t__ util_file_read_i(int *, int, char *);
char *util_path_sysfs(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ warnx(char *, char *);
int util_sys_get_partnum(dev_t dev) {
  int partnum = -1;
  char *path;
  path = util_path_sysfs("dev/block/%u:%u/partition", major(dev), minor(dev));
  if (util_file_read_i(&partnum, 10, path)) {
    warnx("Could not read from path '%s'", path);
    goto out;
  }
  if (partnum <= 0) {
    warnx("Bad partition number in '%s'", path);
    partnum = -1;
    goto out;
  }
out:
  free(path);
  return partnum;
}


x86 assembly (clang, -O0):
<util_sys_get_partnum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0xffffffff,-0x8(%rbp)
mov    -0x4(%rbp),%edi
call   1060 <major@plt>
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <minor@plt>
mov    -0x14(%rbp),%esi
mov    %eax,%edx
lea    0xe6f(%rip),%rdi
call   1080 <util_path_sysfs@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdx
lea    -0x8(%rbp),%rdi
mov    $0xa,%esi
call   1070 <util_file_read_i@plt>
cmp    $0x0,%rax
je     11cb <util_sys_get_partnum+0x6b>
mov    -0x10(%rbp),%rsi
lea    0xe59(%rip),%rdi
call   1050 <warnx@plt>
jmp    11f6 <util_sys_get_partnum+0x96>
cmpl   $0x0,-0x8(%rbp)
jg     11f1 <util_sys_get_partnum+0x91>
mov    -0x10(%rbp),%rsi
lea    0xe58(%rip),%rdi
call   1050 <warnx@plt>
movl   $0xffffffff,-0x8(%rbp)
jmp    11f6 <util_sys_get_partnum+0x96>
jmp    11f6 <util_sys_get_partnum+0x96>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_sys_get_partnum>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
movl   $0xffffffff,0x4(%rsp)
ff
call   1060 <major@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <minor@plt>
lea    0xe7e(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1080 <util_path_sysfs@plt>
mov    %rax,%rbx
lea    0x4(%rsp),%rdi
mov    $0xa,%esi
mov    %rax,%rdx
call   1070 <util_file_read_i@plt>
test   %rax,%rax
je     11b6 <util_sys_get_partnum+0x56>
lea    0xe6e(%rip),%rdi
mov    %rbx,%rsi
call   1050 <warnx@plt>
jmp    11d4 <util_sys_get_partnum+0x74>
cmpl   $0x0,0x4(%rsp)
jg     11d4 <util_sys_get_partnum+0x74>
lea    0xe74(%rip),%rdi
mov    %rbx,%rsi
call   1050 <warnx@plt>
movl   $0xffffffff,0x4(%rsp)
ff
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x4(%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"dev/block/%u:%u/partition\00"
@.str.1 = [30 x i8] c"Could not read from path '%s'\00"
@.str.2 = [29 x i8] c"Bad partition number in '%s'\00"
define i32 @util_sys_get_partnum(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
store i32 %0, i32* %2
store i32 -1, i32* %3
%5 = load i32, i32* %2
%6 = call i32 @major(i32 %5)
%7 = load i32, i32* %2
%8 = call i32 @minor(i32 %7)
%9 = call i8* @util_path_sysfs(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %8)
store i8* %9, i8** %4
%10 = load i8*, i8** %4
%11 = call i64 @util_file_read_i(i32* %3, i32 10, i8* %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = load i8*, i8** %4
%15 = call i32 @warnx(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %14)
br label %23
16: ; preds = %1
%17 = load i32, i32* %3
%18 = icmp sle i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i8*, i8** %4
%21 = call i32 @warnx(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %20)
store i32 -1, i32* %3
br label %23
22: ; preds = %16
br label %23
23: ; preds = %22, %19, %13
%24 = load i8*, i8** %4
%25 = call i32 @free(i8* %24)
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
@.str = [26 x i8] c"dev/block/%u:%u/partition\00"
@.str.1 = [30 x i8] c"Could not read from path '%s'\00"
@.str.2 = [29 x i8] c"Bad partition number in '%s'\00"
define i32 @util_sys_get_partnum(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 -1, i32* %2
%4 = tail call i32 @major(i32 %0)
%5 = tail call i32 @minor(i32 %0)
%6 = tail call i8* @util_path_sysfs(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %4, i32 %5)
%7 = call i64 @util_file_read_i(i32* %2, i32 10, i8* %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %1
%10 = call i32 @warnx(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %6)
br label %16
11: ; preds = %1
%12 = load i32, i32* %2
%13 = icmp slt i32 %12, 1
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 @warnx(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %6)
store i32 -1, i32* %2
br label %16
16: ; preds = %11, %14, %9
%17 = call i32 @free(i8* %6)
%18 = load i32, i32* %2
ret i32 %18
}