#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 */

/* Variables and functions */
int path_prepend(char **, char *, int, ...);
int vdev_sysfs_get_sysnum(char const *, int *);
int format_lun_number(char const *devpath, char **path) {
  int lun = 0;
  int rc = vdev_sysfs_get_sysnum(devpath, &lun);
  if (rc != 0) {
    return rc;
  }
  if (lun < 256) {
    return path_prepend(path, "lun-%lu", lun);
  }
  return path_prepend(path, "lun-0x%04lx%04lx00000000", lun & 0xffff,
                      (lun >> 16) & 0xffff);
}


x86 assembly (clang, -O0):
<format_lun_number>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   1040 <vdev_sysfs_get_sysnum@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     115c <format_lun_number+0x3c>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b0 <format_lun_number+0x90>
cmpl   $0x100,-0x1c(%rbp)
jge    1186 <format_lun_number+0x66>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0xe89(%rip),%rsi
mov    $0x0,%al
call   1030 <path_prepend@plt>
mov    %eax,-0x4(%rbp)
jmp    11b0 <format_lun_number+0x90>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
and    $0xffff,%edx
mov    -0x1c(%rbp),%ecx
sar    $0x10,%ecx
and    $0xffff,%ecx
lea    0xe62(%rip),%rsi
mov    $0x0,%al
call   1030 <path_prepend@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_lun_number>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
movl   $0x0,0xc(%rsp)
lea    0xc(%rsp),%rsi
call   1040 <vdev_sysfs_get_sysnum@plt>
test   %eax,%eax
jne    1176 <format_lun_number+0x56>
mov    0xc(%rsp),%ecx
cmp    $0xff,%ecx
jg     115f <format_lun_number+0x3f>
lea    0xeaf(%rip),%rsi
mov    %rbx,%rdi
mov    %ecx,%edx
xor    %eax,%eax
call   1030 <path_prepend@plt>
jmp    1176 <format_lun_number+0x56>
movzwl %cx,%edx
shr    $0x10,%ecx
lea    0xe9c(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1030 <path_prepend@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [8 x i8] c"lun-%lu\00"
@.str.1 = [25 x i8] c"lun-0x%04lx%04lx00000000\00"
define i32 @format_lun_number(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%8 = load i8*, i8** %4
%9 = call i32 @vdev_sysfs_get_sysnum(i8* %8, i32* %6)
store i32 %9, i32* %7
%10 = load i32, i32* %7
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = load i32, i32* %7
store i32 %13, i32* %3
br label %29
14: ; preds = %2
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 256
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i8**, i8*** %5
%19 = load i32, i32* %6
%20 = call i32 (i8**, i8*, i32, ...) @path_prepend(i8** %18, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %19)
store i32 %20, i32* %3
br label %29
21: ; preds = %14
%22 = load i8**, i8*** %5
%23 = load i32, i32* %6
%24 = and i32 %23, 65535
%25 = load i32, i32* %6
%26 = ashr i32 %25, 16
%27 = and i32 %26, 65535
%28 = call i32 (i8**, i8*, i32, ...) @path_prepend(i8** %22, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %24, i32 %27)
store i32 %28, i32* %3
br label %29
29: ; preds = %21, %17, %12
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
@.str = [8 x i8] c"lun-%lu\00"
@.str.1 = [25 x i8] c"lun-0x%04lx%04lx00000000\00"
define i32 @format_lun_number(i8* %0, i8** %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%5 = call i32 @vdev_sysfs_get_sysnum(i8* %0, i32* %3)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %16
7: ; preds = %2
%8 = load i32, i32* %3
%9 = icmp slt i32 %8, 256
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = call i32 (i8**, i8*, i32, ...) @path_prepend(i8** %1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %16
12: ; preds = %7
%13 = and i32 %8, 65535
%14 = lshr i32 %8, 16
%15 = call i32 (i8**, i8*, i32, ...) @path_prepend(i8** %1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 %14)
br label %16
16: ; preds = %2, %12, %10
%17 = phi i32 [ %11, %10 ], [ %15, %12 ], [ %5, %2 ]
ret i32 %17
}