#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 /*<<< orphan*/ F_OK;
int /*<<< orphan*/ O_RDONLY;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int open(char *const, int /*<<< orphan*/);
char *strrchr(char *const, char);
const char *usbfs_dir_find(void) {
  static char usbfs_path_0[] = "/dev/usb/devices";
  static char usbfs_path_1[] = "/proc/bus/usb/devices";
  static char udev_usb_path[] = "/dev/bus/usb";
  static char *const usbfs_paths[] = {usbfs_path_0, usbfs_path_1};
  static char *const *end =
      usbfs_paths + sizeof usbfs_paths / sizeof *usbfs_paths;
  char *const *it = usbfs_paths;
  do {
    int fd = open(*it, O_RDONLY);
    close(fd);
    if (fd >= 0) {
      strrchr(*it, '/')[0] = '\0';
      return *it;
    }
  } while (++it != end);
  if (access(udev_usb_path, F_OK) == 0)
    return udev_usb_path;
  return NULL;
}


x86 assembly (clang, -O0):
<usbfs_dir_find>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0x2cb1(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2e87(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x0,-0x14(%rbp)
jl     11a1 <usbfs_dir_find+0x61>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    $0x2f,%esi
call   1030 <strrchr@plt>
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    11f6 <usbfs_dir_find+0xb6>
jmp    11a6 <usbfs_dir_find+0x66>
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
cmp    0x2edf(%rip),%rax
jne    1153 <usbfs_dir_find+0x13>
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
lea    0x2eb7(%rip),%rdi
call   1060 <access@plt>
cmp    $0x0,%rax
jne    11ee <usbfs_dir_find+0xae>
lea    0x2ea1(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11f6 <usbfs_dir_find+0xb6>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<usbfs_dir_find>:
push   %r14
push   %rbx
push   %rax
mov    0x2e9d(%rip),%r14
mov    (%r14),%esi
lea    0x2efb(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1040 <close@plt>
test   %ebx,%ebx
js     1170 <usbfs_dir_find+0x30>
lea    0x2c92(%rip),%rbx
jmp    1193 <usbfs_dir_find+0x53>
mov    (%r14),%esi
lea    0x2ef6(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1040 <close@plt>
test   %ebx,%ebx
js     11ae <usbfs_dir_find+0x6e>
lea    0x2c75(%rip),%rbx
mov    (%rbx),%rdi
mov    $0x2f,%esi
call   1030 <strrchr@plt>
movb   $0x0,(%rax)
mov    (%rbx),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
lea    0x2ec8(%rip),%rbx
mov    %rbx,%rdi
call   1060 <access@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
cmove  %rbx,%rax
jmp    11a6 <usbfs_dir_find+0x66>

LLVM IR (-O0):
@usbfs_dir_find.usbfs_path_0 = internal global [17 x i8] c"/dev/usb/devices\00"
@usbfs_dir_find.usbfs_path_1 = internal global [22 x i8] c"/proc/bus/usb/devices\00"
@usbfs_dir_find.udev_usb_path = internal global [13 x i8] c"/dev/bus/usb\00"
@usbfs_dir_find.usbfs_paths = internal constant [2 x i8*] [i8* getelementptr inbounds ([17 x i8], [17 x i8]* @usbfs_dir_find.usbfs_path_0, i32 0, i32 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @usbfs_dir_find.usbfs_path_1, i32 0, i32 0)]
@usbfs_dir_find.end = internal global i8** bitcast (i8* getelementptr (i8, i8* bitcast ([2 x i8*]* @usbfs_dir_find.usbfs_paths to i8*), i64 16) to i8**)
@O_RDONLY = global i32 0
@F_OK = global i32 0
define i8* @usbfs_dir_find() {
%1 = alloca i8*
%2 = alloca i8**
%3 = alloca i32
store i8** getelementptr inbounds ([2 x i8*], [2 x i8*]* @usbfs_dir_find.usbfs_paths, i64 0, i64 0), i8*** %2
br label %4
4: ; preds = %21, %0
%5 = load i8**, i8*** %2
%6 = load i8*, i8** %5
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %6, i32 %7)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = call i32 @close(i32 %9)
%11 = load i32, i32* %3
%12 = icmp sge i32 %11, 0
br i1 %12, label %13, label %20
13: ; preds = %4
%14 = load i8**, i8*** %2
%15 = load i8*, i8** %14
%16 = call i8* @strrchr(i8* %15, i8 signext 47)
%17 = getelementptr inbounds i8, i8* %16, i64 0
store i8 0, i8* %17
%18 = load i8**, i8*** %2
%19 = load i8*, i8** %18
store i8* %19, i8** %1
br label %32
20: ; preds = %4
br label %21
21: ; preds = %20
%22 = load i8**, i8*** %2
%23 = getelementptr inbounds i8*, i8** %22, i32 1
store i8** %23, i8*** %2
%24 = load i8**, i8*** @usbfs_dir_find.end
%25 = icmp ne i8** %23, %24
br i1 %25, label %4, label %26
26: ; preds = %21
%27 = load i32, i32* @F_OK
%28 = call i64 @access(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @usbfs_dir_find.udev_usb_path, i64 0, i64 0), i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %26
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @usbfs_dir_find.udev_usb_path, i64 0, i64 0), i8** %1
br label %32
31: ; preds = %26
store i8* null, i8** %1
br label %32
32: ; preds = %31, %30, %13
%33 = load i8*, i8** %1
ret i8* %33
}

LLVM IR (-O2):
@usbfs_dir_find.usbfs_path_0 = internal global [17 x i8] c"/dev/usb/devices\00"
@usbfs_dir_find.usbfs_path_1 = internal global [22 x i8] c"/proc/bus/usb/devices\00"
@usbfs_dir_find.udev_usb_path = internal global [13 x i8] c"/dev/bus/usb\00"
@usbfs_dir_find.usbfs_paths = internal unnamed_addr constant [2 x i8*] [i8* getelementptr inbounds ([17 x i8], [17 x i8]* @usbfs_dir_find.usbfs_path_0, i32 0, i32 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @usbfs_dir_find.usbfs_path_1, i32 0, i32 0)]
@O_RDONLY = global i32 0
@F_OK = global i32 0
define i8* @usbfs_dir_find() {
%1 = load i32, i32* @O_RDONLY
%2 = tail call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @usbfs_dir_find.usbfs_path_0, i64 0, i64 0), i32 %1)
%3 = tail call i32 @close(i32 %2)
%4 = icmp sgt i32 %2, -1
br i1 %4, label %5, label %10
5: ; preds = %10, %0
%6 = phi i8** [ getelementptr inbounds ([2 x i8*], [2 x i8*]* @usbfs_dir_find.usbfs_paths, i64 0, i64 0), %0 ], [ getelementptr inbounds ([2 x i8*], [2 x i8*]* @usbfs_dir_find.usbfs_paths, i64 0, i64 1), %10 ]
%7 = load i8*, i8** %6
%8 = tail call i8* @strrchr(i8* dereferenceable(1) %7, i8 signext 47)
store i8 0, i8* %8
%9 = load i8*, i8** %6
br label %20
10: ; preds = %0
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @usbfs_dir_find.usbfs_path_1, i64 0, i64 0), i32 %11)
%13 = tail call i32 @close(i32 %12)
%14 = icmp sgt i32 %12, -1
br i1 %14, label %5, label %15
15: ; preds = %10
%16 = load i32, i32* @F_OK
%17 = tail call i64 @access(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @usbfs_dir_find.udev_usb_path, i64 0, i64 0), i32 %16)
%18 = icmp eq i64 %17, 0
%19 = select i1 %18, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @usbfs_dir_find.udev_usb_path, i64 0, i64 0), i8* null
br label %20
20: ; preds = %5, %15
%21 = phi i8* [ %19, %15 ], [ %9, %5 ]
ret i8* %21
}