#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;

/* Variables and functions */
int /*<<< orphan*/ fill_target_address(int *, void const *);
int /*<<< orphan*/ i2c_transmit(int, int *, int, int);
int /*<<< orphan*/ wait_ms(int);
void eeprom_write_block(const void *buf, void *addr, size_t len) {
  uint8_t complete_packet[2 + 32];
  uint8_t *read_buf = (uint8_t *)buf;
  uintptr_t target_addr = (uintptr_t)addr;
  while (len > 0) {
    uintptr_t page_offset = target_addr % 32;
    int write_length = 32 - page_offset;
    if (write_length > len) {
      write_length = len;
    }
    fill_target_address(complete_packet, (const void *)target_addr);
    for (uint8_t i = 0; i < write_length; i++) {
      complete_packet[2 + i] = read_buf[i];
    }
    i2c_transmit((0b10100000), complete_packet, 2 + write_length, 100);
    wait_ms(5);
    read_buf += write_length;
    target_addr += write_length;
    len -= write_length;
  }
}


x86 assembly (clang, -O0):
<eeprom_write_block>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0xa8(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0xb0(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    1280 <eeprom_write_block+0x150>
mov    -0xb0(%rbp),%rax
and    $0x1f,%rax
mov    %rax,-0xb8(%rbp)
mov    $0x20,%eax
sub    -0xb8(%rbp),%rax
mov    %eax,-0xbc(%rbp)
movslq -0xbc(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    11a7 <eeprom_write_block+0x77>
mov    -0x18(%rbp),%rax
mov    %eax,-0xbc(%rbp)
lea    -0xa0(%rbp),%rdi
mov    -0xb0(%rbp),%rsi
call   1030 <fill_target_address@plt>
movl   $0x0,-0xc0(%rbp)
mov    -0xc0(%rbp),%eax
cmp    -0xbc(%rbp),%eax
jge    120d <eeprom_write_block+0xdd>
mov    -0xa8(%rbp),%rax
movslq -0xc0(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
mov    -0xc0(%rbp),%eax
add    $0x2,%eax
cltq
mov    %ecx,-0xa0(%rbp,%rax,4)
mov    -0xc0(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc0(%rbp)
jmp    11c4 <eeprom_write_block+0x94>
lea    -0xa0(%rbp),%rsi
mov    -0xbc(%rbp),%edx
add    $0x2,%edx
mov    $0xa0,%edi
mov    $0x64,%ecx
call   1050 <i2c_transmit@plt>
mov    $0x5,%edi
call   1040 <wait_ms@plt>
mov    -0xbc(%rbp),%ecx
mov    -0xa8(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0xa8(%rbp)
movslq -0xbc(%rbp),%rax
add    -0xb0(%rbp),%rax
mov    %rax,-0xb0(%rbp)
movslq -0xbc(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    115d <eeprom_write_block+0x2d>
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eeprom_write_block>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
test   %rdx,%rdx
je     11d5 <eeprom_write_block+0x95>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0x8(%rsp),%r12
mov    %rsp,%r13
jmp    119e <eeprom_write_block+0x5e>
nopl   0x0(%rax)
lea    0x2(%rbp),%edx
mov    $0xa0,%edi
mov    %r13,%rsi
mov    $0x64,%ecx
call   1060 <i2c_transmit@plt>
mov    $0x5,%edi
call   1050 <wait_ms@plt>
movslq %ebp,%rax
lea    (%r15,%rax,4),%r15
add    %rax,%rbx
sub    %rax,%r14
je     11d5 <eeprom_write_block+0x95>
mov    %ebx,%eax
and    $0x1f,%eax
mov    $0x20,%ecx
sub    %eax,%ecx
cmp    %rcx,%r14
mov    %r14d,%ebp
cmovae %ecx,%ebp
mov    %r13,%rdi
mov    %rbx,%rsi
call   1040 <fill_target_address@plt>
test   %ebp,%ebp
jle    1170 <eeprom_write_block+0x30>
mov    %ebp,%edx
shl    $0x2,%rdx
mov    %r12,%rdi
mov    %r15,%rsi
call   1030 <memcpy@plt>
jmp    1170 <eeprom_write_block+0x30>
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @eeprom_write_block(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca [34 x i32]
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%13 = load i8*, i8** %4
%14 = bitcast i8* %13 to i32*
store i32* %14, i32** %8
%15 = load i8*, i8** %5
%16 = ptrtoint i8* %15 to i64
store i64 %16, i64* %9
br label %17
17: ; preds = %55, %3
%18 = load i64, i64* %6
%19 = icmp ugt i64 %18, 0
br i1 %19, label %20, label %73
20: ; preds = %17
%21 = load i64, i64* %9
%22 = urem i64 %21, 32
store i64 %22, i64* %10
%23 = load i64, i64* %10
%24 = sub i64 32, %23
%25 = trunc i64 %24 to i32
store i32 %25, i32* %11
%26 = load i32, i32* %11
%27 = sext i32 %26 to i64
%28 = load i64, i64* %6
%29 = icmp ugt i64 %27, %28
br i1 %29, label %30, label %33
30: ; preds = %20
%31 = load i64, i64* %6
%32 = trunc i64 %31 to i32
store i32 %32, i32* %11
br label %33
33: ; preds = %30, %20
%34 = getelementptr inbounds [34 x i32], [34 x i32]* %7, i64 0, i64 0
%35 = load i64, i64* %9
%36 = inttoptr i64 %35 to i8*
%37 = call i32 @fill_target_address(i32* %34, i8* %36)
store i32 0, i32* %12
br label %38
38: ; preds = %52, %33
%39 = load i32, i32* %12
%40 = load i32, i32* %11
%41 = icmp slt i32 %39, %40
br i1 %41, label %42, label %55
42: ; preds = %38
%43 = load i32*, i32** %8
%44 = load i32, i32* %12
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i32, i32* %43, i64 %45
%47 = load i32, i32* %46
%48 = load i32, i32* %12
%49 = add nsw i32 2, %48
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [34 x i32], [34 x i32]* %7, i64 0, i64 %50
store i32 %47, i32* %51
br label %52
52: ; preds = %42
%53 = load i32, i32* %12
%54 = add nsw i32 %53, 1
store i32 %54, i32* %12
br label %38
55: ; preds = %38
%56 = getelementptr inbounds [34 x i32], [34 x i32]* %7, i64 0, i64 0
%57 = load i32, i32* %11
%58 = add nsw i32 2, %57
%59 = call i32 @i2c_transmit(i32 160, i32* %56, i32 %58, i32 100)
%60 = call i32 @wait_ms(i32 5)
%61 = load i32, i32* %11
%62 = load i32*, i32** %8
%63 = sext i32 %61 to i64
%64 = getelementptr inbounds i32, i32* %62, i64 %63
store i32* %64, i32** %8
%65 = load i32, i32* %11
%66 = sext i32 %65 to i64
%67 = load i64, i64* %9
%68 = add i64 %67, %66
store i64 %68, i64* %9
%69 = load i32, i32* %11
%70 = sext i32 %69 to i64
%71 = load i64, i64* %6
%72 = sub i64 %71, %70
store i64 %72, i64* %6
br label %17
73: ; preds = %17
ret void
}

LLVM IR (-O2):
define void @eeprom_write_block(i8* nocapture %0, i8* %1, i64 %2) {
%4 = alloca [34 x i32]
%5 = bitcast [34 x i32]* %4 to i8*
%6 = icmp eq i64 %2, 0
br i1 %6, label %40, label %7
7: ; preds = %3
%8 = ptrtoint i8* %1 to i64
%9 = bitcast i8* %0 to i32*
%10 = getelementptr inbounds [34 x i32], [34 x i32]* %4, i64 0, i64 0
%11 = getelementptr inbounds [34 x i32], [34 x i32]* %4, i64 0, i64 2
%12 = bitcast i32* %11 to i8*
br label %13
13: ; preds = %7, %31
%14 = phi i64 [ %2, %7 ], [ %38, %31 ]
%15 = phi i64 [ %8, %7 ], [ %37, %31 ]
%16 = phi i32* [ %9, %7 ], [ %36, %31 ]
%17 = trunc i64 %15 to i32
%18 = and i32 %17, 31
%19 = sub nuw nsw i32 32, %18
%20 = zext i32 %19 to i64
%21 = icmp ult i64 %14, %20
%22 = trunc i64 %14 to i32
%23 = select i1 %21, i32 %22, i32 %19
%24 = inttoptr i64 %15 to i8*
%25 = call i32 @fill_target_address(i32* %10, i8* %24)
%26 = icmp sgt i32 %23, 0
br i1 %26, label %27, label %31
27: ; preds = %13
%28 = bitcast i32* %16 to i8*
%29 = zext i32 %23 to i64
%30 = shl nuw nsw i64 %29, 2
br label %31
31: ; preds = %27, %13
%32 = add nsw i32 %23, 2
%33 = call i32 @i2c_transmit(i32 160, i32* %10, i32 %32, i32 100)
%34 = call i32 @wait_ms(i32 5)
%35 = sext i32 %23 to i64
%36 = getelementptr inbounds i32, i32* %16, i64 %35
%37 = add i64 %15, %35
%38 = sub i64 %14, %35
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %13
40: ; preds = %31, %3
ret void
}