#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *, int *, int *, int *);
int has_pci_device(int vendor, int device) {
  FILE *f;
  int num, fn, ven, dev;
  int res = 1;
  f = fopen("proc/bus/pci/devices", "r");
  if (!f)
    return 0;
  while (fscanf(f, "%02x%02x\t%04x%04x\t%*[^\n]", &num, &fn, &ven, &dev) == 4) {
    if (ven == vendor && dev == device)
      goto found;
  }
  res = 0;
found:
  fclose(f);
  return res;
}


x86 assembly (clang, -O0):
<has_pci_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x1,-0x2c(%rbp)
lea    0xeb4(%rip),%rdi
lea    0xec2(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1173 <has_pci_device+0x43>
movl   $0x0,-0x4(%rbp)
jmp    11d9 <has_pci_device+0xa9>
jmp    1178 <has_pci_device+0x48>
mov    -0x18(%rbp),%rdi
lea    0xe94(%rip),%rsi
lea    -0x1c(%rbp),%rdx
lea    -0x20(%rbp),%rcx
lea    -0x24(%rbp),%r8
lea    -0x28(%rbp),%r9
call   1040 <fscanf@plt>
cmp    $0x4,%eax
jne    11c3 <has_pci_device+0x93>
mov    -0x24(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    11be <has_pci_device+0x8e>
mov    -0x28(%rbp),%eax
cmp    -0xc(%rbp),%eax
jne    11be <has_pci_device+0x8e>
jmp    11ca <has_pci_device+0x9a>
jmp    1178 <has_pci_device+0x48>
movl   $0x0,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<has_pci_device>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,0x4(%rsp)
mov    %edi,%ebp
lea    0xeb5(%rip),%rdi
lea    0xec3(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11b7 <has_pci_device+0x87>
mov    %rax,%rbx
lea    0x14(%rsp),%r12
lea    0x10(%rsp),%r13
lea    0xc(%rsp),%r14
lea    0x8(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
lea    0xe8d(%rip),%rsi
mov    %r12,%rdx
mov    %r13,%rcx
mov    %r14,%r8
mov    %r15,%r9
call   1040 <fscanf@plt>
cmp    $0x4,%eax
jne    11bb <has_pci_device+0x8b>
cmp    %ebp,0xc(%rsp)
jne    1180 <has_pci_device+0x50>
mov    0x4(%rsp),%eax
cmp    %eax,0x8(%rsp)
jne    1180 <has_pci_device+0x50>
mov    $0x1,%ebp
jmp    11bd <has_pci_device+0x8d>
xor    %ebp,%ebp
jmp    11c5 <has_pci_device+0x95>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"proc/bus/pci/devices\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [25 x i8] c"%02x%02x\09%04x%04x\09%*[^\0A]\00"
define i32 @has_pci_device(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 1, i32* %11
%12 = call i32* @fopen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %6
%13 = load i32*, i32** %6
%14 = icmp ne i32* %13, null
br i1 %14, label %16, label %15
15: ; preds = %2
store i32 0, i32* %3
br label %36
16: ; preds = %2
br label %17
17: ; preds = %30, %16
%18 = load i32*, i32** %6
%19 = call i32 @fscanf(i32* %18, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32* %7, i32* %8, i32* %9, i32* %10)
%20 = icmp eq i32 %19, 4
br i1 %20, label %21, label %31
21: ; preds = %17
%22 = load i32, i32* %9
%23 = load i32, i32* %4
%24 = icmp eq i32 %22, %23
br i1 %24, label %25, label %30
25: ; preds = %21
%26 = load i32, i32* %10
%27 = load i32, i32* %5
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %25
br label %32
30: ; preds = %25, %21
br label %17
31: ; preds = %17
store i32 0, i32* %11
br label %32
32: ; preds = %31, %29
%33 = load i32*, i32** %6
%34 = call i32 @fclose(i32* %33)
%35 = load i32, i32* %11
store i32 %35, i32* %3
br label %36
36: ; preds = %32, %15
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
@.str = [21 x i8] c"proc/bus/pci/devices\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [25 x i8] c"%02x%02x\09%04x%04x\09%*[^\0A]\00"
define i32 @has_pci_device(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = bitcast i32* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = bitcast i32* %5 to i8*
%10 = bitcast i32* %6 to i8*
%11 = tail call i32* @fopen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %25, label %13
13: ; preds = %2, %16
%14 = call i32 @fscanf(i32* %11, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32* %3, i32* %4, i32* %5, i32* %6)
%15 = icmp eq i32 %14, 4
br i1 %15, label %16, label %22
16: ; preds = %13
%17 = load i32, i32* %5
%18 = icmp eq i32 %17, %0
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, %1
%21 = select i1 %18, i1 %20, i1 false
br i1 %21, label %22, label %13
22: ; preds = %13, %16
%23 = phi i32 [ 1, %16 ], [ 0, %13 ]
%24 = call i32 @fclose(i32* %11)
br label %25
25: ; preds = %2, %22
%26 = phi i32 [ %23, %22 ], [ 0, %2 ]
ret i32 %26
}