#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_WriteByte(void *info, unsigned char val) {
  int eeCtl, i;
  if (EE_WaitIdle(info) & (1 << 19)) {
    return -EINVAL;
  }
  eeCtl = ((1 << 18) | (1 << 16));
  eeCtl |= ((val & 0xff) << 0);
  PEX_8111Write(info, 0x1004, eeCtl);
  for (i = 0; i < 1000; i++) {
    PEX_8111Read(info, 0x1004, &eeCtl);
    if ((eeCtl & (1 << 16)) == 0) {
      break;
    }
  }
  if (eeCtl & (1 << 16)) {
    printf("ERROR: EEPROM is still writting byte (busy)!\n");
    return -EBUSY;
  }
  return 0;
}


x86 assembly (clang, -O0):
<EE_WriteByte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %rdi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <EE_WaitIdle@plt>
and    $0x80000,%eax
cmp    $0x0,%eax
je     117c <EE_WriteByte+0x3c>
xor    %eax,%eax
mov    0x2e6e(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    122c <EE_WriteByte+0xec>
movl   $0x50000,-0x18(%rbp)
movzbl -0x11(%rbp),%eax
and    $0xff,%eax
shl    $0x0,%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    $0x1004,%esi
call   1030 <PEX_8111Write@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x3e8,-0x1c(%rbp)
jge    11f5 <EE_WriteByte+0xb5>
mov    -0x10(%rbp),%rdi
mov    $0x1004,%esi
lea    -0x18(%rbp),%rdx
call   1060 <PEX_8111Read@plt>
mov    -0x18(%rbp),%eax
and    $0x10000,%eax
cmp    $0x0,%eax
jne    11e2 <EE_WriteByte+0xa2>
jmp    11f5 <EE_WriteByte+0xb5>
jmp    11e7 <EE_WriteByte+0xa7>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11ad <EE_WriteByte+0x6d>
mov    -0x18(%rbp),%eax
and    $0x10000,%eax
cmp    $0x0,%eax
je     1225 <EE_WriteByte+0xe5>
lea    0xdf3(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
mov    0x2dbd(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    122c <EE_WriteByte+0xec>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<EE_WriteByte>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
call   1050 <EE_WaitIdle@plt>
test   $0x80000,%eax
jne    11b1 <EE_WriteByte+0x71>
movzbl %bpl,%edx
or     $0x50000,%edx
mov    %edx,0xc(%rsp)
mov    %rbx,%rdi
mov    $0x1004,%esi
call   1040 <PEX_8111Write@plt>
mov    $0x3e8,%ebp
lea    0xc(%rsp),%r14
xchg   %ax,%ax
mov    %rbx,%rdi
mov    $0x1004,%esi
mov    %r14,%rdx
call   1060 <PEX_8111Read@plt>
testb  $0x1,0xe(%rsp)
je     11be <EE_WriteByte+0x7e>
add    $0xffffffff,%ebp
jne    1180 <EE_WriteByte+0x40>
lea    0xe5d(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e29(%rip),%rcx
jmp    11b8 <EE_WriteByte+0x78>
mov    0x2e28(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
jmp    11c0 <EE_WriteByte+0x80>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@EINVAL = global i32 0
@EBUSY = global i32 0
@str = [45 x i8] c"ERROR: EEPROM is still writting byte (busy)!\00"
define i32 @EE_WriteByte(i8* %0, i8 zeroext %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%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 %28
11: ; preds = %2
%12 = zext i8 %1 to i32
%13 = or i32 %12, 327680
store i32 %13, i32* %3
%14 = tail call i32 @PEX_8111Write(i8* %0, i32 4100, i32 %13)
br label %18
15: ; preds = %18
%16 = add nuw nsw i32 %19, 1
%17 = icmp eq i32 %16, 1000
br i1 %17, label %24, label %18
18: ; preds = %11, %15
%19 = phi i32 [ 0, %11 ], [ %16, %15 ]
%20 = call i32 @PEX_8111Read(i8* %0, i32 4100, i32* %3)
%21 = load i32, i32* %3
%22 = and i32 %21, 65536
%23 = icmp eq i32 %22, 0
br i1 %23, label %28, label %15
24: ; preds = %15
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str, i64 0, i64 0))
%26 = load i32, i32* @EBUSY
%27 = sub nsw i32 0, %26
br label %28
28: ; preds = %18, %24, %8
%29 = phi i32 [ %10, %8 ], [ %27, %24 ], [ 0, %18 ]
ret i32 %29
}