#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 */
scalar_t__ asprintf(char **, char *, int /*<<< orphan*/);
int /*<<< orphan*/ format_lun_number(struct udev_device *, char **);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ path_prepend(char **, char *, char const *, char const *,
                                char const *, char *);
scalar_t__ strncmp(char *, char *, int);
struct udev_device *udev_device_get_parent(struct udev_device *);
char *udev_device_get_sysattr_value(struct udev_device *, char *);
char *udev_device_get_sysname(struct udev_device *);
int /*<<< orphan*/ udev_device_get_sysnum(struct udev_device *);
struct udev *udev_device_get_udev(struct udev_device *);
struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *,
                                                           char *, char *);
int /*<<< orphan*/ udev_device_unref(struct udev_device *);
struct udev_device *handle_scsi_iscsi(struct udev_device *parent, char **path) {
  struct udev *udev = udev_device_get_udev(parent);
  struct udev_device *transportdev;
  struct udev_device *sessiondev = NULL;
  const char *target;
  char *connname;
  struct udev_device *conndev = NULL;
  const char *addr;
  const char *port;
  char *lun = NULL;
  transportdev = parent;
  for (;;) {
    transportdev = udev_device_get_parent(transportdev);
    if (transportdev == NULL)
      return NULL;
    if (strncmp(udev_device_get_sysname(transportdev), "session", 7) == 0)
      break;
  }
  sessiondev = udev_device_new_from_subsystem_sysname(
      udev, "iscsi_session", udev_device_get_sysname(transportdev));
  if (sessiondev == NULL)
    return NULL;
  target = udev_device_get_sysattr_value(sessiondev, "targetname");
  if (target == NULL) {
    parent = NULL;
    goto out;
  }
  if (asprintf(&connname, "connection%s:0",
               udev_device_get_sysnum(transportdev)) < 0) {
    parent = NULL;
    goto out;
  }
  conndev = udev_device_new_from_subsystem_sysname(udev, "iscsi_connection",
                                                   connname);
  free(connname);
  if (conndev == NULL) {
    parent = NULL;
    goto out;
  }
  addr = udev_device_get_sysattr_value(conndev, "persistent_address");
  port = udev_device_get_sysattr_value(conndev, "persistent_port");
  if (addr == NULL || port == NULL) {
    parent = NULL;
    goto out;
  }
  format_lun_number(parent, &lun);
  path_prepend(path, "ip-%s:%s-iscsi-%s-%s", addr, port, target, lun);
  if (lun)
    free(lun);
out:
  udev_device_unref(sessiondev);
  udev_device_unref(conndev);
  return parent;
}


x86 assembly (clang, -O0):
<handle_scsi_iscsi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%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,-0x30(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x60(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1080 <udev_device_get_parent@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1222 <handle_scsi_iscsi+0x62>
movq   $0x0,-0x8(%rbp)
jmp    13d5 <handle_scsi_iscsi+0x215>
mov    -0x28(%rbp),%rdi
call   10d0 <udev_device_get_sysname@plt>
mov    %rax,%rdi
lea    0xdd1(%rip),%rsi
mov    $0x7,%edx
call   1060 <strncmp@plt>
cmp    $0x0,%rax
jne    124e <handle_scsi_iscsi+0x8e>
jmp    1253 <handle_scsi_iscsi+0x93>
jmp    11fd <handle_scsi_iscsi+0x3d>
mov    -0x20(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x28(%rbp),%rdi
call   10d0 <udev_device_get_sysname@plt>
mov    -0x68(%rbp),%rdi
mov    %rax,%rdx
lea    0xd8e(%rip),%rsi
call   1070 <udev_device_new_from_subsystem_sysname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1293 <handle_scsi_iscsi+0xd3>
movq   $0x0,-0x8(%rbp)
jmp    13d5 <handle_scsi_iscsi+0x215>
mov    -0x30(%rbp),%rdi
lea    0xd70(%rip),%rsi
call   10e0 <udev_device_get_sysattr_value@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12bf <handle_scsi_iscsi+0xff>
movq   $0x0,-0x10(%rbp)
jmp    13bb <handle_scsi_iscsi+0x1fb>
mov    -0x28(%rbp),%rdi
call   10c0 <udev_device_get_sysnum@plt>
mov    %eax,%edx
lea    -0x40(%rbp),%rdi
lea    0xd44(%rip),%rsi
call   10b0 <asprintf@plt>
cmp    $0x0,%rax
jge    12f1 <handle_scsi_iscsi+0x131>
movq   $0x0,-0x10(%rbp)
jmp    13bb <handle_scsi_iscsi+0x1fb>
mov    -0x20(%rbp),%rdi
mov    -0x40(%rbp),%rdx
lea    0xd28(%rip),%rsi
call   1070 <udev_device_new_from_subsystem_sysname@plt>
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
cmpq   $0x0,-0x48(%rbp)
jne    132a <handle_scsi_iscsi+0x16a>
movq   $0x0,-0x10(%rbp)
jmp    13bb <handle_scsi_iscsi+0x1fb>
mov    -0x48(%rbp),%rdi
lea    0xd04(%rip),%rsi
call   10e0 <udev_device_get_sysattr_value@plt>
mov    %rax,-0x50(%rbp)
mov    -0x48(%rbp),%rdi
lea    0xd03(%rip),%rsi
call   10e0 <udev_device_get_sysattr_value@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     1368 <handle_scsi_iscsi+0x1a8>
cmpq   $0x0,-0x58(%rbp)
jne    1375 <handle_scsi_iscsi+0x1b5>
movq   $0x0,-0x10(%rbp)
jmp    13bb <handle_scsi_iscsi+0x1fb>
mov    -0x10(%rbp),%rdi
lea    -0x60(%rbp),%rsi
call   1090 <format_lun_number@plt>
mov    -0x18(%rbp),%rdi
mov    -0x50(%rbp),%rdx
mov    -0x58(%rbp),%rcx
mov    -0x38(%rbp),%r8
mov    -0x60(%rbp),%r9
lea    0xcbf(%rip),%rsi
call   1030 <path_prepend@plt>
cmpq   $0x0,-0x60(%rbp)
je     13b6 <handle_scsi_iscsi+0x1f6>
mov    -0x60(%rbp),%rdi
call   1040 <free@plt>
jmp    13bb <handle_scsi_iscsi+0x1fb>
mov    -0x30(%rbp),%rdi
call   10a0 <udev_device_unref@plt>
mov    -0x48(%rbp),%rdi
call   10a0 <udev_device_unref@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_scsi_iscsi>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%r13
call   1050 <udev_device_get_udev@plt>
mov    %rax,%r12
movq   $0x0,(%rsp)
lea    0xe1b(%rip),%rbx
mov    %r13,%rbp
xchg   %ax,%ax
mov    %rbp,%rdi
call   1080 <udev_device_get_parent@plt>
test   %rax,%rax
je     1318 <handle_scsi_iscsi+0x158>
mov    %rax,%rbp
mov    %rax,%rdi
call   10d0 <udev_device_get_sysname@plt>
mov    %rax,%rdi
mov    %rbx,%rsi
mov    $0x7,%edx
call   1060 <strncmp@plt>
test   %rax,%rax
jne    11f0 <handle_scsi_iscsi+0x30>
mov    %rbp,%rdi
call   10d0 <udev_device_get_sysname@plt>
lea    0xdd0(%rip),%rsi
mov    %r12,%rdi
mov    %rax,%rdx
call   1070 <udev_device_new_from_subsystem_sysname@plt>
test   %rax,%rax
je     1318 <handle_scsi_iscsi+0x158>
mov    %rax,%r15
lea    0xdc0(%rip),%rsi
mov    %rax,%rdi
call   10e0 <udev_device_get_sysattr_value@plt>
test   %rax,%rax
je     131d <handle_scsi_iscsi+0x15d>
mov    %rax,%rbx
mov    %rbp,%rdi
call   10c0 <udev_device_get_sysnum@plt>
lea    0xda8(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %eax,%edx
call   10b0 <asprintf@plt>
test   %rax,%rax
js     131d <handle_scsi_iscsi+0x15d>
mov    0x8(%rsp),%rdx
lea    0xd96(%rip),%rsi
mov    %r12,%rdi
call   1070 <udev_device_new_from_subsystem_sysname@plt>
mov    %rax,%r12
mov    0x8(%rsp),%rdi
call   1040 <free@plt>
test   %r12,%r12
je     131d <handle_scsi_iscsi+0x15d>
lea    0xd86(%rip),%rsi
mov    %r12,%rdi
call   10e0 <udev_device_get_sysattr_value@plt>
mov    %rax,%rbp
lea    0xd87(%rip),%rsi
mov    %r12,%rdi
call   10e0 <udev_device_get_sysattr_value@plt>
mov    %rbp,0x10(%rsp)
test   %rbp,%rbp
je     1320 <handle_scsi_iscsi+0x160>
test   %rax,%rax
je     1320 <handle_scsi_iscsi+0x160>
mov    %rsp,%rsi
mov    %r13,%rdi
mov    %rax,%rbp
call   1090 <format_lun_number@plt>
mov    (%rsp),%r9
lea    0xd67(%rip),%rsi
mov    %r14,%rdi
mov    0x10(%rsp),%rdx
mov    %rbp,%rcx
mov    %rbx,%r8
call   1030 <path_prepend@plt>
mov    (%rsp),%rdi
test   %rdi,%rdi
je     1323 <handle_scsi_iscsi+0x163>
call   1040 <free@plt>
jmp    1323 <handle_scsi_iscsi+0x163>
xor    %r13d,%r13d
jmp    1333 <handle_scsi_iscsi+0x173>
xor    %r12d,%r12d
xor    %r13d,%r13d
mov    %r15,%rdi
call   10a0 <udev_device_unref@plt>
mov    %r12,%rdi
call   10a0 <udev_device_unref@plt>
mov    %r13,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.udev_device = type { i32 }
%struct.udev = type { i32 }
@.str = [8 x i8] c"session\00"
@.str.1 = [14 x i8] c"iscsi_session\00"
@.str.2 = [11 x i8] c"targetname\00"
@.str.3 = [15 x i8] c"connection%s:0\00"
@.str.4 = [17 x i8] c"iscsi_connection\00"
@.str.5 = [19 x i8] c"persistent_address\00"
@.str.6 = [16 x i8] c"persistent_port\00"
@.str.7 = [21 x i8] c"ip-%s:%s-iscsi-%s-%s\00"
define %struct.udev_device* @handle_scsi_iscsi(%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 i8*
%10 = alloca i8*
%11 = alloca %struct.udev_device*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
store %struct.udev_device* %0, %struct.udev_device** %4
store i8** %1, i8*** %5
%15 = load %struct.udev_device*, %struct.udev_device** %4
%16 = call %struct.udev* @udev_device_get_udev(%struct.udev_device* %15)
store %struct.udev* %16, %struct.udev** %6
store %struct.udev_device* null, %struct.udev_device** %8
store %struct.udev_device* null, %struct.udev_device** %11
store i8* null, i8** %14
%17 = load %struct.udev_device*, %struct.udev_device** %4
store %struct.udev_device* %17, %struct.udev_device** %7
br label %18
18: ; preds = %30, %2
%19 = load %struct.udev_device*, %struct.udev_device** %7
%20 = call %struct.udev_device* @udev_device_get_parent(%struct.udev_device* %19)
store %struct.udev_device* %20, %struct.udev_device** %7
%21 = load %struct.udev_device*, %struct.udev_device** %7
%22 = icmp eq %struct.udev_device* %21, null
br i1 %22, label %23, label %24
23: ; preds = %18
store %struct.udev_device* null, %struct.udev_device** %3
br label %92
24: ; preds = %18
%25 = load %struct.udev_device*, %struct.udev_device** %7
%26 = call i8* @udev_device_get_sysname(%struct.udev_device* %25)
%27 = call i64 @strncmp(i8* %26, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 7)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %24
br label %31
30: ; preds = %24
br label %18
31: ; preds = %29
%32 = load %struct.udev*, %struct.udev** %6
%33 = load %struct.udev_device*, %struct.udev_device** %7
%34 = call i8* @udev_device_get_sysname(%struct.udev_device* %33)
%35 = call %struct.udev_device* @udev_device_new_from_subsystem_sysname(%struct.udev* %32, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %34)
store %struct.udev_device* %35, %struct.udev_device** %8
%36 = load %struct.udev_device*, %struct.udev_device** %8
%37 = icmp eq %struct.udev_device* %36, null
br i1 %37, label %38, label %39
38: ; preds = %31
store %struct.udev_device* null, %struct.udev_device** %3
br label %92
39: ; preds = %31
%40 = load %struct.udev_device*, %struct.udev_device** %8
%41 = call i8* @udev_device_get_sysattr_value(%struct.udev_device* %40, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
store i8* %41, i8** %9
%42 = load i8*, i8** %9
%43 = icmp eq i8* %42, null
br i1 %43, label %44, label %45
44: ; preds = %39
store %struct.udev_device* null, %struct.udev_device** %4
br label %86
45: ; preds = %39
%46 = load %struct.udev_device*, %struct.udev_device** %7
%47 = call i32 @udev_device_get_sysnum(%struct.udev_device* %46)
%48 = call i64 @asprintf(i8** %10, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %47)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %45
store %struct.udev_device* null, %struct.udev_device** %4
br label %86
51: ; preds = %45
%52 = load %struct.udev*, %struct.udev** %6
%53 = load i8*, i8** %10
%54 = call %struct.udev_device* @udev_device_new_from_subsystem_sysname(%struct.udev* %52, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %53)
store %struct.udev_device* %54, %struct.udev_device** %11
%55 = load i8*, i8** %10
%56 = call i32 @free(i8* %55)
%57 = load %struct.udev_device*, %struct.udev_device** %11
%58 = icmp eq %struct.udev_device* %57, null
br i1 %58, label %59, label %60
59: ; preds = %51
store %struct.udev_device* null, %struct.udev_device** %4
br label %86
60: ; preds = %51
%61 = load %struct.udev_device*, %struct.udev_device** %11
%62 = call i8* @udev_device_get_sysattr_value(%struct.udev_device* %61, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
store i8* %62, i8** %12
%63 = load %struct.udev_device*, %struct.udev_device** %11
%64 = call i8* @udev_device_get_sysattr_value(%struct.udev_device* %63, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
store i8* %64, i8** %13
%65 = load i8*, i8** %12
%66 = icmp eq i8* %65, null
br i1 %66, label %70, label %67
67: ; preds = %60
%68 = load i8*, i8** %13
%69 = icmp eq i8* %68, null
br i1 %69, label %70, label %71
70: ; preds = %67, %60
store %struct.udev_device* null, %struct.udev_device** %4
br label %86
71: ; preds = %67
%72 = load %struct.udev_device*, %struct.udev_device** %4
%73 = call i32 @format_lun_number(%struct.udev_device* %72, i8** %14)
%74 = load i8**, i8*** %5
%75 = load i8*, i8** %12
%76 = load i8*, i8** %13
%77 = load i8*, i8** %9
%78 = load i8*, i8** %14
%79 = call i32 @path_prepend(i8** %74, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0), i8* %75, i8* %76, i8* %77, i8* %78)
%80 = load i8*, i8** %14
%81 = icmp ne i8* %80, null
br i1 %81, label %82, label %85
82: ; preds = %71
%83 = load i8*, i8** %14
%84 = call i32 @free(i8* %83)
br label %85
85: ; preds = %82, %71
br label %86
86: ; preds = %85, %70, %59, %50, %44
%87 = load %struct.udev_device*, %struct.udev_device** %8
%88 = call i32 @udev_device_unref(%struct.udev_device* %87)
%89 = load %struct.udev_device*, %struct.udev_device** %11
%90 = call i32 @udev_device_unref(%struct.udev_device* %89)
%91 = load %struct.udev_device*, %struct.udev_device** %4
store %struct.udev_device* %91, %struct.udev_device** %3
br label %92
92: ; preds = %86, %38, %23
%93 = load %struct.udev_device*, %struct.udev_device** %3
ret %struct.udev_device* %93
}

LLVM IR (-O2):
%struct.udev_device = type { i32 }
%struct.udev = type { i32 }
@.str = [8 x i8] c"session\00"
@.str.1 = [14 x i8] c"iscsi_session\00"
@.str.2 = [11 x i8] c"targetname\00"
@.str.3 = [15 x i8] c"connection%s:0\00"
@.str.4 = [17 x i8] c"iscsi_connection\00"
@.str.5 = [19 x i8] c"persistent_address\00"
@.str.6 = [16 x i8] c"persistent_port\00"
@.str.7 = [21 x i8] c"ip-%s:%s-iscsi-%s-%s\00"
define %struct.udev_device* @handle_scsi_iscsi(%struct.udev_device* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = tail call %struct.udev* @udev_device_get_udev(%struct.udev_device* %0)
%6 = bitcast i8** %3 to i8*
%7 = bitcast i8** %4 to i8*
store i8* null, i8** %4
br label %8
8: ; preds = %12, %2
%9 = phi %struct.udev_device* [ %0, %2 ], [ %10, %12 ]
%10 = tail call %struct.udev_device* @udev_device_get_parent(%struct.udev_device* %9)
%11 = icmp eq %struct.udev_device* %10, null
br i1 %11, label %52, label %12
12: ; preds = %8
%13 = tail call i8* @udev_device_get_sysname(%struct.udev_device* %10)
%14 = tail call i64 @strncmp(i8* %13, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 7)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %8
16: ; preds = %12
%17 = tail call i8* @udev_device_get_sysname(%struct.udev_device* %10)
%18 = tail call %struct.udev_device* @udev_device_new_from_subsystem_sysname(%struct.udev* %5, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = icmp eq %struct.udev_device* %18, null
br i1 %19, label %52, label %20
20: ; preds = %16
%21 = tail call i8* @udev_device_get_sysattr_value(%struct.udev_device* %18, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp eq i8* %21, null
br i1 %22, label %47, label %23
23: ; preds = %20
%24 = tail call i32 @udev_device_get_sysnum(%struct.udev_device* %10)
%25 = call i64 @asprintf(i8** %3, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %47, label %27
27: ; preds = %23
%28 = load i8*, i8** %3
%29 = call %struct.udev_device* @udev_device_new_from_subsystem_sysname(%struct.udev* %5, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %28)
%30 = load i8*, i8** %3
%31 = call i32 @free(i8* %30)
%32 = icmp eq %struct.udev_device* %29, null
br i1 %32, label %47, label %33
33: ; preds = %27
%34 = call i8* @udev_device_get_sysattr_value(%struct.udev_device* %29, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%35 = call i8* @udev_device_get_sysattr_value(%struct.udev_device* %29, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
%36 = icmp eq i8* %34, null
%37 = icmp eq i8* %35, null
%38 = select i1 %36, i1 true, i1 %37
br i1 %38, label %47, label %39
39: ; preds = %33
%40 = call i32 @format_lun_number(%struct.udev_device* %0, i8** %4)
%41 = load i8*, i8** %4
%42 = call i32 @path_prepend(i8** %1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0), i8* %34, i8* %35, i8* %21, i8* %41)
%43 = load i8*, i8** %4
%44 = icmp eq i8* %43, null
br i1 %44, label %47, label %45
45: ; preds = %39
%46 = call i32 @free(i8* %43)
br label %47
47: ; preds = %33, %27, %23, %20, %39, %45
%48 = phi %struct.udev_device* [ %29, %45 ], [ %29, %39 ], [ null, %20 ], [ null, %23 ], [ null, %27 ], [ %29, %33 ]
%49 = phi %struct.udev_device* [ %0, %45 ], [ %0, %39 ], [ null, %20 ], [ null, %23 ], [ null, %27 ], [ null, %33 ]
%50 = call i32 @udev_device_unref(%struct.udev_device* %18)
%51 = call i32 @udev_device_unref(%struct.udev_device* %48)
br label %52
52: ; preds = %8, %16, %47
%53 = phi %struct.udev_device* [ %49, %47 ], [ null, %16 ], [ null, %8 ]
ret %struct.udev_device* %53
}