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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
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 sscanf(int /*<<< orphan*/, char *, int *, int *, int *, int *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int pci_list_sysfs(void) {
  DIR *dir = opendir("/sys/bus/pci/devices");
  struct dirent *de;
  if (!dir) {
    fprintf(stderr, "opendir(%s): %s\n", "/sys/bus/pci/devices",
            strerror(errno));
    return -1;
  }
  while ((de = readdir(dir))) {
    int seg, bus, dev, fun;
    int r = sscanf(de->d_name, "%04x:%02x:%02x.%x", &seg, &bus, &dev, &fun);
    if (r == 4) {
      printf("%d %d %d\n", bus, dev, fun);
    }
  }
  closedir(dir);
  return 0;
}


x86 assembly (clang, -O0):
<pci_list_sysfs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xe81(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d2 <pci_list_sysfs+0x62>
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rcx
lea    0xe5b(%rip),%rsi
lea    0xe3f(%rip),%rdx
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1247 <pci_list_sysfs+0xd7>
jmp    11d7 <pci_list_sysfs+0x67>
mov    -0x10(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1237 <pci_list_sysfs+0xc7>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
lea    -0x1c(%rbp),%rdx
lea    -0x20(%rbp),%rcx
lea    -0x24(%rbp),%r8
lea    -0x28(%rbp),%r9
call   1080 <sscanf@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x4,-0x2c(%rbp)
jne    1232 <pci_list_sysfs+0xc2>
mov    -0x20(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    -0x28(%rbp),%ecx
lea    0xe0b(%rip),%rdi
call   1040 <printf@plt>
jmp    11d7 <pci_list_sysfs+0x67>
mov    -0x10(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pci_list_sysfs>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe7b(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     120e <pci_list_sysfs+0x9e>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1202 <pci_list_sysfs+0x92>
lea    0xe80(%rip),%r14
lea    0x14(%rsp),%r15
lea    0x10(%rsp),%r12
lea    0xc(%rsp),%r13
lea    0x8(%rsp),%rbp
jmp    11cd <pci_list_sysfs+0x5d>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1202 <pci_list_sysfs+0x92>
mov    (%rax),%edi
mov    %r14,%rsi
mov    %r15,%rdx
mov    %r12,%rcx
mov    %r13,%r8
mov    %rbp,%r9
call   1080 <sscanf@plt>
cmp    $0x4,%eax
jne    11c0 <pci_list_sysfs+0x50>
mov    0x10(%rsp),%esi
mov    0xc(%rsp),%edx
mov    0x8(%rsp),%ecx
lea    0xe3d(%rip),%rdi
call   1040 <printf@plt>
jmp    11c0 <pci_list_sysfs+0x50>
mov    %rbx,%rdi
call   1050 <closedir@plt>
xor    %eax,%eax
jmp    1242 <pci_list_sysfs+0xd2>
mov    0x2dcb(%rip),%rax
mov    (%rax),%ebx
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xde9(%rip),%rsi
lea    0xdcd(%rip),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [21 x i8] c"/sys/bus/pci/devices\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"opendir(%s): %s\0A\00"
@errno = global i32 0
@.str.2 = [18 x i8] c"%04x:%02x:%02x.%x\00"
@.str.3 = [10 x i8] c"%d %d %d\0A\00"
define i32 @pci_list_sysfs() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca %struct.dirent*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = call i32* @opendir(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %2
%10 = load i32*, i32** %2
%11 = icmp ne i32* %10, null
br i1 %11, label %17, label %12
12: ; preds = %0
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 -1, i32* %1
br label %38
17: ; preds = %0
br label %18
18: ; preds = %34, %17
%19 = load i32*, i32** %2
%20 = call %struct.dirent* @readdir(i32* %19)
store %struct.dirent* %20, %struct.dirent** %3
%21 = icmp ne %struct.dirent* %20, null
br i1 %21, label %22, label %35
22: ; preds = %18
%23 = load %struct.dirent*, %struct.dirent** %3
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @sscanf(i32 %25, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32* %4, i32* %5, i32* %6, i32* %7)
store i32 %26, i32* %8
%27 = load i32, i32* %8
%28 = icmp eq i32 %27, 4
br i1 %28, label %29, label %34
29: ; preds = %22
%30 = load i32, i32* %5
%31 = load i32, i32* %6
%32 = load i32, i32* %7
%33 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %30, i32 %31, i32 %32)
br label %34
34: ; preds = %29, %22
br label %18
35: ; preds = %18
%36 = load i32*, i32** %2
%37 = call i32 @closedir(i32* %36)
store i32 0, i32* %1
br label %38
38: ; preds = %35, %12
%39 = load i32, i32* %1
ret i32 %39
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [21 x i8] c"/sys/bus/pci/devices\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"opendir(%s): %s\0A\00"
@errno = global i32 0
@.str.2 = [18 x i8] c"%04x:%02x:%02x.%x\00"
@.str.3 = [10 x i8] c"%d %d %d\0A\00"
define i32 @pci_list_sysfs() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = tail call i32* @opendir(i8* getelementptr inbounds ([21 x i8], [21 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 %34, label %10
10: ; preds = %7
%11 = bitcast i32* %1 to i8*
%12 = bitcast i32* %2 to i8*
%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 ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %18)
br label %36
20: ; preds = %10, %31
%21 = phi %struct.dirent* [ %8, %10 ], [ %32, %31 ]
%22 = getelementptr inbounds %struct.dirent, %struct.dirent* %21, i64 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @sscanf(i32 %23, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32* %1, i32* %2, i32* %3, i32* %4)
%25 = icmp eq i32 %24, 4
br i1 %25, label %26, label %31
26: ; preds = %20
%27 = load i32, i32* %2
%28 = load i32, i32* %3
%29 = load i32, i32* %4
%30 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %27, i32 %28, i32 %29)
br label %31
31: ; preds = %26, %20
%32 = call %struct.dirent* @readdir(i32* %5)
%33 = icmp eq %struct.dirent* %32, null
br i1 %33, label %34, label %20
34: ; preds = %31, %7
%35 = call i32 @closedir(i32* %5)
br label %36
36: ; preds = %34, %15
%37 = phi i32 [ 0, %34 ], [ -1, %15 ]
ret i32 %37
}