#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 dirent {
  char const *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int NAME_MAX;
int PATH_MAX;
int /*<<< orphan*/ R_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char const *);
int /*<<< orphan*/ strlen(char *);
scalar_t__ strncmp(char const *, char *, int /*<<< orphan*/);
int find_dev_nod_path(const char *d_name, char *dev_path, size_t len) {
  DIR *dir;
  struct dirent *de;
  char name[NAME_MAX];
  char path[PATH_MAX];
  int r = -1;
  snprintf(name, NAME_MAX, "%s:", d_name);
  dir = opendir("/sys/bus/usb/devices");
  while (((de = readdir(dir)) != NULL) && (r != 0)) {
    DIR *subdir;
    if (strncmp(de->d_name, name, strlen(name)) != 0)
      continue;
    snprintf(path, PATH_MAX,
             "/sys/bus/usb/devices"
             "/%s/tty",
             de->d_name);
    if (access(path, R_OK) != 0)
      continue;
    subdir = opendir(path);
    while (((de = readdir(subdir)) != NULL) && (r != 0)) {
      if (strncmp(de->d_name, "tty", strlen("tty")) != 0)
        continue;
      r = 0;
      snprintf(dev_path, len, "/dev/%s", de->d_name);
    }
    closedir(subdir);
  }
  closedir(dir);
  return r;
}


x86 assembly (clang, -O0):
<find_dev_nod_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x60(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x38(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
movl   $0xffffffff,-0x44(%rbp)
mov    0x2de2(%rip),%rax
movslq (%rax),%rsi
mov    -0x8(%rbp),%rcx
lea    0xe0c(%rip),%rdx
call   1060 <snprintf@plt>
lea    0xe04(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,%rcx
mov    %rcx,-0x28(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x61(%rbp)
je     1232 <find_dev_nod_path+0xc2>
cmpl   $0x0,-0x44(%rbp)
setne  %al
mov    %al,-0x61(%rbp)
mov    -0x61(%rbp),%al
test   $0x1,%al
jne    1242 <find_dev_nod_path+0xd2>
jmp    1369 <find_dev_nod_path+0x1f9>
mov    -0x60(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x70(%rbp)
call   1050 <strlen@plt>
mov    -0x70(%rbp),%rdi
mov    -0x60(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     1274 <find_dev_nod_path+0x104>
jmp    1209 <find_dev_nod_path+0x99>
mov    -0x58(%rbp),%rdi
mov    0x2d71(%rip),%rax
movslq (%rax),%rsi
mov    -0x28(%rbp),%rax
mov    (%rax),%rcx
lea    0xd89(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x58(%rbp),%rdi
mov    0x2d38(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
cmp    $0x0,%rax
je     12b6 <find_dev_nod_path+0x146>
jmp    1209 <find_dev_nod_path+0x99>
mov    -0x58(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,%rcx
mov    %rcx,-0x28(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x71(%rbp)
je     12ec <find_dev_nod_path+0x17c>
cmpl   $0x0,-0x44(%rbp)
setne  %al
mov    %al,-0x71(%rbp)
mov    -0x71(%rbp),%al
test   $0x1,%al
jne    12fc <find_dev_nod_path+0x18c>
jmp    135b <find_dev_nod_path+0x1eb>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x80(%rbp)
lea    0xd23(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x80(%rbp),%rdi
mov    %eax,%edx
lea    0xd11(%rip),%rsi
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     1334 <find_dev_nod_path+0x1c4>
jmp    12c3 <find_dev_nod_path+0x153>
movl   $0x0,-0x44(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    (%rax),%rcx
lea    0xce4(%rip),%rdx
call   1060 <snprintf@plt>
jmp    12c3 <find_dev_nod_path+0x153>
mov    -0x50(%rbp),%rdi
call   1070 <closedir@plt>
jmp    1209 <find_dev_nod_path+0x99>
mov    -0x20(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x44(%rbp),%eax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_dev_nod_path>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,-0x48(%rbp)
mov    %rsi,-0x40(%rbp)
mov    %rdi,%rcx
mov    0x2e35(%rip),%rax
movslq (%rax),%rsi
mov    %esi,%eax
mov    %rsp,%r13
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
mov    0x2e40(%rip),%r15
mov    (%r15),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
lea    0xe35(%rip),%rdx
mov    %r13,%rdi
call   1060 <snprintf@plt>
lea    0xe2a(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x38(%rbp)
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     131c <find_dev_nod_path+0x1ac>
mov    %rax,%rbx
mov    %r12,-0x50(%rbp)
mov    %r13,-0x30(%rbp)
nop
mov    (%rbx),%r14
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %r14,%rdi
mov    %r13,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
jne    1300 <find_dev_nod_path+0x190>
movslq (%r15),%rsi
mov    (%rbx),%rcx
mov    %r12,%rdi
lea    0xde2(%rip),%rdx
call   1060 <snprintf@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   1090 <access@plt>
test   %rax,%rax
jne    1300 <find_dev_nod_path+0x190>
mov    %r12,%rdi
call   1040 <opendir@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   1080 <readdir@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     12eb <find_dev_nod_path+0x17b>
mov    %rax,%rbx
nopw   0x0(%rax,%rax,1)
mov    (%rbx),%r15
lea    0xda7(%rip),%r13
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %r15,%rdi
mov    %r13,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     12be <find_dev_nod_path+0x14e>
mov    %rax,%r15
mov    %r12,%rdi
call   1080 <readdir@plt>
test   %r15,%r15
je     12e0 <find_dev_nod_path+0x170>
mov    %rax,%rbx
test   %rax,%rax
jne    1280 <find_dev_nod_path+0x110>
jmp    12e0 <find_dev_nod_path+0x170>
mov    (%rbx),%rcx
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rsi
lea    0xd65(%rip),%rdx
call   1060 <snprintf@plt>
mov    %r12,%rdi
call   1080 <readdir@plt>
xor    %r14d,%r14d
mov    0x2d09(%rip),%r15
mov    -0x30(%rbp),%r13
mov    %r12,%rdi
call   1070 <closedir@plt>
mov    -0x50(%rbp),%r12
nopw   0x0(%rax,%rax,1)
mov    -0x38(%rbp),%rdi
call   1080 <readdir@plt>
test   %r14d,%r14d
je     1322 <find_dev_nod_path+0x1b2>
mov    %rax,%rbx
test   %rax,%rax
jne    1200 <find_dev_nod_path+0x90>
jmp    1322 <find_dev_nod_path+0x1b2>
mov    $0xffffffff,%r14d
mov    -0x38(%rbp),%rdi
call   1070 <closedir@plt>
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@NAME_MAX = global i32 0
@PATH_MAX = global i32 0
@.str = [4 x i8] c"%s:\00"
@.str.1 = [21 x i8] c"/sys/bus/usb/devices\00"
@.str.2 = [28 x i8] c"/sys/bus/usb/devices/%s/tty\00"
@R_OK = global i32 0
@.str.3 = [4 x i8] c"tty\00"
@.str.4 = [8 x i8] c"/dev/%s\00"
define i32 @find_dev_nod_path(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32*
%8 = alloca %struct.dirent*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%14 = load i32, i32* @NAME_MAX
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %9
%17 = alloca i8, i64 %15
store i64 %15, i64* %10
%18 = load i32, i32* @PATH_MAX
%19 = zext i32 %18 to i64
%20 = alloca i8, i64 %19
store i64 %19, i64* %11
store i32 -1, i32* %12
%21 = load i32, i32* @NAME_MAX
%22 = sext i32 %21 to i64
%23 = load i8*, i8** %4
%24 = call i32 @snprintf(i8* %17, i64 %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = call i32* @opendir(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i32* %25, i32** %7
br label %26
26: ; preds = %80, %53, %42, %3
%27 = load i32*, i32** %7
%28 = call %struct.dirent* @readdir(i32* %27)
store %struct.dirent* %28, %struct.dirent** %8
%29 = icmp ne %struct.dirent* %28, null
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = load i32, i32* %12
%32 = icmp ne i32 %31, 0
br label %33
33: ; preds = %30, %26
%34 = phi i1 [ false, %26 ], [ %32, %30 ]
br i1 %34, label %35, label %83
35: ; preds = %33
%36 = load %struct.dirent*, %struct.dirent** %8
%37 = getelementptr inbounds %struct.dirent, %struct.dirent* %36, i32 0, i32 0
%38 = load i8*, i8** %37
%39 = call i32 @strlen(i8* %17)
%40 = call i64 @strncmp(i8* %38, i8* %17, i32 %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %35
br label %26
43: ; preds = %35
%44 = load i32, i32* @PATH_MAX
%45 = sext i32 %44 to i64
%46 = load %struct.dirent*, %struct.dirent** %8
%47 = getelementptr inbounds %struct.dirent, %struct.dirent* %46, i32 0, i32 0
%48 = load i8*, i8** %47
%49 = call i32 @snprintf(i8* %20, i64 %45, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %48)
%50 = load i32, i32* @R_OK
%51 = call i64 @access(i8* %20, i32 %50)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %43
br label %26
54: ; preds = %43
%55 = call i32* @opendir(i8* %20)
store i32* %55, i32** %13
br label %56
56: ; preds = %73, %72, %54
%57 = load i32*, i32** %13
%58 = call %struct.dirent* @readdir(i32* %57)
store %struct.dirent* %58, %struct.dirent** %8
%59 = icmp ne %struct.dirent* %58, null
br i1 %59, label %60, label %63
60: ; preds = %56
%61 = load i32, i32* %12
%62 = icmp ne i32 %61, 0
br label %63
63: ; preds = %60, %56
%64 = phi i1 [ false, %56 ], [ %62, %60 ]
br i1 %64, label %65, label %80
65: ; preds = %63
%66 = load %struct.dirent*, %struct.dirent** %8
%67 = getelementptr inbounds %struct.dirent, %struct.dirent* %66, i32 0, i32 0
%68 = load i8*, i8** %67
%69 = call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%70 = call i64 @strncmp(i8* %68, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %69)
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %73
72: ; preds = %65
br label %56
73: ; preds = %65
store i32 0, i32* %12
%74 = load i8*, i8** %5
%75 = load i64, i64* %6
%76 = load %struct.dirent*, %struct.dirent** %8
%77 = getelementptr inbounds %struct.dirent, %struct.dirent* %76, i32 0, i32 0
%78 = load i8*, i8** %77
%79 = call i32 @snprintf(i8* %74, i64 %75, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %78)
br label %56
80: ; preds = %63
%81 = load i32*, i32** %13
%82 = call i32 @closedir(i32* %81)
br label %26
83: ; preds = %33
%84 = load i32*, i32** %7
%85 = call i32 @closedir(i32* %84)
%86 = load i32, i32* %12
%87 = load i8*, i8** %9
ret i32 %86
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@NAME_MAX = global i32 0
@PATH_MAX = global i32 0
@.str = [4 x i8] c"%s:\00"
@.str.1 = [21 x i8] c"/sys/bus/usb/devices\00"
@.str.2 = [28 x i8] c"/sys/bus/usb/devices/%s/tty\00"
@R_OK = global i32 0
@.str.3 = [4 x i8] c"tty\00"
@.str.4 = [8 x i8] c"/dev/%s\00"
define i32 @find_dev_nod_path(i8* %0, i8* %1, i64 %2) {
%4 = load i32, i32* @NAME_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = load i32, i32* @PATH_MAX
%8 = zext i32 %7 to i64
%9 = alloca i8, i64 %8
%10 = sext i32 %4 to i64
%11 = call i32 @snprintf(i8* %6, i64 %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %0)
%12 = call i32* @opendir(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%13 = call %struct.dirent* @readdir(i32* %12)
%14 = icmp eq %struct.dirent* %13, null
br i1 %14, label %59, label %15
15: ; preds = %3, %53
%16 = phi %struct.dirent* [ %55, %53 ], [ %13, %3 ]
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i64 0, i32 0
%18 = load i8*, i8** %17
%19 = call i32 @strlen(i8* %6)
%20 = call i64 @strncmp(i8* %18, i8* %6, i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %53
22: ; preds = %15
%23 = load i32, i32* @PATH_MAX
%24 = sext i32 %23 to i64
%25 = load i8*, i8** %17
%26 = call i32 @snprintf(i8* %9, i64 %24, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %25)
%27 = load i32, i32* @R_OK
%28 = call i64 @access(i8* %9, i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %53
30: ; preds = %22
%31 = call i32* @opendir(i8* %9)
%32 = call %struct.dirent* @readdir(i32* %31)
%33 = icmp eq %struct.dirent* %32, null
br i1 %33, label %50, label %34
34: ; preds = %30, %46
%35 = phi %struct.dirent* [ %47, %46 ], [ %32, %30 ]
%36 = getelementptr inbounds %struct.dirent, %struct.dirent* %35, i64 0, i32 0
%37 = load i8*, i8** %36
%38 = call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%39 = call i64 @strncmp(i8* %37, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %34
%42 = getelementptr inbounds %struct.dirent, %struct.dirent* %35, i64 0, i32 0
%43 = load i8*, i8** %42
%44 = call i32 @snprintf(i8* %1, i64 %2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %43)
%45 = call %struct.dirent* @readdir(i32* %31)
br label %50
46: ; preds = %34
%47 = call %struct.dirent* @readdir(i32* %31)
%48 = icmp eq %struct.dirent* %47, null
%49 = or i1 %40, %48
br i1 %49, label %50, label %34
50: ; preds = %46, %41, %30
%51 = phi i32 [ -1, %30 ], [ 0, %41 ], [ -1, %46 ]
%52 = call i32 @closedir(i32* %31)
br label %53
53: ; preds = %22, %15, %50
%54 = phi i32 [ %51, %50 ], [ -1, %15 ], [ -1, %22 ]
%55 = call %struct.dirent* @readdir(i32* %12)
%56 = icmp ne %struct.dirent* %55, null
%57 = icmp ne i32 %54, 0
%58 = and i1 %57, %56
br i1 %58, label %15, label %59
59: ; preds = %53, %3
%60 = phi i32 [ -1, %3 ], [ %54, %53 ]
%61 = call i32 @closedir(i32* %12)
ret i32 %60
}