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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
unsigned short ipchksum(unsigned char *, int, unsigned short);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, unsigned char *, int);
unsigned short fchksum(int fd) {
  unsigned char buf[128];
  int byteRead;
  unsigned short resid = 0;
  int i = 0;
  lseek(fd, 0, SEEK_SET);
  while ((byteRead = read(fd, buf, sizeof(buf))) > 0) {
    resid = ipchksum(buf, byteRead, resid);
  }
  return ~resid;
}


x86 assembly (clang, -O0):
<fchksum>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0x4(%rbp)
movw   $0x0,-0x96(%rbp)
movl   $0x0,-0x9c(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e85(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    -0x4(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1050 <read@plt>
mov    %eax,-0x94(%rbp)
cmp    $0x0,%eax
jle    11ac <fchksum+0x7c>
lea    -0x90(%rbp),%rdi
mov    -0x94(%rbp),%esi
movzwl -0x96(%rbp),%edx
call   1030 <ipchksum@plt>
mov    %ax,-0x96(%rbp)
jmp    1164 <fchksum+0x34>
movzwl -0x96(%rbp),%eax
xor    $0xffffffff,%eax
movzwl %ax,%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fchksum>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    %edi,%r14d
mov    0x2e9b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x80,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    1197 <fchksum+0x67>
xor    %ebp,%ebp
mov    %rsp,%rbx
nopw   0x0(%rax,%rax,1)
movzwl %bp,%edx
mov    %rbx,%rdi
mov    %eax,%esi
call   1030 <ipchksum@plt>
mov    %eax,%ebp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x80,%edx
call   1050 <read@plt>
test   %eax,%eax
jg     1170 <fchksum+0x40>
not    %ebp
jmp    119b <fchksum+0x6b>
mov    $0xffff,%bp
mov    %ebp,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
define zeroext i16 @fchksum(i32 %0) {
%2 = alloca i32
%3 = alloca [128 x i8]
%4 = alloca i32
%5 = alloca i16
%6 = alloca i32
store i32 %0, i32* %2
store i16 0, i16* %5
store i32 0, i32* %6
%7 = load i32, i32* %2
%8 = load i32, i32* @SEEK_SET
%9 = call i32 @lseek(i32 %7, i32 0, i32 %8)
br label %10
10: ; preds = %15, %1
%11 = load i32, i32* %2
%12 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%13 = call i32 @read(i32 %11, i8* %12, i32 128)
store i32 %13, i32* %4
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %10
%16 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%17 = load i32, i32* %4
%18 = load i16, i16* %5
%19 = call zeroext i16 @ipchksum(i8* %16, i32 %17, i16 zeroext %18)
store i16 %19, i16* %5
br label %10
20: ; preds = %10
%21 = load i16, i16* %5
%22 = zext i16 %21 to i32
%23 = xor i32 %22, -1
%24 = trunc i32 %23 to i16
ret i16 %24
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
define zeroext i16 @fchksum(i32 %0) {
%2 = alloca [128 x i8]
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @SEEK_SET
%5 = tail call i32 @lseek(i32 %0, i32 0, i32 %4)
%6 = call i32 @read(i32 %0, i8* %3, i32 128)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %16
8: ; preds = %1, %8
%9 = phi i32 [ %12, %8 ], [ %6, %1 ]
%10 = phi i16 [ %11, %8 ], [ 0, %1 ]
%11 = call zeroext i16 @ipchksum(i8* %3, i32 %9, i16 zeroext %10)
%12 = call i32 @read(i32 %0, i8* %3, i32 128)
%13 = icmp sgt i32 %12, 0
br i1 %13, label %8, label %14
14: ; preds = %8
%15 = xor i16 %11, -1
br label %16
16: ; preds = %14, %1
%17 = phi i16 [ -1, %1 ], [ %15, %14 ]
ret i16 %17
}