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

/* Variables and functions */
scalar_t__ DT_DIR;
int /*<<< orphan*/ asprintf(char **, char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *opendir(char *);
int read_from_file(struct dirent const *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int strlen(int /*<<< orphan*/);
bool detect_2200_ohci(const struct dirent *parent_dir) {
  bool ohci_found = false;
  char *parent_dir_name;
  DIR *dir;
  const struct dirent *dev_dir;
  asprintf(&parent_dir_name, "%s/%s", "/sys/bus/pci/devices",
           parent_dir->d_name);
  dir = opendir(parent_dir_name);
  if (dir) {
    for (;;) {
      dev_dir = readdir(dir);
      if (dev_dir == NULL)
        break;
      if (dev_dir->d_type != DT_DIR || strlen(dev_dir->d_name) < 10)
        continue;
      ohci_found = read_from_file(dev_dir, "vendor") == 0x104c &&
                   read_from_file(dev_dir, "device") == 0x8235;
      if (ohci_found)
        break;
    }
    closedir(dir);
  }
  free(parent_dir_name);
  return ohci_found;
}


x86 assembly (clang, -O0):
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%ecx
lea    -0x18(%rbp),%rdi
lea    0xe6b(%rip),%rsi
lea    0xe6a(%rip),%rdx
call   1090 <asprintf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1272 <detect_2200_ohci+0x102>
jmp    11be <detect_2200_ohci+0x4e>
mov    -0x20(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11db <detect_2200_ohci+0x6b>
jmp    1269 <detect_2200_ohci+0xf9>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    0x2df7(%rip),%rcx
cmp    (%rcx),%rax
jne    1207 <detect_2200_ohci+0x97>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%edi
call   1060 <strlen@plt>
cmp    $0xa,%eax
jge    120c <detect_2200_ohci+0x9c>
jmp    11be <detect_2200_ohci+0x4e>
mov    -0x28(%rbp),%rdi
lea    0xe04(%rip),%rsi
call   1040 <read_from_file@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x104c,%ecx
mov    %al,-0x29(%rbp)
jne    124a <detect_2200_ohci+0xda>
mov    -0x28(%rbp),%rdi
lea    0xde8(%rip),%rsi
call   1040 <read_from_file@plt>
cmp    $0x8235,%eax
sete   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1264 <detect_2200_ohci+0xf4>
jmp    1269 <detect_2200_ohci+0xf9>
jmp    11be <detect_2200_ohci+0x4e>
mov    -0x20(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0xc(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x8(%rdi),%ecx
lea    0xe7b(%rip),%rsi
lea    0xe7a(%rip),%rdx
mov    %rsp,%rdi
call   1090 <asprintf@plt>
mov    (%rsp),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     123b <detect_2200_ohci+0xcb>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <readdir@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     1231 <detect_2200_ohci+0xc1>
mov    %rax,%rbp
mov    0x2e1d(%rip),%r13
lea    0xe51(%rip),%r12
lea    0xe51(%rip),%r15
jmp    11f0 <detect_2200_ohci+0x80>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1231 <detect_2200_ohci+0xc1>
mov    0x0(%rbp),%rax
cmp    0x0(%r13),%rax
jne    11e0 <detect_2200_ohci+0x70>
mov    0x8(%rbp),%edi
call   1060 <strlen@plt>
cmp    $0x9,%eax
jle    11e0 <detect_2200_ohci+0x70>
mov    %rbp,%rdi
mov    %r12,%rsi
call   1040 <read_from_file@plt>
cmp    $0x104c,%eax
jne    11e0 <detect_2200_ohci+0x70>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1040 <read_from_file@plt>
cmp    $0x8235,%eax
jne    11e0 <detect_2200_ohci+0x70>
mov    $0x1,%r14d
mov    %rbx,%rdi
call   1070 <closedir@plt>
jmp    123e <detect_2200_ohci+0xce>
xor    %r14d,%r14d
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i64, i32 }
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [21 x i8] c"/sys/bus/pci/devices\00"
@DT_DIR = global i64 0
@.str.2 = [7 x i8] c"vendor\00"
@.str.3 = [7 x i8] c"device\00"
define i32 @detect_2200_ohci(%struct.dirent* %0) {
%2 = alloca %struct.dirent*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca %struct.dirent*
store %struct.dirent* %0, %struct.dirent** %2
store i32 0, i32* %3
%7 = load %struct.dirent*, %struct.dirent** %2
%8 = getelementptr inbounds %struct.dirent, %struct.dirent* %7, i32 0, i32 1
%9 = load i32, i32* %8
%10 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = load i8*, i8** %4
%12 = call i32* @opendir(i8* %11)
store i32* %12, i32** %5
%13 = load i32*, i32** %5
%14 = icmp ne i32* %13, null
br i1 %14, label %15, label %53
15: ; preds = %1
br label %16
16: ; preds = %49, %34, %15
%17 = load i32*, i32** %5
%18 = call %struct.dirent* @readdir(i32* %17)
store %struct.dirent* %18, %struct.dirent** %6
%19 = load %struct.dirent*, %struct.dirent** %6
%20 = icmp eq %struct.dirent* %19, null
br i1 %20, label %21, label %22
21: ; preds = %16
br label %50
22: ; preds = %16
%23 = load %struct.dirent*, %struct.dirent** %6
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i32 0, i32 0
%25 = load i64, i64* %24
%26 = load i64, i64* @DT_DIR
%27 = icmp ne i64 %25, %26
br i1 %27, label %34, label %28
28: ; preds = %22
%29 = load %struct.dirent*, %struct.dirent** %6
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i32 0, i32 1
%31 = load i32, i32* %30
%32 = call i32 @strlen(i32 %31)
%33 = icmp slt i32 %32, 10
br i1 %33, label %34, label %35
34: ; preds = %28, %22
br label %16
35: ; preds = %28
%36 = load %struct.dirent*, %struct.dirent** %6
%37 = call i32 @read_from_file(%struct.dirent* %36, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%38 = icmp eq i32 %37, 4172
br i1 %38, label %39, label %43
39: ; preds = %35
%40 = load %struct.dirent*, %struct.dirent** %6
%41 = call i32 @read_from_file(%struct.dirent* %40, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%42 = icmp eq i32 %41, 33333
br label %43
43: ; preds = %39, %35
%44 = phi i1 [ false, %35 ], [ %42, %39 ]
%45 = zext i1 %44 to i32
store i32 %45, i32* %3
%46 = load i32, i32* %3
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %43
br label %50
49: ; preds = %43
br label %16
50: ; preds = %48, %21
%51 = load i32*, i32** %5
%52 = call i32 @closedir(i32* %51)
br label %53
53: ; preds = %50, %1
%54 = load i8*, i8** %4
%55 = call i32 @free(i8* %54)
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
%struct.dirent = type { i64, i32 }
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [21 x i8] c"/sys/bus/pci/devices\00"
@DT_DIR = global i64 0
@.str.2 = [7 x i8] c"vendor\00"
@.str.3 = [7 x i8] c"device\00"
define i32 @detect_2200_ohci(%struct.dirent* nocapture %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = getelementptr inbounds %struct.dirent, %struct.dirent* %0, i64 0, i32 1
%5 = load i32, i32* %4
%6 = call i32 @asprintf(i8** %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = load i8*, i8** %2
%8 = call i32* @opendir(i8* %7)
%9 = icmp eq i32* %8, null
br i1 %9, label %36, label %10
10: ; preds = %1
%11 = call %struct.dirent* @readdir(i32* %8)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %33, label %13
13: ; preds = %10, %24
%14 = phi %struct.dirent* [ %25, %24 ], [ %11, %10 ]
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%16 = load i64, i64* %15
%17 = load i64, i64* @DT_DIR
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %24
19: ; preds = %13
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 1
%21 = load i32, i32* %20
%22 = call i32 @strlen(i32 %21)
%23 = icmp slt i32 %22, 10
br i1 %23, label %24, label %27
24: ; preds = %13, %19, %30, %27
%25 = call %struct.dirent* @readdir(i32* %8)
%26 = icmp eq %struct.dirent* %25, null
br i1 %26, label %33, label %13
27: ; preds = %19
%28 = call i32 @read_from_file(%struct.dirent* %14, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%29 = icmp eq i32 %28, 4172
br i1 %29, label %30, label %24
30: ; preds = %27
%31 = call i32 @read_from_file(%struct.dirent* %14, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%32 = icmp eq i32 %31, 33333
br i1 %32, label %33, label %24
33: ; preds = %24, %30, %10
%34 = phi i32 [ 0, %10 ], [ 1, %30 ], [ 0, %24 ]
%35 = call i32 @closedir(i32* %8)
br label %36
36: ; preds = %33, %1
%37 = phi i32 [ %34, %33 ], [ 0, %1 ]
%38 = load i8*, i8** %2
%39 = call i32 @free(i8* %38)
ret i32 %37
}