#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_list_node {
  int dummy;
};
struct udev {
  int dummy;
};
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ dirname;
typedef int /*<<< orphan*/ device;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int strlen(int /*<<< orphan*/);
int /*<<< orphan*/ udev_get_dev_path(struct udev *);
int /*<<< orphan*/ udev_get_sys_path(struct udev *);
int /*<<< orphan*/ udev_list_entry_add(struct udev *, struct udev_list_node *,
                                       char *, int /*<<< orphan*/ *, int,
                                       int /*<<< orphan*/);
int /*<<< orphan*/ udev_log_null(struct udev *, char *, char *);
int /*<<< orphan*/ util_path_decode(char *);
int /*<<< orphan*/ util_path_encode(char *, int);
size_t util_strlcat(char *, char const *, int);
int /*<<< orphan*/ util_strlcpy(char *, int /*<<< orphan*/, int);
int name_index_get_devices(struct udev *udev, const char *name,
                           struct udev_list_node *dev_list) {
  char dirname[PATH_MAX];
  size_t devlen = strlen(udev_get_dev_path(udev)) + 1;
  size_t start;
  DIR *dir;
  int count = 0;
  util_strlcpy(dirname, udev_get_dev_path(udev), sizeof(dirname));
  start = util_strlcat(dirname, "/.udev/names/", sizeof(dirname));
  util_strlcat(dirname, &name[devlen], sizeof(dirname));
  util_path_encode(&dirname[start], sizeof(dirname) - start);
  dir = opendir(dirname);
  if (dir == NULL) {
    udev_log_null(udev, "no index directory '%s': %m\n", dirname);
    count = -1;
    goto out;
  }
  udev_log_null(udev, "found index directory '%s'\n", dirname);
  while (1) {
    struct dirent *ent;
    char device[1024];
    ent = readdir(dir);
    if (ent == NULL || ent->d_name[0] == '\0')
      break;
    if (ent->d_name[0] == '.')
      continue;
    util_strlcpy(device, udev_get_sys_path(udev), sizeof(device));
    util_strlcat(device, ent->d_name, sizeof(device));
    util_path_decode(device);
    udev_list_entry_add(udev, dev_list, device, NULL, 1, 0);
    count++;
  }
  closedir(dir);
out:
  return count;
}


x86 assembly (clang, -O0):
<name_index_get_devices>:
push   %rbp
mov    %rsp,%rbp
sub    $0x470,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x460(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x458(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   10a0 <udev_get_dev_path@plt>
mov    %eax,%edi
call   1080 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x8(%rbp),%rdi
call   10a0 <udev_get_dev_path@plt>
mov    -0x458(%rbp),%rdi
mov    %eax,%esi
mov    -0x460(%rbp),%rax
mov    %eax,%edx
call   10d0 <util_strlcpy@plt>
mov    -0x458(%rbp),%rdi
mov    -0x460(%rbp),%rax
mov    %eax,%edx
lea    0xd9c(%rip),%rsi
call   10e0 <util_strlcat@plt>
mov    -0x458(%rbp),%rdi
mov    %rax,%rcx
mov    -0x460(%rbp),%rax
mov    %rcx,-0x38(%rbp)
mov    -0x10(%rbp),%rsi
add    -0x30(%rbp),%rsi
mov    %eax,%edx
call   10e0 <util_strlcat@plt>
mov    -0x458(%rbp),%rdi
mov    -0x460(%rbp),%rax
add    -0x38(%rbp),%rdi
sub    -0x38(%rbp),%rax
mov    %eax,%esi
call   10c0 <util_path_encode@plt>
mov    -0x458(%rbp),%rdi
call   1070 <opendir@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    12e8 <name_index_get_devices+0x128>
mov    -0x458(%rbp),%rdx
mov    -0x8(%rbp),%rdi
lea    0xd37(%rip),%rsi
call   1060 <udev_log_null@plt>
movl   $0xffffffff,-0x44(%rbp)
jmp    13d2 <name_index_get_devices+0x212>
mov    -0x458(%rbp),%rdx
mov    -0x8(%rbp),%rdi
lea    0xd31(%rip),%rsi
call   1060 <udev_log_null@plt>
jmp    1304 <name_index_get_devices+0x144>
mov    -0x40(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     132f <name_index_get_devices+0x16f>
mov    -0x50(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1334 <name_index_get_devices+0x174>
jmp    13c9 <name_index_get_devices+0x209>
mov    -0x50(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    134c <name_index_get_devices+0x18c>
jmp    12ff <name_index_get_devices+0x13f>
lea    -0x450(%rbp),%rax
mov    %rax,-0x468(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <udev_get_sys_path@plt>
mov    -0x468(%rbp),%rdi
mov    %eax,%esi
mov    $0x400,%edx
call   10d0 <util_strlcpy@plt>
lea    -0x450(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    (%rax),%rsi
mov    $0x400,%edx
call   10e0 <util_strlcat@plt>
lea    -0x450(%rbp),%rdi
call   1030 <util_path_decode@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x450(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    $0x1,%r8d
xor    %r9d,%r9d
call   1050 <udev_list_entry_add@plt>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    12ff <name_index_get_devices+0x13f>
mov    -0x40(%rbp),%rdi
call   1090 <closedir@plt>
mov    -0x44(%rbp),%eax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<name_index_get_devices>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rdx,-0x38(%rbp)
mov    %rsi,-0x30(%rbp)
mov    %rdi,%r15
mov    0x2e0a(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
mov    %r12,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
call   10a0 <udev_get_dev_path@plt>
mov    %eax,%edi
call   1080 <strlen@plt>
movslq %eax,%r13
mov    %r15,%rdi
call   10a0 <udev_get_dev_path@plt>
mov    %rbx,%rdi
mov    %eax,%esi
mov    %r12d,%edx
call   10d0 <util_strlcpy@plt>
lea    0xdd8(%rip),%rsi
mov    %rbx,%rdi
mov    %r12d,%edx
call   10e0 <util_strlcat@plt>
mov    %rax,%r14
mov    -0x30(%rbp),%rax
lea    (%rax,%r13,1),%rsi
add    $0x1,%rsi
mov    %rbx,%rdi
mov    %r12d,%edx
call   10e0 <util_strlcat@plt>
lea    (%rbx,%r14,1),%rdi
sub    %r14d,%r12d
mov    %r12d,%esi
call   10c0 <util_path_encode@plt>
mov    %rbx,%rdi
call   1070 <opendir@plt>
test   %rax,%rax
je     131c <name_index_get_devices+0x15c>
mov    %rax,%r12
lea    0xdb4(%rip),%rsi
mov    %r15,%rdi
mov    %rbx,%rdx
call   1060 <udev_log_null@plt>
mov    %r12,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     1335 <name_index_get_devices+0x175>
mov    %rax,%r14
xor    %ebx,%ebx
lea    -0x440(%rbp),%r13
jmp    12c0 <name_index_get_devices+0x100>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   10b0 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     1337 <name_index_get_devices+0x177>
mov    (%r14),%rax
movzbl (%rax),%eax
cmp    $0x2e,%al
je     12b0 <name_index_get_devices+0xf0>
test   %al,%al
je     1337 <name_index_get_devices+0x177>
mov    %r15,%rdi
call   1040 <udev_get_sys_path@plt>
mov    %r13,%rdi
mov    %eax,%esi
mov    $0x400,%edx
call   10d0 <util_strlcpy@plt>
mov    (%r14),%rsi
mov    %r13,%rdi
mov    $0x400,%edx
call   10e0 <util_strlcat@plt>
mov    %r13,%rdi
call   1030 <util_path_decode@plt>
mov    %r15,%rdi
mov    -0x38(%rbp),%rsi
mov    %r13,%rdx
xor    %ecx,%ecx
mov    $0x1,%r8d
xor    %r9d,%r9d
call   1050 <udev_list_entry_add@plt>
add    $0x1,%ebx
jmp    12b0 <name_index_get_devices+0xf0>
lea    0xceb(%rip),%rsi
mov    %r15,%rdi
mov    %rbx,%rdx
call   1060 <udev_log_null@plt>
mov    $0xffffffff,%ebx
jmp    133f <name_index_get_devices+0x17f>
xor    %ebx,%ebx
mov    %r12,%rdi
call   1090 <closedir@plt>
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.udev = type { i32 }
%struct.udev_list_node = type { i32 }
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [14 x i8] c"/.udev/names/\00"
@.str.1 = [29 x i8] c"no index directory '%s': %m\0A\00"
@.str.2 = [28 x i8] c"found index directory '%s'\0A\00"
define i32 @name_index_get_devices(%struct.udev* %0, i8* %1, %struct.udev_list_node* %2) {
%4 = alloca %struct.udev*
%5 = alloca i8*
%6 = alloca %struct.udev_list_node*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32*
%12 = alloca i32
%13 = alloca %struct.dirent*
%14 = alloca [1024 x i8]
store %struct.udev* %0, %struct.udev** %4
store i8* %1, i8** %5
store %struct.udev_list_node* %2, %struct.udev_list_node** %6
%15 = load i32, i32* @PATH_MAX
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %7
%18 = alloca i8, i64 %16
store i64 %16, i64* %8
%19 = load %struct.udev*, %struct.udev** %4
%20 = call i32 @udev_get_dev_path(%struct.udev* %19)
%21 = call i32 @strlen(i32 %20)
%22 = add nsw i32 %21, 1
%23 = sext i32 %22 to i64
store i64 %23, i64* %9
store i32 0, i32* %12
%24 = load %struct.udev*, %struct.udev** %4
%25 = call i32 @udev_get_dev_path(%struct.udev* %24)
%26 = trunc i64 %16 to i32
%27 = call i32 @util_strlcpy(i8* %18, i32 %25, i32 %26)
%28 = trunc i64 %16 to i32
%29 = call i64 @util_strlcat(i8* %18, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %28)
store i64 %29, i64* %10
%30 = load i8*, i8** %5
%31 = load i64, i64* %9
%32 = getelementptr inbounds i8, i8* %30, i64 %31
%33 = trunc i64 %16 to i32
%34 = call i64 @util_strlcat(i8* %18, i8* %32, i32 %33)
%35 = load i64, i64* %10
%36 = getelementptr inbounds i8, i8* %18, i64 %35
%37 = load i64, i64* %10
%38 = sub i64 %16, %37
%39 = trunc i64 %38 to i32
%40 = call i32 @util_path_encode(i8* %36, i32 %39)
%41 = call i32* @opendir(i8* %18)
store i32* %41, i32** %11
%42 = load i32*, i32** %11
%43 = icmp eq i32* %42, null
br i1 %43, label %44, label %47
44: ; preds = %3
%45 = load %struct.udev*, %struct.udev** %4
%46 = call i32 @udev_log_null(%struct.udev* %45, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %18)
store i32 -1, i32* %12
br label %95
47: ; preds = %3
%48 = load %struct.udev*, %struct.udev** %4
%49 = call i32 @udev_log_null(%struct.udev* %48, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %18)
br label %50
50: ; preds = %74, %73, %47
br label %51
51: ; preds = %50
%52 = load i32*, i32** %11
%53 = call %struct.dirent* @readdir(i32* %52)
store %struct.dirent* %53, %struct.dirent** %13
%54 = load %struct.dirent*, %struct.dirent** %13
%55 = icmp eq %struct.dirent* %54, null
br i1 %55, label %64, label %56
56: ; preds = %51
%57 = load %struct.dirent*, %struct.dirent** %13
%58 = getelementptr inbounds %struct.dirent, %struct.dirent* %57, i32 0, i32 0
%59 = load i8*, i8** %58
%60 = getelementptr inbounds i8, i8* %59, i64 0
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = icmp eq i32 %62, 0
br i1 %63, label %64, label %65
64: ; preds = %56, %51
br label %92
65: ; preds = %56
%66 = load %struct.dirent*, %struct.dirent** %13
%67 = getelementptr inbounds %struct.dirent, %struct.dirent* %66, i32 0, i32 0
%68 = load i8*, i8** %67
%69 = getelementptr inbounds i8, i8* %68, i64 0
%70 = load i8, i8* %69
%71 = sext i8 %70 to i32
%72 = icmp eq i32 %71, 46
br i1 %72, label %73, label %74
73: ; preds = %65
br label %50
74: ; preds = %65
%75 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%76 = load %struct.udev*, %struct.udev** %4
%77 = call i32 @udev_get_sys_path(%struct.udev* %76)
%78 = call i32 @util_strlcpy(i8* %75, i32 %77, i32 1024)
%79 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%80 = load %struct.dirent*, %struct.dirent** %13
%81 = getelementptr inbounds %struct.dirent, %struct.dirent* %80, i32 0, i32 0
%82 = load i8*, i8** %81
%83 = call i64 @util_strlcat(i8* %79, i8* %82, i32 1024)
%84 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%85 = call i32 @util_path_decode(i8* %84)
%86 = load %struct.udev*, %struct.udev** %4
%87 = load %struct.udev_list_node*, %struct.udev_list_node** %6
%88 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%89 = call i32 @udev_list_entry_add(%struct.udev* %86, %struct.udev_list_node* %87, i8* %88, i32* null, i32 1, i32 0)
%90 = load i32, i32* %12
%91 = add nsw i32 %90, 1
store i32 %91, i32* %12
br label %50
92: ; preds = %64
%93 = load i32*, i32** %11
%94 = call i32 @closedir(i32* %93)
br label %95
95: ; preds = %92, %44
%96 = load i32, i32* %12
%97 = load i8*, i8** %7
ret i32 %96
}

LLVM IR (-O2):
%struct.udev = type { i32 }
%struct.udev_list_node = type { i32 }
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [14 x i8] c"/.udev/names/\00"
@.str.1 = [29 x i8] c"no index directory '%s': %m\0A\00"
@.str.2 = [28 x i8] c"found index directory '%s'\0A\00"
define i32 @name_index_get_devices(%struct.udev* %0, i8* %1, %struct.udev_list_node* %2) {
%4 = alloca [1024 x i8]
%5 = load i32, i32* @PATH_MAX
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = tail call i32 @udev_get_dev_path(%struct.udev* %0)
%9 = tail call i32 @strlen(i32 %8)
%10 = add nsw i32 %9, 1
%11 = sext i32 %10 to i64
%12 = tail call i32 @udev_get_dev_path(%struct.udev* %0)
%13 = call i32 @util_strlcpy(i8* %7, i32 %12, i32 %5)
%14 = call i64 @util_strlcat(i8* %7, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %5)
%15 = getelementptr inbounds i8, i8* %1, i64 %11
%16 = call i64 @util_strlcat(i8* %7, i8* %15, i32 %5)
%17 = getelementptr inbounds i8, i8* %7, i64 %14
%18 = trunc i64 %14 to i32
%19 = sub i32 %5, %18
%20 = call i32 @util_path_encode(i8* %17, i32 %19)
%21 = call i32* @opendir(i8* %7)
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %25
23: ; preds = %3
%24 = call i32 @udev_log_null(%struct.udev* %0, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %7)
br label %51
25: ; preds = %3
%26 = call i32 @udev_log_null(%struct.udev* %0, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %7)
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%28 = call %struct.dirent* @readdir(i32* %21)
%29 = icmp eq %struct.dirent* %28, null
br i1 %29, label %48, label %30
30: ; preds = %25, %44
%31 = phi %struct.dirent* [ %46, %44 ], [ %28, %25 ]
%32 = phi i32 [ %45, %44 ], [ 0, %25 ]
%33 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i64 0, i32 0
%34 = load i8*, i8** %33
%35 = load i8, i8* %34
switch i8 %35, label %36 [
i8 0, label %48
i8 46, label %44
]
36: ; preds = %30
%37 = call i32 @udev_get_sys_path(%struct.udev* %0)
%38 = call i32 @util_strlcpy(i8* %27, i32 %37, i32 1024)
%39 = load i8*, i8** %33
%40 = call i64 @util_strlcat(i8* %27, i8* %39, i32 1024)
%41 = call i32 @util_path_decode(i8* %27)
%42 = call i32 @udev_list_entry_add(%struct.udev* %0, %struct.udev_list_node* %2, i8* %27, i32* null, i32 1, i32 0)
%43 = add nsw i32 %32, 1
br label %44
44: ; preds = %30, %36
%45 = phi i32 [ %43, %36 ], [ %32, %30 ]
%46 = call %struct.dirent* @readdir(i32* %21)
%47 = icmp eq %struct.dirent* %46, null
br i1 %47, label %48, label %30
48: ; preds = %44, %30, %25
%49 = phi i32 [ 0, %25 ], [ %32, %30 ], [ %45, %44 ]
%50 = call i32 @closedir(i32* %21)
br label %51
51: ; preds = %48, %23
%52 = phi i32 [ -1, %23 ], [ %49, %48 ]
ret i32 %52
}