#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*/ uint8_t;
typedef int uint32_t;
typedef int uint16_t;
typedef int /*<<< orphan*/ int32_t;

/* Variables and functions */
int read(int /*<<< orphan*/, int /*<<< orphan*/ *, int);
uint16_t calc_checksum(int32_t bug_fd) {
  uint32_t checksum_var = 0;
  uint8_t buf[2];
  int n;
  while ((n = read(bug_fd, buf, 2))) {
    checksum_var = checksum_var + *(uint16_t *)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)
movl   $0x0,-0x8(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x2,%edx
call   1030 <read@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     116a <calc_checksum+0x5a>
mov    -0x8(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
sar    $0x10,%eax
cmp    $0x0,%eax
je     1165 <calc_checksum+0x55>
mov    -0x8(%rbp),%eax
and    $0xffff,%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1122 <calc_checksum+0x12>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

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

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

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