#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 uint8_t;
typedef int uint32_t;
typedef int uint16_t;

/* Variables and functions */
int inp(int);
int inpd(int);
int /*<<< orphan*/ pci16_select_reg(int, int);
uint16_t pci16_find_device(uint32_t search_item, uint16_t index,
                           int search_class) {
  uint32_t data;
  uint16_t bus_dev_fn;
  uint8_t max_bus;
  uint8_t hdr_type;
  uint8_t subordinate;
  int step;
  int found;
  if (search_class) {

    ;
  } else

    ;
  bus_dev_fn = 0;
  max_bus = 0;
  do {
    if ((bus_dev_fn & 7) == 0) {
      pci16_select_reg(bus_dev_fn, 0x0E);
      hdr_type = inp(0xCFC + (0x0E & 3));
      if (hdr_type == 0xFF) {
        bus_dev_fn += 8;
        continue;
      }
      if (hdr_type & 0x80)
        step = 1;
      else
        step = 8;
    }
    if ((hdr_type & 7) == 1 && (bus_dev_fn >> 8) == 0) {
      pci16_select_reg(bus_dev_fn, 0x1A);
      subordinate = inp(0xCFC + (0x1A & 3));
      if (subordinate > max_bus)
        max_bus = subordinate;
    }
    pci16_select_reg(bus_dev_fn, search_class ? 0x08 : 0x00);
    data = inpd(0xCFC);
    found = 0;
    if (search_class)
      data >>= 8;
    if (data == search_item)
      found = 1;
    if (found && !index--)
      break;
    bus_dev_fn += step;
  } while ((bus_dev_fn >> 8) <= max_bus);
  if (index == 0xFFFF)

    ;
  return index == 0xFFFF ? bus_dev_fn : 0xFFFF;
}


x86 assembly (clang, -O0):
<pci16_find_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1150 <pci16_find_device+0x20>
jmp    1155 <pci16_find_device+0x25>
jmp    1155 <pci16_find_device+0x25>
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x14(%rbp),%eax
and    $0x7,%eax
cmp    $0x0,%eax
jne    11d0 <pci16_find_device+0xa0>
mov    -0x14(%rbp),%edi
mov    $0xe,%esi
call   1030 <pci16_select_reg@plt>
mov    $0xcfe,%edi
call   1040 <inp@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xff,-0x1c(%rbp)
jne    11a7 <pci16_find_device+0x77>
mov    -0x14(%rbp),%eax
add    $0x8,%eax
mov    %eax,-0x14(%rbp)
jmp    129d <pci16_find_device+0x16d>
mov    -0x1c(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
je     11c4 <pci16_find_device+0x94>
movl   $0x1,-0x24(%rbp)
jmp    11cb <pci16_find_device+0x9b>
movl   $0x8,-0x24(%rbp)
jmp    11d0 <pci16_find_device+0xa0>
mov    -0x1c(%rbp),%eax
and    $0x7,%eax
cmp    $0x1,%eax
jne    121f <pci16_find_device+0xef>
mov    -0x14(%rbp),%eax
sar    $0x8,%eax
cmp    $0x0,%eax
jne    121f <pci16_find_device+0xef>
mov    -0x14(%rbp),%edi
mov    $0x1a,%esi
call   1030 <pci16_select_reg@plt>
mov    $0xcfe,%edi
call   1040 <inp@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x18(%rbp),%eax
jle    121a <pci16_find_device+0xea>
mov    -0x20(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    121f <pci16_find_device+0xef>
mov    -0x14(%rbp),%edi
mov    -0xc(%rbp),%ecx
xor    %esi,%esi
mov    $0x8,%eax
cmp    $0x0,%ecx
cmovne %eax,%esi
call   1030 <pci16_select_reg@plt>
mov    $0xcfc,%edi
call   1050 <inpd@plt>
mov    %eax,-0x10(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     125e <pci16_find_device+0x12e>
mov    -0x10(%rbp),%eax
sar    $0x8,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x4(%rbp),%eax
jne    1271 <pci16_find_device+0x141>
movl   $0x1,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     1294 <pci16_find_device+0x164>
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x8(%rbp)
cmp    $0x0,%eax
jne    1294 <pci16_find_device+0x164>
jmp    12ac <pci16_find_device+0x17c>
mov    -0x24(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
sar    $0x8,%eax
cmp    -0x18(%rbp),%eax
jle    1163 <pci16_find_device+0x33>
cmpl   $0xffff,-0x8(%rbp)
jne    12be <pci16_find_device+0x18e>
jmp    12be <pci16_find_device+0x18e>
cmpl   $0xffff,-0x8(%rbp)
jne    12d6 <pci16_find_device+0x1a6>
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    12e3 <pci16_find_device+0x1b3>
mov    $0xffff,%eax
mov    %eax,-0x2c(%rbp)
jmp    12e3 <pci16_find_device+0x1b3>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pci16_find_device>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,(%rsp)
xor    %r12d,%r12d
mov    %edx,0x4(%rsp)
test   %edx,%edx
setne  %r12b
shl    $0x3,%r12d
xor    %ebp,%ebp
xor    %r14d,%r14d
jmp    1171 <pci16_find_device+0x41>
nopl   0x0(%rax,%rax,1)
add    %r15d,%ebp
mov    %ebp,%eax
sar    $0x8,%eax
cmp    %r14d,%eax
jg     1229 <pci16_find_device+0xf9>
test   $0x7,%bpl
je     11e0 <pci16_find_device+0xb0>
mov    %r13d,%eax
and    $0x7,%eax
cmp    $0x1,%eax
jne    11ab <pci16_find_device+0x7b>
cmp    $0xff,%ebp
ja     11ab <pci16_find_device+0x7b>
mov    %ebp,%edi
mov    $0x1a,%esi
call   1030 <pci16_select_reg@plt>
mov    $0xcfe,%edi
call   1040 <inp@plt>
cmp    %r14d,%eax
jg     11a8 <pci16_find_device+0x78>
mov    %r14d,%eax
mov    %eax,%r14d
mov    %ebp,%edi
mov    %r12d,%esi
call   1030 <pci16_select_reg@plt>
mov    $0xcfc,%edi
call   1050 <inpd@plt>
mov    %eax,%ecx
sar    $0x8,%ecx
cmpl   $0x0,0x4(%rsp)
cmove  %eax,%ecx
cmp    (%rsp),%ecx
jne    1160 <pci16_find_device+0x30>
sub    $0x1,%ebx
jae    1160 <pci16_find_device+0x30>
jmp    1239 <pci16_find_device+0x109>
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    $0xe,%esi
call   1030 <pci16_select_reg@plt>
mov    $0xcfe,%edi
call   1040 <inp@plt>
cmp    $0xff,%eax
jne    120b <pci16_find_device+0xdb>
add    $0x8,%ebp
mov    $0xff,%r13d
jmp    1163 <pci16_find_device+0x33>
mov    %eax,%r13d
mov    $0x8,%r15d
test   $0x80,%r13b
je     1177 <pci16_find_device+0x47>
mov    $0x1,%r15d
jmp    1177 <pci16_find_device+0x47>
cmp    $0xffff,%ebx
mov    $0xffff,%eax
cmove  %ebp,%eax
jmp    123e <pci16_find_device+0x10e>
mov    $0xffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @pci16_find_device(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%14 = load i32, i32* %6
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %3
br label %18
17: ; preds = %3
br label %18
18: ; preds = %17, %16
store i32 0, i32* %8
store i32 0, i32* %9
br label %19
19: ; preds = %87, %18
%20 = load i32, i32* %8
%21 = and i32 %20, 7
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %39
23: ; preds = %19
%24 = load i32, i32* %8
%25 = call i32 @pci16_select_reg(i32 %24, i32 14)
%26 = call i32 @inp(i32 3326)
store i32 %26, i32* %10
%27 = load i32, i32* %10
%28 = icmp eq i32 %27, 255
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = load i32, i32* %8
%31 = add nsw i32 %30, 8
store i32 %31, i32* %8
br label %87
32: ; preds = %23
%33 = load i32, i32* %10
%34 = and i32 %33, 128
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %32
store i32 1, i32* %12
br label %38
37: ; preds = %32
store i32 8, i32* %12
br label %38
38: ; preds = %37, %36
br label %39
39: ; preds = %38, %19
%40 = load i32, i32* %10
%41 = and i32 %40, 7
%42 = icmp eq i32 %41, 1
br i1 %42, label %43, label %57
43: ; preds = %39
%44 = load i32, i32* %8
%45 = ashr i32 %44, 8
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %57
47: ; preds = %43
%48 = load i32, i32* %8
%49 = call i32 @pci16_select_reg(i32 %48, i32 26)
%50 = call i32 @inp(i32 3326)
store i32 %50, i32* %11
%51 = load i32, i32* %11
%52 = load i32, i32* %9
%53 = icmp sgt i32 %51, %52
br i1 %53, label %54, label %56
54: ; preds = %47
%55 = load i32, i32* %11
store i32 %55, i32* %9
br label %56
56: ; preds = %54, %47
br label %57
57: ; preds = %56, %43, %39
%58 = load i32, i32* %8
%59 = load i32, i32* %6
%60 = icmp ne i32 %59, 0
%61 = zext i1 %60 to i64
%62 = select i1 %60, i32 8, i32 0
%63 = call i32 @pci16_select_reg(i32 %58, i32 %62)
%64 = call i32 @inpd(i32 3324)
store i32 %64, i32* %7
store i32 0, i32* %13
%65 = load i32, i32* %6
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %70
67: ; preds = %57
%68 = load i32, i32* %7
%69 = ashr i32 %68, 8
store i32 %69, i32* %7
br label %70
70: ; preds = %67, %57
%71 = load i32, i32* %7
%72 = load i32, i32* %4
%73 = icmp eq i32 %71, %72
br i1 %73, label %74, label %75
74: ; preds = %70
store i32 1, i32* %13
br label %75
75: ; preds = %74, %70
%76 = load i32, i32* %13
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %83
78: ; preds = %75
%79 = load i32, i32* %5
%80 = add nsw i32 %79, -1
store i32 %80, i32* %5
%81 = icmp ne i32 %79, 0
br i1 %81, label %83, label %82
82: ; preds = %78
br label %92
83: ; preds = %78, %75
%84 = load i32, i32* %12
%85 = load i32, i32* %8
%86 = add nsw i32 %85, %84
store i32 %86, i32* %8
br label %87
87: ; preds = %83, %29
%88 = load i32, i32* %8
%89 = ashr i32 %88, 8
%90 = load i32, i32* %9
%91 = icmp sle i32 %89, %90
br i1 %91, label %19, label %92
92: ; preds = %87, %82
%93 = load i32, i32* %5
%94 = icmp eq i32 %93, 65535
br i1 %94, label %95, label %96
95: ; preds = %92
br label %96
96: ; preds = %95, %92
%97 = load i32, i32* %5
%98 = icmp eq i32 %97, 65535
br i1 %98, label %99, label %101
99: ; preds = %96
%100 = load i32, i32* %8
br label %102
101: ; preds = %96
br label %102
102: ; preds = %101, %99
%103 = phi i32 [ %100, %99 ], [ 65535, %101 ]
ret i32 %103
}

LLVM IR (-O2):
define i32 @pci16_find_device(i32 %0, i32 %1, i32 %2) {
%4 = icmp eq i32 %2, 0
%5 = select i1 %4, i32 0, i32 8
br label %6
6: ; preds = %49, %3
%7 = phi i32 [ 0, %3 ], [ %50, %49 ]
%8 = phi i32 [ 0, %3 ], [ %51, %49 ]
%9 = phi i32 [ undef, %3 ], [ %52, %49 ]
%10 = phi i32 [ %1, %3 ], [ %53, %49 ]
%11 = phi i32 [ undef, %3 ], [ %54, %49 ]
%12 = and i32 %7, 7
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %24
14: ; preds = %6
%15 = tail call i32 @pci16_select_reg(i32 %7, i32 14)
%16 = tail call i32 @inp(i32 3326)
%17 = icmp eq i32 %16, 255
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = add nsw i32 %7, 8
br label %49
20: ; preds = %14
%21 = and i32 %16, 128
%22 = icmp eq i32 %21, 0
%23 = select i1 %22, i32 8, i32 1
br label %24
24: ; preds = %20, %6
%25 = phi i32 [ %9, %6 ], [ %16, %20 ]
%26 = phi i32 [ %11, %6 ], [ %23, %20 ]
%27 = and i32 %25, 7
%28 = icmp eq i32 %27, 1
%29 = icmp ult i32 %7, 256
%30 = select i1 %28, i1 %29, i1 false
br i1 %30, label %31, label %36
31: ; preds = %24
%32 = tail call i32 @pci16_select_reg(i32 %7, i32 26)
%33 = tail call i32 @inp(i32 3326)
%34 = icmp sgt i32 %33, %8
%35 = select i1 %34, i32 %33, i32 %8
br label %36
36: ; preds = %31, %24
%37 = phi i32 [ %8, %24 ], [ %35, %31 ]
%38 = tail call i32 @pci16_select_reg(i32 %7, i32 %5)
%39 = tail call i32 @inpd(i32 3324)
%40 = ashr i32 %39, 8
%41 = select i1 %4, i32 %39, i32 %40
%42 = icmp eq i32 %41, %0
br i1 %42, label %43, label %46
43: ; preds = %36
%44 = add nsw i32 %10, -1
%45 = icmp eq i32 %10, 0
br i1 %45, label %60, label %46
46: ; preds = %43, %36
%47 = phi i32 [ %44, %43 ], [ %10, %36 ]
%48 = add nsw i32 %26, %7
br label %49
49: ; preds = %46, %18
%50 = phi i32 [ %19, %18 ], [ %48, %46 ]
%51 = phi i32 [ %8, %18 ], [ %37, %46 ]
%52 = phi i32 [ 255, %18 ], [ %25, %46 ]
%53 = phi i32 [ %10, %18 ], [ %47, %46 ]
%54 = phi i32 [ %11, %18 ], [ %26, %46 ]
%55 = ashr i32 %50, 8
%56 = icmp sgt i32 %55, %51
br i1 %56, label %57, label %6
57: ; preds = %49
%58 = icmp eq i32 %53, 65535
%59 = select i1 %58, i32 %50, i32 65535
br label %60
60: ; preds = %43, %57
%61 = phi i32 [ %59, %57 ], [ 65535, %43 ]
ret i32 %61
}