#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 {
  int dummy;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ check_device(struct dirent *, char *, int *, int *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int find_device(char *vidpid, int *bus, int *address) {
  DIR *dir;
  struct dirent *de;
  char buf[16];
  int found;
  if (!(dir = opendir("/sys/bus/usb/devices")))
    return 0;
  found = 0;
  while ((de = readdir(dir))) {
    if (check_device(de, vidpid, bus, address)) {
      found = 1;
      break;
    }
  }
  closedir(dir);
  return found;
}


x86 assembly (clang, -O0):
<find_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    0xea5(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    117a <find_device+0x3a>
movl   $0x0,-0x4(%rbp)
jmp    11d7 <find_device+0x97>
movl   $0x0,-0x44(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     11c8 <find_device+0x88>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1060 <check_device@plt>
cmp    $0x0,%rax
je     11c3 <find_device+0x83>
movl   $0x1,-0x44(%rbp)
jmp    11c8 <find_device+0x88>
jmp    1181 <find_device+0x41>
mov    -0x28(%rbp),%rdi
call   1040 <closedir@plt>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_device>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
lea    0xea8(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     119a <find_device+0x5a>
mov    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1050 <readdir@plt>
test   %rax,%rax
je     119e <find_device+0x5e>
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1060 <check_device@plt>
test   %rax,%rax
je     1170 <find_device+0x30>
mov    $0x1,%ebp
jmp    11a0 <find_device+0x60>
xor    %ebp,%ebp
jmp    11a8 <find_device+0x68>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1040 <closedir@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [21 x i8] c"/sys/bus/usb/devices\00"
define i32 @find_device(i8* %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca %struct.dirent*
%10 = alloca [16 x i8]
%11 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
%12 = call i32* @opendir(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %8
%13 = icmp ne i32* %12, null
br i1 %13, label %15, label %14
14: ; preds = %3
store i32 0, i32* %4
br label %33
15: ; preds = %3
store i32 0, i32* %11
br label %16
16: ; preds = %28, %15
%17 = load i32*, i32** %8
%18 = call %struct.dirent* @readdir(i32* %17)
store %struct.dirent* %18, %struct.dirent** %9
%19 = icmp ne %struct.dirent* %18, null
br i1 %19, label %20, label %29
20: ; preds = %16
%21 = load %struct.dirent*, %struct.dirent** %9
%22 = load i8*, i8** %5
%23 = load i32*, i32** %6
%24 = load i32*, i32** %7
%25 = call i64 @check_device(%struct.dirent* %21, i8* %22, i32* %23, i32* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %20
store i32 1, i32* %11
br label %29
28: ; preds = %20
br label %16
29: ; preds = %27, %16
%30 = load i32*, i32** %8
%31 = call i32 @closedir(i32* %30)
%32 = load i32, i32* %11
store i32 %32, i32* %4
br label %33
33: ; preds = %29, %14
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [21 x i8] c"/sys/bus/usb/devices\00"
define i32 @find_device(i8* %0, i32* %1, i32* %2) {
%4 = tail call i32* @opendir(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %15, label %6
6: ; preds = %3, %9
%7 = tail call %struct.dirent* @readdir(i32* %4)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %12, label %9
9: ; preds = %6
%10 = tail call i64 @check_device(%struct.dirent* %7, i8* %0, i32* %1, i32* %2)
%11 = icmp eq i64 %10, 0
br i1 %11, label %6, label %12
12: ; preds = %9, %6
%13 = phi i32 [ 0, %6 ], [ 1, %9 ]
%14 = tail call i32 @closedir(i32* %4)
br label %15
15: ; preds = %3, %12
%16 = phi i32 [ %13, %12 ], [ 0, %3 ]
ret i32 %16
}