#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 */
typedef int /*<<< orphan*/ uint8_t;

/* Variables and functions */
int /*<<< orphan*/ print(char *);
unsigned int readSeqRegisters16add(int, unsigned int, unsigned int,
                                   int /*<<< orphan*/ *);
uint8_t eepromRead(unsigned int nBytes, unsigned int address, uint8_t *dout) {
  unsigned int bytes_read = 0;
  unsigned n_retries = 0;
  if (nBytes > 128000) {
    do {
      print("[WARNING]:");
      print("Number of bytes to be read is greater than memory size");
      print("\r\n");
    } while (0);
    nBytes = 128000;
  }
  if (nBytes + address > 128000) {
    do {
      print("[WARNING]:");
      print("It is not possible to roll over memory boundaries");
      print("\r\n");
    } while (0);
    nBytes = 128000 - address;
  }
  while (bytes_read < nBytes) {
    unsigned int last_bytes_read = readSeqRegisters16add(
        0xA0, address, nBytes - bytes_read, dout + bytes_read);
    bytes_read += last_bytes_read;
    address += last_bytes_read;
    if (last_bytes_read == 0) {
      n_retries++;
    } else {
      n_retries = 0;
    }
    if (n_retries > 10) {
      do {
        print("[WARNING]:");
        print("Reach max number of retries. EEPROM read is incomplete");
        print("\r\n");
      } while (0);
      break;
    }
  }
  return (uint8_t)bytes_read;
}


x86 assembly (clang, -O0):
<eepromRead>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x1f400,-0x4(%rbp)
jbe    117d <eepromRead+0x5d>
jmp    1152 <eepromRead+0x32>
lea    0xea7(%rip),%rdi
call   1040 <print@plt>
lea    0xea6(%rip),%rdi
call   1040 <print@plt>
lea    0xed1(%rip),%rdi
call   1040 <print@plt>
movl   $0x1f400,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    -0x8(%rbp),%eax
cmp    $0x1f400,%eax
jbe    11c2 <eepromRead+0xa2>
jmp    1193 <eepromRead+0x73>
lea    0xe66(%rip),%rdi
call   1040 <print@plt>
lea    0xe9f(%rip),%rdi
call   1040 <print@plt>
lea    0xe90(%rip),%rdi
call   1040 <print@plt>
mov    $0x1f400,%eax
sub    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c7 <eepromRead+0xa7>
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jae    1265 <eepromRead+0x145>
mov    -0x8(%rbp),%esi
mov    -0x4(%rbp),%edx
sub    -0x14(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    -0x14(%rbp),%eax
shl    $0x2,%rax
add    %rax,%rcx
mov    $0xa0,%edi
call   1030 <readSeqRegisters16add@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1221 <eepromRead+0x101>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1228 <eepromRead+0x108>
movl   $0x0,-0x18(%rbp)
cmpl   $0xa,-0x18(%rbp)
jbe    1260 <eepromRead+0x140>
jmp    1237 <eepromRead+0x117>
lea    0xdc2(%rip),%rdi
call   1040 <print@plt>
lea    0xe2d(%rip),%rdi
call   1040 <print@plt>
lea    0xdec(%rip),%rdi
call   1040 <print@plt>
jmp    1265 <eepromRead+0x145>
jmp    11c7 <eepromRead+0xa7>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eepromRead>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %esi,%ebx
mov    %edi,%r15d
cmp    $0x1f401,%edi
jb     1162 <eepromRead+0x42>
lea    0xec1(%rip),%rdi
call   1040 <print@plt>
lea    0xec0(%rip),%rdi
call   1040 <print@plt>
lea    0xeeb(%rip),%rdi
call   1040 <print@plt>
mov    $0x1f400,%r15d
lea    (%r15,%rbx,1),%eax
cmp    $0x1f401,%eax
jb     119a <eepromRead+0x7a>
lea    0xe8c(%rip),%rdi
call   1040 <print@plt>
lea    0xec5(%rip),%rdi
call   1040 <print@plt>
lea    0xeb6(%rip),%rdi
call   1040 <print@plt>
mov    $0x1f400,%r15d
sub    %ebx,%r15d
xor    %ebp,%ebp
xor    %r12d,%r12d
jmp    11bd <eepromRead+0x9d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %r12d,%r12d
add    %eax,%ebp
add    %eax,%ebx
cmp    $0xb,%r12d
jae    11e0 <eepromRead+0xc0>
mov    %r15d,%edx
sub    %ebp,%edx
jbe    1204 <eepromRead+0xe4>
mov    %ebp,%eax
lea    (%r14,%rax,4),%rcx
mov    $0xa0,%edi
mov    %ebx,%esi
call   1030 <readSeqRegisters16add@plt>
test   %eax,%eax
jne    11b0 <eepromRead+0x90>
add    $0x1,%r12d
jmp    11b3 <eepromRead+0x93>
lea    0xe19(%rip),%rdi
call   1040 <print@plt>
lea    0xe84(%rip),%rdi
call   1040 <print@plt>
lea    0xe43(%rip),%rdi
call   1040 <print@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"[WARNING]:\00"
@.str.1 = [55 x i8] c"Number of bytes to be read is greater than memory size\00"
@.str.2 = [3 x i8] c"\0D\0A\00"
@.str.3 = [50 x i8] c"It is not possible to roll over memory boundaries\00"
@.str.4 = [55 x i8] c"Reach max number of retries. EEPROM read is incomplete\00"
define i32 @eepromRead(i32 %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32* %2, i32** %6
store i32 0, i32* %7
store i32 0, i32* %8
%10 = load i32, i32* %4
%11 = icmp ugt i32 %10, 128000
br i1 %11, label %12, label %18
12: ; preds = %3
br label %13
13: ; preds = %12
%14 = call i32 @print(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @print(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @print(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %13
store i32 128000, i32* %4
br label %18
18: ; preds = %17, %3
%19 = load i32, i32* %4
%20 = load i32, i32* %5
%21 = add i32 %19, %20
%22 = icmp ugt i32 %21, 128000
br i1 %22, label %23, label %31
23: ; preds = %18
br label %24
24: ; preds = %23
%25 = call i32 @print(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%26 = call i32 @print(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0))
%27 = call i32 @print(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %24
%29 = load i32, i32* %5
%30 = sub i32 128000, %29
store i32 %30, i32* %4
br label %31
31: ; preds = %28, %18
br label %32
32: ; preds = %67, %31
%33 = load i32, i32* %7
%34 = load i32, i32* %4
%35 = icmp ult i32 %33, %34
br i1 %35, label %36, label %68
36: ; preds = %32
%37 = load i32, i32* %5
%38 = load i32, i32* %4
%39 = load i32, i32* %7
%40 = sub i32 %38, %39
%41 = load i32*, i32** %6
%42 = load i32, i32* %7
%43 = zext i32 %42 to i64
%44 = getelementptr inbounds i32, i32* %41, i64 %43
%45 = call i32 @readSeqRegisters16add(i32 160, i32 %37, i32 %40, i32* %44)
store i32 %45, i32* %9
%46 = load i32, i32* %9
%47 = load i32, i32* %7
%48 = add i32 %47, %46
store i32 %48, i32* %7
%49 = load i32, i32* %9
%50 = load i32, i32* %5
%51 = add i32 %50, %49
store i32 %51, i32* %5
%52 = load i32, i32* %9
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %36
%55 = load i32, i32* %8
%56 = add i32 %55, 1
store i32 %56, i32* %8
br label %58
57: ; preds = %36
store i32 0, i32* %8
br label %58
58: ; preds = %57, %54
%59 = load i32, i32* %8
%60 = icmp ugt i32 %59, 10
br i1 %60, label %61, label %67
61: ; preds = %58
br label %62
62: ; preds = %61
%63 = call i32 @print(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%64 = call i32 @print(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.4, i64 0, i64 0))
%65 = call i32 @print(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %66
66: ; preds = %62
br label %68
67: ; preds = %58
br label %32
68: ; preds = %66, %32
%69 = load i32, i32* %7
ret i32 %69
}

LLVM IR (-O2):
@.str = [11 x i8] c"[WARNING]:\00"
@.str.1 = [55 x i8] c"Number of bytes to be read is greater than memory size\00"
@.str.2 = [3 x i8] c"\0D\0A\00"
@.str.3 = [50 x i8] c"It is not possible to roll over memory boundaries\00"
@.str.4 = [55 x i8] c"Reach max number of retries. EEPROM read is incomplete\00"
define i32 @eepromRead(i32 %0, i32 %1, i32* %2) {
%4 = icmp ugt i32 %0, 128000
br i1 %4, label %5, label %9
5: ; preds = %3
%6 = tail call i32 @print(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @print(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @print(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %9
9: ; preds = %5, %3
%10 = phi i32 [ 128000, %5 ], [ %0, %3 ]
%11 = add i32 %10, %1
%12 = icmp ugt i32 %11, 128000
br i1 %12, label %13, label %18
13: ; preds = %9
%14 = tail call i32 @print(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 @print(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0))
%16 = tail call i32 @print(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%17 = sub i32 128000, %1
br label %18
18: ; preds = %13, %9
%19 = phi i32 [ %17, %13 ], [ %10, %9 ]
br label %20
20: ; preds = %25, %18
%21 = phi i32 [ %1, %18 ], [ %31, %25 ]
%22 = phi i32 [ 0, %18 ], [ %30, %25 ]
%23 = phi i32 [ 0, %18 ], [ %34, %25 ]
%24 = icmp ugt i32 %19, %22
br i1 %24, label %25, label %40
25: ; preds = %20
%26 = sub i32 %19, %22
%27 = zext i32 %22 to i64
%28 = getelementptr inbounds i32, i32* %2, i64 %27
%29 = tail call i32 @readSeqRegisters16add(i32 160, i32 %21, i32 %26, i32* %28)
%30 = add i32 %29, %22
%31 = add i32 %29, %21
%32 = icmp eq i32 %29, 0
%33 = add nuw nsw i32 %23, 1
%34 = select i1 %32, i32 %33, i32 0
%35 = icmp ugt i32 %34, 10
br i1 %35, label %36, label %20
36: ; preds = %25
%37 = tail call i32 @print(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%38 = tail call i32 @print(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.4, i64 0, i64 0))
%39 = tail call i32 @print(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %20, %36
%41 = phi i32 [ %30, %36 ], [ %22, %20 ]
ret i32 %41
}