#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;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
scalar_t__ get_dword_from_buf(unsigned char *);
scalar_t__ lseek(int, int, int /*<<< orphan*/);
int read(int, int *, int);
int checksum(int fno, uint32_t *lptr, uint32_t offset) {
  uint32_t sum = 0;
  uint32_t i, size;
  unsigned char *ptr;
  uint32_t l;
  if (lseek(fno, offset, SEEK_SET) < 0)
    return (-1);
  for (; (size = read(fno, &l, sizeof(uint32_t))) == sizeof(uint32_t);)
    sum += get_dword_from_buf((unsigned char *)&l);
  if (size & 0x3) {
    union {
      unsigned char c[4];
      uint32_t l;
    } buf;
    buf.l = 0;
    for (i = 0, ptr = (unsigned char *)&l; i < (size & 0x3); i++)
      buf.c[i] = *ptr++;
    sum += get_dword_from_buf(&buf.c[0]);
  }
  *lptr = sum;
  return (0);
}


x86 assembly (clang, -O0):
<checksum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    0x2e8a(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
cmp    $0x0,%rax
jge    1173 <checksum+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    1236 <checksum+0x106>
jmp    1178 <checksum+0x48>
mov    -0x8(%rbp),%edi
lea    -0x2c(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    %eax,-0x20(%rbp)
cltq
cmp    $0x4,%rax
jne    11b3 <checksum+0x83>
lea    -0x2c(%rbp),%rdi
call   1030 <get_dword_from_buf@plt>
mov    %rax,%rcx
movslq -0x18(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x18(%rbp)
jmp    1178 <checksum+0x48>
mov    -0x20(%rbp),%eax
and    $0x3,%eax
cmp    $0x0,%eax
je     1226 <checksum+0xf6>
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x2c(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x20(%rbp),%ecx
and    $0x3,%ecx
cmp    %ecx,%eax
jge    1210 <checksum+0xe0>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
mov    (%rax),%cl
movslq -0x1c(%rbp),%rax
mov    %cl,-0x30(%rbp,%rax,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11d8 <checksum+0xa8>
lea    -0x30(%rbp),%rdi
call   1030 <get_dword_from_buf@plt>
mov    %rax,%rcx
movslq -0x18(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checksum>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %edi,%r15d
mov    0x2e89(%rip),%rax
mov    (%rax),%eax
mov    %edx,%esi
mov    %eax,%edx
call   1040 <lseek@plt>
test   %rax,%rax
js     11e4 <checksum+0xa4>
lea    0xc(%rsp),%rsi
mov    %r15d,%edi
mov    $0x4,%edx
call   1050 <read@plt>
xor    %ebp,%ebp
cmp    $0x4,%eax
jne    11af <checksum+0x6f>
lea    0xc(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1030 <get_dword_from_buf@plt>
add    %eax,%ebp
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1050 <read@plt>
cmp    $0x4,%eax
je     1190 <checksum+0x50>
test   $0x3,%al
je     11dd <checksum+0x9d>
movl   $0x0,0x10(%rsp)
and    $0x3,%eax
lea    0x10(%rsp),%rbx
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
mov    %rax,%rdx
call   1060 <memcpy@plt>
mov    %rbx,%rdi
call   1030 <get_dword_from_buf@plt>
add    %eax,%ebp
mov    %ebp,(%r14)
xor    %eax,%eax
jmp    11e9 <checksum+0xa9>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%union.anon = type { i32 }
@SEEK_SET = global i32 0
define i32 @checksum(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 i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca %union.anon
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i32 0, i32* %8
%14 = load i32, i32* %5
%15 = load i32, i32* %7
%16 = load i32, i32* @SEEK_SET
%17 = call i64 @lseek(i32 %14, i32 %15, i32 %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %3
store i32 -1, i32* %4
br label %67
20: ; preds = %3
br label %21
21: ; preds = %26, %20
%22 = load i32, i32* %5
%23 = call i32 @read(i32 %22, i32* %12, i32 4)
store i32 %23, i32* %10
%24 = sext i32 %23 to i64
%25 = icmp eq i64 %24, 4
br i1 %25, label %26, label %33
26: ; preds = %21
%27 = bitcast i32* %12 to i8*
%28 = call i64 @get_dword_from_buf(i8* %27)
%29 = load i32, i32* %8
%30 = sext i32 %29 to i64
%31 = add nsw i64 %30, %28
%32 = trunc i64 %31 to i32
store i32 %32, i32* %8
br label %21
33: ; preds = %21
%34 = load i32, i32* %10
%35 = and i32 %34, 3
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %64
37: ; preds = %33
%38 = bitcast %union.anon* %13 to i32*
store i32 0, i32* %38
store i32 0, i32* %9
%39 = bitcast i32* %12 to i8*
store i8* %39, i8** %11
br label %40
40: ; preds = %53, %37
%41 = load i32, i32* %9
%42 = load i32, i32* %10
%43 = and i32 %42, 3
%44 = icmp slt i32 %41, %43
br i1 %44, label %45, label %56
45: ; preds = %40
%46 = load i8*, i8** %11
%47 = getelementptr inbounds i8, i8* %46, i32 1
store i8* %47, i8** %11
%48 = load i8, i8* %46
%49 = bitcast %union.anon* %13 to [4 x i8]*
%50 = load i32, i32* %9
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [4 x i8], [4 x i8]* %49, i64 0, i64 %51
store i8 %48, i8* %52
br label %53
53: ; preds = %45
%54 = load i32, i32* %9
%55 = add nsw i32 %54, 1
store i32 %55, i32* %9
br label %40
56: ; preds = %40
%57 = bitcast %union.anon* %13 to [4 x i8]*
%58 = getelementptr inbounds [4 x i8], [4 x i8]* %57, i64 0, i64 0
%59 = call i64 @get_dword_from_buf(i8* %58)
%60 = load i32, i32* %8
%61 = sext i32 %60 to i64
%62 = add nsw i64 %61, %59
%63 = trunc i64 %62 to i32
store i32 %63, i32* %8
br label %64
64: ; preds = %56, %33
%65 = load i32, i32* %8
%66 = load i32*, i32** %6
store i32 %65, i32* %66
store i32 0, i32* %4
br label %67
67: ; preds = %64, %19
%68 = load i32, i32* %4
ret i32 %68
}

LLVM IR (-O2):
%union.anon = type { i32 }
@SEEK_SET = global i32 0
define i32 @checksum(i32 %0, i32* nocapture writeonly %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = alloca %union.anon
%7 = bitcast %union.anon* %6 to i8*
%8 = load i32, i32* @SEEK_SET
%9 = tail call i64 @lseek(i32 %0, i32 %2, i32 %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %35, label %11
11: ; preds = %3
%12 = call i32 @read(i32 %0, i32* %4, i32 4)
%13 = icmp eq i32 %12, 4
br i1 %13, label %14, label %21
14: ; preds = %11, %14
%15 = phi i32 [ %18, %14 ], [ 0, %11 ]
%16 = call i64 @get_dword_from_buf(i8* %5)
%17 = trunc i64 %16 to i32
%18 = add i32 %15, %17
%19 = call i32 @read(i32 %0, i32* %4, i32 4)
%20 = icmp eq i32 %19, 4
br i1 %20, label %14, label %21
21: ; preds = %14, %11
%22 = phi i32 [ 0, %11 ], [ %18, %14 ]
%23 = phi i32 [ %12, %11 ], [ %19, %14 ]
%24 = and i32 %23, 3
%25 = icmp eq i32 %24, 0
br i1 %25, label %33, label %26
26: ; preds = %21
%27 = getelementptr inbounds %union.anon, %union.anon* %6, i64 0, i32 0
store i32 0, i32* %27
%28 = and i32 %23, 3
%29 = zext i32 %28 to i64
%30 = call i64 @get_dword_from_buf(i8* %7)
%31 = trunc i64 %30 to i32
%32 = add i32 %22, %31
br label %33
33: ; preds = %26, %21
%34 = phi i32 [ %32, %26 ], [ %22, %21 ]
store i32 %34, i32* %1
br label %35
35: ; preds = %3, %33
%36 = phi i32 [ 0, %33 ], [ -1, %3 ]
ret i32 %36
}