#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 */
scalar_t__ CMOS_READ(int);
int /*<<< orphan*/ CMOS_WRITE(unsigned int, int);
void rtc_set_checksum(int range_start, int range_end, int cks_loc) {
  int i;
  unsigned sum;
  sum = 0;
  for (i = range_start; i <= range_end; i++) {
    sum += CMOS_READ(i);
  }
  sum = ~(sum & 0x0ffff);
  CMOS_WRITE(((sum >> 8) & 0x0ff), cks_loc);
  CMOS_WRITE(((sum >> 0) & 0x0ff), cks_loc + 1);
}


x86 assembly (clang, -O0):
<rtc_set_checksum>:
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     116c <rtc_set_checksum+0x4c>
mov    -0x10(%rbp),%edi
call   1040 <CMOS_READ@plt>
mov    %rax,%rcx
mov    -0x14(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    113e <rtc_set_checksum+0x1e>
mov    -0x14(%rbp),%eax
and    $0xffff,%eax
xor    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
shr    $0x8,%edi
and    $0xff,%edi
mov    -0xc(%rbp),%esi
call   1030 <CMOS_WRITE@plt>
mov    -0x14(%rbp),%edi
shr    $0x0,%edi
and    $0xff,%edi
mov    -0xc(%rbp),%esi
add    $0x1,%esi
call   1030 <CMOS_WRITE@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtc_set_checksum>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    $0xffffffff,%eax
cmp    %esi,%edi
jg     115c <rtc_set_checksum+0x3c>
mov    %esi,%r15d
mov    %edi,%ebx
add    $0x1,%r15d
xor    %eax,%eax
xchg   %ax,%ax
mov    %ebx,%edi
mov    %eax,%ebp
call   1040 <CMOS_READ@plt>
add    %eax,%ebp
mov    %ebp,%eax
add    $0x1,%ebx
cmp    %ebx,%r15d
jne    1140 <rtc_set_checksum+0x20>
not    %eax
or     $0xffff0000,%eax
movzbl %ah,%edi
mov    %r14d,%esi
mov    %eax,%ebx
call   1030 <CMOS_WRITE@plt>
movzbl %bl,%edi
add    $0x1,%r14d
mov    %r14d,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <CMOS_WRITE@plt>

LLVM IR (-O0):
define void @rtc_set_checksum(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 0, i32* %8
%9 = load i32, i32* %4
store i32 %9, i32* %7
br label %10
10: ; preds = %21, %3
%11 = load i32, i32* %7
%12 = load i32, i32* %5
%13 = icmp sle i32 %11, %12
br i1 %13, label %14, label %24
14: ; preds = %10
%15 = load i32, i32* %7
%16 = call i64 @CMOS_READ(i32 %15)
%17 = load i32, i32* %8
%18 = zext i32 %17 to i64
%19 = add nsw i64 %18, %16
%20 = trunc i64 %19 to i32
store i32 %20, i32* %8
br label %21
21: ; preds = %14
%22 = load i32, i32* %7
%23 = add nsw i32 %22, 1
store i32 %23, i32* %7
br label %10
24: ; preds = %10
%25 = load i32, i32* %8
%26 = and i32 %25, 65535
%27 = xor i32 %26, -1
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = lshr i32 %28, 8
%30 = and i32 %29, 255
%31 = load i32, i32* %6
%32 = call i32 @CMOS_WRITE(i32 %30, i32 %31)
%33 = load i32, i32* %8
%34 = lshr i32 %33, 0
%35 = and i32 %34, 255
%36 = load i32, i32* %6
%37 = add nsw i32 %36, 1
%38 = call i32 @CMOS_WRITE(i32 %35, i32 %37)
ret void
}

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