#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*/ O_RDONLY;
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/skel0";
  static char usbfs_path_1[] = "/proc/bus/usb/devices";
  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);
  return NULL;
}


x86 assembly (clang, -O0):
<usbfs_dir_find>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0x2cc1(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2e8f(%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     1191 <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    11b7 <usbfs_dir_find+0x87>
jmp    1196 <usbfs_dir_find+0x66>
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
cmp    0x2ebf(%rip),%rax
jne    1143 <usbfs_dir_find+0x13>
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    0x2ea5(%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     1160 <usbfs_dir_find+0x30>
lea    0x2ca2(%rip),%rbx
jmp    1183 <usbfs_dir_find+0x53>
mov    (%r14),%esi
lea    0x2ee6(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1040 <close@plt>
test   %ebx,%ebx
js     119e <usbfs_dir_find+0x6e>
lea    0x2c85(%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
xor    %eax,%eax
jmp    1196 <usbfs_dir_find+0x66>

LLVM IR (-O0):
@usbfs_dir_find.usbfs_path_0 = internal global [11 x i8] c"/dev/skel0\00"
@usbfs_dir_find.usbfs_path_1 = internal global [22 x i8] c"/proc/bus/usb/devices\00"
@usbfs_dir_find.usbfs_paths = internal constant [2 x i8*] [i8* getelementptr inbounds ([11 x i8], [11 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
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 %27
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
store i8* null, i8** %1
br label %27
27: ; preds = %26, %13
%28 = load i8*, i8** %1
ret i8* %28
}

LLVM IR (-O2):
@usbfs_dir_find.usbfs_path_0 = internal global [11 x i8] c"/dev/skel0\00"
@usbfs_dir_find.usbfs_path_1 = internal global [22 x i8] c"/proc/bus/usb/devices\00"
@usbfs_dir_find.usbfs_paths = internal unnamed_addr constant [2 x i8*] [i8* getelementptr inbounds ([11 x i8], [11 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
define i8* @usbfs_dir_find() {
%1 = load i32, i32* @O_RDONLY
%2 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 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 %15
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, %5
%16 = phi i8* [ %9, %5 ], [ null, %10 ]
ret i8* %16
}