#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 uint32_t;
typedef int len;
typedef int bsd;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
char EOF;
char fgetc(int /*<<< orphan*/ *);
int bsdsum(FILE *file, uint32_t *bsdsum, int *bsdlen) {
  if (!file)
    return 1;
  uint32_t bsd = 0;
  int len = 0;
  char ch;
  while ((ch = fgetc(file)) != EOF) {
    bsd = (((bsd) >> 1 | ((bsd)&1) << 15) + ch) & 0xffff;
    len++;
  }
  *bsdlen = (((len) + (1024) - 1) / (1024));
  *bsdsum = bsd;
  return 0;
}


x86 assembly (clang, -O0):
<bsdsum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    113b <bsdsum+0x2b>
movl   $0x1,-0x4(%rbp)
jmp    11c2 <bsdsum+0xb2>
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %al,-0x29(%rbp)
movsbl %al,%eax
mov    0x2e89(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     1197 <bsdsum+0x87>
mov    -0x24(%rbp),%eax
sar    $0x1,%eax
mov    -0x24(%rbp),%ecx
and    $0x1,%ecx
shl    $0xf,%ecx
or     %ecx,%eax
movsbl -0x29(%rbp),%ecx
add    %ecx,%eax
and    $0xffff,%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1149 <bsdsum+0x39>
mov    -0x28(%rbp),%eax
add    $0x400,%eax
sub    $0x1,%eax
mov    $0x400,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bsdsum>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     117f <bsdsum+0x6f>
mov    %rsi,%r15
mov    %rdi,%r13
mov    %rdx,(%rsp)
call   1030 <fgetc@plt>
mov    0x2eb2(%rip),%r14
xor    %r12d,%r12d
mov    $0x0,%ebp
mov    $0x0,%ebx
cmp    (%r14),%al
je     1174 <bsdsum+0x64>
xor    %ebp,%ebp
mov    $0x3ff,%ebx
nop
movsbl %al,%eax
mov    %ebp,%ecx
shr    %ecx
shl    $0xf,%ebp
or     %ecx,%ebp
add    %eax,%ebp
movzwl %bp,%ebp
mov    %r13,%rdi
call   1030 <fgetc@plt>
add    $0x1,%ebx
cmp    (%r14),%al
jne    1150 <bsdsum+0x40>
shr    $0xa,%ebx
mov    (%rsp),%rax
mov    %ebx,(%rax)
mov    %ebp,(%r15)
jmp    1185 <bsdsum+0x75>
mov    $0x1,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EOF = global i8 0
define i32 @bsdsum(i32* %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8
store i32* %0, i32** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
%11 = load i32*, i32** %5
%12 = icmp ne i32* %11, null
br i1 %12, label %14, label %13
13: ; preds = %3
store i32 1, i32* %4
br label %43
14: ; preds = %3
store i32 0, i32* %8
store i32 0, i32* %9
br label %15
15: ; preds = %22, %14
%16 = load i32*, i32** %5
%17 = call signext i8 @fgetc(i32* %16)
store i8 %17, i8* %10
%18 = sext i8 %17 to i32
%19 = load i8, i8* @EOF
%20 = sext i8 %19 to i32
%21 = icmp ne i32 %18, %20
br i1 %21, label %22, label %35
22: ; preds = %15
%23 = load i32, i32* %8
%24 = ashr i32 %23, 1
%25 = load i32, i32* %8
%26 = and i32 %25, 1
%27 = shl i32 %26, 15
%28 = or i32 %24, %27
%29 = load i8, i8* %10
%30 = sext i8 %29 to i32
%31 = add nsw i32 %28, %30
%32 = and i32 %31, 65535
store i32 %32, i32* %8
%33 = load i32, i32* %9
%34 = add nsw i32 %33, 1
store i32 %34, i32* %9
br label %15
35: ; preds = %15
%36 = load i32, i32* %9
%37 = add nsw i32 %36, 1024
%38 = sub nsw i32 %37, 1
%39 = sdiv i32 %38, 1024
%40 = load i32*, i32** %7
store i32 %39, i32* %40
%41 = load i32, i32* %8
%42 = load i32*, i32** %6
store i32 %41, i32* %42
store i32 0, i32* %4
br label %43
43: ; preds = %35, %13
%44 = load i32, i32* %4
ret i32 %44
}

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