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

/* Variables and functions */
scalar_t__ DT_DIR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, int, int, int);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int sscanf(char *, char *, int *, ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int pci_list_procfs(void) {
  DIR *dir = opendir("/proc/bus/pci");
  struct dirent *de;
  if (!dir) {
    fprintf(stderr, "opendir(%s): %s\n", "/proc/bus/pci", strerror(errno));
    return -1;
  }
  while ((de = readdir(dir))) {
    int bus;
    int r = sscanf(de->d_name, "%02x", &bus);
    if (r == 1 && de->d_type == DT_DIR) {
      char buf[1024];
      snprintf(buf, sizeof(buf), "%s/%s", "/proc/bus/pci", de->d_name);
      DIR *subdir = opendir(buf);
      struct dirent *subde;
      if (!subdir) {
        fprintf(stderr, "opendir(%s): %s\n", buf, strerror(errno));
        return -1;
      }
      while ((subde = readdir(subdir))) {
        int dev, fun;
        r = sscanf(subde->d_name, "%02x.%x", &dev, &fun);
        if (r == 2) {
          printf("%d %d %d\n", bus, dev, fun);
        }
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<pci_list_procfs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
lea    0xe7e(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11db <pci_list_procfs+0x6b>
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x43c(%rbp)
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x43c(%rbp),%edi
mov    %rax,%rcx
lea    0xe4b(%rip),%rsi
lea    0xe36(%rip),%rdx
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1355 <pci_list_procfs+0x1e5>
jmp    11e0 <pci_list_procfs+0x70>
mov    -0x10(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     134e <pci_list_procfs+0x1de>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xe1a(%rip),%rsi
lea    -0x1c(%rbp),%rdx
mov    $0x0,%al
call   1080 <sscanf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x1,-0x20(%rbp)
jne    1349 <pci_list_procfs+0x1d9>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2dac(%rip),%rcx
cmp    (%rcx),%rax
jne    1349 <pci_list_procfs+0x1d9>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%r8
mov    $0x400,%esi
lea    0xdd5(%rip),%rdx
lea    0xdaa(%rip),%rcx
call   1050 <snprintf@plt>
lea    -0x420(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    12cf <pci_list_procfs+0x15f>
mov    0x2d5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44c(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x448(%rbp)
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x44c(%rbp),%edi
mov    -0x448(%rbp),%rdx
mov    %rax,%rcx
lea    0xd50(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1355 <pci_list_procfs+0x1e5>
jmp    12d4 <pci_list_procfs+0x164>
mov    -0x428(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x430(%rbp)
cmp    $0x0,%rax
je     1344 <pci_list_procfs+0x1d4>
mov    -0x430(%rbp),%rax
mov    (%rax),%rdi
lea    0xd28(%rip),%rsi
lea    -0x434(%rbp),%rdx
lea    -0x438(%rbp),%rcx
mov    $0x0,%al
call   1080 <sscanf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x2,-0x20(%rbp)
jne    133f <pci_list_procfs+0x1cf>
mov    -0x1c(%rbp),%esi
mov    -0x434(%rbp),%edx
mov    -0x438(%rbp),%ecx
lea    0xcf8(%rip),%rdi
call   1040 <printf@plt>
jmp    12d4 <pci_list_procfs+0x164>
jmp    1349 <pci_list_procfs+0x1d9>
jmp    11e0 <pci_list_procfs+0x70>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pci_list_procfs>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
lea    0xe78(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     12b3 <pci_list_procfs+0x143>
mov    %rax,%r15
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     12af <pci_list_procfs+0x13f>
mov    %rax,%rbx
lea    0xe6b(%rip),%rbp
lea    0xe6f(%rip),%r12
lea    0x8(%rsp),%r13
lea    0x4(%rsp),%r14
jmp    11ee <pci_list_procfs+0x7e>
nopw   0x0(%rax,%rax,1)
mov    %rbp,%r15
lea    0xe45(%rip),%rbp
mov    %r15,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     12af <pci_list_procfs+0x13f>
mov    (%rbx),%rdi
mov    %rbp,%rsi
lea    0xc(%rsp),%rdx
xor    %eax,%eax
call   1080 <sscanf@plt>
cmp    $0x1,%eax
jne    11da <pci_list_procfs+0x6a>
mov    0x8(%rbx),%rax
mov    0x2dc8(%rip),%rcx
cmp    (%rcx),%rax
jne    11da <pci_list_procfs+0x6a>
mov    %r15,%rbp
mov    (%rbx),%r8
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
lea    0xdf5(%rip),%rdx
lea    0xdca(%rip),%rcx
call   1050 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     12f9 <pci_list_procfs+0x189>
mov    %rax,%r15
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
lea    0xdd1(%rip),%rbx
jne    1281 <pci_list_procfs+0x111>
jmp    11d0 <pci_list_procfs+0x60>
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11d0 <pci_list_procfs+0x60>
mov    (%rax),%rdi
mov    %r12,%rsi
mov    %r13,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1080 <sscanf@plt>
cmp    $0x2,%eax
jne    1270 <pci_list_procfs+0x100>
mov    0xc(%rsp),%esi
mov    0x8(%rsp),%edx
mov    0x4(%rsp),%ecx
mov    %rbx,%rdi
call   1040 <printf@plt>
jmp    1270 <pci_list_procfs+0x100>
xor    %eax,%eax
jmp    12e7 <pci_list_procfs+0x177>
mov    0x2d26(%rip),%rax
mov    (%rax),%ebx
mov    0x2d0d(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd3d(%rip),%rsi
lea    0xd28(%rip),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2ce0(%rip),%rax
mov    (%rax),%ebx
mov    0x2cc7(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xcf7(%rip),%rsi
lea    0x10(%rsp),%rdx
jmp    12d8 <pci_list_procfs+0x168>

LLVM IR (-O0):
%struct.dirent = type { i8*, i64 }
@.str = [14 x i8] c"/proc/bus/pci\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"opendir(%s): %s\0A\00"
@errno = global i32 0
@.str.2 = [5 x i8] c"%02x\00"
@DT_DIR = global i64 0
@.str.3 = [6 x i8] c"%s/%s\00"
@.str.4 = [8 x i8] c"%02x.%x\00"
@.str.5 = [10 x i8] c"%d %d %d\0A\00"
define i32 @pci_list_procfs() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca %struct.dirent*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca i32*
%8 = alloca %struct.dirent*
%9 = alloca i32
%10 = alloca i32
%11 = call i32* @opendir(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %2
%12 = load i32*, i32** %2
%13 = icmp ne i32* %12, null
br i1 %13, label %19, label %14
14: ; preds = %0
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 -1, i32* %1
br label %74
19: ; preds = %0
br label %20
20: ; preds = %72, %19
%21 = load i32*, i32** %2
%22 = call %struct.dirent* @readdir(i32* %21)
store %struct.dirent* %22, %struct.dirent** %3
%23 = icmp ne %struct.dirent* %22, null
br i1 %23, label %24, label %73
24: ; preds = %20
%25 = load %struct.dirent*, %struct.dirent** %3
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i32 0, i32 0
%27 = load i8*, i8** %26
%28 = call i32 (i8*, i8*, i32*, ...) @sscanf(i8* %27, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32* %4)
store i32 %28, i32* %5
%29 = load i32, i32* %5
%30 = icmp eq i32 %29, 1
br i1 %30, label %31, label %72
31: ; preds = %24
%32 = load %struct.dirent*, %struct.dirent** %3
%33 = getelementptr inbounds %struct.dirent, %struct.dirent* %32, i32 0, i32 1
%34 = load i64, i64* %33
%35 = load i64, i64* @DT_DIR
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %72
37: ; preds = %31
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%39 = load %struct.dirent*, %struct.dirent** %3
%40 = getelementptr inbounds %struct.dirent, %struct.dirent* %39, i32 0, i32 0
%41 = load i8*, i8** %40
%42 = call i32 @snprintf(i8* %38, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %41)
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%44 = call i32* @opendir(i8* %43)
store i32* %44, i32** %7
%45 = load i32*, i32** %7
%46 = icmp ne i32* %45, null
br i1 %46, label %53, label %47
47: ; preds = %37
%48 = load i32, i32* @stderr
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%50 = load i32, i32* @errno
%51 = call i8* @strerror(i32 %50)
%52 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %49, i8* %51)
store i32 -1, i32* %1
br label %74
53: ; preds = %37
br label %54
54: ; preds = %70, %53
%55 = load i32*, i32** %7
%56 = call %struct.dirent* @readdir(i32* %55)
store %struct.dirent* %56, %struct.dirent** %8
%57 = icmp ne %struct.dirent* %56, null
br i1 %57, label %58, label %71
58: ; preds = %54
%59 = load %struct.dirent*, %struct.dirent** %8
%60 = getelementptr inbounds %struct.dirent, %struct.dirent* %59, i32 0, i32 0
%61 = load i8*, i8** %60
%62 = call i32 (i8*, i8*, i32*, ...) @sscanf(i8* %61, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32* %9, i32* %10)
store i32 %62, i32* %5
%63 = load i32, i32* %5
%64 = icmp eq i32 %63, 2
br i1 %64, label %65, label %70
65: ; preds = %58
%66 = load i32, i32* %4
%67 = load i32, i32* %9
%68 = load i32, i32* %10
%69 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %66, i32 %67, i32 %68)
br label %70
70: ; preds = %65, %58
br label %54
71: ; preds = %54
br label %72
72: ; preds = %71, %31, %24
br label %20
73: ; preds = %20
store i32 0, i32* %1
br label %74
74: ; preds = %73, %47, %14
%75 = load i32, i32* %1
ret i32 %75
}

LLVM IR (-O2):
%struct.dirent = type { i8*, i64 }
@.str = [14 x i8] c"/proc/bus/pci\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"opendir(%s): %s\0A\00"
@errno = global i32 0
@.str.2 = [5 x i8] c"%02x\00"
@DT_DIR = global i64 0
@.str.3 = [6 x i8] c"%s/%s\00"
@.str.4 = [8 x i8] c"%02x.%x\00"
@.str.5 = [10 x i8] c"%d %d %d\0A\00"
define i32 @pci_list_procfs() {
%1 = alloca i32
%2 = alloca [1024 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = tail call i32* @opendir(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %15, label %7
7: ; preds = %0
%8 = call %struct.dirent* @readdir(i32* %5)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %62, label %10
10: ; preds = %7
%11 = bitcast i32* %1 to i8*
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%13 = bitcast i32* %3 to i8*
%14 = bitcast i32* %4 to i8*
br label %20
15: ; preds = %0
%16 = load i32, i32* @stderr
%17 = load i32, i32* @errno
%18 = tail call i8* @strerror(i32 %17)
%19 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %18)
br label %62
20: ; preds = %10, %54
%21 = phi %struct.dirent* [ %8, %10 ], [ %55, %54 ]
%22 = getelementptr inbounds %struct.dirent, %struct.dirent* %21, i64 0, i32 0
%23 = load i8*, i8** %22
%24 = call i32 (i8*, i8*, i32*, ...) @sscanf(i8* %23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%25 = icmp eq i32 %24, 1
br i1 %25, label %26, label %54
26: ; preds = %20
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %21, i64 0, i32 1
%28 = load i64, i64* %27
%29 = load i64, i64* @DT_DIR
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %54
31: ; preds = %26
%32 = load i8*, i8** %22
%33 = call i32 @snprintf(i8* %12, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %32)
%34 = call i32* @opendir(i8* %12)
%35 = icmp eq i32* %34, null
br i1 %35, label %57, label %36
36: ; preds = %31
%37 = call %struct.dirent* @readdir(i32* %34)
%38 = icmp eq %struct.dirent* %37, null
br i1 %38, label %53, label %39
39: ; preds = %36, %50
%40 = phi %struct.dirent* [ %51, %50 ], [ %37, %36 ]
%41 = getelementptr inbounds %struct.dirent, %struct.dirent* %40, i64 0, i32 0
%42 = load i8*, i8** %41
%43 = call i32 (i8*, i8*, i32*, ...) @sscanf(i8* %42, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32* %3, i32* %4)
%44 = icmp eq i32 %43, 2
br i1 %44, label %45, label %50
45: ; preds = %39
%46 = load i32, i32* %1
%47 = load i32, i32* %3
%48 = load i32, i32* %4
%49 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %46, i32 %47, i32 %48)
br label %50
50: ; preds = %45, %39
%51 = call %struct.dirent* @readdir(i32* %34)
%52 = icmp eq %struct.dirent* %51, null
br i1 %52, label %53, label %39
53: ; preds = %50, %36
br label %54
54: ; preds = %53, %26, %20
%55 = call %struct.dirent* @readdir(i32* %5)
%56 = icmp eq %struct.dirent* %55, null
br i1 %56, label %62, label %20
57: ; preds = %31
%58 = load i32, i32* @stderr
%59 = load i32, i32* @errno
%60 = call i8* @strerror(i32 %59)
%61 = call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %12, i8* %60)
br label %62
62: ; preds = %54, %7, %57, %15
%63 = phi i32 [ -1, %15 ], [ -1, %57 ], [ 0, %7 ], [ 0, %54 ]
ret i32 %63
}