#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 stat {
  int /*<<< orphan*/ st_mode;
};
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ path;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int PATH_MAX;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ pci_checkdevice(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int stat(char *, struct stat *);
void pci_detect(char *dirname) {
  char path[PATH_MAX];
  DIR *dr;
  struct dirent *de;
  struct stat st;
  if (dirname == NULL) {
    dirname = "/proc/bus/pci";
  }
  if ((dr = opendir(dirname)) == NULL) {
    return;
  }
  while ((de = readdir(dr)) != NULL) {
    if (de->d_name[0] < '0' || de->d_name[0] > '9')
      continue;
    snprintf(path, sizeof(path), "%s/%s", dirname, de->d_name);
    if (stat(path, &st) == -1)
      continue;
    if (S_ISDIR(st.st_mode)) {
      pci_detect(path);
      continue;
    }
    pci_checkdevice(path);
  }
  closedir(dr);
}


x86 assembly (clang, -O0):
<pci_detect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x48(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11d2 <pci_detect+0x52>
lea    0xe32(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11f5 <pci_detect+0x75>
movl   $0x1,-0x34(%rbp)
jmp    12b6 <pci_detect+0x136>
jmp    11fa <pci_detect+0x7a>
mov    -0x20(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     12a6 <pci_detect+0x126>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
jl     1237 <pci_detect+0xb7>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x39,%eax
jle    123c <pci_detect+0xbc>
jmp    11fa <pci_detect+0x7a>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rax
mov    %eax,%esi
mov    -0x8(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    (%rax),%r8
lea    0xdb6(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x40(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
jne    1278 <pci_detect+0xf8>
jmp    11fa <pci_detect+0x7a>
mov    -0x30(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1298 <pci_detect+0x118>
mov    -0x40(%rbp),%rdi
call   10a0 <pci_detect@plt>
jmp    11fa <pci_detect+0x7a>
mov    -0x40(%rbp),%rdi
call   1030 <pci_checkdevice@plt>
jmp    11fa <pci_detect+0x7a>
mov    -0x20(%rbp),%rdi
call   1070 <closedir@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pci_detect>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e5b(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
test   %rdi,%rdi
lea    0xe4d(%rip),%r15
cmovne %rdi,%r15
mov    %r15,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     123e <pci_detect+0xbe>
mov    %rax,%r12
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1236 <pci_detect+0xb6>
lea    -0x30(%rbp),%r13
jmp    11ed <pci_detect+0x6d>
call   10a0 <pci_detect@plt>
nop
mov    %r12,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1236 <pci_detect+0xb6>
mov    (%rax),%r8
movzbl (%r8),%eax
add    $0xc6,%al
cmp    $0xf6,%al
jb     11e0 <pci_detect+0x60>
mov    %rbx,%rdi
mov    %r14d,%esi
lea    0xe07(%rip),%rdx
mov    %r15,%rcx
call   1060 <snprintf@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
je     11e0 <pci_detect+0x60>
mov    -0x30(%rbp),%edi
call   1050 <S_ISDIR@plt>
mov    %rbx,%rdi
test   %rax,%rax
jne    11da <pci_detect+0x5a>
call   1030 <pci_checkdevice@plt>
jmp    11e0 <pci_detect+0x60>
mov    %r12,%rdi
call   1070 <closedir@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [14 x i8] c"/proc/bus/pci\00"
@.str.1 = [6 x i8] c"%s/%s\00"
define void @pci_detect(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca %struct.stat
%8 = alloca i32
store i8* %0, i8** %2
%9 = load i32, i32* @PATH_MAX
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %3
%12 = alloca i8, i64 %10
store i64 %10, i64* %4
%13 = load i8*, i8** %2
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %16
15: ; preds = %1
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8** %2
br label %16
16: ; preds = %15, %1
%17 = load i8*, i8** %2
%18 = call i32* @opendir(i8* %17)
store i32* %18, i32** %5
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %21
20: ; preds = %16
store i32 1, i32* %8
br label %64
21: ; preds = %16
br label %22
22: ; preds = %59, %58, %52, %42, %21
%23 = load i32*, i32** %5
%24 = call %struct.dirent* @readdir(i32* %23)
store %struct.dirent* %24, %struct.dirent** %6
%25 = icmp ne %struct.dirent* %24, null
br i1 %25, label %26, label %61
26: ; preds = %22
%27 = load %struct.dirent*, %struct.dirent** %6
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i32 0, i32 0
%29 = load i8*, i8** %28
%30 = getelementptr inbounds i8, i8* %29, i64 0
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp slt i32 %32, 48
br i1 %33, label %42, label %34
34: ; preds = %26
%35 = load %struct.dirent*, %struct.dirent** %6
%36 = getelementptr inbounds %struct.dirent, %struct.dirent* %35, i32 0, i32 0
%37 = load i8*, i8** %36
%38 = getelementptr inbounds i8, i8* %37, i64 0
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp sgt i32 %40, 57
br i1 %41, label %42, label %43
42: ; preds = %34, %26
br label %22
43: ; preds = %34
%44 = trunc i64 %10 to i32
%45 = load i8*, i8** %2
%46 = load %struct.dirent*, %struct.dirent** %6
%47 = getelementptr inbounds %struct.dirent, %struct.dirent* %46, i32 0, i32 0
%48 = load i8*, i8** %47
%49 = call i32 @snprintf(i8* %12, i32 %44, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %45, i8* %48)
%50 = call i32 @stat(i8* %12, %struct.stat* %7)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %53
52: ; preds = %43
br label %22
53: ; preds = %43
%54 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%55 = load i32, i32* %54
%56 = call i64 @S_ISDIR(i32 %55)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %53
br label %22
59: ; preds = %53
%60 = call i32 @pci_checkdevice(i8* %12)
br label %22
61: ; preds = %22
%62 = load i32*, i32** %5
%63 = call i32 @closedir(i32* %62)
store i32 0, i32* %8
br label %64
64: ; preds = %61, %20
%65 = load i8*, i8** %3
%66 = load i32, i32* %8
switch i32 %66, label %68 [
i32 0, label %67
i32 1, label %67
]
67: ; preds = %64, %64
ret void
68: ; preds = %64
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [14 x i8] c"/proc/bus/pci\00"
@.str.1 = [6 x i8] c"%s/%s\00"
define void @pci_detect(i8* %0) {
%2 = alloca %struct.stat
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = bitcast %struct.stat* %2 to i8*
%7 = icmp eq i8* %0, null
%8 = select i1 %7, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %0
%9 = tail call i32* @opendir(i8* %8)
%10 = icmp eq i32* %9, null
br i1 %10, label %39, label %11
11: ; preds = %1
%12 = call %struct.dirent* @readdir(i32* %9)
%13 = icmp eq %struct.dirent* %12, null
br i1 %13, label %37, label %14
14: ; preds = %11
%15 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
br label %16
16: ; preds = %14, %32
%17 = phi %struct.dirent* [ %12, %14 ], [ %33, %32 ]
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i64 0, i32 0
%19 = load i8*, i8** %18
%20 = load i8, i8* %19
%21 = add i8 %20, -58
%22 = icmp ult i8 %21, -10
br i1 %22, label %32, label %23
23: ; preds = %16
%24 = call i32 @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %8, i8* %19)
%25 = call i32 @stat(i8* %5, %struct.stat* %2)
%26 = icmp eq i32 %25, -1
br i1 %26, label %32, label %27
27: ; preds = %23
%28 = load i32, i32* %15
%29 = call i64 @S_ISDIR(i32 %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %35, label %31
31: ; preds = %27
br label %32
32: ; preds = %31, %35, %23, %16
%33 = call %struct.dirent* @readdir(i32* %9)
%34 = icmp eq %struct.dirent* %33, null
br i1 %34, label %37, label %16
35: ; preds = %27
%36 = call i32 @pci_checkdevice(i8* %5)
br label %32
37: ; preds = %32, %11
%38 = call i32 @closedir(i32* %9)
br label %39
39: ; preds = %1, %37
ret void
}