#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 */
int CMOS_READ(int);
int rtc_checksum_valid(int range_start, int range_end, int cks_loc) {
  int i;
  unsigned sum, old_sum;
  sum = 0;
  for (i = range_start; i <= range_end; i++) {
    sum += CMOS_READ(i);
  }
  sum = (~sum) & 0x0ffff;
  old_sum = ((CMOS_READ(cks_loc) << 8) | CMOS_READ(cks_loc + 1)) & 0x0ffff;
  return sum == old_sum;
}


x86 assembly (clang, -O0):
<rtc_checksum_valid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
jg     1156 <rtc_checksum_valid+0x46>
mov    -0x10(%rbp),%edi
call   1030 <CMOS_READ@plt>
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    112e <rtc_checksum_valid+0x1e>
mov    -0x14(%rbp),%eax
xor    $0xffffffff,%eax
and    $0xffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <CMOS_READ@plt>
shl    $0x8,%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
add    $0x1,%edi
call   1030 <CMOS_READ@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
or     %ecx,%eax
and    $0xffff,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtc_checksum_valid>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    $0xffff,%r15d
cmp    %esi,%edi
jg     1147 <rtc_checksum_valid+0x37>
mov    %esi,%r12d
mov    %edi,%ebx
add    $0x1,%r12d
xor    %ebp,%ebp
mov    %ebx,%edi
call   1030 <CMOS_READ@plt>
add    %eax,%ebp
add    $0x1,%ebx
cmp    %ebx,%r12d
jne    1130 <rtc_checksum_valid+0x20>
not    %ebp
movzwl %bp,%r15d
mov    %r14d,%edi
call   1030 <CMOS_READ@plt>
mov    %eax,%ebx
shl    $0x8,%ebx
add    $0x1,%r14d
mov    %r14d,%edi
call   1030 <CMOS_READ@plt>
or     %ebx,%eax
movzwl %ax,%ecx
xor    %eax,%eax
cmp    %ecx,%r15d
sete   %al
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @rtc_checksum_valid(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* %8
%10 = load i32, i32* %4
store i32 %10, i32* %7
br label %11
11: ; preds = %20, %3
%12 = load i32, i32* %7
%13 = load i32, i32* %5
%14 = icmp sle i32 %12, %13
br i1 %14, label %15, label %23
15: ; preds = %11
%16 = load i32, i32* %7
%17 = call i32 @CMOS_READ(i32 %16)
%18 = load i32, i32* %8
%19 = add i32 %18, %17
store i32 %19, i32* %8
br label %20
20: ; preds = %15
%21 = load i32, i32* %7
%22 = add nsw i32 %21, 1
store i32 %22, i32* %7
br label %11
23: ; preds = %11
%24 = load i32, i32* %8
%25 = xor i32 %24, -1
%26 = and i32 %25, 65535
store i32 %26, i32* %8
%27 = load i32, i32* %6
%28 = call i32 @CMOS_READ(i32 %27)
%29 = shl i32 %28, 8
%30 = load i32, i32* %6
%31 = add nsw i32 %30, 1
%32 = call i32 @CMOS_READ(i32 %31)
%33 = or i32 %29, %32
%34 = and i32 %33, 65535
store i32 %34, i32* %9
%35 = load i32, i32* %8
%36 = load i32, i32* %9
%37 = icmp eq i32 %35, %36
%38 = zext i1 %37 to i32
ret i32 %38
}

LLVM IR (-O2):
define i32 @rtc_checksum_valid(i32 %0, i32 %1, i32 %2) {
%4 = icmp sgt i32 %0, %1
br i1 %4, label %15, label %5
5: ; preds = %3, %5
%6 = phi i32 [ %10, %5 ], [ %0, %3 ]
%7 = phi i32 [ %9, %5 ], [ 0, %3 ]
%8 = tail call i32 @CMOS_READ(i32 %6)
%9 = add i32 %8, %7
%10 = add i32 %6, 1
%11 = icmp eq i32 %6, %1
br i1 %11, label %12, label %5
12: ; preds = %5
%13 = and i32 %9, 65535
%14 = xor i32 %13, 65535
br label %15
15: ; preds = %12, %3
%16 = phi i32 [ 65535, %3 ], [ %14, %12 ]
%17 = tail call i32 @CMOS_READ(i32 %2)
%18 = shl i32 %17, 8
%19 = add nsw i32 %2, 1
%20 = tail call i32 @CMOS_READ(i32 %19)
%21 = or i32 %18, %20
%22 = and i32 %21, 65535
%23 = icmp eq i32 %16, %22
%24 = zext i1 %23 to i32
ret i32 %24
}