#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 ca5_readR(unsigned int);
int /*<<< orphan*/ printf(char *);
int ca5_digitalRead(int pin) {
  int bank = pin >> 5;
  int index = pin - (bank << 5);
  int val;
  unsigned int base_address = 0;
  unsigned int phys_OEN_R;
  unsigned int phys_SET_R;
  unsigned int phys_VAL_R;
  if (bank == 2)
    return -1;
  if (bank == 0)
    base_address = 0x20930000;
  else if (bank == 1)
    base_address = 0x20931000;
  else if (bank == 2)
    base_address = 0x11A08000;
  else if (bank == 3)
    base_address = 0x20932000;
  else {
    printf("Bad pin number\n");
    return -1;
  }
  phys_OEN_R = base_address + (0x00);
  phys_SET_R = base_address + (0x10);
  phys_VAL_R = base_address + (0x0C);
  if (ca5_readR(phys_OEN_R) & (1UL << (index)))
    val = (ca5_readR(phys_VAL_R) & (1UL << (index))) ? 1 : 0;
  else
    val = (ca5_readR(phys_SET_R) & (1UL << (index))) ? 1 : 0;
  return val;
}


x86 assembly (clang, -O0):
<ca5_digitalRead>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
sar    $0x5,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    -0xc(%rbp),%ecx
shl    $0x5,%ecx
sub    %ecx,%eax
mov    %eax,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x2,-0xc(%rbp)
jne    115f <ca5_digitalRead+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    127f <ca5_digitalRead+0x15f>
cmpl   $0x0,-0xc(%rbp)
jne    1175 <ca5_digitalRead+0x55>
movl   $0x20930000,-0x18(%rbp)
jmp    11de <ca5_digitalRead+0xbe>
cmpl   $0x1,-0xc(%rbp)
jne    118b <ca5_digitalRead+0x6b>
movl   $0x20931000,-0x18(%rbp)
jmp    11d9 <ca5_digitalRead+0xb9>
cmpl   $0x2,-0xc(%rbp)
jne    11a1 <ca5_digitalRead+0x81>
movl   $0x11a08000,-0x18(%rbp)
jmp    11d4 <ca5_digitalRead+0xb4>
cmpl   $0x3,-0xc(%rbp)
jne    11b7 <ca5_digitalRead+0x97>
movl   $0x20932000,-0x18(%rbp)
jmp    11cf <ca5_digitalRead+0xaf>
lea    0xe42(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127f <ca5_digitalRead+0x15f>
jmp    11d4 <ca5_digitalRead+0xb4>
jmp    11d9 <ca5_digitalRead+0xb9>
jmp    11de <ca5_digitalRead+0xbe>
mov    -0x18(%rbp),%eax
add    $0x0,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
add    $0x10,%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
add    $0xc,%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <ca5_readR@plt>
mov    -0x10(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%rdx
mov    %rdx,%rcx
and    %rcx,%rax
cmp    $0x0,%rax
je     124d <ca5_digitalRead+0x12d>
mov    -0x24(%rbp),%edi
call   1040 <ca5_readR@plt>
mov    %rax,%rdx
mov    -0x10(%rbp),%eax
mov    %eax,%ecx
mov    $0x1,%eax
shl    %cl,%rax
and    %rax,%rdx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    1279 <ca5_digitalRead+0x159>
mov    -0x20(%rbp),%edi
call   1040 <ca5_readR@plt>
mov    %rax,%rdx
mov    -0x10(%rbp),%eax
mov    %eax,%ecx
mov    $0x1,%eax
shl    %cl,%rax
and    %rax,%rdx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ca5_digitalRead>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%eax
sar    $0x5,%eax
mov    $0xffffffff,%ebp
cmp    $0x2,%eax
je     1183 <ca5_digitalRead+0x63>
mov    %edi,%ebx
mov    $0x20930000,%r14d
cmp    $0x20,%edi
jb     1158 <ca5_digitalRead+0x38>
cmp    $0x1,%eax
je     1152 <ca5_digitalRead+0x32>
cmp    $0x3,%eax
jne    118a <ca5_digitalRead+0x6a>
mov    $0x20932000,%r14d
jmp    1158 <ca5_digitalRead+0x38>
mov    $0x20931000,%r14d
and    $0x1f,%ebx
mov    %r14d,%edi
call   1040 <ca5_readR@plt>
xor    %ecx,%ecx
bt     %ebx,%eax
setae  %cl
lea    0xc(,%rcx,4),%edi
or     %r14d,%edi
call   1040 <ca5_readR@plt>
xor    %ebp,%ebp
bt     %ebx,%eax
setb   %bpl
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe6f(%rip),%rdi
call   1030 <puts@plt>
jmp    1183 <ca5_digitalRead+0x63>

LLVM IR (-O0):
@.str = [16 x i8] c"Bad pin number\0A\00"
define i32 @ca5_digitalRead(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %3
%11 = load i32, i32* %3
%12 = ashr i32 %11, 5
store i32 %12, i32* %4
%13 = load i32, i32* %3
%14 = load i32, i32* %4
%15 = shl i32 %14, 5
%16 = sub nsw i32 %13, %15
store i32 %16, i32* %5
store i32 0, i32* %7
%17 = load i32, i32* %4
%18 = icmp eq i32 %17, 2
br i1 %18, label %19, label %20
19: ; preds = %1
store i32 -1, i32* %2
br label %77
20: ; preds = %1
%21 = load i32, i32* %4
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %20
store i32 546504704, i32* %7
br label %41
24: ; preds = %20
%25 = load i32, i32* %4
%26 = icmp eq i32 %25, 1
br i1 %26, label %27, label %28
27: ; preds = %24
store i32 546508800, i32* %7
br label %40
28: ; preds = %24
%29 = load i32, i32* %4
%30 = icmp eq i32 %29, 2
br i1 %30, label %31, label %32
31: ; preds = %28
store i32 295731200, i32* %7
br label %39
32: ; preds = %28
%33 = load i32, i32* %4
%34 = icmp eq i32 %33, 3
br i1 %34, label %35, label %36
35: ; preds = %32
store i32 546512896, i32* %7
br label %38
36: ; preds = %32
%37 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %77
38: ; preds = %35
br label %39
39: ; preds = %38, %31
br label %40
40: ; preds = %39, %27
br label %41
41: ; preds = %40, %23
%42 = load i32, i32* %7
%43 = add i32 %42, 0
store i32 %43, i32* %8
%44 = load i32, i32* %7
%45 = add i32 %44, 16
store i32 %45, i32* %9
%46 = load i32, i32* %7
%47 = add i32 %46, 12
store i32 %47, i32* %10
%48 = load i32, i32* %8
%49 = call i64 @ca5_readR(i32 %48)
%50 = load i32, i32* %5
%51 = zext i32 %50 to i64
%52 = shl i64 1, %51
%53 = and i64 %49, %52
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %65
55: ; preds = %41
%56 = load i32, i32* %10
%57 = call i64 @ca5_readR(i32 %56)
%58 = load i32, i32* %5
%59 = zext i32 %58 to i64
%60 = shl i64 1, %59
%61 = and i64 %57, %60
%62 = icmp ne i64 %61, 0
%63 = zext i1 %62 to i64
%64 = select i1 %62, i32 1, i32 0
store i32 %64, i32* %6
br label %75
65: ; preds = %41
%66 = load i32, i32* %9
%67 = call i64 @ca5_readR(i32 %66)
%68 = load i32, i32* %5
%69 = zext i32 %68 to i64
%70 = shl i64 1, %69
%71 = and i64 %67, %70
%72 = icmp ne i64 %71, 0
%73 = zext i1 %72 to i64
%74 = select i1 %72, i32 1, i32 0
store i32 %74, i32* %6
br label %75
75: ; preds = %65, %55
%76 = load i32, i32* %6
store i32 %76, i32* %2
br label %77
77: ; preds = %75, %36, %19
%78 = load i32, i32* %2
ret i32 %78
}

LLVM IR (-O2):
@str = [15 x i8] c"Bad pin number\00"
define i32 @ca5_digitalRead(i32 %0) {
%2 = ashr i32 %0, 5
%3 = and i32 %0, 31
%4 = icmp eq i32 %2, 2
br i1 %4, label %24, label %5
5: ; preds = %1
%6 = icmp ult i32 %0, 32
br i1 %6, label %11, label %7
7: ; preds = %5
switch i32 %2, label %9 [
i32 1, label %11
i32 3, label %8
]
8: ; preds = %7
br label %11
9: ; preds = %7
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %24
11: ; preds = %7, %5, %8
%12 = phi i32 [ 546512896, %8 ], [ 546504704, %5 ], [ 546508800, %7 ]
%13 = tail call i64 @ca5_readR(i32 %12)
%14 = zext i32 %3 to i64
%15 = shl nuw nsw i64 1, %14
%16 = and i64 %13, %15
%17 = icmp eq i64 %16, 0
%18 = select i1 %17, i32 16, i32 12
%19 = or i32 %12, %18
%20 = tail call i64 @ca5_readR(i32 %19)
%21 = and i64 %20, %15
%22 = icmp ne i64 %21, 0
%23 = zext i1 %22 to i32
br label %24
24: ; preds = %1, %11, %9
%25 = phi i32 [ %23, %11 ], [ -1, %9 ], [ -1, %1 ]
ret i32 %25
}