#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;
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_byte(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, 1, cmd) != 1) {
    retval = 0;
  }
  if (fd > 0) {
    close(fd);
  }
  return retval;
}


x86 assembly (clang, -O0):
<pci_config_read_byte>:
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    0xe94(%rip),%rsi
movzbl %al,%edx
movzbl -0x2(%rbp),%ecx
call   1060 <sprintf@plt>
lea    -0x80(%rbp),%rdi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
mov    0x2e5c(%rip),%rax
or     (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x84(%rbp)
cmpl   $0xffffffff,-0x84(%rbp)
jne    11b3 <pci_config_read_byte+0x73>
movq   $0x0,-0x18(%rbp)
jmp    11e0 <pci_config_read_byte+0xa0>
mov    -0x84(%rbp),%edi
mov    -0xc(%rbp),%ecx
lea    -0x18(%rbp),%rsi
mov    $0x1,%edx
call   1050 <pread@plt>
cmp    $0x1,%eax
je     11db <pci_config_read_byte+0x9b>
movq   $0x0,-0x18(%rbp)
jmp    11e0 <pci_config_read_byte+0xa0>
cmpl   $0x0,-0x84(%rbp)
jle    11f8 <pci_config_read_byte+0xb8>
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_byte>:
push   %rbp
push   %rbx
sub    $0x78,%rsp
mov    %ecx,%ebx
mov    %esi,%ecx
mov    %edi,%edx
lea    0xead(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   1060 <sprintf@plt>
mov    0x2e79(%rip),%rax
mov    0x2e7a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbp,%rdi
call   1040 <open@plt>
cmp    $0xffffffff,%eax
je     11af <pci_config_read_byte+0x6f>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x1,%edx
mov    %ebx,%ecx
call   1050 <pread@plt>
cmp    $0x1,%eax
je     11a2 <pci_config_read_byte+0x62>
movq   $0x0,0x8(%rsp)
test   %ebp,%ebp
jle    11b8 <pci_config_read_byte+0x78>
mov    %ebp,%edi
call   1030 <close@plt>
jmp    11b8 <pci_config_read_byte+0x78>
movq   $0x0,0x8(%rsp)
mov    0x8(%rsp),%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_byte(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 %31
24: ; preds = %4
%25 = load i32, i32* %11
%26 = load i32, i32* %8
%27 = call i32 @pread(i32 %25, i64* %9, i32 1, i32 %26)
%28 = icmp ne i32 %27, 1
br i1 %28, label %29, label %30
29: ; preds = %24
store i64 0, i64* %9
br label %30
30: ; preds = %29, %24
br label %31
31: ; preds = %30, %23
%32 = load i32, i32* %11
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = load i32, i32* %11
%36 = call i32 @close(i32 %35)
br label %37
37: ; preds = %34, %31
%38 = load i64, i64* %9
ret i64 %38
}

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_byte(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 %15, label %16
15: ; preds = %4
store i64 0, i64* %5
br label %24
16: ; preds = %4
%17 = call i32 @pread(i32 %13, i64* %5, i32 1, i32 %3)
%18 = icmp eq i32 %17, 1
br i1 %18, label %20, label %19
19: ; preds = %16
store i64 0, i64* %5
br label %20
20: ; preds = %16, %19
%21 = icmp sgt i32 %13, 0
br i1 %21, label %22, label %24
22: ; preds = %20
%23 = tail call i32 @close(i32 %13)
br label %24
24: ; preds = %15, %22, %20
%25 = load i64, i64* %5
ret i64 %25
}