#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 /*<<< orphan*/ free(char *);
int /*<<< orphan*/ path_prepend(char **, char *, char *);
int skip_subsystem(char const *, char *, char **);
char *strchr(char *, char);
scalar_t__ strcmp(char *, char *);
char *strdup(char const *);
int vdev_sysfs_get_sysname(char const *, char **, size_t *);
int vdev_sysfs_uevent_read_key(char const *, char *, char **, size_t *);
int handle_usb(char const *parent, char **path, char **new_parent) {
  int rc = 0;
  char *devtype = NULL;
  size_t devtype_len = 0;
  char *sysname = NULL;
  size_t sysname_len = 0;
  char *port = NULL;
  rc = vdev_sysfs_uevent_read_key(parent, "DEVTYPE", &devtype, &devtype_len);
  if (rc != 0) {
    *new_parent = strdup(parent);
    if (*new_parent == NULL) {
      return -ENOMEM;
    }
    return 0;
  }
  if (strcmp(devtype, "usb_interface") != 0 &&
      strcmp(devtype, "usb_device") != 0) {
    free(devtype);
    *new_parent = strdup(parent);
    if (*new_parent == NULL) {
      return -ENOMEM;
    }
    return 0;
  }
  free(devtype);
  rc = vdev_sysfs_get_sysname(parent, &sysname, &sysname_len);
  if (rc != 0) {
    return rc;
  }
  port = strchr(sysname, '-');
  if (port == NULL) {
    *new_parent = strdup(parent);
    if (*new_parent == NULL) {
      return -ENOMEM;
    }
    free(sysname);
    return 0;
  }
  port++;
  rc = skip_subsystem(parent, "usb", new_parent);
  if (rc != 0) {
    free(sysname);
    return rc;
  }
  path_prepend(path, "usb-0:%s", port);
  free(sysname);
  return 0;
}


x86 assembly (clang, -O0):
<handle_usb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe32(%rip),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   10a0 <vdev_sysfs_uevent_read_key@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1228 <handle_usb+0xa8>
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    121c <handle_usb+0x9c>
xor    %eax,%eax
mov    0x2dce(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    13a2 <handle_usb+0x222>
movl   $0x0,-0x4(%rbp)
jmp    13a2 <handle_usb+0x222>
mov    -0x30(%rbp),%rdi
lea    0xdd5(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     12a5 <handle_usb+0x125>
mov    -0x30(%rbp),%rdi
lea    0xdc9(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     12a5 <handle_usb+0x125>
mov    -0x30(%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    1299 <handle_usb+0x119>
xor    %eax,%eax
mov    0x2d51(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    13a2 <handle_usb+0x222>
movl   $0x0,-0x4(%rbp)
jmp    13a2 <handle_usb+0x222>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
mov    -0x10(%rbp),%rdi
lea    -0x40(%rbp),%rsi
lea    -0x48(%rbp),%rdx
call   1080 <vdev_sysfs_get_sysname@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     12d7 <handle_usb+0x157>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13a2 <handle_usb+0x222>
mov    -0x40(%rbp),%rdi
mov    $0x2d,%esi
call   1060 <strchr@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    133d <handle_usb+0x1bd>
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    1328 <handle_usb+0x1a8>
xor    %eax,%eax
mov    0x2cc2(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    13a2 <handle_usb+0x222>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    13a2 <handle_usb+0x222>
mov    -0x50(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xcc9(%rip),%rsi
call   1050 <skip_subsystem@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     137e <handle_usb+0x1fe>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13a2 <handle_usb+0x222>
mov    -0x18(%rbp),%rdi
mov    -0x50(%rbp),%rdx
lea    0xc98(%rip),%rsi
call   1030 <path_prepend@plt>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_usb>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
movq   $0x0,(%rsp)
movq   $0x0,0x18(%rsp)
movq   $0x0,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0xe41(%rip),%rsi
mov    %rsp,%rdx
lea    0x18(%rsp),%rcx
call   10a0 <vdev_sysfs_uevent_read_key@plt>
test   %eax,%eax
je     11f6 <handle_usb+0x76>
mov    %rbx,%rdi
call   1090 <strdup@plt>
mov    %rax,(%r15)
xor    %ebp,%ebp
test   %rax,%rax
jne    12c5 <handle_usb+0x145>
mov    0x2df3(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
jmp    12c5 <handle_usb+0x145>
mov    (%rsp),%rdi
lea    0xe07(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     122b <handle_usb+0xab>
mov    (%rsp),%rdi
lea    0xe00(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     122b <handle_usb+0xab>
mov    (%rsp),%rdi
call   1040 <free@plt>
jmp    11d0 <handle_usb+0x50>
mov    (%rsp),%rdi
call   1040 <free@plt>
lea    0x8(%rsp),%rsi
lea    0x10(%rsp),%rdx
mov    %rbx,%rdi
call   1080 <vdev_sysfs_get_sysname@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    12c5 <handle_usb+0x145>
mov    0x8(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x2d,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     128a <handle_usb+0x10a>
mov    %rax,%r12
lea    0xdb4(%rip),%rsi
mov    %rbx,%rdi
mov    %r15,%rdx
call   1050 <skip_subsystem@plt>
test   %eax,%eax
je     12a3 <handle_usb+0x123>
mov    %eax,%ebp
mov    0x8(%rsp),%rdi
call   1040 <free@plt>
jmp    12c5 <handle_usb+0x145>
mov    %rbx,%rdi
call   1090 <strdup@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     11e6 <handle_usb+0x66>
mov    %rbp,%rdi
jmp    12be <handle_usb+0x13e>
add    $0x1,%r12
lea    0xd77(%rip),%rsi
mov    %r14,%rdi
mov    %r12,%rdx
call   1030 <path_prepend@plt>
mov    0x8(%rsp),%rdi
call   1040 <free@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [8 x i8] c"DEVTYPE\00"
@ENOMEM = global i32 0
@.str.1 = [14 x i8] c"usb_interface\00"
@.str.2 = [11 x i8] c"usb_device\00"
@.str.3 = [4 x i8] c"usb\00"
@.str.4 = [9 x i8] c"usb-0:%s\00"
define i32 @handle_usb(i8* %0, i8** %1, i8** %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%9 = bitcast i64* %5 to i8*
store i64 0, i64* %5
%10 = bitcast i8** %6 to i8*
store i8* null, i8** %6
%11 = bitcast i64* %7 to i8*
store i64 0, i64* %7
%12 = call i32 @vdev_sysfs_uevent_read_key(i8* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %4, i64* %5)
%13 = icmp eq i32 %12, 0
br i1 %13, label %20, label %14
14: ; preds = %3
%15 = call i8* @strdup(i8* %0)
store i8* %15, i8** %2
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %64
17: ; preds = %14
%18 = load i32, i32* @ENOMEM
%19 = sub nsw i32 0, %18
br label %64
20: ; preds = %3
%21 = load i8*, i8** %4
%22 = call i64 @strcmp(i8* %21, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %36, label %24
24: ; preds = %20
%25 = load i8*, i8** %4
%26 = call i64 @strcmp(i8* %25, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %36, label %28
28: ; preds = %24
%29 = load i8*, i8** %4
%30 = call i32 @free(i8* %29)
%31 = call i8* @strdup(i8* %0)
store i8* %31, i8** %2
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %64
33: ; preds = %28
%34 = load i32, i32* @ENOMEM
%35 = sub nsw i32 0, %34
br label %64
36: ; preds = %24, %20
%37 = load i8*, i8** %4
%38 = call i32 @free(i8* %37)
%39 = call i32 @vdev_sysfs_get_sysname(i8* %0, i8** %6, i64* %7)
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %64
41: ; preds = %36
%42 = load i8*, i8** %6
%43 = call i8* @strchr(i8* dereferenceable(1) %42, i8 signext 45)
%44 = icmp eq i8* %43, null
br i1 %44, label %45, label %53
45: ; preds = %41
%46 = call i8* @strdup(i8* %0)
store i8* %46, i8** %2
%47 = icmp eq i8* %46, null
br i1 %47, label %48, label %51
48: ; preds = %45
%49 = load i32, i32* @ENOMEM
%50 = sub nsw i32 0, %49
br label %64
51: ; preds = %45
%52 = call i32 @free(i8* %42)
br label %64
53: ; preds = %41
%54 = call i32 @skip_subsystem(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8** %2)
%55 = icmp eq i32 %54, 0
br i1 %55, label %59, label %56
56: ; preds = %53
%57 = load i8*, i8** %6
%58 = call i32 @free(i8* %57)
br label %64
59: ; preds = %53
%60 = getelementptr inbounds i8, i8* %43, i64 1
%61 = call i32 @path_prepend(i8** %1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* %60)
%62 = load i8*, i8** %6
%63 = call i32 @free(i8* %62)
br label %64
64: ; preds = %36, %28, %14, %59, %56, %51, %48, %33, %17
%65 = phi i32 [ %19, %17 ], [ %35, %33 ], [ %50, %48 ], [ 0, %51 ], [ %54, %56 ], [ 0, %59 ], [ 0, %14 ], [ 0, %28 ], [ %39, %36 ]
ret i32 %65
}