#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*/ free(char *);
int /*<<< orphan*/ path_prepend(char **, char *, char const *, char const *,
                                char const *, int /*<<< orphan*/);
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;
  transportdev = parent;
  while (1) {
    transportdev = udev_device_get_parent(transportdev);
    if (transportdev == NULL)
      return NULL;
    if (strncmp(udev_device_get_sysname(transportdev), "session", 7) == 0)
      break;
  }
  if (transportdev == NULL)
    return NULL;
  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;
  }
  path_prepend(path, "ip-%s:%s-iscsi-%s-lun-%s", addr, port, target,
               udev_device_get_sysnum(parent));
out:
  udev_device_unref(sessiondev);
  udev_device_unref(conndev);
  return parent;
}


x86 assembly (clang, -O0):
<handle_scsi_iscsi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%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)
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    120d <handle_scsi_iscsi+0x5d>
movq   $0x0,-0x8(%rbp)
jmp    13da <handle_scsi_iscsi+0x22a>
mov    -0x28(%rbp),%rdi
call   10c0 <udev_device_get_sysname@plt>
mov    %rax,%rdi
lea    0xde6(%rip),%rsi
mov    $0x7,%edx
call   1060 <strncmp@plt>
cmp    $0x0,%rax
jne    1239 <handle_scsi_iscsi+0x89>
jmp    123e <handle_scsi_iscsi+0x8e>
jmp    11e8 <handle_scsi_iscsi+0x38>
cmpq   $0x0,-0x28(%rbp)
jne    1256 <handle_scsi_iscsi+0xa6>
movq   $0x0,-0x8(%rbp)
jmp    13da <handle_scsi_iscsi+0x22a>
mov    -0x20(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x28(%rbp),%rdi
call   10c0 <udev_device_get_sysname@plt>
mov    -0x60(%rbp),%rdi
mov    %rax,%rdx
lea    0xd8b(%rip),%rsi
call   1070 <udev_device_new_from_subsystem_sysname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1296 <handle_scsi_iscsi+0xe6>
movq   $0x0,-0x8(%rbp)
jmp    13da <handle_scsi_iscsi+0x22a>
mov    -0x30(%rbp),%rdi
lea    0xd6d(%rip),%rsi
call   10d0 <udev_device_get_sysattr_value@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12c2 <handle_scsi_iscsi+0x112>
movq   $0x0,-0x10(%rbp)
jmp    13c0 <handle_scsi_iscsi+0x210>
mov    -0x28(%rbp),%rdi
call   10b0 <udev_device_get_sysnum@plt>
mov    %eax,%edx
lea    -0x40(%rbp),%rdi
lea    0xd41(%rip),%rsi
call   10a0 <asprintf@plt>
cmp    $0x0,%rax
jge    12f4 <handle_scsi_iscsi+0x144>
movq   $0x0,-0x10(%rbp)
jmp    13c0 <handle_scsi_iscsi+0x210>
mov    -0x20(%rbp),%rdi
mov    -0x40(%rbp),%rdx
lea    0xd25(%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    132d <handle_scsi_iscsi+0x17d>
movq   $0x0,-0x10(%rbp)
jmp    13c0 <handle_scsi_iscsi+0x210>
mov    -0x48(%rbp),%rdi
lea    0xd01(%rip),%rsi
call   10d0 <udev_device_get_sysattr_value@plt>
mov    %rax,-0x50(%rbp)
mov    -0x48(%rbp),%rdi
lea    0xd00(%rip),%rsi
call   10d0 <udev_device_get_sysattr_value@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     136b <handle_scsi_iscsi+0x1bb>
cmpq   $0x0,-0x58(%rbp)
jne    1378 <handle_scsi_iscsi+0x1c8>
movq   $0x0,-0x10(%rbp)
jmp    13c0 <handle_scsi_iscsi+0x210>
mov    -0x18(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x10(%rbp),%rdi
call   10b0 <udev_device_get_sysnum@plt>
mov    -0x80(%rbp),%rdi
mov    -0x78(%rbp),%rdx
mov    -0x70(%rbp),%rcx
mov    -0x68(%rbp),%r8
mov    %eax,%r9d
lea    0xca1(%rip),%rsi
call   1030 <path_prepend@plt>
mov    -0x30(%rbp),%rdi
call   1090 <udev_device_unref@plt>
mov    -0x48(%rbp),%rdi
call   1090 <udev_device_unref@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x80,%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
lea    0xe33(%rip),%rbx
mov    %r13,%rbp
cs nopw 0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1080 <udev_device_get_parent@plt>
test   %rax,%rax
je     12f6 <handle_scsi_iscsi+0x146>
mov    %rax,%rbp
mov    %rax,%rdi
call   10c0 <udev_device_get_sysname@plt>
mov    %rax,%rdi
mov    %rbx,%rsi
mov    $0x7,%edx
call   1060 <strncmp@plt>
test   %rax,%rax
jne    11e0 <handle_scsi_iscsi+0x30>
mov    %rbp,%rdi
call   10c0 <udev_device_get_sysname@plt>
lea    0xde0(%rip),%rsi
mov    %r12,%rdi
mov    %rax,%rdx
call   1070 <udev_device_new_from_subsystem_sysname@plt>
test   %rax,%rax
je     12f6 <handle_scsi_iscsi+0x146>
mov    %rax,%r15
lea    0xdd0(%rip),%rsi
mov    %rax,%rdi
call   10d0 <udev_device_get_sysattr_value@plt>
test   %rax,%rax
je     12fb <handle_scsi_iscsi+0x14b>
mov    %rax,%rbx
mov    %rbp,%rdi
call   10b0 <udev_device_get_sysnum@plt>
lea    0xdb8(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %eax,%edx
call   10a0 <asprintf@plt>
test   %rax,%rax
js     12fb <handle_scsi_iscsi+0x14b>
mov    0x8(%rsp),%rdx
lea    0xda6(%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     12fb <handle_scsi_iscsi+0x14b>
lea    0xd96(%rip),%rsi
mov    %r12,%rdi
call   10d0 <udev_device_get_sysattr_value@plt>
mov    %rax,%rbp
lea    0xd97(%rip),%rsi
mov    %r12,%rdi
call   10d0 <udev_device_get_sysattr_value@plt>
mov    %rbp,0x10(%rsp)
test   %rbp,%rbp
je     12fe <handle_scsi_iscsi+0x14e>
test   %rax,%rax
je     12fe <handle_scsi_iscsi+0x14e>
mov    %r13,%rdi
mov    %rax,%rbp
call   10b0 <udev_device_get_sysnum@plt>
lea    0xd7e(%rip),%rsi
mov    %r14,%rdi
mov    0x10(%rsp),%rdx
mov    %rbp,%rcx
mov    %rbx,%r8
mov    %eax,%r9d
call   1030 <path_prepend@plt>
jmp    1301 <handle_scsi_iscsi+0x151>
xor    %r13d,%r13d
jmp    1311 <handle_scsi_iscsi+0x161>
xor    %r12d,%r12d
xor    %r13d,%r13d
mov    %r15,%rdi
call   1090 <udev_device_unref@plt>
mov    %r12,%rdi
call   1090 <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 = [25 x i8] c"ip-%s:%s-iscsi-%s-lun-%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*
store %struct.udev_device* %0, %struct.udev_device** %4
store i8** %1, i8*** %5
%14 = load %struct.udev_device*, %struct.udev_device** %4
%15 = call %struct.udev* @udev_device_get_udev(%struct.udev_device* %14)
store %struct.udev* %15, %struct.udev** %6
store %struct.udev_device* null, %struct.udev_device** %8
store %struct.udev_device* null, %struct.udev_device** %11
%16 = load %struct.udev_device*, %struct.udev_device** %4
store %struct.udev_device* %16, %struct.udev_device** %7
br label %17
17: ; preds = %2, %29
%18 = load %struct.udev_device*, %struct.udev_device** %7
%19 = call %struct.udev_device* @udev_device_get_parent(%struct.udev_device* %18)
store %struct.udev_device* %19, %struct.udev_device** %7
%20 = load %struct.udev_device*, %struct.udev_device** %7
%21 = icmp eq %struct.udev_device* %20, null
br i1 %21, label %22, label %23
22: ; preds = %17
store %struct.udev_device* null, %struct.udev_device** %3
br label %88
23: ; preds = %17
%24 = load %struct.udev_device*, %struct.udev_device** %7
%25 = call i8* @udev_device_get_sysname(%struct.udev_device* %24)
%26 = call i64 @strncmp(i8* %25, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 7)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %23
br label %30
29: ; preds = %23
br label %17
30: ; preds = %28
%31 = load %struct.udev_device*, %struct.udev_device** %7
%32 = icmp eq %struct.udev_device* %31, null
br i1 %32, label %33, label %34
33: ; preds = %30
store %struct.udev_device* null, %struct.udev_device** %3
br label %88
34: ; preds = %30
%35 = load %struct.udev*, %struct.udev** %6
%36 = load %struct.udev_device*, %struct.udev_device** %7
%37 = call i8* @udev_device_get_sysname(%struct.udev_device* %36)
%38 = call %struct.udev_device* @udev_device_new_from_subsystem_sysname(%struct.udev* %35, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %37)
store %struct.udev_device* %38, %struct.udev_device** %8
%39 = load %struct.udev_device*, %struct.udev_device** %8
%40 = icmp eq %struct.udev_device* %39, null
br i1 %40, label %41, label %42
41: ; preds = %34
store %struct.udev_device* null, %struct.udev_device** %3
br label %88
42: ; preds = %34
%43 = load %struct.udev_device*, %struct.udev_device** %8
%44 = call i8* @udev_device_get_sysattr_value(%struct.udev_device* %43, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
store i8* %44, i8** %9
%45 = load i8*, i8** %9
%46 = icmp eq i8* %45, null
br i1 %46, label %47, label %48
47: ; preds = %42
store %struct.udev_device* null, %struct.udev_device** %4
br label %82
48: ; preds = %42
%49 = load %struct.udev_device*, %struct.udev_device** %7
%50 = call i32 @udev_device_get_sysnum(%struct.udev_device* %49)
%51 = call i64 @asprintf(i8** %10, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %50)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %48
store %struct.udev_device* null, %struct.udev_device** %4
br label %82
54: ; preds = %48
%55 = load %struct.udev*, %struct.udev** %6
%56 = load i8*, i8** %10
%57 = call %struct.udev_device* @udev_device_new_from_subsystem_sysname(%struct.udev* %55, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %56)
store %struct.udev_device* %57, %struct.udev_device** %11
%58 = load i8*, i8** %10
%59 = call i32 @free(i8* %58)
%60 = load %struct.udev_device*, %struct.udev_device** %11
%61 = icmp eq %struct.udev_device* %60, null
br i1 %61, label %62, label %63
62: ; preds = %54
store %struct.udev_device* null, %struct.udev_device** %4
br label %82
63: ; preds = %54
%64 = load %struct.udev_device*, %struct.udev_device** %11
%65 = call i8* @udev_device_get_sysattr_value(%struct.udev_device* %64, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
store i8* %65, i8** %12
%66 = load %struct.udev_device*, %struct.udev_device** %11
%67 = call i8* @udev_device_get_sysattr_value(%struct.udev_device* %66, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
store i8* %67, i8** %13
%68 = load i8*, i8** %12
%69 = icmp eq i8* %68, null
br i1 %69, label %73, label %70
70: ; preds = %63
%71 = load i8*, i8** %13
%72 = icmp eq i8* %71, null
br i1 %72, label %73, label %74
73: ; preds = %70, %63
store %struct.udev_device* null, %struct.udev_device** %4
br label %82
74: ; preds = %70
%75 = load i8**, i8*** %5
%76 = load i8*, i8** %12
%77 = load i8*, i8** %13
%78 = load i8*, i8** %9
%79 = load %struct.udev_device*, %struct.udev_device** %4
%80 = call i32 @udev_device_get_sysnum(%struct.udev_device* %79)
%81 = call i32 @path_prepend(i8** %75, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0), i8* %76, i8* %77, i8* %78, i32 %80)
br label %82
82: ; preds = %74, %73, %62, %53, %47
%83 = load %struct.udev_device*, %struct.udev_device** %8
%84 = call i32 @udev_device_unref(%struct.udev_device* %83)
%85 = load %struct.udev_device*, %struct.udev_device** %11
%86 = call i32 @udev_device_unref(%struct.udev_device* %85)
%87 = load %struct.udev_device*, %struct.udev_device** %4
store %struct.udev_device* %87, %struct.udev_device** %3
br label %88
88: ; preds = %82, %41, %33, %22
%89 = load %struct.udev_device*, %struct.udev_device** %3
ret %struct.udev_device* %89
}

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 = [25 x i8] c"ip-%s:%s-iscsi-%s-lun-%s\00"
define %struct.udev_device* @handle_scsi_iscsi(%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*
br label %6
6: ; preds = %10, %2
%7 = phi %struct.udev_device* [ %0, %2 ], [ %8, %10 ]
%8 = tail call %struct.udev_device* @udev_device_get_parent(%struct.udev_device* %7)
%9 = icmp eq %struct.udev_device* %8, null
br i1 %9, label %45, label %10
10: ; preds = %6
%11 = tail call i8* @udev_device_get_sysname(%struct.udev_device* %8)
%12 = tail call i64 @strncmp(i8* %11, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 7)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %6
14: ; preds = %10
%15 = tail call i8* @udev_device_get_sysname(%struct.udev_device* %8)
%16 = tail call %struct.udev_device* @udev_device_new_from_subsystem_sysname(%struct.udev* %4, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%17 = icmp eq %struct.udev_device* %16, null
br i1 %17, label %45, label %18
18: ; preds = %14
%19 = tail call i8* @udev_device_get_sysattr_value(%struct.udev_device* %16, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp eq i8* %19, null
br i1 %20, label %40, label %21
21: ; preds = %18
%22 = tail call i32 @udev_device_get_sysnum(%struct.udev_device* %8)
%23 = call i64 @asprintf(i8** %3, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %40, label %25
25: ; preds = %21
%26 = load i8*, i8** %3
%27 = call %struct.udev_device* @udev_device_new_from_subsystem_sysname(%struct.udev* %4, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %26)
%28 = load i8*, i8** %3
%29 = call i32 @free(i8* %28)
%30 = icmp eq %struct.udev_device* %27, null
br i1 %30, label %40, label %31
31: ; preds = %25
%32 = call i8* @udev_device_get_sysattr_value(%struct.udev_device* %27, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%33 = call i8* @udev_device_get_sysattr_value(%struct.udev_device* %27, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
%34 = icmp eq i8* %32, null
%35 = icmp eq i8* %33, null
%36 = select i1 %34, i1 true, i1 %35
br i1 %36, label %40, label %37
37: ; preds = %31
%38 = call i32 @udev_device_get_sysnum(%struct.udev_device* %0)
%39 = call i32 @path_prepend(i8** %1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0), i8* %32, i8* %33, i8* %19, i32 %38)
br label %40
40: ; preds = %31, %25, %21, %18, %37
%41 = phi %struct.udev_device* [ %27, %37 ], [ null, %18 ], [ null, %21 ], [ null, %25 ], [ %27, %31 ]
%42 = phi %struct.udev_device* [ %0, %37 ], [ null, %18 ], [ null, %21 ], [ null, %25 ], [ null, %31 ]
%43 = call i32 @udev_device_unref(%struct.udev_device* %16)
%44 = call i32 @udev_device_unref(%struct.udev_device* %41)
br label %45
45: ; preds = %6, %14, %40
%46 = phi %struct.udev_device* [ %42, %40 ], [ null, %14 ], [ null, %6 ]
ret %struct.udev_device* %46
}