#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 */
unsigned long in_long(int);
int /*<<< orphan*/ out_long(int, unsigned long);
int /*<<< orphan*/ printl(char *);
void pci_scan_devices(int bus_nr) {
  unsigned long conf_reg = 0x80000000;
  unsigned long tmp = 0L;
  printl("Scaning PCI device.\n");
  tmp = bus_nr;
  tmp &= 0x000000FF;
  conf_reg += (tmp << 16);
  int i = 0;
  for (i = 0; i < 0x100; i++) {
    conf_reg &= 0xFFFF0000;
    conf_reg += (i << 8);
    out_long(0xCF8, conf_reg);
    tmp = in_long(0xCFC);
    if (0xFFFFFFFF == tmp)
      continue;
    printl("A PCI device found.\n");
  }
  return;
}


x86 assembly (clang, -O0):
<pci_scan_devices>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x80000000,%eax
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    0xead(%rip),%rdi
call   1040 <printl@plt>
movslq -0x4(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
and    $0xff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
shl    $0x10,%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x100,-0x1c(%rbp)
jge    1205 <pci_scan_devices+0xd5>
movabs $0xffff0000,%rax
and    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x1c(%rbp),%eax
shl    $0x8,%eax
cltq
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
mov    $0xcf8,%edi
call   1050 <out_long@plt>
mov    $0xcfc,%edi
call   1030 <in_long@plt>
mov    %rax,-0x18(%rbp)
mov    $0xffffffff,%eax
cmp    -0x18(%rbp),%rax
jne    11eb <pci_scan_devices+0xbb>
jmp    11f7 <pci_scan_devices+0xc7>
lea    0xe23(%rip),%rdi
call   1040 <printl@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    118c <pci_scan_devices+0x5c>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pci_scan_devices>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0xebf(%rip),%rdi
call   1040 <printl@plt>
movzbl %bl,%r15d
shl    $0x10,%r15d
or     $0x80000000,%r15d
xor    %ebx,%ebx
mov    $0xffffffff,%r12d
lea    0xeb1(%rip),%r14
jmp    1180 <pci_scan_devices+0x50>
cs nopw 0x0(%rax,%rax,1)
add    $0x100,%rbx
cmp    $0x10000,%rbx
je     11b0 <pci_scan_devices+0x80>
and    $0xffff0000,%r15d
add    %rbx,%r15
mov    $0xcf8,%edi
mov    %r15,%rsi
call   1050 <out_long@plt>
mov    $0xcfc,%edi
call   1030 <in_long@plt>
cmp    %r12,%rax
je     1170 <pci_scan_devices+0x40>
mov    %r14,%rdi
call   1040 <printl@plt>
jmp    1170 <pci_scan_devices+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [21 x i8] c"Scaning PCI device.\0A\00"
@.str.1 = [21 x i8] c"A PCI device found.\0A\00"
define void @pci_scan_devices(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i64
%5 = alloca i32
store i32 %0, i32* %2
store i64 2147483648, i64* %3
store i64 0, i64* %4
%6 = call i32 @printl(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* %2
%8 = sext i32 %7 to i64
store i64 %8, i64* %4
%9 = load i64, i64* %4
%10 = and i64 %9, 255
store i64 %10, i64* %4
%11 = load i64, i64* %4
%12 = shl i64 %11, 16
%13 = load i64, i64* %3
%14 = add i64 %13, %12
store i64 %14, i64* %3
store i32 0, i32* %5
store i32 0, i32* %5
br label %15
15: ; preds = %34, %1
%16 = load i32, i32* %5
%17 = icmp slt i32 %16, 256
br i1 %17, label %18, label %37
18: ; preds = %15
%19 = load i64, i64* %3
%20 = and i64 %19, 4294901760
store i64 %20, i64* %3
%21 = load i32, i32* %5
%22 = shl i32 %21, 8
%23 = sext i32 %22 to i64
%24 = load i64, i64* %3
%25 = add i64 %24, %23
store i64 %25, i64* %3
%26 = load i64, i64* %3
%27 = call i32 @out_long(i32 3320, i64 %26)
%28 = call i64 @in_long(i32 3324)
store i64 %28, i64* %4
%29 = load i64, i64* %4
%30 = icmp eq i64 4294967295, %29
br i1 %30, label %31, label %32
31: ; preds = %18
br label %34
32: ; preds = %18
%33 = call i32 @printl(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %32, %31
%35 = load i32, i32* %5
%36 = add nsw i32 %35, 1
store i32 %36, i32* %5
br label %15
37: ; preds = %15
ret void
}

LLVM IR (-O2):
@.str = [21 x i8] c"Scaning PCI device.\0A\00"
@.str.1 = [21 x i8] c"A PCI device found.\0A\00"
define void @pci_scan_devices(i32 %0) {
%2 = tail call i32 @printl(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%3 = shl i32 %0, 16
%4 = and i32 %3, 16711680
%5 = or i32 %4, -2147483648
%6 = zext i32 %5 to i64
br label %7
7: ; preds = %1, %18
%8 = phi i64 [ 0, %1 ], [ %19, %18 ]
%9 = phi i64 [ %6, %1 ], [ %12, %18 ]
%10 = and i64 %9, 4294901760
%11 = shl nuw nsw i64 %8, 8
%12 = add nuw nsw i64 %10, %11
%13 = tail call i32 @out_long(i32 3320, i64 %12)
%14 = tail call i64 @in_long(i32 3324)
%15 = icmp eq i64 %14, 4294967295
br i1 %15, label %18, label %16
16: ; preds = %7
%17 = tail call i32 @printl(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %7, %16
%19 = add nuw nsw i64 %8, 1
%20 = icmp eq i64 %19, 256
br i1 %20, label %21, label %7
21: ; preds = %18
ret void
}