#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 /*<<< orphan*/ DM_in_32(int, unsigned long *);
int /*<<< orphan*/ DM_out_32(int, int);
int locate_pci_device(unsigned long vendor_id, unsigned long device_id) {
  unsigned long reg;
  int device;
  for (device = 0; device < 32; device++) {
    DM_out_32(0x0cf8, (0 << 2) | (0x00 << 8) | (device << 11) | (0x00 << 16) |
                          (0x80000000));
    DM_in_32(0x0cfc, &reg);
    if ((vendor_id | (device_id << 16)) == reg)
      return device;
  }
  return -1;
}


x86 assembly (clang, -O0):
<locate_pci_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x20,-0x24(%rbp)
jge    11a2 <locate_pci_device+0x82>
mov    -0x24(%rbp),%esi
shl    $0xb,%esi
or     $0x0,%esi
or     $0x0,%esi
or     $0x80000000,%esi
mov    $0xcf8,%edi
call   1040 <DM_out_32@plt>
mov    $0xcfc,%edi
lea    -0x20(%rbp),%rsi
call   1030 <DM_in_32@plt>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
shl    $0x10,%rcx
or     %rcx,%rax
cmp    -0x20(%rbp),%rax
jne    118f <locate_pci_device+0x6f>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11a9 <locate_pci_device+0x89>
jmp    1194 <locate_pci_device+0x74>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1137 <locate_pci_device+0x17>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<locate_pci_device>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
shl    $0x10,%r15
or     %rdi,%r15
xor    %ebx,%ebx
mov    %rsp,%r14
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    %ebx,%esi
or     $0x80000000,%esi
mov    $0xcf8,%edi
call   1040 <DM_out_32@plt>
mov    $0xcfc,%edi
mov    %r14,%rsi
call   1030 <DM_in_32@plt>
cmp    (%rsp),%r15
je     1178 <locate_pci_device+0x58>
add    $0x1,%ebp
add    $0x800,%ebx
cmp    $0x20,%ebp
jne    1140 <locate_pci_device+0x20>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @locate_pci_device(i64 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
store i64 %0, i64* %4
store i64 %1, i64* %5
store i32 0, i32* %7
br label %8
8: ; preds = %28, %2
%9 = load i32, i32* %7
%10 = icmp slt i32 %9, 32
br i1 %10, label %11, label %31
11: ; preds = %8
%12 = load i32, i32* %7
%13 = shl i32 %12, 11
%14 = or i32 0, %13
%15 = or i32 %14, 0
%16 = or i32 %15, -2147483648
%17 = call i32 @DM_out_32(i32 3320, i32 %16)
%18 = call i32 @DM_in_32(i32 3324, i64* %6)
%19 = load i64, i64* %4
%20 = load i64, i64* %5
%21 = shl i64 %20, 16
%22 = or i64 %19, %21
%23 = load i64, i64* %6
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %11
%26 = load i32, i32* %7
store i32 %26, i32* %3
br label %32
27: ; preds = %11
br label %28
28: ; preds = %27
%29 = load i32, i32* %7
%30 = add nsw i32 %29, 1
store i32 %30, i32* %7
br label %8
31: ; preds = %8
store i32 -1, i32* %3
br label %32
32: ; preds = %31, %25
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
define i32 @locate_pci_device(i64 %0, i64 %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
%5 = shl i64 %1, 16
%6 = or i64 %5, %0
br label %7
7: ; preds = %2, %15
%8 = phi i32 [ 0, %2 ], [ %16, %15 ]
%9 = shl nuw nsw i32 %8, 11
%10 = or i32 %9, -2147483648
%11 = call i32 @DM_out_32(i32 3320, i32 %10)
%12 = call i32 @DM_in_32(i32 3324, i64* %3)
%13 = load i64, i64* %3
%14 = icmp eq i64 %6, %13
br i1 %14, label %18, label %15
15: ; preds = %7
%16 = add nuw nsw i32 %8, 1
%17 = icmp eq i32 %16, 32
br i1 %17, label %18, label %7
18: ; preds = %15, %7
%19 = phi i32 [ %8, %7 ], [ -1, %15 ]
ret i32 %19
}