#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*/ PCI_DEVICE_ID;
int /*<<< orphan*/ PCI_VENDOR_ID;
short bswap_16(short);
int /*<<< orphan*/ close(int);
int open(char *, int);
int pread(int, short *, int, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char, unsigned char);
int pci_get_vendor(unsigned char bus, unsigned char dev, int func) {
  int retval;
  char path[100];
  int fd;
  short vendor, device;
  sprintf(path, "/proc/bus/pci/%02d/%02x.0", bus, dev);
  fd = open(path, O_RDONLY | O_SYNC);
  if (fd == -1) {
    retval = 0xFFFF;
  } else if (pread(fd, &vendor, 2, PCI_VENDOR_ID) == 2 &&
             pread(fd, &device, 2, PCI_DEVICE_ID) == 2) {
    vendor = bswap_16(vendor);
    device = bswap_16(device);
    retval = vendor + (device << 16);
  } else {
    retval = 0xFFFF;
  }
  if (fd > 0) {
    close(fd);
  }
  return retval;
}


x86 assembly (clang, -O0):
<pci_get_vendor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %sil,%al
mov    %dil,%cl
mov    %cl,-0x1(%rbp)
mov    %al,-0x2(%rbp)
mov    %edx,-0x8(%rbp)
lea    -0x70(%rbp),%rdi
mov    -0x1(%rbp),%al
lea    0xe88(%rip),%rsi
movzbl %al,%edx
movzbl -0x2(%rbp),%ecx
call   1070 <sprintf@plt>
lea    -0x70(%rbp),%rdi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
mov    0x2e48(%rip),%rax
or     (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0xffffffff,-0x74(%rbp)
jne    11b8 <pci_get_vendor+0x68>
movl   $0xffff,-0xc(%rbp)
jmp    1239 <pci_get_vendor+0xe9>
mov    -0x74(%rbp),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%ecx
lea    -0x76(%rbp),%rsi
mov    $0x2,%edx
call   1060 <pread@plt>
cmp    $0x2,%eax
jne    122d <pci_get_vendor+0xdd>
mov    -0x74(%rbp),%edi
mov    0x2e03(%rip),%rax
mov    (%rax),%ecx
lea    -0x78(%rbp),%rsi
mov    $0x2,%edx
call   1060 <pread@plt>
cmp    $0x2,%eax
jne    122d <pci_get_vendor+0xdd>
movswl -0x76(%rbp),%edi
call   1050 <bswap_16@plt>
mov    %ax,-0x76(%rbp)
movswl -0x78(%rbp),%edi
call   1050 <bswap_16@plt>
mov    %ax,-0x78(%rbp)
movswl -0x76(%rbp),%eax
movswl -0x78(%rbp),%ecx
shl    $0x10,%ecx
add    %ecx,%eax
mov    %eax,-0xc(%rbp)
jmp    1234 <pci_get_vendor+0xe4>
movl   $0xffff,-0xc(%rbp)
jmp    1239 <pci_get_vendor+0xe9>
cmpl   $0x0,-0x74(%rbp)
jle    124b <pci_get_vendor+0xfb>
mov    -0x74(%rbp),%edi
call   1030 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pci_get_vendor>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    %esi,%ecx
mov    %edi,%edx
lea    0xe9a(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
mov    0x2e5e(%rip),%rax
mov    0x2e5f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1040 <open@plt>
cmp    $0xffffffff,%eax
je     1206 <pci_get_vendor+0xb6>
mov    %eax,%ebx
mov    0x2e2d(%rip),%rax
mov    (%rax),%ecx
lea    0xe(%rsp),%rsi
mov    %ebx,%edi
mov    $0x2,%edx
call   1060 <pread@plt>
mov    $0xffff,%ebp
cmp    $0x2,%eax
jne    11f9 <pci_get_vendor+0xa9>
mov    0x2e29(%rip),%rax
mov    (%rax),%ecx
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
mov    $0x2,%edx
call   1060 <pread@plt>
cmp    $0x2,%eax
jne    11f9 <pci_get_vendor+0xa9>
movswl 0xe(%rsp),%edi
call   1050 <bswap_16@plt>
mov    %eax,%r14d
movswl 0xc(%rsp),%edi
call   1050 <bswap_16@plt>
mov    %eax,%ebp
movswl %r14w,%eax
shl    $0x10,%ebp
add    %eax,%ebp
test   %ebx,%ebx
jle    120b <pci_get_vendor+0xbb>
mov    %ebx,%edi
call   1030 <close@plt>
jmp    120b <pci_get_vendor+0xbb>
mov    $0xffff,%ebp
mov    %ebp,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
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
@PCI_VENDOR_ID = global i32 0
@PCI_DEVICE_ID = global i32 0
define i32 @pci_get_vendor(i8 zeroext %0, i8 zeroext %1, i32 %2) {
%4 = alloca i8
%5 = alloca i8
%6 = alloca i32
%7 = alloca i32
%8 = alloca [100 x i8]
%9 = alloca i32
%10 = alloca i16
%11 = alloca i16
store i8 %0, i8* %4
store i8 %1, i8* %5
store i32 %2, i32* %6
%12 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%13 = load i8, i8* %4
%14 = load i8, i8* %5
%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]* %8, 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* %9
%21 = load i32, i32* %9
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %3
store i32 65535, i32* %7
br label %47
24: ; preds = %3
%25 = load i32, i32* %9
%26 = load i32, i32* @PCI_VENDOR_ID
%27 = call i32 @pread(i32 %25, i16* %10, i32 2, i32 %26)
%28 = icmp eq i32 %27, 2
br i1 %28, label %29, label %45
29: ; preds = %24
%30 = load i32, i32* %9
%31 = load i32, i32* @PCI_DEVICE_ID
%32 = call i32 @pread(i32 %30, i16* %11, i32 2, i32 %31)
%33 = icmp eq i32 %32, 2
br i1 %33, label %34, label %45
34: ; preds = %29
%35 = load i16, i16* %10
%36 = call signext i16 @bswap_16(i16 signext %35)
store i16 %36, i16* %10
%37 = load i16, i16* %11
%38 = call signext i16 @bswap_16(i16 signext %37)
store i16 %38, i16* %11
%39 = load i16, i16* %10
%40 = sext i16 %39 to i32
%41 = load i16, i16* %11
%42 = sext i16 %41 to i32
%43 = shl i32 %42, 16
%44 = add nsw i32 %40, %43
store i32 %44, i32* %7
br label %46
45: ; preds = %29, %24
store i32 65535, i32* %7
br label %46
46: ; preds = %45, %34
br label %47
47: ; preds = %46, %23
%48 = load i32, i32* %9
%49 = icmp sgt i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %47
%51 = load i32, i32* %9
%52 = call i32 @close(i32 %51)
br label %53
53: ; preds = %50, %47
%54 = load i32, i32* %7
ret i32 %54
}

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
@PCI_VENDOR_ID = global i32 0
@PCI_DEVICE_ID = global i32 0
define i32 @pci_get_vendor(i8 zeroext %0, i8 zeroext %1, i32 %2) {
%4 = alloca [100 x i8]
%5 = alloca i16
%6 = alloca i16
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%8 = bitcast i16* %5 to i8*
%9 = bitcast i16* %6 to i8*
%10 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8 zeroext %0, i8 zeroext %1)
%11 = load i32, i32* @O_RDONLY
%12 = load i32, i32* @O_SYNC
%13 = or i32 %12, %11
%14 = call i32 @open(i8* %7, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %38, label %16
16: ; preds = %3
%17 = load i32, i32* @PCI_VENDOR_ID
%18 = call i32 @pread(i32 %14, i16* %5, i32 2, i32 %17)
%19 = icmp eq i32 %18, 2
br i1 %19, label %20, label %33
20: ; preds = %16
%21 = load i32, i32* @PCI_DEVICE_ID
%22 = call i32 @pread(i32 %14, i16* %6, i32 2, i32 %21)
%23 = icmp eq i32 %22, 2
br i1 %23, label %24, label %33
24: ; preds = %20
%25 = load i16, i16* %5
%26 = tail call signext i16 @bswap_16(i16 signext %25)
%27 = load i16, i16* %6
%28 = tail call signext i16 @bswap_16(i16 signext %27)
%29 = sext i16 %26 to i32
%30 = zext i16 %28 to i32
%31 = shl nuw i32 %30, 16
%32 = add nsw i32 %31, %29
br label %33
33: ; preds = %16, %20, %24
%34 = phi i32 [ %32, %24 ], [ 65535, %20 ], [ 65535, %16 ]
%35 = icmp sgt i32 %14, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = tail call i32 @close(i32 %14)
br label %38
38: ; preds = %3, %36, %33
%39 = phi i32 [ %34, %36 ], [ %34, %33 ], [ 65535, %3 ]
ret i32 %39
}