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


x86 assembly (clang, -O0):
<i8254_mm_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_mm_read+0x28>
movl   $0xffffffff,-0x4(%rbp)
jmp    11aa <i8254_mm_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   1040 <writeb@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
shl    %cl,%eax
mov    %eax,%eax
add    %rax,%rdi
call   1030 <readb@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   1030 <readb@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_mm_read>:
mov    $0xffffffff,%eax
cmp    $0x2,%edx
ja     1170 <i8254_mm_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   1040 <writeb@plt>
mov    %ebp,%ecx
shl    %cl,%ebx
add    %r14,%rbx
mov    %rbx,%rdi
call   1030 <readb@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <readb@plt>
shl    $0x8,%eax
add    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @i8254_mm_read(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %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 i8*, i8** %5
%18 = load i32, i32* %6
%19 = shl i32 3, %18
%20 = sext i32 %19 to i64
%21 = getelementptr i8, i8* %17, i64 %20
%22 = call i32 @writeb(i32 %16, i8* %21)
%23 = load i8*, i8** %5
%24 = load i32, i32* %7
%25 = load i32, i32* %6
%26 = shl i32 %24, %25
%27 = zext i32 %26 to i64
%28 = getelementptr i8, i8* %23, i64 %27
%29 = call i32 @readb(i8* %28)
store i32 %29, i32* %9
%30 = load i8*, i8** %5
%31 = load i32, i32* %7
%32 = load i32, i32* %6
%33 = shl i32 %31, %32
%34 = zext i32 %33 to i64
%35 = getelementptr i8, i8* %30, i64 %34
%36 = call i32 @readb(i8* %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_mm_read(i8* %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 = getelementptr i8, i8* %0, i64 %8
%10 = tail call i32 @writeb(i32 %6, i8* %9)
%11 = shl i32 %2, %1
%12 = zext i32 %11 to i64
%13 = getelementptr i8, i8* %0, i64 %12
%14 = tail call i32 @readb(i8* %13)
%15 = tail call i32 @readb(i8* %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
}