#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 uint8_t;
typedef int uint32_t;
typedef int int32_t;

/* Variables and functions */
int I2C_Xfer(int, int *, int, int);
int /*<<< orphan*/ memcpy(int *, int *, int);
int /*<<< orphan*/ printf(char *);
int32_t EEPROM_Write(uint32_t startdestpos, uint8_t *src, uint32_t len) {
  int32_t retval = 0;
  if (startdestpos < 256 && len && len <= 256) {
    uint8_t tmpbuf[9];
    uint8_t i = startdestpos;
    while (len) {
      uint32_t loopcnt = 0;
      uint8_t startoffset = i & 0x07;
      uint8_t maxcopysize = 8 - startoffset;
      uint8_t bytestocopy = (len > maxcopysize) ? maxcopysize : len;
      tmpbuf[0] = i;
      memcpy(tmpbuf + 1, src, bytestocopy);
      retval = I2C_Xfer((0x50 << 1), tmpbuf, bytestocopy + 1, 1);
      if (!retval) {
        do {
          retval = I2C_Xfer((0x50 << 1), tmpbuf, 1, 1);
          loopcnt++;
        } while (retval && loopcnt < 400);
        if (retval) {
          printf("\nTimeout getting ACK from EEPROM during write!");
          break;
        }
        len -= bytestocopy;
        i += bytestocopy;
        src += bytestocopy;
      } else {
        printf("\nFailed to write to EEPROM!");
        retval = -2;
        break;
      }
    }
  } else {
    printf("\nInvalid EEPROM addressing");
    retval = -3;
  }
  return retval;
}


x86 assembly (clang, -O0):
<EEPROM_Write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x100,-0x4(%rbp)
jge    12b3 <EEPROM_Write+0x183>
cmpl   $0x0,-0x14(%rbp)
je     12b3 <EEPROM_Write+0x183>
cmpl   $0x100,-0x14(%rbp)
jg     12b3 <EEPROM_Write+0x183>
mov    -0x4(%rbp),%eax
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     12ae <EEPROM_Write+0x17e>
movl   $0x0,-0x48(%rbp)
mov    -0x44(%rbp),%eax
and    $0x7,%eax
mov    %eax,-0x4c(%rbp)
mov    $0x8,%eax
sub    -0x4c(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x50(%rbp),%eax
jle    11af <EEPROM_Write+0x7f>
mov    -0x50(%rbp),%eax
mov    %eax,-0x58(%rbp)
jmp    11b5 <EEPROM_Write+0x85>
mov    -0x14(%rbp),%eax
mov    %eax,-0x58(%rbp)
mov    -0x58(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x44(%rbp),%eax
mov    %eax,-0x40(%rbp)
lea    -0x40(%rbp),%rdi
add    $0x4,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x54(%rbp),%edx
call   1050 <memcpy@plt>
lea    -0x40(%rbp),%rsi
mov    -0x54(%rbp),%edx
add    $0x1,%edx
mov    $0xa0,%edi
mov    $0x1,%ecx
call   1040 <I2C_Xfer@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1291 <EEPROM_Write+0x161>
jmp    1200 <EEPROM_Write+0xd0>
lea    -0x40(%rbp),%rsi
mov    $0xa0,%edi
mov    $0x1,%ecx
mov    %ecx,%edx
call   1040 <I2C_Xfer@plt>
mov    %eax,-0x18(%rbp)
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x18(%rbp)
mov    %al,-0x59(%rbp)
je     123d <EEPROM_Write+0x10d>
cmpl   $0x190,-0x48(%rbp)
setl   %al
mov    %al,-0x59(%rbp)
mov    -0x59(%rbp),%al
test   $0x1,%al
jne    1200 <EEPROM_Write+0xd0>
cmpl   $0x0,-0x18(%rbp)
je     1263 <EEPROM_Write+0x133>
lea    0xda7(%rip),%rdi
call   1030 <printf@plt>
jmp    12ae <EEPROM_Write+0x17e>
mov    -0x54(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x54(%rbp),%eax
add    -0x44(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x54(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    12a9 <EEPROM_Write+0x179>
lea    0xd97(%rip),%rdi
call   1030 <printf@plt>
movl   $0xfffffffe,-0x18(%rbp)
jmp    12ae <EEPROM_Write+0x17e>
jmp    1173 <EEPROM_Write+0x43>
jmp    12c6 <EEPROM_Write+0x196>
lea    0xd91(%rip),%rdi
call   1030 <printf@plt>
movl   $0xfffffffd,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<EEPROM_Write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
cmp    $0x100,%edx
jg     1210 <EEPROM_Write+0xe0>
mov    %edi,%r14d
cmp    $0xff,%edi
jg     1210 <EEPROM_Write+0xe0>
mov    %edx,%r13d
test   %edx,%edx
je     1210 <EEPROM_Write+0xe0>
mov    %rsi,%rbx
lea    0x10(%rsp),%rbp
nopl   0x0(%rax)
mov    %r14d,%eax
and    $0x7,%eax
mov    $0x8,%r15d
sub    %eax,%r15d
cmp    %r15d,%r13d
cmovle %r13d,%r15d
mov    %r14d,0x10(%rsp)
lea    0x14(%rsp),%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1050 <memcpy@plt>
lea    0x1(%r15),%edx
mov    $0xa0,%edi
mov    %rbp,%rsi
mov    $0x1,%ecx
call   1040 <I2C_Xfer@plt>
test   %eax,%eax
jne    1224 <EEPROM_Write+0xf4>
mov    %r14d,0xc(%rsp)
xor    %r14d,%r14d
nopl   (%rax)
mov    $0xa0,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
mov    $0x1,%ecx
call   1040 <I2C_Xfer@plt>
mov    %eax,%r12d
test   %eax,%eax
je     11ee <EEPROM_Write+0xbe>
lea    0x1(%r14),%eax
cmp    $0x18f,%r14d
mov    %eax,%r14d
jb     11c0 <EEPROM_Write+0x90>
test   %r12d,%r12d
jne    1233 <EEPROM_Write+0x103>
mov    0xc(%rsp),%r14d
add    %r15d,%r14d
movslq %r15d,%rax
lea    (%rbx,%rax,4),%rbx
sub    %r15d,%r13d
jne    1170 <EEPROM_Write+0x40>
xor    %r12d,%r12d
jmp    123f <EEPROM_Write+0x10f>
lea    0xe34(%rip),%rdi
call   1030 <printf@plt>
mov    $0xfffffffd,%r12d
jmp    123f <EEPROM_Write+0x10f>
mov    $0xfffffffe,%r12d
lea    0xdfe(%rip),%rdi
jmp    123a <EEPROM_Write+0x10a>
lea    0xdc6(%rip),%rdi
call   1030 <printf@plt>
mov    %r12d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [47 x i8] c"\0ATimeout getting ACK from EEPROM during write!\00"
@.str.1 = [28 x i8] c"\0AFailed to write to EEPROM!\00"
@.str.2 = [27 x i8] c"\0AInvalid EEPROM addressing\00"
define i32 @EEPROM_Write(i32 %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [9 x i32]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
store i32 %2, i32* %6
store i32 0, i32* %7
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 256
br i1 %15, label %16, label %88
16: ; preds = %3
%17 = load i32, i32* %6
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %88
19: ; preds = %16
%20 = load i32, i32* %6
%21 = icmp sle i32 %20, 256
br i1 %21, label %22, label %88
22: ; preds = %19
%23 = load i32, i32* %4
store i32 %23, i32* %9
br label %24
24: ; preds = %86, %22
%25 = load i32, i32* %6
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %87
27: ; preds = %24
store i32 0, i32* %10
%28 = load i32, i32* %9
%29 = and i32 %28, 7
store i32 %29, i32* %11
%30 = load i32, i32* %11
%31 = sub nsw i32 8, %30
store i32 %31, i32* %12
%32 = load i32, i32* %6
%33 = load i32, i32* %12
%34 = icmp sgt i32 %32, %33
br i1 %34, label %35, label %37
35: ; preds = %27
%36 = load i32, i32* %12
br label %39
37: ; preds = %27
%38 = load i32, i32* %6
br label %39
39: ; preds = %37, %35
%40 = phi i32 [ %36, %35 ], [ %38, %37 ]
store i32 %40, i32* %13
%41 = load i32, i32* %9
%42 = getelementptr inbounds [9 x i32], [9 x i32]* %8, i64 0, i64 0
store i32 %41, i32* %42
%43 = getelementptr inbounds [9 x i32], [9 x i32]* %8, i64 0, i64 0
%44 = getelementptr inbounds i32, i32* %43, i64 1
%45 = load i32*, i32** %5
%46 = load i32, i32* %13
%47 = call i32 @memcpy(i32* %44, i32* %45, i32 %46)
%48 = getelementptr inbounds [9 x i32], [9 x i32]* %8, i64 0, i64 0
%49 = load i32, i32* %13
%50 = add nsw i32 %49, 1
%51 = call i32 @I2C_Xfer(i32 160, i32* %48, i32 %50, i32 1)
store i32 %51, i32* %7
%52 = load i32, i32* %7
%53 = icmp ne i32 %52, 0
br i1 %53, label %84, label %54
54: ; preds = %39
br label %55
55: ; preds = %66, %54
%56 = getelementptr inbounds [9 x i32], [9 x i32]* %8, i64 0, i64 0
%57 = call i32 @I2C_Xfer(i32 160, i32* %56, i32 1, i32 1)
store i32 %57, i32* %7
%58 = load i32, i32* %10
%59 = add nsw i32 %58, 1
store i32 %59, i32* %10
br label %60
60: ; preds = %55
%61 = load i32, i32* %7
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %66
63: ; preds = %60
%64 = load i32, i32* %10
%65 = icmp slt i32 %64, 400
br label %66
66: ; preds = %63, %60
%67 = phi i1 [ false, %60 ], [ %65, %63 ]
br i1 %67, label %55, label %68
68: ; preds = %66
%69 = load i32, i32* %7
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %73
71: ; preds = %68
%72 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
br label %87
73: ; preds = %68
%74 = load i32, i32* %13
%75 = load i32, i32* %6
%76 = sub nsw i32 %75, %74
store i32 %76, i32* %6
%77 = load i32, i32* %13
%78 = load i32, i32* %9
%79 = add nsw i32 %78, %77
store i32 %79, i32* %9
%80 = load i32, i32* %13
%81 = load i32*, i32** %5
%82 = sext i32 %80 to i64
%83 = getelementptr inbounds i32, i32* %81, i64 %82
store i32* %83, i32** %5
br label %86
84: ; preds = %39
%85 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i32 -2, i32* %7
br label %87
86: ; preds = %73
br label %24
87: ; preds = %84, %71, %24
br label %90
88: ; preds = %19, %16, %3
%89 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
store i32 -3, i32* %7
br label %90
90: ; preds = %88, %87
%91 = load i32, i32* %7
ret i32 %91
}

LLVM IR (-O2):
@.str = [47 x i8] c"\0ATimeout getting ACK from EEPROM during write!\00"
@.str.1 = [28 x i8] c"\0AFailed to write to EEPROM!\00"
@.str.2 = [27 x i8] c"\0AInvalid EEPROM addressing\00"
define i32 @EEPROM_Write(i32 %0, i32* %1, i32 %2) {
%4 = alloca [9 x i32]
%5 = icmp slt i32 %0, 256
%6 = icmp ne i32 %2, 0
%7 = and i1 %5, %6
%8 = icmp slt i32 %2, 257
%9 = and i1 %8, %7
br i1 %9, label %10, label %46
10: ; preds = %3
%11 = bitcast [9 x i32]* %4 to i8*
%12 = getelementptr inbounds [9 x i32], [9 x i32]* %4, i64 0, i64 0
%13 = getelementptr inbounds [9 x i32], [9 x i32]* %4, i64 0, i64 1
br label %14
14: ; preds = %10, %34
%15 = phi i32* [ %1, %10 ], [ %38, %34 ]
%16 = phi i32 [ %2, %10 ], [ %35, %34 ]
%17 = phi i32 [ %0, %10 ], [ %36, %34 ]
%18 = and i32 %17, 7
%19 = sub nuw nsw i32 8, %18
%20 = icmp sgt i32 %16, %19
%21 = select i1 %20, i32 %19, i32 %16
store i32 %17, i32* %12
%22 = call i32 @memcpy(i32* %13, i32* %15, i32 %21)
%23 = add nsw i32 %21, 1
%24 = call i32 @I2C_Xfer(i32 160, i32* %12, i32 %23, i32 1)
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %40
26: ; preds = %14, %26
%27 = phi i32 [ %29, %26 ], [ 0, %14 ]
%28 = call i32 @I2C_Xfer(i32 160, i32* %12, i32 1, i32 1)
%29 = add nuw nsw i32 %27, 1
%30 = icmp ne i32 %28, 0
%31 = icmp ult i32 %27, 399
%32 = select i1 %30, i1 %31, i1 false
br i1 %32, label %26, label %33
33: ; preds = %26
br i1 %30, label %40, label %34
34: ; preds = %33
%35 = sub nsw i32 %16, %21
%36 = add nsw i32 %21, %17
%37 = sext i32 %21 to i64
%38 = getelementptr inbounds i32, i32* %15, i64 %37
%39 = icmp eq i32 %35, 0
br i1 %39, label %44, label %14
40: ; preds = %14, %33
%41 = phi i8* [ getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), %33 ], [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), %14 ]
%42 = phi i32 [ %28, %33 ], [ -2, %14 ]
%43 = call i32 @printf(i8* dereferenceable(1) %41)
br label %44
44: ; preds = %34, %40
%45 = phi i32 [ %42, %40 ], [ 0, %34 ]
br label %48
46: ; preds = %3
%47 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %48
48: ; preds = %46, %44
%49 = phi i32 [ %45, %44 ], [ -3, %46 ]
ret i32 %49
}