#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 udev_device {
  int dummy;
};
struct udev {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ format_lun_number(struct udev_device *, char **);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ path_prepend(char **, char *, char const *, char *);
struct udev_device *udev_device_get_parent(struct udev_device *);
struct udev_device *
udev_device_get_parent_with_subsystem_devtype(struct udev_device *, char *,
                                              char *);
char *udev_device_get_sysattr_value(struct udev_device *, char *);
int /*<<< orphan*/ udev_device_get_sysname(struct udev_device *);
struct udev *udev_device_get_udev(struct udev_device *);
struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *,
                                                           char *,
                                                           int /*<<< orphan*/);
int /*<<< orphan*/ udev_device_unref(struct udev_device *);
struct udev_device *handle_scsi_sas(struct udev_device *parent, char **path) {
  struct udev *udev = udev_device_get_udev(parent);
  struct udev_device *targetdev;
  struct udev_device *target_parent;
  struct udev_device *sasdev;
  const char *sas_address;
  char *lun = NULL;
  targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi",
                                                            "scsi_target");
  if (targetdev == NULL)
    return NULL;
  target_parent = udev_device_get_parent(targetdev);
  if (target_parent == NULL)
    return NULL;
  sasdev = udev_device_new_from_subsystem_sysname(
      udev, "sas_device", udev_device_get_sysname(target_parent));
  if (sasdev == NULL)
    return NULL;
  sas_address = udev_device_get_sysattr_value(sasdev, "sas_address");
  if (sas_address == NULL) {
    parent = NULL;
    goto out;
  }
  format_lun_number(parent, &lun);
  path_prepend(path, "sas-%s-%s", sas_address, lun);
  if (lun)
    free(lun);
out:
  udev_device_unref(sasdev);
  return parent;
}


x86 assembly (clang, -O0):
<handle_scsi_sas>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <udev_device_get_udev@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe30(%rip),%rsi
lea    0xe2e(%rip),%rdx
call   1070 <udev_device_get_parent_with_subsystem_devtype@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11f8 <handle_scsi_sas+0x58>
movq   $0x0,-0x8(%rbp)
jmp    12d7 <handle_scsi_sas+0x137>
mov    -0x28(%rbp),%rdi
call   1080 <udev_device_get_parent@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    121d <handle_scsi_sas+0x7d>
movq   $0x0,-0x8(%rbp)
jmp    12d7 <handle_scsi_sas+0x137>
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x30(%rbp),%rdi
call   10b0 <udev_device_get_sysname@plt>
mov    -0x50(%rbp),%rdi
mov    %eax,%edx
lea    0xdd6(%rip),%rsi
call   1060 <udev_device_new_from_subsystem_sysname@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    125c <handle_scsi_sas+0xbc>
movq   $0x0,-0x8(%rbp)
jmp    12d7 <handle_scsi_sas+0x137>
mov    -0x38(%rbp),%rdi
lea    0xdb5(%rip),%rsi
call   10c0 <udev_device_get_sysattr_value@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1288 <handle_scsi_sas+0xe8>
movq   $0x0,-0x10(%rbp)
jmp    12c6 <handle_scsi_sas+0x126>
mov    -0x10(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   1090 <format_lun_number@plt>
mov    -0x18(%rbp),%rdi
mov    -0x40(%rbp),%rdx
mov    -0x48(%rbp),%rcx
lea    0xd80(%rip),%rsi
call   1030 <path_prepend@plt>
cmpq   $0x0,-0x48(%rbp)
je     12c1 <handle_scsi_sas+0x121>
mov    -0x48(%rbp),%rdi
call   1040 <free@plt>
jmp    12c6 <handle_scsi_sas+0x126>
mov    -0x38(%rbp),%rdi
call   10a0 <udev_device_unref@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_scsi_sas>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1050 <udev_device_get_udev@plt>
mov    %rax,%r15
movq   $0x0,(%rsp)
lea    0xe3b(%rip),%rsi
lea    0xe39(%rip),%rdx
mov    %rbx,%rdi
call   1070 <udev_device_get_parent_with_subsystem_devtype@plt>
test   %rax,%rax
je     124f <handle_scsi_sas+0xaf>
mov    %rax,%rdi
call   1080 <udev_device_get_parent@plt>
test   %rax,%rax
je     124f <handle_scsi_sas+0xaf>
mov    %rax,%rdi
call   10b0 <udev_device_get_sysname@plt>
lea    0xe1c(%rip),%rsi
mov    %r15,%rdi
mov    %eax,%edx
call   1060 <udev_device_new_from_subsystem_sysname@plt>
test   %rax,%rax
je     124f <handle_scsi_sas+0xaf>
mov    %rax,%r15
lea    0xe0e(%rip),%rsi
mov    %rax,%rdi
call   10c0 <udev_device_get_sysattr_value@plt>
test   %rax,%rax
je     1253 <handle_scsi_sas+0xb3>
mov    %rax,%r12
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1090 <format_lun_number@plt>
mov    (%rsp),%rcx
lea    0xdf4(%rip),%rsi
mov    %r14,%rdi
mov    %r12,%rdx
call   1030 <path_prepend@plt>
mov    (%rsp),%rdi
test   %rdi,%rdi
je     1255 <handle_scsi_sas+0xb5>
call   1040 <free@plt>
jmp    1255 <handle_scsi_sas+0xb5>
xor    %ebx,%ebx
jmp    125d <handle_scsi_sas+0xbd>
xor    %ebx,%ebx
mov    %r15,%rdi
call   10a0 <udev_device_unref@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.udev_device = type { i32 }
%struct.udev = type { i32 }
@.str = [5 x i8] c"scsi\00"
@.str.1 = [12 x i8] c"scsi_target\00"
@.str.2 = [11 x i8] c"sas_device\00"
@.str.3 = [12 x i8] c"sas_address\00"
@.str.4 = [10 x i8] c"sas-%s-%s\00"
define %struct.udev_device* @handle_scsi_sas(%struct.udev_device* %0, i8** %1) {
%3 = alloca %struct.udev_device*
%4 = alloca %struct.udev_device*
%5 = alloca i8**
%6 = alloca %struct.udev*
%7 = alloca %struct.udev_device*
%8 = alloca %struct.udev_device*
%9 = alloca %struct.udev_device*
%10 = alloca i8*
%11 = alloca i8*
store %struct.udev_device* %0, %struct.udev_device** %4
store i8** %1, i8*** %5
%12 = load %struct.udev_device*, %struct.udev_device** %4
%13 = call %struct.udev* @udev_device_get_udev(%struct.udev_device* %12)
store %struct.udev* %13, %struct.udev** %6
store i8* null, i8** %11
%14 = load %struct.udev_device*, %struct.udev_device** %4
%15 = call %struct.udev_device* @udev_device_get_parent_with_subsystem_devtype(%struct.udev_device* %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store %struct.udev_device* %15, %struct.udev_device** %7
%16 = load %struct.udev_device*, %struct.udev_device** %7
%17 = icmp eq %struct.udev_device* %16, null
br i1 %17, label %18, label %19
18: ; preds = %2
store %struct.udev_device* null, %struct.udev_device** %3
br label %56
19: ; preds = %2
%20 = load %struct.udev_device*, %struct.udev_device** %7
%21 = call %struct.udev_device* @udev_device_get_parent(%struct.udev_device* %20)
store %struct.udev_device* %21, %struct.udev_device** %8
%22 = load %struct.udev_device*, %struct.udev_device** %8
%23 = icmp eq %struct.udev_device* %22, null
br i1 %23, label %24, label %25
24: ; preds = %19
store %struct.udev_device* null, %struct.udev_device** %3
br label %56
25: ; preds = %19
%26 = load %struct.udev*, %struct.udev** %6
%27 = load %struct.udev_device*, %struct.udev_device** %8
%28 = call i32 @udev_device_get_sysname(%struct.udev_device* %27)
%29 = call %struct.udev_device* @udev_device_new_from_subsystem_sysname(%struct.udev* %26, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %28)
store %struct.udev_device* %29, %struct.udev_device** %9
%30 = load %struct.udev_device*, %struct.udev_device** %9
%31 = icmp eq %struct.udev_device* %30, null
br i1 %31, label %32, label %33
32: ; preds = %25
store %struct.udev_device* null, %struct.udev_device** %3
br label %56
33: ; preds = %25
%34 = load %struct.udev_device*, %struct.udev_device** %9
%35 = call i8* @udev_device_get_sysattr_value(%struct.udev_device* %34, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
store i8* %35, i8** %10
%36 = load i8*, i8** %10
%37 = icmp eq i8* %36, null
br i1 %37, label %38, label %39
38: ; preds = %33
store %struct.udev_device* null, %struct.udev_device** %4
br label %52
39: ; preds = %33
%40 = load %struct.udev_device*, %struct.udev_device** %4
%41 = call i32 @format_lun_number(%struct.udev_device* %40, i8** %11)
%42 = load i8**, i8*** %5
%43 = load i8*, i8** %10
%44 = load i8*, i8** %11
%45 = call i32 @path_prepend(i8** %42, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %43, i8* %44)
%46 = load i8*, i8** %11
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %51
48: ; preds = %39
%49 = load i8*, i8** %11
%50 = call i32 @free(i8* %49)
br label %51
51: ; preds = %48, %39
br label %52
52: ; preds = %51, %38
%53 = load %struct.udev_device*, %struct.udev_device** %9
%54 = call i32 @udev_device_unref(%struct.udev_device* %53)
%55 = load %struct.udev_device*, %struct.udev_device** %4
store %struct.udev_device* %55, %struct.udev_device** %3
br label %56
56: ; preds = %52, %32, %24, %18
%57 = load %struct.udev_device*, %struct.udev_device** %3
ret %struct.udev_device* %57
}

LLVM IR (-O2):
%struct.udev_device = type { i32 }
%struct.udev = type { i32 }
@.str = [5 x i8] c"scsi\00"
@.str.1 = [12 x i8] c"scsi_target\00"
@.str.2 = [11 x i8] c"sas_device\00"
@.str.3 = [12 x i8] c"sas_address\00"
@.str.4 = [10 x i8] c"sas-%s-%s\00"
define %struct.udev_device* @handle_scsi_sas(%struct.udev_device* %0, i8** %1) {
%3 = alloca i8*
%4 = tail call %struct.udev* @udev_device_get_udev(%struct.udev_device* %0)
%5 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%6 = tail call %struct.udev_device* @udev_device_get_parent_with_subsystem_devtype(%struct.udev_device* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq %struct.udev_device* %6, null
br i1 %7, label %29, label %8
8: ; preds = %2
%9 = tail call %struct.udev_device* @udev_device_get_parent(%struct.udev_device* %6)
%10 = icmp eq %struct.udev_device* %9, null
br i1 %10, label %29, label %11
11: ; preds = %8
%12 = tail call i32 @udev_device_get_sysname(%struct.udev_device* %9)
%13 = tail call %struct.udev_device* @udev_device_new_from_subsystem_sysname(%struct.udev* %4, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = icmp eq %struct.udev_device* %13, null
br i1 %14, label %29, label %15
15: ; preds = %11
%16 = tail call i8* @udev_device_get_sysattr_value(%struct.udev_device* %13, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%17 = icmp eq i8* %16, null
br i1 %17, label %26, label %18
18: ; preds = %15
%19 = call i32 @format_lun_number(%struct.udev_device* %0, i8** %3)
%20 = load i8*, i8** %3
%21 = call i32 @path_prepend(i8** %1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %16, i8* %20)
%22 = load i8*, i8** %3
%23 = icmp eq i8* %22, null
br i1 %23, label %26, label %24
24: ; preds = %18
%25 = call i32 @free(i8* %22)
br label %26
26: ; preds = %15, %18, %24
%27 = phi %struct.udev_device* [ %0, %24 ], [ %0, %18 ], [ null, %15 ]
%28 = call i32 @udev_device_unref(%struct.udev_device* %13)
br label %29
29: ; preds = %11, %8, %2, %26
%30 = phi %struct.udev_device* [ %27, %26 ], [ null, %2 ], [ null, %8 ], [ null, %11 ]
ret %struct.udev_device* %30
}