#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 ENODATA;
int ETIMEDOUT;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ path_prepend(char **, char *, int, int, int, int);
int scsi_host_offset(char *);
int scsi_read_host_info(char const *, char **, int *, int *, int *, int *);
int /*<<< orphan*/ sleep(int);
scalar_t__ strcmp(char *, char *);
int vdev_sysfs_get_sysname(char *, char **, size_t *);
int handle_scsi_default(char const *parent, char **path, char **new_parent) {
  int rc = 0;
  int attempts = 5;
  while (1) {
    int host = 0, bus = 0, target = 0, lun = 0;
    int host2 = 0, bus2 = 0, target2 = 0, lun2 = 0;
    int host_offset = 0;
    char *hostdev = NULL;
    char *hostdev2 = NULL;
    char *hostdev_sysname = NULL;
    size_t hostdev_sysname_len = 0;
    rc = scsi_read_host_info(parent, &hostdev, &host, &bus, &target, &lun);
    if (rc != 0) {
      return rc;
    }
    rc =
        vdev_sysfs_get_sysname(hostdev, &hostdev_sysname, &hostdev_sysname_len);
    if (rc != 0) {
      free(hostdev);
      return rc;
    }
    host_offset = scsi_host_offset(hostdev_sysname);
    free(hostdev_sysname);
    if (host_offset < 0) {
      return -ENODATA;
    }
    rc = scsi_read_host_info(parent, &hostdev2, &host2, &bus2, &target2, &lun2);
    if (rc != 0) {
      free(hostdev);
      return rc;
    }
    if (strcmp(hostdev, hostdev2) != 0 || host != host2 || bus != bus2 ||
        target != target2 || lun != lun2) {
      free(hostdev);
      free(hostdev2);
      sleep(1);
      attempts--;
      if (attempts <= 0) {
        return -ETIMEDOUT;
      }
      continue;
    }
    free(hostdev2);
    host -= host_offset;
    path_prepend(path, "scsi-%u:%u:%u:%u", host, bus, target, lun);
    *new_parent = hostdev;
    break;
  }
  return 0;
}


x86 assembly (clang, -O0):
<handle_scsi_default>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x5,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
movl   $0x0,-0x48(%rbp)
movl   $0x0,-0x4c(%rbp)
movq   $0x0,-0x58(%rbp)
movq   $0x0,-0x60(%rbp)
movq   $0x0,-0x68(%rbp)
movq   $0x0,-0x70(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x58(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
lea    -0x30(%rbp),%rcx
lea    -0x34(%rbp),%r8
lea    -0x38(%rbp),%r9
call   1080 <scsi_read_host_info@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1226 <handle_scsi_default+0xb6>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    139a <handle_scsi_default+0x22a>
mov    -0x58(%rbp),%rdi
lea    -0x68(%rbp),%rsi
lea    -0x70(%rbp),%rdx
call   1070 <vdev_sysfs_get_sysname@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1258 <handle_scsi_default+0xe8>
mov    -0x58(%rbp),%rdi
call   1040 <free@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    139a <handle_scsi_default+0x22a>
mov    -0x68(%rbp),%rdi
call   1060 <scsi_host_offset@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x68(%rbp),%rdi
call   1040 <free@plt>
cmpl   $0x0,-0x4c(%rbp)
jge    128a <handle_scsi_default+0x11a>
xor    %eax,%eax
mov    0x2d68(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    139a <handle_scsi_default+0x22a>
mov    -0x10(%rbp),%rdi
lea    -0x60(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
lea    -0x40(%rbp),%rcx
lea    -0x44(%rbp),%r8
lea    -0x48(%rbp),%r9
call   1080 <scsi_read_host_info@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     12c8 <handle_scsi_default+0x158>
mov    -0x58(%rbp),%rdi
call   1040 <free@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    139a <handle_scsi_default+0x22a>
mov    -0x58(%rbp),%rdi
mov    -0x60(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    130f <handle_scsi_default+0x19f>
mov    -0x2c(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jne    130f <handle_scsi_default+0x19f>
mov    -0x30(%rbp),%eax
cmp    -0x40(%rbp),%eax
jne    130f <handle_scsi_default+0x19f>
mov    -0x34(%rbp),%eax
cmp    -0x44(%rbp),%eax
jne    130f <handle_scsi_default+0x19f>
mov    -0x38(%rbp),%eax
cmp    -0x48(%rbp),%eax
je     1356 <handle_scsi_default+0x1e6>
mov    -0x58(%rbp),%rdi
call   1040 <free@plt>
mov    -0x60(%rbp),%rdi
call   1040 <free@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0x28(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jg     1351 <handle_scsi_default+0x1e1>
xor    %eax,%eax
mov    0x2c91(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    139a <handle_scsi_default+0x22a>
jmp    1192 <handle_scsi_default+0x22>
mov    -0x60(%rbp),%rdi
call   1040 <free@plt>
mov    -0x4c(%rbp),%ecx
mov    -0x2c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x30(%rbp),%ecx
mov    -0x34(%rbp),%r8d
mov    -0x38(%rbp),%r9d
lea    0xc7d(%rip),%rsi
call   1030 <path_prepend@plt>
mov    -0x58(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_scsi_default>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %rdx,0x48(%rsp)
mov    %rsi,0x40(%rsp)
mov    %rdi,%rbx
mov    $0x5,%ebp
lea    0x10(%rsp),%r14
lea    0xc(%rsp),%r15
nopw   0x0(%rax,%rax,1)
movl   $0x0,0x8(%rsp)
movl   $0x0,0x14(%rsp)
movl   $0x0,0x10(%rsp)
movl   $0x0,0xc(%rsp)
movl   $0x0,0x2c(%rsp)
movl   $0x0,0x28(%rsp)
movl   $0x0,0x24(%rsp)
movl   $0x0,0x20(%rsp)
movq   $0x0,0x18(%rsp)
movq   $0x0,0x30(%rsp)
movq   $0x0,0x38(%rsp)
movq   $0x0,0x50(%rsp)
mov    %rbx,%rdi
lea    0x18(%rsp),%rsi
lea    0x8(%rsp),%rdx
lea    0x14(%rsp),%rcx
mov    %r14,%r8
mov    %r15,%r9
call   1080 <scsi_read_host_info@plt>
test   %eax,%eax
jne    12fb <handle_scsi_default+0x18b>
mov    0x18(%rsp),%rdi
lea    0x38(%rsp),%rsi
lea    0x50(%rsp),%rdx
call   1070 <vdev_sysfs_get_sysname@plt>
test   %eax,%eax
jne    1300 <handle_scsi_default+0x190>
mov    0x38(%rsp),%rdi
call   1060 <scsi_host_offset@plt>
mov    %eax,%r13d
mov    0x38(%rsp),%rdi
call   1040 <free@plt>
test   %r13d,%r13d
js     130f <handle_scsi_default+0x19f>
mov    %rbx,%rdi
lea    0x30(%rsp),%rsi
lea    0x2c(%rsp),%rdx
lea    0x28(%rsp),%rcx
lea    0x24(%rsp),%r8
lea    0x20(%rsp),%r9
call   1080 <scsi_read_host_info@plt>
mov    0x18(%rsp),%rdi
test   %eax,%eax
jne    1318 <handle_scsi_default+0x1a8>
mov    0x30(%rsp),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    12d0 <handle_scsi_default+0x160>
mov    0x8(%rsp),%eax
cmp    0x2c(%rsp),%eax
jne    12d0 <handle_scsi_default+0x160>
mov    0x14(%rsp),%eax
cmp    0x28(%rsp),%eax
jne    12d0 <handle_scsi_default+0x160>
mov    0x10(%rsp),%eax
cmp    0x24(%rsp),%eax
jne    12d0 <handle_scsi_default+0x160>
mov    0xc(%rsp),%eax
cmp    0x20(%rsp),%eax
je     1341 <handle_scsi_default+0x1d1>
nopw   0x0(%rax,%rax,1)
mov    0x18(%rsp),%rdi
call   1040 <free@plt>
mov    0x30(%rsp),%rdi
call   1040 <free@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
cmp    $0x1,%ebp
jbe    1322 <handle_scsi_default+0x1b2>
add    $0xffffffff,%ebp
jmp    11a0 <handle_scsi_default+0x30>
mov    %eax,%r12d
jmp    132f <handle_scsi_default+0x1bf>
mov    %eax,%r12d
mov    0x18(%rsp),%rdi
call   1040 <free@plt>
jmp    132f <handle_scsi_default+0x1bf>
mov    0x2cd2(%rip),%rax
jmp    1329 <handle_scsi_default+0x1b9>
mov    %eax,%r12d
call   1040 <free@plt>
jmp    132f <handle_scsi_default+0x1bf>
mov    0x2caf(%rip),%rax
xor    %r12d,%r12d
sub    (%rax),%r12d
mov    %r12d,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x30(%rsp),%rdi
call   1040 <free@plt>
mov    0x8(%rsp),%edx
sub    %r13d,%edx
mov    %edx,0x8(%rsp)
mov    0x14(%rsp),%ecx
mov    0x10(%rsp),%r8d
mov    0xc(%rsp),%r9d
lea    0xc95(%rip),%rsi
mov    0x40(%rsp),%rdi
call   1030 <path_prepend@plt>
mov    0x18(%rsp),%rax
mov    0x48(%rsp),%rcx
mov    %rax,(%rcx)
xor    %r12d,%r12d
jmp    132f <handle_scsi_default+0x1bf>

LLVM IR (-O0):
@ENODATA = global i32 0
@ETIMEDOUT = global i32 0
@.str = [17 x i8] c"scsi-%u:%u:%u:%u\00"
define i32 @handle_scsi_default(i8* %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i8*
%20 = alloca i8*
%21 = alloca i8*
%22 = alloca i64
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
store i32 0, i32* %8
store i32 5, i32* %9
br label %23
23: ; preds = %3, %92
store i32 0, i32* %10
store i32 0, i32* %11
store i32 0, i32* %12
store i32 0, i32* %13
store i32 0, i32* %14
store i32 0, i32* %15
store i32 0, i32* %16
store i32 0, i32* %17
store i32 0, i32* %18
store i8* null, i8** %19
store i8* null, i8** %20
store i8* null, i8** %21
store i64 0, i64* %22
%24 = load i8*, i8** %5
%25 = call i32 @scsi_read_host_info(i8* %24, i8** %19, i32* %10, i32* %11, i32* %12, i32* %13)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = load i32, i32* %8
store i32 %29, i32* %4
br label %108
30: ; preds = %23
%31 = load i8*, i8** %19
%32 = call i32 @vdev_sysfs_get_sysname(i8* %31, i8** %21, i64* %22)
store i32 %32, i32* %8
%33 = load i32, i32* %8
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %30
%36 = load i8*, i8** %19
%37 = call i32 @free(i8* %36)
%38 = load i32, i32* %8
store i32 %38, i32* %4
br label %108
39: ; preds = %30
%40 = load i8*, i8** %21
%41 = call i32 @scsi_host_offset(i8* %40)
store i32 %41, i32* %18
%42 = load i8*, i8** %21
%43 = call i32 @free(i8* %42)
%44 = load i32, i32* %18
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %39
%47 = load i32, i32* @ENODATA
%48 = sub nsw i32 0, %47
store i32 %48, i32* %4
br label %108
49: ; preds = %39
%50 = load i8*, i8** %5
%51 = call i32 @scsi_read_host_info(i8* %50, i8** %20, i32* %14, i32* %15, i32* %16, i32* %17)
store i32 %51, i32* %8
%52 = load i32, i32* %8
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %49
%55 = load i8*, i8** %19
%56 = call i32 @free(i8* %55)
%57 = load i32, i32* %8
store i32 %57, i32* %4
br label %108
58: ; preds = %49
%59 = load i8*, i8** %19
%60 = load i8*, i8** %20
%61 = call i64 @strcmp(i8* %59, i8* %60)
%62 = icmp ne i64 %61, 0
br i1 %62, label %79, label %63
63: ; preds = %58
%64 = load i32, i32* %10
%65 = load i32, i32* %14
%66 = icmp ne i32 %64, %65
br i1 %66, label %79, label %67
67: ; preds = %63
%68 = load i32, i32* %11
%69 = load i32, i32* %15
%70 = icmp ne i32 %68, %69
br i1 %70, label %79, label %71
71: ; preds = %67
%72 = load i32, i32* %12
%73 = load i32, i32* %16
%74 = icmp ne i32 %72, %73
br i1 %74, label %79, label %75
75: ; preds = %71
%76 = load i32, i32* %13
%77 = load i32, i32* %17
%78 = icmp ne i32 %76, %77
br i1 %78, label %79, label %93
79: ; preds = %75, %71, %67, %63, %58
%80 = load i8*, i8** %19
%81 = call i32 @free(i8* %80)
%82 = load i8*, i8** %20
%83 = call i32 @free(i8* %82)
%84 = call i32 @sleep(i32 1)
%85 = load i32, i32* %9
%86 = add nsw i32 %85, -1
store i32 %86, i32* %9
%87 = load i32, i32* %9
%88 = icmp sle i32 %87, 0
br i1 %88, label %89, label %92
89: ; preds = %79
%90 = load i32, i32* @ETIMEDOUT
%91 = sub nsw i32 0, %90
store i32 %91, i32* %4
br label %108
92: ; preds = %79
br label %23
93: ; preds = %75
%94 = load i8*, i8** %20
%95 = call i32 @free(i8* %94)
%96 = load i32, i32* %18
%97 = load i32, i32* %10
%98 = sub nsw i32 %97, %96
store i32 %98, i32* %10
%99 = load i8**, i8*** %6
%100 = load i32, i32* %10
%101 = load i32, i32* %11
%102 = load i32, i32* %12
%103 = load i32, i32* %13
%104 = call i32 @path_prepend(i8** %99, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %100, i32 %101, i32 %102, i32 %103)
%105 = load i8*, i8** %19
%106 = load i8**, i8*** %7
store i8* %105, i8** %106
br label %107
107: ; preds = %93
store i32 0, i32* %4
br label %108
108: ; preds = %107, %89, %54, %46, %35, %28
%109 = load i32, i32* %4
ret i32 %109
}

LLVM IR (-O2):
@ENODATA = global i32 0
@ETIMEDOUT = global i32 0
@.str = [17 x i8] c"scsi-%u:%u:%u:%u\00"
define i32 @handle_scsi_default(i8* %0, i8** %1, i8** nocapture writeonly %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i64
%16 = bitcast i32* %4 to i8*
%17 = bitcast i32* %5 to i8*
%18 = bitcast i32* %6 to i8*
%19 = bitcast i32* %7 to i8*
%20 = bitcast i32* %8 to i8*
%21 = bitcast i32* %9 to i8*
%22 = bitcast i32* %10 to i8*
%23 = bitcast i32* %11 to i8*
%24 = bitcast i8** %12 to i8*
%25 = bitcast i8** %13 to i8*
%26 = bitcast i8** %14 to i8*
%27 = bitcast i64* %15 to i8*
br label %28
28: ; preds = %86, %3
%29 = phi i32 [ 5, %3 ], [ %87, %86 ]
store i32 0, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
store i32 0, i32* %11
store i8* null, i8** %12
store i8* null, i8** %13
store i8* null, i8** %14
store i64 0, i64* %15
%30 = call i32 @scsi_read_host_info(i8* %0, i8** %12, i32* %4, i32* %5, i32* %6, i32* %7)
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %84
32: ; preds = %28
%33 = load i8*, i8** %12
%34 = call i32 @vdev_sysfs_get_sysname(i8* %33, i8** %14, i64* %15)
%35 = icmp eq i32 %34, 0
br i1 %35, label %39, label %36
36: ; preds = %32
%37 = load i8*, i8** %12
%38 = call i32 @free(i8* %37)
br label %84
39: ; preds = %32
%40 = load i8*, i8** %14
%41 = call i32 @scsi_host_offset(i8* %40)
%42 = load i8*, i8** %14
%43 = call i32 @free(i8* %42)
%44 = icmp slt i32 %41, 0
br i1 %44, label %45, label %48
45: ; preds = %39
%46 = load i32, i32* @ENODATA
%47 = sub nsw i32 0, %46
br label %84
48: ; preds = %39
%49 = call i32 @scsi_read_host_info(i8* %0, i8** %13, i32* %8, i32* %9, i32* %10, i32* %11)
%50 = icmp eq i32 %49, 0
%51 = load i8*, i8** %12
br i1 %50, label %54, label %52
52: ; preds = %48
%53 = call i32 @free(i8* %51)
br label %84
54: ; preds = %48
%55 = load i8*, i8** %13
%56 = call i64 @strcmp(i8* %51, i8* %55)
%57 = icmp eq i64 %56, 0
br i1 %57, label %58, label %74
58: ; preds = %54
%59 = load i32, i32* %4
%60 = load i32, i32* %8
%61 = icmp eq i32 %59, %60
br i1 %61, label %62, label %74
62: ; preds = %58
%63 = load i32, i32* %5
%64 = load i32, i32* %9
%65 = icmp eq i32 %63, %64
br i1 %65, label %66, label %74
66: ; preds = %62
%67 = load i32, i32* %6
%68 = load i32, i32* %10
%69 = icmp eq i32 %67, %68
br i1 %69, label %70, label %74
70: ; preds = %66
%71 = load i32, i32* %7
%72 = load i32, i32* %11
%73 = icmp eq i32 %71, %72
br i1 %73, label %88, label %74
74: ; preds = %70, %66, %62, %58, %54
%75 = load i8*, i8** %12
%76 = call i32 @free(i8* %75)
%77 = load i8*, i8** %13
%78 = call i32 @free(i8* %77)
%79 = call i32 @sleep(i32 1)
%80 = icmp ult i32 %29, 2
br i1 %80, label %81, label %86
81: ; preds = %74
%82 = load i32, i32* @ETIMEDOUT
%83 = sub nsw i32 0, %82
br label %84
84: ; preds = %28, %36, %45, %52, %81
%85 = phi i32 [ %83, %81 ], [ %49, %52 ], [ %47, %45 ], [ %34, %36 ], [ %30, %28 ]
br label %98
86: ; preds = %74
%87 = add nsw i32 %29, -1
br label %28
88: ; preds = %70
%89 = load i8*, i8** %13
%90 = call i32 @free(i8* %89)
%91 = load i32, i32* %4
%92 = sub nsw i32 %91, %41
store i32 %92, i32* %4
%93 = load i32, i32* %5
%94 = load i32, i32* %6
%95 = load i32, i32* %7
%96 = call i32 @path_prepend(i8** %1, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %92, i32 %93, i32 %94, i32 %95)
%97 = load i8*, i8** %12
store i8* %97, i8** %2
br label %98
98: ; preds = %84, %88
%99 = phi i32 [ 0, %88 ], [ %85, %84 ]
ret i32 %99
}