#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 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_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_fibre_channel(char const *parent, char **path,
                              char **new_parent) {
  char *port = NULL;
  size_t port_len = 0;
  char *lun = NULL;
  char *targetdev = NULL;
  size_t targetdev_len = 0;
  char *targetdev_sysname = NULL;
  size_t targetdev_sysname_len = 0;
  char *fcdev = NULL;
  size_t fcdev_len = 0;
  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_sysname(targetdev, &targetdev_sysname,
                              &targetdev_sysname_len);
  if (rc != 0) {
    free(targetdev);
    return rc;
  }
  rc = vdev_sysfs_device_path_from_subsystem_sysname(
      "/sys", "fc_transport", targetdev_sysname, &fcdev, &fcdev_len);
  if (rc != 0) {
    free(targetdev);
    free(targetdev_sysname);
    return rc;
  }
  free(targetdev_sysname);
  rc = vdev_sysfs_read_attr(fcdev, "port_name", &port, &port_len);
  if (rc != 0) {
    free(targetdev);
    return rc;
  }
  free(fcdev);
  rc = format_lun_number(parent, &lun);
  path_prepend(path, "fc-%s-%s", port, lun);
  if (lun != NULL) {
    free(lun);
  }
  *new_parent = strdup(parent);
  if (*new_parent == NULL) {
    return -ENOMEM;
  }
  return 0;
}


x86 assembly (clang, -O0):
<handle_scsi_fibre_channel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%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)
movl   $0x0,-0x6c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe12(%rip),%rsi
lea    0xe10(%rip),%rdx
lea    -0x40(%rbp),%rcx
lea    -0x48(%rbp),%r8
call   1050 <vdev_sysfs_get_parent_with_subsystem_devtype@plt>
mov    %eax,-0x6c(%rbp)
cmpl   $0x0,-0x6c(%rbp)
je     121a <handle_scsi_fibre_channel+0x9a>
mov    -0x6c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1357 <handle_scsi_fibre_channel+0x1d7>
mov    -0x40(%rbp),%rdi
lea    -0x50(%rbp),%rsi
lea    -0x58(%rbp),%rdx
call   1070 <vdev_sysfs_get_sysname@plt>
mov    %eax,-0x6c(%rbp)
cmpl   $0x0,-0x6c(%rbp)
je     124c <handle_scsi_fibre_channel+0xcc>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
mov    -0x6c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1357 <handle_scsi_fibre_channel+0x1d7>
mov    -0x50(%rbp),%rdx
lea    0xdba(%rip),%rdi
lea    0xdb8(%rip),%rsi
lea    -0x60(%rbp),%rcx
lea    -0x68(%rbp),%r8
call   10a0 <vdev_sysfs_device_path_from_subsystem_sysname@plt>
mov    %eax,-0x6c(%rbp)
cmpl   $0x0,-0x6c(%rbp)
je     1295 <handle_scsi_fibre_channel+0x115>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
mov    -0x50(%rbp),%rdi
call   1040 <free@plt>
mov    -0x6c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1357 <handle_scsi_fibre_channel+0x1d7>
mov    -0x50(%rbp),%rdi
call   1040 <free@plt>
mov    -0x60(%rbp),%rdi
lea    0xd7a(%rip),%rsi
lea    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1060 <vdev_sysfs_read_attr@plt>
mov    %eax,-0x6c(%rbp)
cmpl   $0x0,-0x6c(%rbp)
je     12d7 <handle_scsi_fibre_channel+0x157>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
mov    -0x6c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1357 <handle_scsi_fibre_channel+0x1d7>
mov    -0x60(%rbp),%rdi
call   1040 <free@plt>
mov    -0x10(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1080 <format_lun_number@plt>
mov    %eax,-0x6c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x38(%rbp),%rcx
lea    0xd2a(%rip),%rsi
call   1030 <path_prepend@plt>
cmpq   $0x0,-0x38(%rbp)
je     131c <handle_scsi_fibre_channel+0x19c>
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
mov    -0x10(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1350 <handle_scsi_fibre_channel+0x1d0>
xor    %eax,%eax
mov    0x2c9a(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1357 <handle_scsi_fibre_channel+0x1d7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_scsi_fibre_channel>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
movq   $0x0,0x20(%rsp)
movq   $0x0,0x40(%rsp)
movq   $0x0,0x18(%rsp)
movq   $0x0,(%rsp)
movq   $0x0,0x38(%rsp)
movq   $0x0,0x8(%rsp)
movq   $0x0,0x30(%rsp)
movq   $0x0,0x10(%rsp)
movq   $0x0,0x28(%rsp)
lea    0xe16(%rip),%rsi
lea    0xe14(%rip),%rdx
mov    %rsp,%rcx
lea    0x38(%rsp),%r8
call   1050 <vdev_sysfs_get_parent_with_subsystem_devtype@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    125e <handle_scsi_fibre_channel+0xde>
mov    (%rsp),%rdi
lea    0x8(%rsp),%rsi
lea    0x30(%rsp),%rdx
call   1070 <vdev_sysfs_get_sysname@plt>
test   %eax,%eax
je     1223 <handle_scsi_fibre_channel+0xa3>
mov    %eax,%ebp
mov    (%rsp),%rdi
jmp    1259 <handle_scsi_fibre_channel+0xd9>
mov    0x8(%rsp),%rdx
lea    0xde2(%rip),%rdi
lea    0xde0(%rip),%rsi
lea    0x10(%rsp),%rcx
lea    0x28(%rsp),%r8
call   10a0 <vdev_sysfs_device_path_from_subsystem_sysname@plt>
test   %eax,%eax
je     126b <handle_scsi_fibre_channel+0xeb>
mov    %eax,%ebp
mov    (%rsp),%rdi
call   1040 <free@plt>
mov    0x8(%rsp),%rdi
call   1040 <free@plt>
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsp),%rdi
call   1040 <free@plt>
mov    0x10(%rsp),%rdi
lea    0xda2(%rip),%rsi
lea    0x20(%rsp),%rdx
lea    0x40(%rsp),%rcx
call   1060 <vdev_sysfs_read_attr@plt>
test   %eax,%eax
jne    121b <handle_scsi_fibre_channel+0x9b>
mov    0x10(%rsp),%rdi
call   1040 <free@plt>
lea    0x18(%rsp),%rsi
mov    %rbx,%rdi
call   1080 <format_lun_number@plt>
mov    0x20(%rsp),%rdx
mov    0x18(%rsp),%rcx
lea    0xd71(%rip),%rsi
mov    %r15,%rdi
call   1030 <path_prepend@plt>
mov    0x18(%rsp),%rdi
test   %rdi,%rdi
je     12d3 <handle_scsi_fibre_channel+0x153>
call   1040 <free@plt>
mov    %rbx,%rdi
call   1090 <strdup@plt>
mov    %rax,(%r14)
xor    %ebp,%ebp
test   %rax,%rax
jne    125e <handle_scsi_fibre_channel+0xde>
mov    0x2cf0(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
jmp    125e <handle_scsi_fibre_channel+0xde>

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

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