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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(void *, int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ log_errno(char *, ...);
void *malloc(int /*<<< orphan*/);
char *options_get_string(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, int, int, int);
scalar_t__ stat(char *, struct stat *);
void *pci_read_configuration(int bus, int device, int function) {
  struct stat st;
  char buf[512];
  sprintf(buf, "%s/%02x_%02x.%d", options_get_string("proc_pci_bus"), bus,
          device, function);
  if (stat(buf, &st) != 0) {
    sprintf(buf, "%s/%02x/%02x.%d", options_get_string("proc_pci_bus"), bus,
            device, function);
    if (stat(buf, &st) != 0) {
      return NULL;
    }
  }
  FILE *f = fopen(buf, "r");
  if (!f) {
    log_errno("Unable to read PCI configuration data from %s", buf);
    return NULL;
  }
  void *data = malloc(st.st_size);
  int n = fread(data, st.st_size, 1, f);
  if (n != 1) {
    log_errno("Unable to read the CPU PCI configuration data. You may not "
              "have the necessary user privileges, please retry with "
              "administrator root privileges.\n");
    return NULL;
  }
  fclose(f);
  return data;
}


x86 assembly (clang, -O0):
<pci_read_configuration>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x220(%rbp),%rax
mov    %rax,-0x240(%rbp)
lea    0xe67(%rip),%rdi
call   1050 <options_get_string@plt>
mov    -0x240(%rbp),%rdi
mov    %rax,%rdx
mov    -0xc(%rbp),%ecx
mov    -0x10(%rbp),%r8d
mov    -0x14(%rbp),%r9d
lea    0xe36(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x220(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     1250 <pci_read_configuration+0xd0>
lea    -0x220(%rbp),%rax
mov    %rax,-0x248(%rbp)
lea    0xe12(%rip),%rdi
call   1050 <options_get_string@plt>
mov    -0x248(%rbp),%rdi
mov    %rax,%rdx
mov    -0xc(%rbp),%ecx
mov    -0x10(%rbp),%r8d
mov    -0x14(%rbp),%r9d
lea    0xdfe(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x220(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     124b <pci_read_configuration+0xcb>
movq   $0x0,-0x8(%rbp)
jmp    1309 <pci_read_configuration+0x189>
jmp    1250 <pci_read_configuration+0xd0>
lea    -0x220(%rbp),%rdi
lea    0xdcf(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x228(%rbp)
cmpq   $0x0,-0x228(%rbp)
jne    129a <pci_read_configuration+0x11a>
lea    -0x220(%rbp),%rsi
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1030 <log_errno@plt>
movq   $0x0,-0x8(%rbp)
jmp    1309 <pci_read_configuration+0x189>
mov    -0x18(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x230(%rbp)
mov    -0x230(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    -0x228(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
mov    %eax,-0x234(%rbp)
cmpl   $0x1,-0x234(%rbp)
je     12f2 <pci_read_configuration+0x172>
lea    0xd7f(%rip),%rdi
mov    $0x0,%al
call   1030 <log_errno@plt>
movq   $0x0,-0x8(%rbp)
jmp    1309 <pci_read_configuration+0x189>
mov    -0x228(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x230(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pci_read_configuration>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x218,%rsp
mov    %edx,%r14d
mov    %esi,%r15d
mov    %edi,%ebx
lea    0xe74(%rip),%rdi
call   1050 <options_get_string@plt>
lea    0xe58(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    %rax,%rdx
mov    %ebx,%ecx
mov    %r15d,%r8d
mov    %r14d,%r9d
call   10a0 <sprintf@plt>
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   1070 <stat@plt>
test   %rax,%rax
je     1213 <pci_read_configuration+0x93>
lea    0xe37(%rip),%rdi
call   1050 <options_get_string@plt>
lea    0xe38(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    %rax,%rdx
mov    %ebx,%ecx
mov    %r15d,%r8d
mov    %r14d,%r9d
call   10a0 <sprintf@plt>
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   1070 <stat@plt>
test   %rax,%rax
je     1213 <pci_read_configuration+0x93>
xor    %ebx,%ebx
jmp    1285 <pci_read_configuration+0x105>
lea    0xe13(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     125e <pci_read_configuration+0xde>
mov    %rax,%rbp
mov    0x8(%rsp),%r14d
mov    %r14d,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14d,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    1275 <pci_read_configuration+0xf5>
mov    %rbp,%rdi
call   1060 <fclose@plt>
jmp    1285 <pci_read_configuration+0x105>
lea    0xdca(%rip),%rdi
xor    %ebx,%ebx
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1030 <log_errno@plt>
jmp    1285 <pci_read_configuration+0x105>
lea    0xde1(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <log_errno@plt>
mov    %rbx,%rax
add    $0x218,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [16 x i8] c"%s/%02x_%02x.%d\00"
@.str.1 = [13 x i8] c"proc_pci_bus\00"
@.str.2 = [16 x i8] c"%s/%02x/%02x.%d\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [46 x i8] c"Unable to read PCI configuration data from %s\00"
@.str.5 = [145 x i8] c"Unable to read the CPU PCI configuration data. You may not have the necessary user privileges, please retry with administrator root privileges.\0A\00"
define i8* @pci_read_configuration(i32 %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca [512 x i8]
%10 = alloca i32*
%11 = alloca i8*
%12 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%13 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%14 = call i8* @options_get_string(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* %5
%16 = load i32, i32* %6
%17 = load i32, i32* %7
%18 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %14, i32 %15, i32 %16, i32 %17)
%19 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%20 = call i64 @stat(i8* %19, %struct.stat* %8)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %34
22: ; preds = %3
%23 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%24 = call i8* @options_get_string(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* %5
%26 = load i32, i32* %6
%27 = load i32, i32* %7
%28 = call i32 @sprintf(i8* %23, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %24, i32 %25, i32 %26, i32 %27)
%29 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%30 = call i64 @stat(i8* %29, %struct.stat* %8)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %22
store i8* null, i8** %4
br label %59
33: ; preds = %22
br label %34
34: ; preds = %33, %3
%35 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%36 = call i32* @fopen(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %36, i32** %10
%37 = load i32*, i32** %10
%38 = icmp ne i32* %37, null
br i1 %38, label %42, label %39
39: ; preds = %34
%40 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%41 = call i32 (i8*, ...) @log_errno(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), i8* %40)
store i8* null, i8** %4
br label %59
42: ; preds = %34
%43 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i8* @malloc(i32 %44)
store i8* %45, i8** %11
%46 = load i8*, i8** %11
%47 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%48 = load i32, i32* %47
%49 = load i32*, i32** %10
%50 = call i32 @fread(i8* %46, i32 %48, i32 1, i32* %49)
store i32 %50, i32* %12
%51 = load i32, i32* %12
%52 = icmp ne i32 %51, 1
br i1 %52, label %53, label %55
53: ; preds = %42
%54 = call i32 (i8*, ...) @log_errno(i8* getelementptr inbounds ([145 x i8], [145 x i8]* @.str.5, i64 0, i64 0))
store i8* null, i8** %4
br label %59
55: ; preds = %42
%56 = load i32*, i32** %10
%57 = call i32 @fclose(i32* %56)
%58 = load i8*, i8** %11
store i8* %58, i8** %4
br label %59
59: ; preds = %55, %53, %39, %32
%60 = load i8*, i8** %4
ret i8* %60
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [16 x i8] c"%s/%02x_%02x.%d\00"
@.str.1 = [13 x i8] c"proc_pci_bus\00"
@.str.2 = [16 x i8] c"%s/%02x/%02x.%d\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [46 x i8] c"Unable to read PCI configuration data from %s\00"
@.str.5 = [145 x i8] c"Unable to read the CPU PCI configuration data. You may not have the necessary user privileges, please retry with administrator root privileges.\0A\00"
define noalias i8* @pci_read_configuration(i32 %0, i32 %1, i32 %2) {
%4 = alloca %struct.stat
%5 = alloca [512 x i8]
%6 = bitcast %struct.stat* %4 to i8*
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%8 = tail call i8* @options_get_string(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %8, i32 %0, i32 %1, i32 %2)
%10 = call i64 @stat(i8* %7, %struct.stat* %4)
%11 = icmp eq i64 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %3
%13 = call i8* @options_get_string(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %13, i32 %0, i32 %1, i32 %2)
%15 = call i64 @stat(i8* %7, %struct.stat* %4)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %32
17: ; preds = %12, %3
%18 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 (i8*, ...) @log_errno(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), i8* %7)
br label %32
22: ; preds = %17
%23 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%24 = load i32, i32* %23
%25 = call i8* @malloc(i32 %24)
%26 = call i32 @fread(i8* %25, i32 %24, i32 1, i32* %18)
%27 = icmp eq i32 %26, 1
br i1 %27, label %30, label %28
28: ; preds = %22
%29 = call i32 (i8*, ...) @log_errno(i8* getelementptr inbounds ([145 x i8], [145 x i8]* @.str.5, i64 0, i64 0))
br label %32
30: ; preds = %22
%31 = call i32 @fclose(i32* %18)
br label %32
32: ; preds = %20, %30, %28, %12
%33 = phi i8* [ null, %12 ], [ null, %20 ], [ null, %28 ], [ %25, %30 ]
ret i8* %33
}