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

/* Variables and functions */
uint16_t compute_checksum(uint16_t *ROM) {
  uint16_t checksum = 0;
  int i;
  for (i = 0; i < 0xfff; i++) {
    checksum += ROM[i];
    if (checksum > 0x3ff)
      checksum = (checksum & 0x03ff) + 1;
  }
  return ((~checksum) + 1) & 0x3ff;
}


x86 assembly (clang, -O0):
<compute_checksum>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0xfff,-0x10(%rbp)
jge    1162 <compute_checksum+0x62>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x3ff,-0xc(%rbp)
jle    114f <compute_checksum+0x4f>
mov    -0xc(%rbp),%eax
and    $0x3ff,%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1154 <compute_checksum+0x54>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1116 <compute_checksum+0x16>
mov    -0xc(%rbp),%eax
xor    $0xffffffff,%eax
add    $0x1,%eax
and    $0x3ff,%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<compute_checksum>:
xor    %ecx,%ecx
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    (%rdi,%rcx,4),%eax
mov    %eax,%edx
and    $0x3ff,%edx
add    $0x1,%edx
cmp    $0x400,%eax
cmovl  %eax,%edx
add    0x4(%rdi,%rcx,4),%edx
mov    %edx,%esi
and    $0x3ff,%esi
add    $0x1,%esi
cmp    $0x400,%edx
cmovl  %edx,%esi
add    0x8(%rdi,%rcx,4),%esi
mov    %esi,%eax
and    $0x3ff,%eax
add    $0x1,%eax
cmp    $0x400,%esi
cmovl  %esi,%eax
add    $0x3,%rcx
cmp    $0xfff,%rcx
jne    1110 <compute_checksum+0x10>
neg    %eax
and    $0x3ff,%eax
ret

LLVM IR (-O0):
define i32 @compute_checksum(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
store i32* %0, i32** %2
store i32 0, i32* %3
store i32 0, i32* %4
br label %5
5: ; preds = %23, %1
%6 = load i32, i32* %4
%7 = icmp slt i32 %6, 4095
br i1 %7, label %8, label %26
8: ; preds = %5
%9 = load i32*, i32** %2
%10 = load i32, i32* %4
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i32, i32* %9, i64 %11
%13 = load i32, i32* %12
%14 = load i32, i32* %3
%15 = add nsw i32 %14, %13
store i32 %15, i32* %3
%16 = load i32, i32* %3
%17 = icmp sgt i32 %16, 1023
br i1 %17, label %18, label %22
18: ; preds = %8
%19 = load i32, i32* %3
%20 = and i32 %19, 1023
%21 = add nsw i32 %20, 1
store i32 %21, i32* %3
br label %22
22: ; preds = %18, %8
br label %23
23: ; preds = %22
%24 = load i32, i32* %4
%25 = add nsw i32 %24, 1
store i32 %25, i32* %4
br label %5
26: ; preds = %5
%27 = load i32, i32* %3
%28 = xor i32 %27, -1
%29 = add nsw i32 %28, 1
%30 = and i32 %29, 1023
ret i32 %30
}

LLVM IR (-O2):
define i32 @compute_checksum(i32* nocapture %0) {
br label %2
2: ; preds = %2, %1
%3 = phi i64 [ 0, %1 ], [ %28, %2 ]
%4 = phi i32 [ 0, %1 ], [ %27, %2 ]
%5 = getelementptr inbounds i32, i32* %0, i64 %3
%6 = load i32, i32* %5
%7 = add nsw i32 %6, %4
%8 = icmp sgt i32 %7, 1023
%9 = and i32 %7, 1023
%10 = add nuw nsw i32 %9, 1
%11 = select i1 %8, i32 %10, i32 %7
%12 = add nuw nsw i64 %3, 1
%13 = getelementptr inbounds i32, i32* %0, i64 %12
%14 = load i32, i32* %13
%15 = add nsw i32 %14, %11
%16 = icmp sgt i32 %15, 1023
%17 = and i32 %15, 1023
%18 = add nuw nsw i32 %17, 1
%19 = select i1 %16, i32 %18, i32 %15
%20 = add nuw nsw i64 %3, 2
%21 = getelementptr inbounds i32, i32* %0, i64 %20
%22 = load i32, i32* %21
%23 = add nsw i32 %22, %19
%24 = icmp sgt i32 %23, 1023
%25 = and i32 %23, 1023
%26 = add nuw nsw i32 %25, 1
%27 = select i1 %24, i32 %26, i32 %23
%28 = add nuw nsw i64 %3, 3
%29 = icmp eq i64 %28, 4095
br i1 %29, label %30, label %2
30: ; preds = %2
%31 = sub i32 0, %27
%32 = and i32 %31, 1023
ret i32 %32
}