#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 EBUSY;
int EE_WaitIdle(void *);
int EINVAL;
int /*<<< orphan*/ PEX_8111Read(void *, int, int *);
int /*<<< orphan*/ PEX_8111Write(void *, int, int);
int /*<<< orphan*/ printf(char *);
int EE_ReadByte(void *info, unsigned char *data) {
  int eeCtl, i;
  *data = 0x00;
  if (EE_WaitIdle(info) & (1 << 19)) {
    return -EINVAL;
  }
  eeCtl = ((1 << 18) | (1 << 17));
  PEX_8111Write(info, 0x1004, eeCtl);
  for (i = 0; i < 1000; i++) {
    PEX_8111Read(info, 0x1004, &eeCtl);
    if ((eeCtl & (1 << 17)) == 0) {
      break;
    }
  }
  if ((eeCtl & (1 << 17)) != 0) {
    printf("ERROR: EEPROM is still reading byte (busy)!\n");
    return -EBUSY;
  }
  EE_WaitIdle(info);
  PEX_8111Read(info, 0x1004, &eeCtl);
  *data = (eeCtl >> 8) & 0xFF;
  return 0;
}


x86 assembly (clang, -O0):
<EE_ReadByte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1050 <EE_WaitIdle@plt>
and    $0x80000,%eax
cmp    $0x0,%eax
je     1181 <EE_ReadByte+0x41>
xor    %eax,%eax
mov    0x2e69(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    124d <EE_ReadByte+0x10d>
movl   $0x60000,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    $0x1004,%esi
call   1030 <PEX_8111Write@plt>
movl   $0x0,-0x20(%rbp)
cmpl   $0x3e8,-0x20(%rbp)
jge    11e8 <EE_ReadByte+0xa8>
mov    -0x10(%rbp),%rdi
mov    $0x1004,%esi
lea    -0x1c(%rbp),%rdx
call   1060 <PEX_8111Read@plt>
mov    -0x1c(%rbp),%eax
and    $0x20000,%eax
cmp    $0x0,%eax
jne    11d5 <EE_ReadByte+0x95>
jmp    11e8 <EE_ReadByte+0xa8>
jmp    11da <EE_ReadByte+0x9a>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11a0 <EE_ReadByte+0x60>
mov    -0x1c(%rbp),%eax
and    $0x20000,%eax
cmp    $0x0,%eax
je     1218 <EE_ReadByte+0xd8>
lea    0xe00(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
mov    0x2dca(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    124d <EE_ReadByte+0x10d>
mov    -0x10(%rbp),%rdi
call   1050 <EE_WaitIdle@plt>
mov    -0x10(%rbp),%rdi
mov    $0x1004,%esi
lea    -0x1c(%rbp),%rdx
call   1060 <PEX_8111Read@plt>
mov    -0x1c(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<EE_ReadByte>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
movb   $0x0,(%rsi)
call   1050 <EE_WaitIdle@plt>
test   $0x80000,%eax
jne    11b1 <EE_ReadByte+0x71>
movl   $0x60000,0x4(%rsp)
mov    %rbx,%rdi
mov    $0x1004,%esi
mov    $0x60000,%edx
call   1040 <PEX_8111Write@plt>
mov    $0x3e8,%ebp
lea    0x4(%rsp),%r15
mov    %rbx,%rdi
mov    $0x1004,%esi
mov    %r15,%rdx
call   1060 <PEX_8111Read@plt>
testb  $0x2,0x6(%rsp)
je     11be <EE_ReadByte+0x7e>
add    $0xffffffff,%ebp
jne    1180 <EE_ReadByte+0x40>
lea    0xe5d(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e29(%rip),%rcx
jmp    11b8 <EE_ReadByte+0x78>
mov    0x2e28(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
jmp    11e1 <EE_ReadByte+0xa1>
mov    %rbx,%rdi
call   1050 <EE_WaitIdle@plt>
lea    0x4(%rsp),%rdx
mov    %rbx,%rdi
mov    $0x1004,%esi
call   1060 <PEX_8111Read@plt>
mov    0x5(%rsp),%al
mov    %al,(%r14)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@.str = [45 x i8] c"ERROR: EEPROM is still reading byte (busy)!\0A\00"
@EBUSY = global i32 0
define i32 @EE_ReadByte(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
store i8 0, i8* %8
%9 = load i8*, i8** %4
%10 = call i32 @EE_WaitIdle(i8* %9)
%11 = and i32 %10, 524288
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @EINVAL
%15 = sub nsw i32 0, %14
store i32 %15, i32* %3
br label %52
16: ; preds = %2
store i32 393216, i32* %6
%17 = load i8*, i8** %4
%18 = load i32, i32* %6
%19 = call i32 @PEX_8111Write(i8* %17, i32 4100, i32 %18)
store i32 0, i32* %7
br label %20
20: ; preds = %31, %16
%21 = load i32, i32* %7
%22 = icmp slt i32 %21, 1000
br i1 %22, label %23, label %34
23: ; preds = %20
%24 = load i8*, i8** %4
%25 = call i32 @PEX_8111Read(i8* %24, i32 4100, i32* %6)
%26 = load i32, i32* %6
%27 = and i32 %26, 131072
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %23
br label %34
30: ; preds = %23
br label %31
31: ; preds = %30
%32 = load i32, i32* %7
%33 = add nsw i32 %32, 1
store i32 %33, i32* %7
br label %20
34: ; preds = %29, %20
%35 = load i32, i32* %6
%36 = and i32 %35, 131072
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %34
%39 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
%40 = load i32, i32* @EBUSY
%41 = sub nsw i32 0, %40
store i32 %41, i32* %3
br label %52
42: ; preds = %34
%43 = load i8*, i8** %4
%44 = call i32 @EE_WaitIdle(i8* %43)
%45 = load i8*, i8** %4
%46 = call i32 @PEX_8111Read(i8* %45, i32 4100, i32* %6)
%47 = load i32, i32* %6
%48 = ashr i32 %47, 8
%49 = and i32 %48, 255
%50 = trunc i32 %49 to i8
%51 = load i8*, i8** %5
store i8 %50, i8* %51
store i32 0, i32* %3
br label %52
52: ; preds = %42, %38, %13
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@EINVAL = global i32 0
@EBUSY = global i32 0
@str = [44 x i8] c"ERROR: EEPROM is still reading byte (busy)!\00"
define i32 @EE_ReadByte(i8* %0, i8* nocapture writeonly %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i8 0, i8* %1
%5 = tail call i32 @EE_WaitIdle(i8* %0)
%6 = and i32 %5, 524288
%7 = icmp eq i32 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i32, i32* @EINVAL
%10 = sub nsw i32 0, %9
br label %32
11: ; preds = %2
store i32 393216, i32* %3
%12 = tail call i32 @PEX_8111Write(i8* %0, i32 4100, i32 393216)
br label %16
13: ; preds = %16
%14 = add nuw nsw i32 %17, 1
%15 = icmp eq i32 %14, 1000
br i1 %15, label %22, label %16
16: ; preds = %11, %13
%17 = phi i32 [ 0, %11 ], [ %14, %13 ]
%18 = call i32 @PEX_8111Read(i8* %0, i32 4100, i32* %3)
%19 = load i32, i32* %3
%20 = and i32 %19, 131072
%21 = icmp eq i32 %20, 0
br i1 %21, label %26, label %13
22: ; preds = %13
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str, i64 0, i64 0))
%24 = load i32, i32* @EBUSY
%25 = sub nsw i32 0, %24
br label %32
26: ; preds = %16
%27 = call i32 @EE_WaitIdle(i8* %0)
%28 = call i32 @PEX_8111Read(i8* %0, i32 4100, i32* %3)
%29 = load i32, i32* %3
%30 = lshr i32 %29, 8
%31 = trunc i32 %30 to i8
store i8 %31, i8* %1
br label %32
32: ; preds = %26, %22, %8
%33 = phi i32 [ %10, %8 ], [ %25, %22 ], [ 0, %26 ]
ret i32 %33
}