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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ errno;
scalar_t__ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int fseek(int /*<<< orphan*/ *, unsigned int, int /*<<< orphan*/);
int fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
unsigned copysum(FILE *s, FILE *d, unsigned len, unsigned off) {
  unsigned char temp;
  unsigned sum = 0;
  unsigned i;
  if (fseek(s, off, SEEK_SET) == -1) {
    fprintf(stderr, "fseek failed: %s\n", strerror(errno));
    return -1;
  }
  for (i = 0; i < len; i++) {
    if (fread(&temp, 1, 1, s) != 1) {
      fprintf(stderr, "Failure in copysum: ");
      if (ferror(s))
        fprintf(stderr, "fread error: %s\n", strerror(errno));
      else if (feof(s))
        fprintf(stderr, "fread length 1 at offset %d hit EOF.\n", off);
      return -1;
    }
    sum = sum + (temp & 0xff);
    if (d)
      if (fwrite(&temp, 1, 1, d) != 1) {
        fprintf(stderr, "Failure in copysum; fwrite error: %s\n",
                strerror(errno));
        return -1;
      }
  }
  return sum;
}


x86 assembly (clang, -O0):
<copysum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
cmp    $0xffffffff,%eax
jne    11e5 <copysum+0x75>
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rdx
lea    0xe2e(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1342 <copysum+0x1d2>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jae    133c <copysum+0x1cc>
mov    -0x10(%rbp),%rcx
lea    -0x21(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
cmp    $0x1,%eax
je     12b0 <copysum+0x140>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
lea    0xded(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1272 <copysum+0x102>
mov    0x2d9a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
lea    0xdc1(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    12a4 <copysum+0x134>
mov    -0x10(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%rax
je     129f <copysum+0x12f>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
lea    0xda0(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    12a4 <copysum+0x134>
movl   $0xffffffff,-0x4(%rbp)
jmp    1342 <copysum+0x1d2>
mov    -0x28(%rbp),%eax
movzbl -0x21(%rbp),%ecx
and    $0xff,%ecx
add    %ecx,%eax
mov    %eax,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1329 <copysum+0x1b9>
mov    -0x18(%rbp),%rcx
lea    -0x21(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1080 <fwrite@plt>
cmp    $0x1,%eax
je     1324 <copysum+0x1b4>
mov    0x2cef(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2cdb(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x38(%rbp),%edi
mov    %rax,%rdx
lea    0xd4d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1342 <copysum+0x1d2>
jmp    1329 <copysum+0x1b9>
jmp    132e <copysum+0x1be>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11ec <copysum+0x7c>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copysum>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%ebp
mov    %edx,%ebx
mov    %rsi,%r12
mov    %rdi,%r15
mov    0x2e44(%rip),%rax
mov    (%rax),%edx
mov    %ecx,%esi
call   1070 <fseek@plt>
cmp    $0xffffffff,%eax
je     122b <copysum+0xbb>
test   %ebx,%ebx
je     124b <copysum+0xdb>
mov    %ebp,0x4(%rsp)
xor    %ebp,%ebp
lea    0x3(%rsp),%r13
jmp    11cc <copysum+0x5c>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    %r14d,%ebp
add    $0xffffffff,%ebx
je     129b <copysum+0x12b>
mov    %r13,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    124f <copysum+0xdf>
movzbl 0x3(%rsp),%r14d
test   %r12,%r12
je     11c0 <copysum+0x50>
mov    %r13,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   1080 <fwrite@plt>
cmp    $0x1,%eax
je     11c0 <copysum+0x50>
mov    0x2dce(%rip),%rax
mov    (%rax),%ebx
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe35(%rip),%rsi
jmp    128a <copysum+0x11a>
mov    0x2dae(%rip),%rax
mov    (%rax),%ebx
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdb7(%rip),%rsi
jmp    128a <copysum+0x11a>
xor    %ebp,%ebp
jmp    129b <copysum+0x12b>
mov    0x2d8a(%rip),%rbx
mov    (%rbx),%edi
lea    0xdb3(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r15,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     12ac <copysum+0x13c>
mov    (%rbx),%ebx
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd9d(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15,%rdi
call   1060 <feof@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     129b <copysum+0x12b>
mov    (%rbx),%edi
lea    0xd71(%rip),%rsi
mov    0x4(%rsp),%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    129b <copysum+0x12b>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"fseek failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [21 x i8] c"Failure in copysum: \00"
@.str.2 = [17 x i8] c"fread error: %s\0A\00"
@.str.3 = [38 x i8] c"fread length 1 at offset %d hit EOF.\0A\00"
@.str.4 = [38 x i8] c"Failure in copysum; fwrite error: %s\0A\00"
define i32 @copysum(i32* %0, i32* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8
%11 = alloca i32
%12 = alloca i32
store i32* %0, i32** %6
store i32* %1, i32** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 0, i32* %11
%13 = load i32*, i32** %6
%14 = load i32, i32* %9
%15 = load i32, i32* @SEEK_SET
%16 = call i32 @fseek(i32* %13, i32 %14, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %23
18: ; preds = %4
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %21)
store i32 -1, i32* %5
br label %77
23: ; preds = %4
store i32 0, i32* %12
br label %24
24: ; preds = %72, %23
%25 = load i32, i32* %12
%26 = load i32, i32* %8
%27 = icmp ult i32 %25, %26
br i1 %27, label %28, label %75
28: ; preds = %24
%29 = load i32*, i32** %6
%30 = call i32 @fread(i8* %10, i32 1, i32 1, i32* %29)
%31 = icmp ne i32 %30, 1
br i1 %31, label %32, label %53
32: ; preds = %28
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%35 = load i32*, i32** %6
%36 = call i64 @ferror(i32* %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %32
%39 = load i32, i32* @stderr
%40 = load i32, i32* @errno
%41 = call i8* @strerror(i32 %40)
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %41)
br label %52
43: ; preds = %32
%44 = load i32*, i32** %6
%45 = call i64 @feof(i32* %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %43
%48 = load i32, i32* @stderr
%49 = load i32, i32* %9
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %49)
br label %51
51: ; preds = %47, %43
br label %52
52: ; preds = %51, %38
store i32 -1, i32* %5
br label %77
53: ; preds = %28
%54 = load i32, i32* %11
%55 = load i8, i8* %10
%56 = zext i8 %55 to i32
%57 = and i32 %56, 255
%58 = add i32 %54, %57
store i32 %58, i32* %11
%59 = load i32*, i32** %7
%60 = icmp ne i32* %59, null
br i1 %60, label %61, label %71
61: ; preds = %53
%62 = load i32*, i32** %7
%63 = call i32 @fwrite(i8* %10, i32 1, i32 1, i32* %62)
%64 = icmp ne i32 %63, 1
br i1 %64, label %65, label %70
65: ; preds = %61
%66 = load i32, i32* @stderr
%67 = load i32, i32* @errno
%68 = call i8* @strerror(i32 %67)
%69 = call i32 (i32, i8*, ...) @fprintf(i32 %66, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i8* %68)
store i32 -1, i32* %5
br label %77
70: ; preds = %61
br label %71
71: ; preds = %70, %53
br label %72
72: ; preds = %71
%73 = load i32, i32* %12
%74 = add i32 %73, 1
store i32 %74, i32* %12
br label %24
75: ; preds = %24
%76 = load i32, i32* %11
store i32 %76, i32* %5
br label %77
77: ; preds = %75, %65, %52, %18
%78 = load i32, i32* %5
ret i32 %78
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"fseek failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [21 x i8] c"Failure in copysum: \00"
@.str.2 = [17 x i8] c"fread error: %s\0A\00"
@.str.3 = [38 x i8] c"fread length 1 at offset %d hit EOF.\0A\00"
@.str.4 = [38 x i8] c"Failure in copysum; fwrite error: %s\0A\00"
define i32 @copysum(i32* nocapture %0, i32* %1, i32 %2, i32 %3) {
%5 = alloca i8
%6 = load i32, i32* @SEEK_SET
%7 = tail call i32 @fseek(i32* %0, i32 %3, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %13, label %9
9: ; preds = %4
%10 = icmp eq i32 %2, 0
br i1 %10, label %54, label %11
11: ; preds = %9
%12 = icmp eq i32* %1, null
br label %18
13: ; preds = %4
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %16)
br label %54
18: ; preds = %11, %51
%19 = phi i32 [ 0, %11 ], [ %52, %51 ]
%20 = phi i32 [ 0, %11 ], [ %42, %51 ]
%21 = call i32 @fread(i8* %5, i32 1, i32 1, i32* %0)
%22 = icmp eq i32 %21, 1
br i1 %22, label %39, label %23
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = tail call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%26 = tail call i64 @ferror(i32* %0)
%27 = icmp eq i64 %26, 0
br i1 %27, label %33, label %28
28: ; preds = %23
%29 = load i32, i32* @stderr
%30 = load i32, i32* @errno
%31 = tail call i8* @strerror(i32 %30)
%32 = tail call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %31)
br label %54
33: ; preds = %23
%34 = tail call i64 @feof(i32* %0)
%35 = icmp eq i64 %34, 0
br i1 %35, label %54, label %36
36: ; preds = %33
%37 = load i32, i32* @stderr
%38 = tail call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %3)
br label %54
39: ; preds = %18
%40 = load i8, i8* %5
%41 = zext i8 %40 to i32
%42 = add i32 %20, %41
br i1 %12, label %51, label %43
43: ; preds = %39
%44 = call i32 @fwrite(i8* %5, i32 1, i32 1, i32* %1)
%45 = icmp eq i32 %44, 1
br i1 %45, label %51, label %46
46: ; preds = %43
%47 = load i32, i32* @stderr
%48 = load i32, i32* @errno
%49 = tail call i8* @strerror(i32 %48)
%50 = tail call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i8* %49)
br label %54
51: ; preds = %39, %43
%52 = add nuw i32 %19, 1
%53 = icmp eq i32 %52, %2
br i1 %53, label %54, label %18
54: ; preds = %51, %9, %28, %36, %33, %46, %13
%55 = phi i32 [ -1, %13 ], [ -1, %46 ], [ -1, %33 ], [ -1, %36 ], [ -1, %28 ], [ 0, %9 ], [ %42, %51 ]
ret i32 %55
}