#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 unsigned int uint8_t;

/* Variables and functions */
int /*<<< orphan*/ print(char *);
unsigned int writeSeqRegisters16add(int, unsigned int, unsigned int,
                                    unsigned int *);
uint8_t eepromWrite(unsigned int nBytes, unsigned int address, uint8_t *din) {
  unsigned int bytes_written = 0;
  unsigned n_retries = 0;
  if (nBytes > 128000) {
    do {
      print("[WARNING]:");
      print("Number of bytes to be written 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_written < nBytes) {
    unsigned int next_page_boundaries = ((address / 256) + 1) * 256;
    unsigned int bytes_to_boundaries = next_page_boundaries - address;
    unsigned int bytes_to_write = bytes_to_boundaries < (nBytes - bytes_written)
                                      ? bytes_to_boundaries
                                      : (nBytes - bytes_written);
    unsigned int last_bytes_written = writeSeqRegisters16add(
        0xA0, address, bytes_to_write, din + bytes_written);
    if (last_bytes_written == 0) {
      n_retries++;
    } else {
      n_retries = 0;
    }
    address += last_bytes_written;
    bytes_written += last_bytes_written;
    if (n_retries > 10) {
      do {
        print("[WARNING]:");
        print("Reach max number of retries. EEPROM written is incomplete");
        print("\r\n");
      } while (0);
      break;
    }
  }
  return bytes_written;
}


x86 assembly (clang, -O0):
<eepromWrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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 <eepromWrite+0x5d>
jmp    1152 <eepromWrite+0x32>
lea    0xea7(%rip),%rdi
call   1040 <print@plt>
lea    0xea6(%rip),%rdi
call   1040 <print@plt>
lea    0xed4(%rip),%rdi
call   1040 <print@plt>
movl   $0x1f400,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    -0x8(%rbp),%eax
cmp    $0x1f400,%eax
jbe    11c2 <eepromWrite+0xa2>
jmp    1193 <eepromWrite+0x73>
lea    0xe66(%rip),%rdi
call   1040 <print@plt>
lea    0xea2(%rip),%rdi
call   1040 <print@plt>
lea    0xe93(%rip),%rdi
call   1040 <print@plt>
mov    $0x1f400,%eax
sub    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c7 <eepromWrite+0xa7>
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jae    12a5 <eepromWrite+0x185>
mov    -0x8(%rbp),%eax
shr    $0x8,%eax
add    $0x1,%eax
shl    $0x8,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
sub    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    -0x4(%rbp),%ecx
sub    -0x14(%rbp),%ecx
cmp    %ecx,%eax
jae    1207 <eepromWrite+0xe7>
mov    -0x20(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    1210 <eepromWrite+0xf0>
mov    -0x4(%rbp),%eax
sub    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    -0x14(%rbp),%eax
shl    $0x2,%rax
add    %rax,%rcx
mov    $0xa0,%edi
call   1030 <writeSeqRegisters16add@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    124f <eepromWrite+0x12f>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1256 <eepromWrite+0x136>
movl   $0x0,-0x18(%rbp)
mov    -0x28(%rbp),%eax
add    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x28(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0xa,-0x18(%rbp)
jbe    12a0 <eepromWrite+0x180>
jmp    1277 <eepromWrite+0x157>
lea    0xd82(%rip),%rdi
call   1040 <print@plt>
lea    0xdf0(%rip),%rdi
call   1040 <print@plt>
lea    0xdaf(%rip),%rdi
call   1040 <print@plt>
jmp    12a5 <eepromWrite+0x185>
jmp    11c7 <eepromWrite+0xa7>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eepromWrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%r13d
mov    %edi,%r15d
cmp    $0x1f401,%edi
jb     1166 <eepromWrite+0x46>
lea    0xebd(%rip),%rdi
call   1040 <print@plt>
lea    0xebc(%rip),%rdi
call   1040 <print@plt>
lea    0xeea(%rip),%rdi
call   1040 <print@plt>
mov    $0x1f400,%r15d
lea    (%r15,%r13,1),%eax
cmp    $0x1f401,%eax
jb     119e <eepromWrite+0x7e>
lea    0xe88(%rip),%rdi
call   1040 <print@plt>
lea    0xec4(%rip),%rdi
call   1040 <print@plt>
lea    0xeb5(%rip),%rdi
call   1040 <print@plt>
mov    $0x1f400,%r15d
sub    %r13d,%r15d
xor    %r12d,%r12d
xor    %ebx,%ebx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15d,%eax
sub    %ebp,%eax
jbe    1216 <eepromWrite+0xf6>
lea    0x100(%r13),%edx
and    $0xffffff00,%edx
sub    %r13d,%edx
cmp    %eax,%edx
cmovae %eax,%edx
mov    %ebp,%eax
lea    (%r14,%rax,4),%rcx
mov    $0xa0,%edi
mov    %r13d,%esi
call   1030 <writeSeqRegisters16add@plt>
add    $0x1,%ebx
test   %eax,%eax
cmovne %r12d,%ebx
add    %eax,%r13d
add    %eax,%ebp
cmp    $0xb,%ebx
jb     11b0 <eepromWrite+0x90>
lea    0xe07(%rip),%rdi
call   1040 <print@plt>
lea    0xe75(%rip),%rdi
call   1040 <print@plt>
lea    0xe34(%rip),%rdi
call   1040 <print@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"[WARNING]:\00"
@.str.1 = [58 x i8] c"Number of bytes to be written 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 = [58 x i8] c"Reach max number of retries. EEPROM written is incomplete\00"
define i32 @eepromWrite(i32 %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = 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
%13 = load i32, i32* %4
%14 = icmp ugt i32 %13, 128000
br i1 %14, label %15, label %21
15: ; preds = %3
br label %16
16: ; preds = %15
%17 = call i32 @print(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @print(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @print(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %16
store i32 128000, i32* %4
br label %21
21: ; preds = %20, %3
%22 = load i32, i32* %4
%23 = load i32, i32* %5
%24 = add i32 %22, %23
%25 = icmp ugt i32 %24, 128000
br i1 %25, label %26, label %34
26: ; preds = %21
br label %27
27: ; preds = %26
%28 = call i32 @print(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%29 = call i32 @print(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0))
%30 = call i32 @print(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %27
%32 = load i32, i32* %5
%33 = sub i32 128000, %32
store i32 %33, i32* %4
br label %34
34: ; preds = %31, %21
br label %35
35: ; preds = %88, %34
%36 = load i32, i32* %7
%37 = load i32, i32* %4
%38 = icmp ult i32 %36, %37
br i1 %38, label %39, label %89
39: ; preds = %35
%40 = load i32, i32* %5
%41 = udiv i32 %40, 256
%42 = add i32 %41, 1
%43 = mul i32 %42, 256
store i32 %43, i32* %9
%44 = load i32, i32* %9
%45 = load i32, i32* %5
%46 = sub i32 %44, %45
store i32 %46, i32* %10
%47 = load i32, i32* %10
%48 = load i32, i32* %4
%49 = load i32, i32* %7
%50 = sub i32 %48, %49
%51 = icmp ult i32 %47, %50
br i1 %51, label %52, label %54
52: ; preds = %39
%53 = load i32, i32* %10
br label %58
54: ; preds = %39
%55 = load i32, i32* %4
%56 = load i32, i32* %7
%57 = sub i32 %55, %56
br label %58
58: ; preds = %54, %52
%59 = phi i32 [ %53, %52 ], [ %57, %54 ]
store i32 %59, i32* %11
%60 = load i32, i32* %5
%61 = load i32, i32* %11
%62 = load i32*, i32** %6
%63 = load i32, i32* %7
%64 = zext i32 %63 to i64
%65 = getelementptr inbounds i32, i32* %62, i64 %64
%66 = call i32 @writeSeqRegisters16add(i32 160, i32 %60, i32 %61, i32* %65)
store i32 %66, i32* %12
%67 = load i32, i32* %12
%68 = icmp eq i32 %67, 0
br i1 %68, label %69, label %72
69: ; preds = %58
%70 = load i32, i32* %8
%71 = add i32 %70, 1
store i32 %71, i32* %8
br label %73
72: ; preds = %58
store i32 0, i32* %8
br label %73
73: ; preds = %72, %69
%74 = load i32, i32* %12
%75 = load i32, i32* %5
%76 = add i32 %75, %74
store i32 %76, i32* %5
%77 = load i32, i32* %12
%78 = load i32, i32* %7
%79 = add i32 %78, %77
store i32 %79, i32* %7
%80 = load i32, i32* %8
%81 = icmp ugt i32 %80, 10
br i1 %81, label %82, label %88
82: ; preds = %73
br label %83
83: ; preds = %82
%84 = call i32 @print(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%85 = call i32 @print(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.4, i64 0, i64 0))
%86 = call i32 @print(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %87
87: ; preds = %83
br label %89
88: ; preds = %73
br label %35
89: ; preds = %87, %35
%90 = load i32, i32* %7
ret i32 %90
}

LLVM IR (-O2):
@.str = [11 x i8] c"[WARNING]:\00"
@.str.1 = [58 x i8] c"Number of bytes to be written 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 = [58 x i8] c"Reach max number of retries. EEPROM written is incomplete\00"
define i32 @eepromWrite(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 ([58 x i8], [58 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 [ 0, %18 ], [ %37, %25 ]
%22 = phi i32 [ 0, %18 ], [ %39, %25 ]
%23 = phi i32 [ %1, %18 ], [ %38, %25 ]
%24 = icmp ugt i32 %19, %22
br i1 %24, label %25, label %45
25: ; preds = %20
%26 = add i32 %23, 256
%27 = and i32 %26, -256
%28 = sub i32 %27, %23
%29 = sub i32 %19, %22
%30 = icmp ult i32 %28, %29
%31 = select i1 %30, i32 %28, i32 %29
%32 = zext i32 %22 to i64
%33 = getelementptr inbounds i32, i32* %2, i64 %32
%34 = tail call i32 @writeSeqRegisters16add(i32 160, i32 %23, i32 %31, i32* %33)
%35 = icmp eq i32 %34, 0
%36 = add nuw nsw i32 %21, 1
%37 = select i1 %35, i32 %36, i32 0
%38 = add i32 %34, %23
%39 = add i32 %34, %22
%40 = icmp ugt i32 %37, 10
br i1 %40, label %41, label %20
41: ; preds = %25
%42 = tail call i32 @print(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%43 = tail call i32 @print(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.4, i64 0, i64 0))
%44 = tail call i32 @print(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %45
45: ; preds = %20, %41
%46 = phi i32 [ %39, %41 ], [ %22, %20 ]
ret i32 %46
}