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

/* Variables and functions */
int read(int /*<<< orphan*/, unsigned char *, int);
unsigned short calc_checksum(int32_t bug_fd) {
  unsigned long checksum_var = 0;
  unsigned char buf[2];
  int n;
  while ((n = read(bug_fd, buf, 2))) {
    checksum_var = checksum_var + *(unsigned short *)buf;
    if (checksum_var >> 16)
      checksum_var = (checksum_var & 0x0000ffff) + 1;
  }
  return checksum_var;
}


x86 assembly (clang, -O0):
<calc_checksum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x12(%rbp),%rsi
mov    $0x2,%edx
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
je     1178 <calc_checksum+0x68>
mov    -0x10(%rbp),%rax
movzwl -0x12(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
shr    $0x10,%rax
cmp    $0x0,%rax
je     1173 <calc_checksum+0x63>
mov    -0x10(%rbp),%rax
and    $0xffff,%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1123 <calc_checksum+0x13>
mov    -0x10(%rbp),%rax
movzwl %ax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calc_checksum>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
lea    0xe(%rsp),%rsi
mov    $0x2,%edx
call   1030 <read@plt>
xor    %ebx,%ebx
test   %eax,%eax
je     116c <calc_checksum+0x5c>
lea    0xe(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movzwl 0xe(%rsp),%eax
add    %rbx,%rax
movzwl %ax,%ebx
add    $0x1,%rbx
cmp    $0x10000,%rax
cmovb  %rax,%rbx
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x2,%edx
call   1030 <read@plt>
test   %eax,%eax
jne    1140 <calc_checksum+0x30>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define zeroext i16 @calc_checksum(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca [2 x i8]
%5 = alloca i32
store i32 %0, i32* %2
store i64 0, i64* %3
br label %6
6: ; preds = %25, %1
%7 = load i32, i32* %2
%8 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%9 = call i32 @read(i32 %7, i8* %8, i32 2)
store i32 %9, i32* %5
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %26
11: ; preds = %6
%12 = load i64, i64* %3
%13 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%14 = bitcast i8* %13 to i16*
%15 = load i16, i16* %14
%16 = zext i16 %15 to i64
%17 = add i64 %12, %16
store i64 %17, i64* %3
%18 = load i64, i64* %3
%19 = lshr i64 %18, 16
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %11
%22 = load i64, i64* %3
%23 = and i64 %22, 65535
%24 = add i64 %23, 1
store i64 %24, i64* %3
br label %25
25: ; preds = %21, %11
br label %6
26: ; preds = %6
%27 = load i64, i64* %3
%28 = trunc i64 %27 to i16
ret i16 %28
}

LLVM IR (-O2):
define zeroext i16 @calc_checksum(i32 %0) {
%2 = alloca i16
%3 = bitcast i16* %2 to i8*
%4 = call i32 @read(i32 %0, i8* %3, i32 2)
%5 = icmp eq i32 %4, 0
br i1 %5, label %19, label %6
6: ; preds = %1, %6
%7 = phi i64 [ %14, %6 ], [ 0, %1 ]
%8 = load i16, i16* %2
%9 = zext i16 %8 to i64
%10 = add i64 %7, %9
%11 = icmp ult i64 %10, 65536
%12 = and i64 %10, 65535
%13 = add nuw nsw i64 %12, 1
%14 = select i1 %11, i64 %10, i64 %13
%15 = call i32 @read(i32 %0, i8* %3, i32 2)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %6
17: ; preds = %6
%18 = trunc i64 %14 to i16
br label %19
19: ; preds = %17, %1
%20 = phi i16 [ 0, %1 ], [ %18, %17 ]
ret i16 %20
}