#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 EINVAL;
int /*<<< orphan*/ free(char *);
int sscanf(char *, char *, int *, int *, int *, int *);
int vdev_sysfs_device_path_from_subsystem_sysname(char const *, char *, char *,
                                                  char **, size_t *);
int vdev_sysfs_get_sysname(char const *, char **, size_t *);
int scsi_read_host_info(char const *parent, char **hostdev, int *host, int *bus,
                        int *target, int *lun) {
  int rc = 0;
  size_t hostdev_len = 0;
  char *name = NULL;
  size_t name_len = 0;
  rc = vdev_sysfs_device_path_from_subsystem_sysname(
      parent, "scsi", "scsi_host", hostdev, &hostdev_len);
  if (rc != 0) {
    return rc;
  }
  rc = vdev_sysfs_get_sysname(parent, &name, &name_len);
  if (rc != 0) {
    free(*hostdev);
    *hostdev = NULL;
    return rc;
  }
  rc = sscanf(name, "%d:%d:%d:%d", host, bus, target, lun);
  if (rc != 4) {
    free(*hostdev);
    *hostdev = NULL;
    free(name);
    return -EINVAL;
  }
  free(name);
  return 0;
}


x86 assembly (clang, -O0):
<scsi_read_host_info>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
movq   $0x0,-0x58(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rcx
lea    0xe72(%rip),%rsi
lea    0xe70(%rip),%rdx
lea    -0x48(%rbp),%r8
call   1060 <vdev_sysfs_device_path_from_subsystem_sysname@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     11b6 <scsi_read_host_info+0x76>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1266 <scsi_read_host_info+0x126>
mov    -0x10(%rbp),%rdi
lea    -0x50(%rbp),%rsi
lea    -0x58(%rbp),%rdx
call   1040 <vdev_sysfs_get_sysname@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     11f6 <scsi_read_host_info+0xb6>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1266 <scsi_read_host_info+0x126>
mov    -0x50(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%r8
mov    -0x38(%rbp),%r9
lea    0xdfe(%rip),%rsi
call   1050 <sscanf@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x4,-0x3c(%rbp)
je     1256 <scsi_read_host_info+0x116>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
mov    0x2d94(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1266 <scsi_read_host_info+0x126>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scsi_read_host_info>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9,0x10(%rsp)
mov    %r8,%r12
mov    %rcx,%r13
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
movq   $0x0,0x20(%rsp)
movq   $0x0,0x8(%rsp)
movq   $0x0,0x18(%rsp)
lea    0xe7c(%rip),%rsi
lea    0xe7a(%rip),%rdx
lea    0x20(%rsp),%r8
mov    %rbx,%rcx
call   1060 <vdev_sysfs_device_path_from_subsystem_sysname@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    121d <scsi_read_host_info+0xdd>
lea    0x8(%rsp),%rsi
lea    0x18(%rsp),%rdx
mov    %r14,%rdi
call   1040 <vdev_sysfs_get_sysname@plt>
test   %eax,%eax
je     11c7 <scsi_read_host_info+0x87>
mov    %eax,%ebp
mov    (%rbx),%rdi
call   1030 <free@plt>
movq   $0x0,(%rbx)
jmp    121d <scsi_read_host_info+0xdd>
mov    0x8(%rsp),%rdi
lea    0xe3c(%rip),%rsi
mov    %r15,%rdx
mov    %r13,%rcx
mov    %r12,%r8
mov    0x10(%rsp),%r9
call   1050 <sscanf@plt>
cmp    $0x4,%eax
jne    11f9 <scsi_read_host_info+0xb9>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
xor    %ebp,%ebp
jmp    121d <scsi_read_host_info+0xdd>
mov    (%rbx),%rdi
call   1030 <free@plt>
movq   $0x0,(%rbx)
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    0x2dc7(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"scsi\00"
@.str.1 = [10 x i8] c"scsi_host\00"
@.str.2 = [12 x i8] c"%d:%d:%d:%d\00"
@EINVAL = global i32 0
define i32 @scsi_read_host_info(i8* %0, i8** %1, i32* %2, i32* %3, i32* %4, i32* %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8**
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca i32*
%14 = alloca i32
%15 = alloca i64
%16 = alloca i8*
%17 = alloca i64
store i8* %0, i8** %8
store i8** %1, i8*** %9
store i32* %2, i32** %10
store i32* %3, i32** %11
store i32* %4, i32** %12
store i32* %5, i32** %13
store i32 0, i32* %14
store i64 0, i64* %15
store i8* null, i8** %16
store i64 0, i64* %17
%18 = load i8*, i8** %8
%19 = load i8**, i8*** %9
%20 = call i32 @vdev_sysfs_device_path_from_subsystem_sysname(i8* %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8** %19, i64* %15)
store i32 %20, i32* %14
%21 = load i32, i32* %14
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %6
%24 = load i32, i32* %14
store i32 %24, i32* %7
br label %57
25: ; preds = %6
%26 = load i8*, i8** %8
%27 = call i32 @vdev_sysfs_get_sysname(i8* %26, i8** %16, i64* %17)
store i32 %27, i32* %14
%28 = load i32, i32* %14
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %36
30: ; preds = %25
%31 = load i8**, i8*** %9
%32 = load i8*, i8** %31
%33 = call i32 @free(i8* %32)
%34 = load i8**, i8*** %9
store i8* null, i8** %34
%35 = load i32, i32* %14
store i32 %35, i32* %7
br label %57
36: ; preds = %25
%37 = load i8*, i8** %16
%38 = load i32*, i32** %10
%39 = load i32*, i32** %11
%40 = load i32*, i32** %12
%41 = load i32*, i32** %13
%42 = call i32 @sscanf(i8* %37, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32* %38, i32* %39, i32* %40, i32* %41)
store i32 %42, i32* %14
%43 = load i32, i32* %14
%44 = icmp ne i32 %43, 4
br i1 %44, label %45, label %54
45: ; preds = %36
%46 = load i8**, i8*** %9
%47 = load i8*, i8** %46
%48 = call i32 @free(i8* %47)
%49 = load i8**, i8*** %9
store i8* null, i8** %49
%50 = load i8*, i8** %16
%51 = call i32 @free(i8* %50)
%52 = load i32, i32* @EINVAL
%53 = sub nsw i32 0, %52
store i32 %53, i32* %7
br label %57
54: ; preds = %36
%55 = load i8*, i8** %16
%56 = call i32 @free(i8* %55)
store i32 0, i32* %7
br label %57
57: ; preds = %54, %45, %30, %23
%58 = load i32, i32* %7
ret i32 %58
}

LLVM IR (-O2):
@.str = [5 x i8] c"scsi\00"
@.str.1 = [10 x i8] c"scsi_host\00"
@.str.2 = [12 x i8] c"%d:%d:%d:%d\00"
@EINVAL = global i32 0
define i32 @scsi_read_host_info(i8* %0, i8** %1, i32* %2, i32* %3, i32* %4, i32* %5) {
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = bitcast i64* %7 to i8*
store i64 0, i64* %7
%11 = bitcast i8** %8 to i8*
store i8* null, i8** %8
%12 = bitcast i64* %9 to i8*
store i64 0, i64* %9
%13 = call i32 @vdev_sysfs_device_path_from_subsystem_sysname(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8** %1, i64* %7)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %35
15: ; preds = %6
%16 = call i32 @vdev_sysfs_get_sysname(i8* %0, i8** %8, i64* %9)
%17 = icmp eq i32 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %15
%19 = load i8*, i8** %1
%20 = call i32 @free(i8* %19)
store i8* null, i8** %1
br label %35
21: ; preds = %15
%22 = load i8*, i8** %8
%23 = call i32 @sscanf(i8* %22, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32* %2, i32* %3, i32* %4, i32* %5)
%24 = icmp eq i32 %23, 4
br i1 %24, label %32, label %25
25: ; preds = %21
%26 = load i8*, i8** %1
%27 = call i32 @free(i8* %26)
store i8* null, i8** %1
%28 = load i8*, i8** %8
%29 = call i32 @free(i8* %28)
%30 = load i32, i32* @EINVAL
%31 = sub nsw i32 0, %30
br label %35
32: ; preds = %21
%33 = load i8*, i8** %8
%34 = call i32 @free(i8* %33)
br label %35
35: ; preds = %6, %32, %25, %18
%36 = phi i32 [ %16, %18 ], [ %31, %25 ], [ 0, %32 ], [ %13, %6 ]
ret i32 %36
}