#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 */

/* Variables and functions */
int O_RDONLY;
int O_SYNC;
long bswap_16(long);
int /*<<< orphan*/ close(int);
int open(char *, int);
int pread(int, long *, int, unsigned int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char, unsigned char);
long pci_config_read_word(unsigned char bus, unsigned char dev, int func,
                          unsigned cmd) {
  long retval;
  char path[100];
  int fd;
  sprintf(path, "/proc/bus/pci/%02d/%02x.0", bus, dev);
  fd = open(path, O_RDONLY | O_SYNC);
  if (fd == -1) {
    retval = 0;
  } else if (pread(fd, &retval, 2, cmd) == 2) {
    retval = bswap_16(retval);
  } else {
    retval = 0;
  }
  if (fd > 0) {
    close(fd);
  }
  return retval;
}


x86 assembly (clang, -O0):
<pci_config_read_word>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %sil,%al
mov    %dil,%sil
mov    %sil,-0x1(%rbp)
mov    %al,-0x2(%rbp)
mov    %edx,-0x8(%rbp)
mov    %ecx,-0xc(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x1(%rbp),%al
lea    0xe84(%rip),%rsi
movzbl %al,%edx
movzbl -0x2(%rbp),%ecx
call   1070 <sprintf@plt>
lea    -0x80(%rbp),%rdi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
mov    0x2e4c(%rip),%rax
or     (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x84(%rbp)
cmpl   $0xffffffff,-0x84(%rbp)
jne    11c3 <pci_config_read_word+0x73>
movq   $0x0,-0x18(%rbp)
jmp    1202 <pci_config_read_word+0xb2>
mov    -0x84(%rbp),%edi
mov    -0xc(%rbp),%ecx
lea    -0x18(%rbp),%rsi
mov    $0x2,%edx
call   1060 <pread@plt>
cmp    $0x2,%eax
jne    11f5 <pci_config_read_word+0xa5>
mov    -0x18(%rbp),%rdi
call   1050 <bswap_16@plt>
mov    %rax,-0x18(%rbp)
jmp    11fd <pci_config_read_word+0xad>
movq   $0x0,-0x18(%rbp)
jmp    1202 <pci_config_read_word+0xb2>
cmpl   $0x0,-0x84(%rbp)
jle    121a <pci_config_read_word+0xca>
mov    -0x84(%rbp),%edi
call   1030 <close@plt>
mov    -0x18(%rbp),%rax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pci_config_read_word>:
push   %rbp
push   %rbx
sub    $0x78,%rsp
mov    %ecx,%ebp
mov    %esi,%ecx
mov    %edi,%edx
lea    0xe9d(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
mov    0x2e69(%rip),%rax
mov    0x2e6a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1040 <open@plt>
cmp    $0xffffffff,%eax
je     11bc <pci_config_read_word+0x6c>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x2,%edx
mov    %ebp,%ecx
call   1060 <pread@plt>
cmp    $0x2,%eax
jne    11c0 <pci_config_read_word+0x70>
mov    0x8(%rsp),%rdi
call   1050 <bswap_16@plt>
mov    %rax,%rbp
test   %ebx,%ebx
jg     11c6 <pci_config_read_word+0x76>
jmp    11cd <pci_config_read_word+0x7d>
xor    %ebp,%ebp
jmp    11cd <pci_config_read_word+0x7d>
xor    %ebp,%ebp
test   %ebx,%ebx
jle    11cd <pci_config_read_word+0x7d>
mov    %ebx,%edi
call   1030 <close@plt>
mov    %rbp,%rax
add    $0x78,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"/proc/bus/pci/%02d/%02x.0\00"
@O_RDONLY = global i32 0
@O_SYNC = global i32 0
define i64 @pci_config_read_word(i8 zeroext %0, i8 zeroext %1, i32 %2, i32 %3) {
%5 = alloca i8
%6 = alloca i8
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca [100 x i8]
%11 = alloca i32
store i8 %0, i8* %5
store i8 %1, i8* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%12 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%13 = load i8, i8* %5
%14 = load i8, i8* %6
%15 = call i32 @sprintf(i8* %12, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8 zeroext %13, i8 zeroext %14)
%16 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%17 = load i32, i32* @O_RDONLY
%18 = load i32, i32* @O_SYNC
%19 = or i32 %17, %18
%20 = call i32 @open(i8* %16, i32 %19)
store i32 %20, i32* %11
%21 = load i32, i32* %11
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %4
store i64 0, i64* %9
br label %34
24: ; preds = %4
%25 = load i32, i32* %11
%26 = load i32, i32* %8
%27 = call i32 @pread(i32 %25, i64* %9, i32 2, i32 %26)
%28 = icmp eq i32 %27, 2
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = load i64, i64* %9
%31 = call i64 @bswap_16(i64 %30)
store i64 %31, i64* %9
br label %33
32: ; preds = %24
store i64 0, i64* %9
br label %33
33: ; preds = %32, %29
br label %34
34: ; preds = %33, %23
%35 = load i32, i32* %11
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = load i32, i32* %11
%39 = call i32 @close(i32 %38)
br label %40
40: ; preds = %37, %34
%41 = load i64, i64* %9
ret i64 %41
}

LLVM IR (-O2):
@.str = [26 x i8] c"/proc/bus/pci/%02d/%02x.0\00"
@O_RDONLY = global i32 0
@O_SYNC = global i32 0
define i64 @pci_config_read_word(i8 zeroext %0, i8 zeroext %1, i32 %2, i32 %3) {
%5 = alloca i64
%6 = alloca [100 x i8]
%7 = bitcast i64* %5 to i8*
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%9 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8 zeroext %0, i8 zeroext %1)
%10 = load i32, i32* @O_RDONLY
%11 = load i32, i32* @O_SYNC
%12 = or i32 %11, %10
%13 = call i32 @open(i8* %8, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %26, label %15
15: ; preds = %4
%16 = call i32 @pread(i32 %13, i64* %5, i32 2, i32 %3)
%17 = icmp eq i32 %16, 2
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i64, i64* %5
%20 = tail call i64 @bswap_16(i64 %19)
br label %21
21: ; preds = %15, %18
%22 = phi i64 [ %20, %18 ], [ 0, %15 ]
%23 = icmp sgt i32 %13, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = tail call i32 @close(i32 %13)
br label %26
26: ; preds = %4, %24, %21
%27 = phi i64 [ %22, %24 ], [ %22, %21 ], [ 0, %4 ]
ret i64 %27
}