#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 inb(unsigned long);
int /*<<< orphan*/ outb(unsigned int, unsigned long);
int i8254_read(unsigned long base_address, unsigned int regshift,
               unsigned int counter_number) {
  unsigned int byte;
  int ret;
  if (counter_number > 2)
    return -1;
  byte = counter_number << 6;
  outb(byte, base_address + (3 << regshift));
  ret = inb(base_address + (counter_number << regshift));
  ret += inb(base_address + (counter_number << regshift)) << 8;
  return ret;
}


x86 assembly (clang, -O0):
<i8254_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
cmpl   $0x2,-0x18(%rbp)
jbe    1148 <i8254_read+0x28>
movl   $0xffffffff,-0x4(%rbp)
jmp    11aa <i8254_read+0x8a>
mov    -0x18(%rbp),%eax
shl    $0x6,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%ecx
mov    $0x3,%eax
shl    %cl,%eax
cltq
add    %rax,%rsi
call   1030 <outb@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
shl    %cl,%eax
mov    %eax,%eax
add    %rax,%rdi
call   1040 <inb@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
shl    %cl,%eax
mov    %eax,%eax
add    %rax,%rdi
call   1040 <inb@plt>
shl    $0x8,%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i8254_read>:
mov    $0xffffffff,%eax
cmp    $0x2,%edx
ja     1170 <i8254_read+0x50>
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebx
mov    %esi,%ebp
mov    %rdi,%r14
mov    $0x3,%eax
mov    %ebp,%ecx
shl    %cl,%eax
mov    %edx,%edi
shl    $0x6,%edi
movslq %eax,%rsi
add    %r14,%rsi
call   1030 <outb@plt>
mov    %ebp,%ecx
shl    %cl,%ebx
add    %r14,%rbx
mov    %rbx,%rdi
call   1040 <inb@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <inb@plt>
shl    $0x8,%eax
add    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @i8254_read(i64 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i64 %0, i64* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i32, i32* %7
%11 = icmp ugt i32 %10, 2
br i1 %11, label %12, label %13
12: ; preds = %3
store i32 -1, i32* %4
br label %41
13: ; preds = %3
%14 = load i32, i32* %7
%15 = shl i32 %14, 6
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = load i64, i64* %5
%18 = load i32, i32* %6
%19 = shl i32 3, %18
%20 = sext i32 %19 to i64
%21 = add i64 %17, %20
%22 = call i32 @outb(i32 %16, i64 %21)
%23 = load i64, i64* %5
%24 = load i32, i32* %7
%25 = load i32, i32* %6
%26 = shl i32 %24, %25
%27 = zext i32 %26 to i64
%28 = add i64 %23, %27
%29 = call i32 @inb(i64 %28)
store i32 %29, i32* %9
%30 = load i64, i64* %5
%31 = load i32, i32* %7
%32 = load i32, i32* %6
%33 = shl i32 %31, %32
%34 = zext i32 %33 to i64
%35 = add i64 %30, %34
%36 = call i32 @inb(i64 %35)
%37 = shl i32 %36, 8
%38 = load i32, i32* %9
%39 = add nsw i32 %38, %37
store i32 %39, i32* %9
%40 = load i32, i32* %9
store i32 %40, i32* %4
br label %41
41: ; preds = %13, %12
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
define i32 @i8254_read(i64 %0, i32 %1, i32 %2) {
%4 = icmp ugt i32 %2, 2
br i1 %4, label %18, label %5
5: ; preds = %3
%6 = shl nuw nsw i32 %2, 6
%7 = shl i32 3, %1
%8 = sext i32 %7 to i64
%9 = add i64 %8, %0
%10 = tail call i32 @outb(i32 %6, i64 %9)
%11 = shl i32 %2, %1
%12 = zext i32 %11 to i64
%13 = add i64 %12, %0
%14 = tail call i32 @inb(i64 %13)
%15 = tail call i32 @inb(i64 %13)
%16 = shl i32 %15, 8
%17 = add nsw i32 %16, %14
br label %18
18: ; preds = %3, %5
%19 = phi i32 [ %17, %5 ], [ -1, %3 ]
ret i32 %19
}