#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 */
unsigned char eeprom_get_char(unsigned int);
int memcpy_from_eeprom_with_checksum(char *destination, unsigned int source,
                                     unsigned int size) {
  unsigned char data, checksum = 0;
  for (; size > 0; size--) {
    data = eeprom_get_char(source++);
    checksum = (checksum << 1) || (checksum >> 7);
    checksum += data;
    *(destination++) = data;
  }
  return (checksum == eeprom_get_char(source));
}


x86 assembly (clang, -O0):
<memcpy_from_eeprom_with_checksum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movb   $0x0,-0x12(%rbp)
cmpl   $0x0,-0x10(%rbp)
jbe    11a2 <memcpy_from_eeprom_with_checksum+0x92>
mov    -0xc(%rbp),%edi
mov    %edi,%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
call   1030 <eeprom_get_char@plt>
mov    %al,-0x11(%rbp)
movzbl -0x12(%rbp),%ecx
shl    $0x1,%ecx
mov    $0x1,%al
cmp    $0x0,%ecx
mov    %al,-0x13(%rbp)
jne    1168 <memcpy_from_eeprom_with_checksum+0x58>
movzbl -0x12(%rbp),%eax
sar    $0x7,%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x13(%rbp)
mov    -0x13(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %al,-0x12(%rbp)
movzbl -0x11(%rbp),%ecx
movzbl -0x12(%rbp),%eax
add    %ecx,%eax
mov    %al,-0x12(%rbp)
mov    -0x11(%rbp),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1126 <memcpy_from_eeprom_with_checksum+0x16>
movzbl -0x12(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <eeprom_get_char@plt>
mov    %al,%cl
mov    -0x18(%rbp),%eax
movzbl %cl,%ecx
cmp    %ecx,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<memcpy_from_eeprom_with_checksum>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
test   %edx,%edx
je     115a <memcpy_from_eeprom_with_checksum+0x4a>
mov    %rdi,%r15
mov    %edx,%r12d
mov    %r14d,%r13d
xor    %ebx,%ebx
xor    %ebp,%ebp
nop
lea    (%rbx,%r13,1),%edi
call   1030 <eeprom_get_char@plt>
xor    %ecx,%ecx
test   %ebp,%ebp
setne  %cl
movzbl %al,%eax
add    %eax,%ecx
mov    %al,(%r15,%rbx,1)
movzbl %cl,%ebp
add    $0x1,%rbx
cmp    %ebx,%r12d
jne    1130 <memcpy_from_eeprom_with_checksum+0x20>
add    %ebx,%r14d
jmp    115c <memcpy_from_eeprom_with_checksum+0x4c>
xor    %ebp,%ebp
mov    %r14d,%edi
call   1030 <eeprom_get_char@plt>
movzbl %al,%ecx
xor    %eax,%eax
cmp    %ecx,%ebp
sete   %al
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @memcpy_from_eeprom_with_checksum(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
%8 = alloca i8
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i8 0, i8* %8
br label %9
9: ; preds = %38, %3
%10 = load i32, i32* %6
%11 = icmp ugt i32 %10, 0
br i1 %11, label %12, label %41
12: ; preds = %9
%13 = load i32, i32* %5
%14 = add i32 %13, 1
store i32 %14, i32* %5
%15 = call zeroext i8 @eeprom_get_char(i32 %13)
store i8 %15, i8* %7
%16 = load i8, i8* %8
%17 = zext i8 %16 to i32
%18 = shl i32 %17, 1
%19 = icmp ne i32 %18, 0
br i1 %19, label %25, label %20
20: ; preds = %12
%21 = load i8, i8* %8
%22 = zext i8 %21 to i32
%23 = ashr i32 %22, 7
%24 = icmp ne i32 %23, 0
br label %25
25: ; preds = %20, %12
%26 = phi i1 [ true, %12 ], [ %24, %20 ]
%27 = zext i1 %26 to i32
%28 = trunc i32 %27 to i8
store i8 %28, i8* %8
%29 = load i8, i8* %7
%30 = zext i8 %29 to i32
%31 = load i8, i8* %8
%32 = zext i8 %31 to i32
%33 = add nsw i32 %32, %30
%34 = trunc i32 %33 to i8
store i8 %34, i8* %8
%35 = load i8, i8* %7
%36 = load i8*, i8** %4
%37 = getelementptr inbounds i8, i8* %36, i32 1
store i8* %37, i8** %4
store i8 %35, i8* %36
br label %38
38: ; preds = %25
%39 = load i32, i32* %6
%40 = add i32 %39, -1
store i32 %40, i32* %6
br label %9
41: ; preds = %9
%42 = load i8, i8* %8
%43 = zext i8 %42 to i32
%44 = load i32, i32* %5
%45 = call zeroext i8 @eeprom_get_char(i32 %44)
%46 = zext i8 %45 to i32
%47 = icmp eq i32 %43, %46
%48 = zext i1 %47 to i32
ret i32 %48
}

LLVM IR (-O2):
define i32 @memcpy_from_eeprom_with_checksum(i8* nocapture writeonly %0, i32 %1, i32 %2) {
%4 = icmp eq i32 %2, 0
br i1 %4, label %20, label %5
5: ; preds = %3, %5
%6 = phi i32 [ %18, %5 ], [ 0, %3 ]
%7 = phi i8* [ %16, %5 ], [ %0, %3 ]
%8 = phi i32 [ %17, %5 ], [ %2, %3 ]
%9 = phi i32 [ %10, %5 ], [ %1, %3 ]
%10 = add i32 %9, 1
%11 = tail call zeroext i8 @eeprom_get_char(i32 %9)
%12 = icmp ne i32 %6, 0
%13 = zext i8 %11 to i32
%14 = zext i1 %12 to i32
%15 = add nuw nsw i32 %13, %14
%16 = getelementptr inbounds i8, i8* %7, i64 1
store i8 %11, i8* %7
%17 = add i32 %8, -1
%18 = and i32 %15, 255
%19 = icmp eq i32 %17, 0
br i1 %19, label %20, label %5
20: ; preds = %5, %3
%21 = phi i32 [ %1, %3 ], [ %10, %5 ]
%22 = phi i32 [ 0, %3 ], [ %18, %5 ]
%23 = tail call zeroext i8 @eeprom_get_char(i32 %21)
%24 = zext i8 %23 to i32
%25 = icmp eq i32 %22, %24
%26 = zext i1 %25 to i32
ret i32 %26
}