#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_rdev;
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ S_ISBLK(int /*<<< orphan*/);
int find_busid_in_sysfs(char *, char *);
int major(int /*<<< orphan*/);
int minor(int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int, int);
scalar_t__ stat(char *, struct stat *);
int u2s_getbusid(char *devicenode, char *busid) {
  int maj, min, rc;
  struct stat stat_buf;
  char dev_string[20];
  if (stat(devicenode, &stat_buf))
    return -1;
  if (!S_ISBLK(stat_buf.st_mode))
    return -1;
  maj = major(stat_buf.st_rdev);
  min = minor(stat_buf.st_rdev);
  snprintf(dev_string, 20, "%u:%u", maj, min);
  rc = find_busid_in_sysfs(dev_string, busid);
  return rc;
}


x86 assembly (clang, -O0):
<u2s_getbusid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
je     1193 <u2s_getbusid+0x33>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f8 <u2s_getbusid+0x98>
mov    -0x2c(%rbp),%edi
call   1070 <S_ISBLK@plt>
cmp    $0x0,%eax
jne    11b0 <u2s_getbusid+0x50>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f8 <u2s_getbusid+0x98>
mov    -0x30(%rbp),%edi
call   1050 <major@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x30(%rbp),%edi
call   1030 <minor@plt>
mov    %eax,-0x20(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x1c(%rbp),%ecx
mov    -0x20(%rbp),%r8d
mov    $0x14,%esi
lea    0xe23(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x50(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <find_busid_in_sysfs@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<u2s_getbusid>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%rbx
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    11cb <u2s_getbusid+0x6b>
mov    0xc(%rsp),%edi
call   1070 <S_ISBLK@plt>
test   %eax,%eax
je     11cb <u2s_getbusid+0x6b>
mov    0x8(%rsp),%edi
call   1050 <major@plt>
mov    %eax,%ebp
mov    0x8(%rsp),%edi
call   1030 <minor@plt>
lea    0xe59(%rip),%rdx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x14,%esi
mov    %ebp,%ecx
mov    %eax,%r8d
call   1040 <snprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <find_busid_in_sysfs@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@.str = [6 x i8] c"%u:%u\00"
define i32 @u2s_getbusid(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.stat
%10 = alloca [20 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = load i8*, i8** %4
%12 = call i64 @stat(i8* %11, %struct.stat* %9)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 -1, i32* %3
br label %36
15: ; preds = %2
%16 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 1
%17 = load i32, i32* %16
%18 = call i32 @S_ISBLK(i32 %17)
%19 = icmp ne i32 %18, 0
br i1 %19, label %21, label %20
20: ; preds = %15
store i32 -1, i32* %3
br label %36
21: ; preds = %15
%22 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @major(i32 %23)
store i32 %24, i32* %6
%25 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @minor(i32 %26)
store i32 %27, i32* %7
%28 = getelementptr inbounds [20 x i8], [20 x i8]* %10, i64 0, i64 0
%29 = load i32, i32* %6
%30 = load i32, i32* %7
%31 = call i32 @snprintf(i8* %28, i32 20, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %29, i32 %30)
%32 = getelementptr inbounds [20 x i8], [20 x i8]* %10, i64 0, i64 0
%33 = load i8*, i8** %5
%34 = call i32 @find_busid_in_sysfs(i8* %32, i8* %33)
store i32 %34, i32* %8
%35 = load i32, i32* %8
store i32 %35, i32* %3
br label %36
36: ; preds = %21, %20, %14
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@.str = [6 x i8] c"%u:%u\00"
define i32 @u2s_getbusid(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = alloca [20 x i8]
%5 = bitcast %struct.stat* %3 to i8*
%6 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%7 = call i64 @stat(i8* %0, %struct.stat* %3)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %22
9: ; preds = %2
%10 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%11 = load i32, i32* %10
%12 = call i32 @S_ISBLK(i32 %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %22, label %14
14: ; preds = %9
%15 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @major(i32 %16)
%18 = load i32, i32* %15
%19 = call i32 @minor(i32 %18)
%20 = call i32 @snprintf(i8* %6, i32 20, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %17, i32 %19)
%21 = call i32 @find_busid_in_sysfs(i8* %6, i8* %1)
br label %22
22: ; preds = %9, %2, %14
%23 = phi i32 [ %21, %14 ], [ -1, %2 ], [ -1, %9 ]
ret i32 %23
}