#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int EOF;
int fgetc(int /*<<< orphan*/ *);
int chksum(FILE *fp, int *bp) {
  int c;
  long bytes = 0;
  int sum = 0;
  while ((c = fgetc(fp)) != EOF) {
    sum = (sum >> 1) + ((sum & 1) << 15);
    sum = (sum + c) & 0xffff;
    bytes++;
  }
  *bp = (bytes + 1023) / 1024;
  return sum;
}


x86 assembly (clang, -O0):
<chksum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x14(%rbp)
mov    0x2ea6(%rip),%rcx
cmp    (%rcx),%eax
je     117d <chksum+0x6d>
mov    -0x24(%rbp),%eax
sar    $0x1,%eax
mov    -0x24(%rbp),%ecx
and    $0x1,%ecx
shl    $0xf,%ecx
add    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    -0x14(%rbp),%eax
and    $0xffff,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    112f <chksum+0x1f>
mov    -0x20(%rbp),%rax
add    $0x3ff,%rax
mov    $0x400,%ecx
cqto
idiv   %rcx
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chksum>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <fgetc@plt>
mov    0x2ebe(%rip),%r12
xor    %ebx,%ebx
mov    $0x0,%ebp
cmp    (%r12),%eax
je     1164 <chksum+0x54>
xor    %ebp,%ebp
mov    $0x3ff,%ebx
xchg   %ax,%ax
mov    %ebp,%ecx
shr    %ecx
shl    $0xf,%ebp
or     %ecx,%ebp
add    %eax,%ebp
movzwl %bp,%ebp
mov    %r15,%rdi
call   1030 <fgetc@plt>
add    $0x1,%rbx
cmp    (%r12),%eax
jne    1140 <chksum+0x30>
shr    $0xa,%rbx
mov    %ebx,(%r14)
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EOF = global i32 0
define i32 @chksum(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
store i32* %0, i32** %3
store i32* %1, i32** %4
store i64 0, i64* %6
store i32 0, i32* %7
br label %8
8: ; preds = %13, %2
%9 = load i32*, i32** %3
%10 = call i32 @fgetc(i32* %9)
store i32 %10, i32* %5
%11 = load i32, i32* @EOF
%12 = icmp ne i32 %10, %11
br i1 %12, label %13, label %26
13: ; preds = %8
%14 = load i32, i32* %7
%15 = ashr i32 %14, 1
%16 = load i32, i32* %7
%17 = and i32 %16, 1
%18 = shl i32 %17, 15
%19 = add nsw i32 %15, %18
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = load i32, i32* %5
%22 = add nsw i32 %20, %21
%23 = and i32 %22, 65535
store i32 %23, i32* %7
%24 = load i64, i64* %6
%25 = add nsw i64 %24, 1
store i64 %25, i64* %6
br label %8
26: ; preds = %8
%27 = load i64, i64* %6
%28 = add nsw i64 %27, 1023
%29 = sdiv i64 %28, 1024
%30 = trunc i64 %29 to i32
%31 = load i32*, i32** %4
store i32 %30, i32* %31
%32 = load i32, i32* %7
ret i32 %32
}

LLVM IR (-O2):
@EOF = global i32 0
define i32 @chksum(i32* nocapture %0, i32* nocapture writeonly %1) {
%3 = tail call i32 @fgetc(i32* %0)
%4 = load i32, i32* @EOF
%5 = icmp eq i32 %3, %4
br i1 %5, label %23, label %6
6: ; preds = %2, %6
%7 = phi i32 [ %16, %6 ], [ %3, %2 ]
%8 = phi i32 [ %14, %6 ], [ 0, %2 ]
%9 = phi i64 [ %15, %6 ], [ 0, %2 ]
%10 = lshr i32 %8, 1
%11 = shl nuw nsw i32 %8, 15
%12 = or i32 %10, %11
%13 = add i32 %12, %7
%14 = and i32 %13, 65535
%15 = add nuw nsw i64 %9, 1
%16 = tail call i32 @fgetc(i32* %0)
%17 = load i32, i32* @EOF
%18 = icmp eq i32 %16, %17
br i1 %18, label %19, label %6
19: ; preds = %6
%20 = add nuw i64 %9, 1024
%21 = lshr i64 %20, 10
%22 = trunc i64 %21 to i32
br label %23
23: ; preds = %19, %2
%24 = phi i32 [ 0, %2 ], [ %22, %19 ]
%25 = phi i32 [ 0, %2 ], [ %14, %19 ]
store i32 %24, i32* %1
ret i32 %25
}