#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 */
typedef int uint8_t;
typedef int uint32_t;
typedef int offset;

/* Variables and functions */
scalar_t__ inportb(int);
scalar_t__ inportl(int);
scalar_t__ inports(int);
int /*<<< orphan*/ outportl(int, int);
uint32_t pci_read_config(uint8_t bus, uint8_t slot, uint8_t func,
                         uint8_t offset, size_t size) {
  outportl(0x0CF8, (0x80000000 | ((bus) << 16) | ((slot) << 11) |
                    ((func) << 8) | ((offset)&0xfc)));
  if (size == 4) {
    return 0xFFFFFFFF & ((uint32_t)inportl(0x0CFC));
  } else if (size == 2) {
    return 0xFFFF & ((uint32_t)inports(0x0CFC + (offset & 2)));
  } else if (size == 1) {
    return 0xFF & ((uint32_t)inportb(0x0CFC + (offset & 3)));
  }
  return 0xFFFF;
}


x86 assembly (clang, -O0):
<pci_read_config>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8,-0x20(%rbp)
mov    -0x8(%rbp),%esi
shl    $0x10,%esi
or     $0x80000000,%esi
mov    -0xc(%rbp),%eax
shl    $0xb,%eax
or     %eax,%esi
mov    -0x10(%rbp),%eax
shl    $0x8,%eax
or     %eax,%esi
mov    -0x14(%rbp),%eax
and    $0xfc,%eax
or     %eax,%esi
mov    $0xcf8,%edi
call   1060 <outportl@plt>
cmpq   $0x4,-0x20(%rbp)
jne    11aa <pci_read_config+0x6a>
mov    $0xcfc,%edi
call   1040 <inportl@plt>
and    $0xffffffff,%eax
mov    %eax,-0x4(%rbp)
jmp    120d <pci_read_config+0xcd>
cmpq   $0x2,-0x20(%rbp)
jne    11d3 <pci_read_config+0x93>
mov    -0x14(%rbp),%edi
and    $0x2,%edi
add    $0xcfc,%edi
call   1030 <inports@plt>
and    $0xffff,%eax
mov    %eax,-0x4(%rbp)
jmp    120d <pci_read_config+0xcd>
cmpq   $0x1,-0x20(%rbp)
jne    11fc <pci_read_config+0xbc>
mov    -0x14(%rbp),%edi
and    $0x3,%edi
add    $0xcfc,%edi
call   1050 <inportb@plt>
and    $0xff,%eax
mov    %eax,-0x4(%rbp)
jmp    120d <pci_read_config+0xcd>
jmp    1201 <pci_read_config+0xc1>
jmp    1206 <pci_read_config+0xc6>
movl   $0xffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pci_read_config>:
push   %r14
push   %rbx
push   %rax
mov    %r8,%r14
mov    %ecx,%ebx
shl    $0x10,%edi
shl    $0xb,%esi
or     %edi,%esi
shl    $0x8,%edx
or     %edx,%esi
mov    %ecx,%eax
and    $0xfc,%eax
or     %eax,%esi
or     $0x80000000,%esi
mov    $0xcf8,%edi
call   1060 <outportl@plt>
cmp    $0x1,%r14
je     1192 <pci_read_config+0x52>
cmp    $0x2,%r14
je     11aa <pci_read_config+0x6a>
mov    $0xffff,%eax
cmp    $0x4,%r14
jne    11bd <pci_read_config+0x7d>
mov    $0xcfc,%edi
call   1040 <inportl@plt>
jmp    11bd <pci_read_config+0x7d>
or     $0xcfc,%ebx
and    $0xcff,%ebx
mov    %ebx,%edi
call   1050 <inportb@plt>
movzbl %al,%eax
jmp    11bd <pci_read_config+0x7d>
and    $0x2,%ebx
or     $0xcfc,%ebx
mov    %ebx,%edi
call   1030 <inports@plt>
movzwl %ax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @pci_read_config(i32 %0, i32 %1, i32 %2, i32 %3, i64 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i64 %4, i64* %11
%12 = load i32, i32* %7
%13 = shl i32 %12, 16
%14 = or i32 -2147483648, %13
%15 = load i32, i32* %8
%16 = shl i32 %15, 11
%17 = or i32 %14, %16
%18 = load i32, i32* %9
%19 = shl i32 %18, 8
%20 = or i32 %17, %19
%21 = load i32, i32* %10
%22 = and i32 %21, 252
%23 = or i32 %20, %22
%24 = call i32 @outportl(i32 3320, i32 %23)
%25 = load i64, i64* %11
%26 = icmp eq i64 %25, 4
br i1 %26, label %27, label %31
27: ; preds = %5
%28 = call i64 @inportl(i32 3324)
%29 = trunc i64 %28 to i32
%30 = and i32 -1, %29
store i32 %30, i32* %6
br label %54
31: ; preds = %5
%32 = load i64, i64* %11
%33 = icmp eq i64 %32, 2
br i1 %33, label %34, label %41
34: ; preds = %31
%35 = load i32, i32* %10
%36 = and i32 %35, 2
%37 = add nsw i32 3324, %36
%38 = call i64 @inports(i32 %37)
%39 = trunc i64 %38 to i32
%40 = and i32 65535, %39
store i32 %40, i32* %6
br label %54
41: ; preds = %31
%42 = load i64, i64* %11
%43 = icmp eq i64 %42, 1
br i1 %43, label %44, label %51
44: ; preds = %41
%45 = load i32, i32* %10
%46 = and i32 %45, 3
%47 = add nsw i32 3324, %46
%48 = call i64 @inportb(i32 %47)
%49 = trunc i64 %48 to i32
%50 = and i32 255, %49
store i32 %50, i32* %6
br label %54
51: ; preds = %41
br label %52
52: ; preds = %51
br label %53
53: ; preds = %52
store i32 65535, i32* %6
br label %54
54: ; preds = %53, %44, %34, %27
%55 = load i32, i32* %6
ret i32 %55
}

LLVM IR (-O2):
define i32 @pci_read_config(i32 %0, i32 %1, i32 %2, i32 %3, i64 %4) {
%6 = shl i32 %0, 16
%7 = shl i32 %1, 11
%8 = shl i32 %2, 8
%9 = and i32 %3, 252
%10 = or i32 %6, %7
%11 = or i32 %10, %8
%12 = or i32 %11, %9
%13 = or i32 %12, -2147483648
%14 = tail call i32 @outportl(i32 3320, i32 %13)
switch i64 %4, label %30 [
i64 4, label %15
i64 2, label %18
i64 1, label %24
]
15: ; preds = %5
%16 = tail call i64 @inportl(i32 3324)
%17 = trunc i64 %16 to i32
br label %30
18: ; preds = %5
%19 = and i32 %3, 2
%20 = or i32 %19, 3324
%21 = tail call i64 @inports(i32 %20)
%22 = trunc i64 %21 to i32
%23 = and i32 %22, 65535
br label %30
24: ; preds = %5
%25 = and i32 %3, 3
%26 = or i32 %25, 3324
%27 = tail call i64 @inportb(i32 %26)
%28 = trunc i64 %27 to i32
%29 = and i32 %28, 255
br label %30
30: ; preds = %5, %24, %18, %15
%31 = phi i32 [ %17, %15 ], [ %23, %18 ], [ %29, %24 ], [ 65535, %5 ]
ret i32 %31
}