#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 ENOMEM;
int /*<<< orphan*/ format_lun_number(char const *, char **);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ path_prepend(char **, char *, char *, char *);
char *strdup(char const *);
int vdev_sysfs_device_path_from_subsystem_sysname(char *, char *, char *,
                                                  char **, size_t *);
int vdev_sysfs_get_parent_device(char const *, char **, size_t *);
int vdev_sysfs_get_parent_with_subsystem_devtype(char const *, char *, char *,
                                                 char **, size_t *);
int vdev_sysfs_get_sysname(char *, char **, size_t *);
int vdev_sysfs_read_attr(char *, char *, char **, size_t *);
int handle_scsi_sas_wide_port(char const *parent, char **path,
                              char **new_parent) {
  char *targetdev = NULL;
  size_t targetdev_len = 0;
  char *targetparent = NULL;
  size_t targetparent_len = 0;
  char *targetparent_sysname = NULL;
  size_t targetparent_sysname_len = 0;
  char *sasdev = NULL;
  size_t sasdev_len = 0;
  char *sas_address = NULL;
  size_t sas_address_len = 0;
  char *lun = NULL;
  int rc = 0;
  rc = vdev_sysfs_get_parent_with_subsystem_devtype(
      parent, "scsi", "scsi_target", &targetdev, &targetdev_len);
  if (rc != 0) {
    return rc;
  }
  rc = vdev_sysfs_get_parent_device(parent, &targetparent, &targetparent_len);
  if (rc != 0) {
    free(targetdev);
    return rc;
  }
  rc = vdev_sysfs_get_sysname(targetparent, &targetparent_sysname,
                              &targetparent_sysname_len);
  if (rc != 0) {
    free(targetdev);
    free(targetparent);
    return rc;
  }
  rc = vdev_sysfs_device_path_from_subsystem_sysname(
      "/sys", "sas_device", targetparent_sysname, &sasdev, &sasdev_len);
  free(targetparent);
  free(targetparent_sysname);
  if (rc != 0) {
    free(targetdev);
    return rc;
  }
  rc = vdev_sysfs_read_attr(sasdev, "sas_address", &sas_address,
                            &sas_address_len);
  if (rc != 0) {
    free(targetdev);
    return rc;
  }
  format_lun_number(parent, &lun);
  path_prepend(path, "sas-%s-%s", sas_address, lun);
  if (lun != NULL) {
    free(lun);
  }
  free(targetdev);
  *new_parent = strdup(parent);
  if (*new_parent == NULL) {
    return -ENOMEM;
  }
  return 0;
}


x86 assembly (clang, -O0):
<handle_scsi_sas_wide_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
movq   $0x0,-0x58(%rbp)
movq   $0x0,-0x60(%rbp)
movq   $0x0,-0x68(%rbp)
movq   $0x0,-0x70(%rbp)
movq   $0x0,-0x78(%rbp)
movl   $0x0,-0x7c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xdef(%rip),%rsi
lea    0xded(%rip),%rdx
lea    -0x28(%rbp),%rcx
lea    -0x30(%rbp),%r8
call   1050 <vdev_sysfs_get_parent_with_subsystem_devtype@plt>
mov    %eax,-0x7c(%rbp)
cmpl   $0x0,-0x7c(%rbp)
je     123d <handle_scsi_sas_wide_port+0xad>
mov    -0x7c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13b2 <handle_scsi_sas_wide_port+0x222>
mov    -0x10(%rbp),%rdi
lea    -0x38(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   1090 <vdev_sysfs_get_parent_device@plt>
mov    %eax,-0x7c(%rbp)
cmpl   $0x0,-0x7c(%rbp)
je     126f <handle_scsi_sas_wide_port+0xdf>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x7c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13b2 <handle_scsi_sas_wide_port+0x222>
mov    -0x38(%rbp),%rdi
lea    -0x48(%rbp),%rsi
lea    -0x50(%rbp),%rdx
call   1070 <vdev_sysfs_get_sysname@plt>
mov    %eax,-0x7c(%rbp)
cmpl   $0x0,-0x7c(%rbp)
je     12aa <handle_scsi_sas_wide_port+0x11a>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
mov    -0x7c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13b2 <handle_scsi_sas_wide_port+0x222>
mov    -0x48(%rbp),%rdx
lea    0xd5c(%rip),%rdi
lea    0xd5a(%rip),%rsi
lea    -0x58(%rbp),%rcx
lea    -0x60(%rbp),%r8
call   10b0 <vdev_sysfs_device_path_from_subsystem_sysname@plt>
mov    %eax,-0x7c(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
mov    -0x48(%rbp),%rdi
call   1040 <free@plt>
cmpl   $0x0,-0x7c(%rbp)
je     12fc <handle_scsi_sas_wide_port+0x16c>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x7c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13b2 <handle_scsi_sas_wide_port+0x222>
mov    -0x58(%rbp),%rdi
lea    0xd1a(%rip),%rsi
lea    -0x68(%rbp),%rdx
lea    -0x70(%rbp),%rcx
call   1060 <vdev_sysfs_read_attr@plt>
mov    %eax,-0x7c(%rbp)
cmpl   $0x0,-0x7c(%rbp)
je     1335 <handle_scsi_sas_wide_port+0x1a5>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x7c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13b2 <handle_scsi_sas_wide_port+0x222>
mov    -0x10(%rbp),%rdi
lea    -0x78(%rbp),%rsi
call   1080 <format_lun_number@plt>
mov    -0x18(%rbp),%rdi
mov    -0x68(%rbp),%rdx
mov    -0x78(%rbp),%rcx
lea    0xcd8(%rip),%rsi
call   1030 <path_prepend@plt>
cmpq   $0x0,-0x78(%rbp)
je     136e <handle_scsi_sas_wide_port+0x1de>
mov    -0x78(%rbp),%rdi
call   1040 <free@plt>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x10(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    13ab <handle_scsi_sas_wide_port+0x21b>
xor    %eax,%eax
mov    0x2c3f(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    13b2 <handle_scsi_sas_wide_port+0x222>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_scsi_sas_wide_port>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x58,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
movq   $0x0,(%rsp)
movq   $0x0,0x50(%rsp)
movq   $0x0,0x8(%rsp)
movq   $0x0,0x48(%rsp)
movq   $0x0,0x18(%rsp)
movq   $0x0,0x40(%rsp)
movq   $0x0,0x28(%rsp)
movq   $0x0,0x38(%rsp)
movq   $0x0,0x20(%rsp)
movq   $0x0,0x30(%rsp)
movq   $0x0,0x10(%rsp)
lea    0xdf4(%rip),%rsi
lea    0xdf2(%rip),%rdx
mov    %rsp,%rcx
lea    0x50(%rsp),%r8
call   1050 <vdev_sysfs_get_parent_with_subsystem_devtype@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1271 <handle_scsi_sas_wide_port+0xe1>
lea    0x8(%rsp),%rsi
lea    0x48(%rsp),%rdx
mov    %rbx,%rdi
call   1090 <vdev_sysfs_get_parent_device@plt>
test   %eax,%eax
je     1244 <handle_scsi_sas_wide_port+0xb4>
mov    %eax,%ebp
mov    (%rsp),%rdi
jmp    126c <handle_scsi_sas_wide_port+0xdc>
mov    0x8(%rsp),%rdi
lea    0x18(%rsp),%rsi
lea    0x40(%rsp),%rdx
call   1070 <vdev_sysfs_get_sysname@plt>
test   %eax,%eax
je     127e <handle_scsi_sas_wide_port+0xee>
mov    %eax,%ebp
mov    (%rsp),%rdi
call   1040 <free@plt>
mov    0x8(%rsp),%rdi
call   1040 <free@plt>
mov    %ebp,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x18(%rsp),%rdx
lea    0xd87(%rip),%rdi
lea    0xd85(%rip),%rsi
lea    0x28(%rsp),%rcx
lea    0x38(%rsp),%r8
call   10b0 <vdev_sysfs_device_path_from_subsystem_sysname@plt>
mov    %eax,%ebp
mov    0x8(%rsp),%rdi
call   1040 <free@plt>
mov    0x18(%rsp),%rdi
call   1040 <free@plt>
test   %ebp,%ebp
jne    123e <handle_scsi_sas_wide_port+0xae>
mov    0x28(%rsp),%rdi
lea    0xd5b(%rip),%rsi
lea    0x20(%rsp),%rdx
lea    0x30(%rsp),%rcx
call   1060 <vdev_sysfs_read_attr@plt>
test   %eax,%eax
jne    123c <handle_scsi_sas_wide_port+0xac>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   1080 <format_lun_number@plt>
mov    0x20(%rsp),%rdx
mov    0x10(%rsp),%rcx
lea    0xd32(%rip),%rsi
mov    %r15,%rdi
call   1030 <path_prepend@plt>
mov    0x10(%rsp),%rdi
test   %rdi,%rdi
je     1312 <handle_scsi_sas_wide_port+0x182>
call   1040 <free@plt>
mov    (%rsp),%rdi
call   1040 <free@plt>
mov    %rbx,%rdi
call   10a0 <strdup@plt>
mov    %rax,(%r14)
xor    %ebp,%ebp
test   %rax,%rax
jne    1271 <handle_scsi_sas_wide_port+0xe1>
mov    0x2ca8(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
jmp    1271 <handle_scsi_sas_wide_port+0xe1>

LLVM IR (-O0):
@.str = [5 x i8] c"scsi\00"
@.str.1 = [12 x i8] c"scsi_target\00"
@.str.2 = [5 x i8] c"/sys\00"
@.str.3 = [11 x i8] c"sas_device\00"
@.str.4 = [12 x i8] c"sas_address\00"
@.str.5 = [10 x i8] c"sas-%s-%s\00"
@ENOMEM = global i32 0
define i32 @handle_scsi_sas_wide_port(i8* %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i8*
%15 = alloca i64
%16 = alloca i8*
%17 = alloca i64
%18 = alloca i8*
%19 = alloca i32
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
store i8* null, i8** %8
store i64 0, i64* %9
store i8* null, i8** %10
store i64 0, i64* %11
store i8* null, i8** %12
store i64 0, i64* %13
store i8* null, i8** %14
store i64 0, i64* %15
store i8* null, i8** %16
store i64 0, i64* %17
store i8* null, i8** %18
store i32 0, i32* %19
%20 = load i8*, i8** %5
%21 = call i32 @vdev_sysfs_get_parent_with_subsystem_devtype(i8* %20, 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), i8** %8, i64* %9)
store i32 %21, i32* %19
%22 = load i32, i32* %19
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %3
%25 = load i32, i32* %19
store i32 %25, i32* %4
br label %93
26: ; preds = %3
%27 = load i8*, i8** %5
%28 = call i32 @vdev_sysfs_get_parent_device(i8* %27, i8** %10, i64* %11)
store i32 %28, i32* %19
%29 = load i32, i32* %19
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %26
%32 = load i8*, i8** %8
%33 = call i32 @free(i8* %32)
%34 = load i32, i32* %19
store i32 %34, i32* %4
br label %93
35: ; preds = %26
%36 = load i8*, i8** %10
%37 = call i32 @vdev_sysfs_get_sysname(i8* %36, i8** %12, i64* %13)
store i32 %37, i32* %19
%38 = load i32, i32* %19
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %46
40: ; preds = %35
%41 = load i8*, i8** %8
%42 = call i32 @free(i8* %41)
%43 = load i8*, i8** %10
%44 = call i32 @free(i8* %43)
%45 = load i32, i32* %19
store i32 %45, i32* %4
br label %93
46: ; preds = %35
%47 = load i8*, i8** %12
%48 = call i32 @vdev_sysfs_device_path_from_subsystem_sysname(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %47, i8** %14, i64* %15)
store i32 %48, i32* %19
%49 = load i8*, i8** %10
%50 = call i32 @free(i8* %49)
%51 = load i8*, i8** %12
%52 = call i32 @free(i8* %51)
%53 = load i32, i32* %19
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %46
%56 = load i8*, i8** %8
%57 = call i32 @free(i8* %56)
%58 = load i32, i32* %19
store i32 %58, i32* %4
br label %93
59: ; preds = %46
%60 = load i8*, i8** %14
%61 = call i32 @vdev_sysfs_read_attr(i8* %60, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8** %16, i64* %17)
store i32 %61, i32* %19
%62 = load i32, i32* %19
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %68
64: ; preds = %59
%65 = load i8*, i8** %8
%66 = call i32 @free(i8* %65)
%67 = load i32, i32* %19
store i32 %67, i32* %4
br label %93
68: ; preds = %59
%69 = load i8*, i8** %5
%70 = call i32 @format_lun_number(i8* %69, i8** %18)
%71 = load i8**, i8*** %6
%72 = load i8*, i8** %16
%73 = load i8*, i8** %18
%74 = call i32 @path_prepend(i8** %71, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %72, i8* %73)
%75 = load i8*, i8** %18
%76 = icmp ne i8* %75, null
br i1 %76, label %77, label %80
77: ; preds = %68
%78 = load i8*, i8** %18
%79 = call i32 @free(i8* %78)
br label %80
80: ; preds = %77, %68
%81 = load i8*, i8** %8
%82 = call i32 @free(i8* %81)
%83 = load i8*, i8** %5
%84 = call i8* @strdup(i8* %83)
%85 = load i8**, i8*** %7
store i8* %84, i8** %85
%86 = load i8**, i8*** %7
%87 = load i8*, i8** %86
%88 = icmp eq i8* %87, null
br i1 %88, label %89, label %92
89: ; preds = %80
%90 = load i32, i32* @ENOMEM
%91 = sub nsw i32 0, %90
store i32 %91, i32* %4
br label %93
92: ; preds = %80
store i32 0, i32* %4
br label %93
93: ; preds = %92, %89, %64, %55, %40, %31, %24
%94 = load i32, i32* %4
ret i32 %94
}

LLVM IR (-O2):
@.str = [5 x i8] c"scsi\00"
@.str.1 = [12 x i8] c"scsi_target\00"
@.str.2 = [5 x i8] c"/sys\00"
@.str.3 = [11 x i8] c"sas_device\00"
@.str.4 = [12 x i8] c"sas_address\00"
@.str.5 = [10 x i8] c"sas-%s-%s\00"
@ENOMEM = global i32 0
define i32 @handle_scsi_sas_wide_port(i8* %0, i8** %1, i8** nocapture writeonly %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i8*
%15 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%16 = bitcast i64* %5 to i8*
store i64 0, i64* %5
%17 = bitcast i8** %6 to i8*
store i8* null, i8** %6
%18 = bitcast i64* %7 to i8*
store i64 0, i64* %7
%19 = bitcast i8** %8 to i8*
store i8* null, i8** %8
%20 = bitcast i64* %9 to i8*
store i64 0, i64* %9
%21 = bitcast i8** %10 to i8*
store i8* null, i8** %10
%22 = bitcast i64* %11 to i8*
store i64 0, i64* %11
%23 = bitcast i8** %12 to i8*
store i8* null, i8** %12
%24 = bitcast i64* %13 to i8*
store i64 0, i64* %13
%25 = bitcast i8** %14 to i8*
store i8* null, i8** %14
%26 = call i32 @vdev_sysfs_get_parent_with_subsystem_devtype(i8* %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), i8** %4, i64* %5)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %78
28: ; preds = %3
%29 = call i32 @vdev_sysfs_get_parent_device(i8* %0, i8** %6, i64* %7)
%30 = icmp eq i32 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %28
%32 = load i8*, i8** %4
%33 = call i32 @free(i8* %32)
br label %78
34: ; preds = %28
%35 = load i8*, i8** %6
%36 = call i32 @vdev_sysfs_get_sysname(i8* %35, i8** %8, i64* %9)
%37 = icmp eq i32 %36, 0
br i1 %37, label %43, label %38
38: ; preds = %34
%39 = load i8*, i8** %4
%40 = call i32 @free(i8* %39)
%41 = load i8*, i8** %6
%42 = call i32 @free(i8* %41)
br label %78
43: ; preds = %34
%44 = load i8*, i8** %8
%45 = call i32 @vdev_sysfs_device_path_from_subsystem_sysname(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %44, i8** %10, i64* %11)
%46 = load i8*, i8** %6
%47 = call i32 @free(i8* %46)
%48 = load i8*, i8** %8
%49 = call i32 @free(i8* %48)
%50 = icmp eq i32 %45, 0
br i1 %50, label %54, label %51
51: ; preds = %43
%52 = load i8*, i8** %4
%53 = call i32 @free(i8* %52)
br label %78
54: ; preds = %43
%55 = load i8*, i8** %10
%56 = call i32 @vdev_sysfs_read_attr(i8* %55, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8** %12, i64* %13)
%57 = icmp eq i32 %56, 0
br i1 %57, label %61, label %58
58: ; preds = %54
%59 = load i8*, i8** %4
%60 = call i32 @free(i8* %59)
br label %78
61: ; preds = %54
%62 = call i32 @format_lun_number(i8* %0, i8** %14)
%63 = load i8*, i8** %12
%64 = load i8*, i8** %14
%65 = call i32 @path_prepend(i8** %1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %63, i8* %64)
%66 = load i8*, i8** %14
%67 = icmp eq i8* %66, null
br i1 %67, label %70, label %68
68: ; preds = %61
%69 = call i32 @free(i8* %66)
br label %70
70: ; preds = %68, %61
%71 = load i8*, i8** %4
%72 = call i32 @free(i8* %71)
%73 = call i8* @strdup(i8* %0)
store i8* %73, i8** %2
%74 = icmp eq i8* %73, null
br i1 %74, label %75, label %78
75: ; preds = %70
%76 = load i32, i32* @ENOMEM
%77 = sub nsw i32 0, %76
br label %78
78: ; preds = %70, %3, %75, %58, %51, %38, %31
%79 = phi i32 [ %29, %31 ], [ %36, %38 ], [ %45, %51 ], [ %56, %58 ], [ %77, %75 ], [ %26, %3 ], [ 0, %70 ]
ret i32 %79
}