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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ lseek(int, int, int /*<<< orphan*/);
int read_buff(int, unsigned char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int test_read_rest(char *filename, int fd, unsigned char *buff,
                   ssize_t trunc_size, size_t size) {
  ssize_t buflen;
  if (lseek(fd, trunc_size, SEEK_SET) < 0) {
    fprintf(stderr, "seek failed: %s: %s\n", filename, strerror(errno));
    return -1;
  }
  buflen = size - trunc_size;
  while (buflen > 0) {
    int j;
    ssize_t n = (buflen > (65536)) ? (65536) : buflen;
    if (read_buff(fd, buff, n) < n) {
      fprintf(stderr, "read failed: %s %s\n", filename, strerror(errno));
      return -1;
    }
    for (j = 0; j < n; j++) {
      if (buff[j] != 0) {
        fprintf(stderr, "Byte %d different from expected value: %d vs %d\n", j,
                0, buff[j]);
        return -1;
      }
    }
    buflen -= n;
  }
  return 0;
}


x86 assembly (clang, -O0):
<test_read_rest>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    0x2e69(%rip),%rax
mov    (%rax),%edx
call   1030 <lseek@plt>
cmp    $0x0,%rax
jge    11be <test_read_rest+0x7e>
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xe55(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e1 <test_read_rest+0x1a1>
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jle    12da <test_read_rest+0x19a>
cmpl   $0x10000,-0x34(%rbp)
jle    11f0 <test_read_rest+0xb0>
mov    $0x10000,%eax
mov    %eax,-0x50(%rbp)
jmp    11f6 <test_read_rest+0xb6>
mov    -0x34(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x50(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1050 <read_buff@plt>
cmp    -0x3c(%rbp),%eax
jge    125a <test_read_rest+0x11a>
mov    0x2dc5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rdx
mov    %rax,%rcx
lea    0xdce(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e1 <test_read_rest+0x1a1>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jge    12ca <test_read_rest+0x18a>
mov    -0x20(%rbp),%rax
movslq -0x38(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     12b7 <test_read_rest+0x177>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%edx
mov    -0x20(%rbp),%rax
movslq -0x38(%rbp),%rcx
movzbl (%rax,%rcx,1),%r8d
lea    0xd87(%rip),%rsi
xor    %ecx,%ecx
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e1 <test_read_rest+0x1a1>
jmp    12bc <test_read_rest+0x17c>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1261 <test_read_rest+0x121>
mov    -0x3c(%rbp),%ecx
mov    -0x34(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x34(%rbp)
jmp    11cc <test_read_rest+0x8c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_read_rest>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8,%r15
mov    %ecx,%ebp
mov    %rdx,%rbx
mov    %esi,%r12d
mov    %rdi,%r14
mov    0x2e73(%rip),%rax
mov    (%rax),%edx
mov    %esi,%edi
mov    %ecx,%esi
call   1030 <lseek@plt>
test   %rax,%rax
js     11cc <test_read_rest+0x8c>
sub    %ebp,%r15d
test   %r15d,%r15d
jle    1220 <test_read_rest+0xe0>
cmp    $0x10000,%r15d
mov    $0x10000,%ebp
cmovb  %r15d,%ebp
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <read_buff@plt>
cmp    %ebp,%eax
jl     122b <test_read_rest+0xeb>
mov    %ebp,%eax
xor    %edx,%edx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl (%rbx,%rdx,1),%r8d
test   %r8d,%r8d
jne    1200 <test_read_rest+0xc0>
add    $0x1,%rdx
cmp    %rdx,%rax
jne    11b0 <test_read_rest+0x70>
xor    %eax,%eax
sub    %ebp,%r15d
jg     1179 <test_read_rest+0x39>
jmp    1222 <test_read_rest+0xe2>
mov    0x2e0d(%rip),%rax
mov    (%rax),%ebx
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe16(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    1222 <test_read_rest+0xe2>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
lea    0xe19(%rip),%rsi
xor    %ecx,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    1222 <test_read_rest+0xe2>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dae(%rip),%rax
mov    (%rax),%ebx
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xdcc(%rip),%rsi
jmp    11ea <test_read_rest+0xaa>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"seek failed: %s: %s\0A\00"
@errno = global i32 0
@.str.1 = [20 x i8] c"read failed: %s %s\0A\00"
@.str.2 = [49 x i8] c"Byte %d different from expected value: %d vs %d\0A\00"
define i32 @test_read_rest(i8* %0, i32 %1, i8* %2, i32 %3, i64 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i8* %0, i8** %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i64 %4, i64* %11
%15 = load i32, i32* %8
%16 = load i32, i32* %10
%17 = load i32, i32* @SEEK_SET
%18 = call i64 @lseek(i32 %15, i32 %16, i32 %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %5
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %7
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %22, i8* %24)
store i32 -1, i32* %6
br label %87
26: ; preds = %5
%27 = load i64, i64* %11
%28 = load i32, i32* %10
%29 = sext i32 %28 to i64
%30 = sub i64 %27, %29
%31 = trunc i64 %30 to i32
store i32 %31, i32* %12
br label %32
32: ; preds = %82, %26
%33 = load i32, i32* %12
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %86
35: ; preds = %32
%36 = load i32, i32* %12
%37 = icmp sgt i32 %36, 65536
br i1 %37, label %38, label %39
38: ; preds = %35
br label %41
39: ; preds = %35
%40 = load i32, i32* %12
br label %41
41: ; preds = %39, %38
%42 = phi i32 [ 65536, %38 ], [ %40, %39 ]
store i32 %42, i32* %14
%43 = load i32, i32* %8
%44 = load i8*, i8** %9
%45 = load i32, i32* %14
%46 = call i32 @read_buff(i32 %43, i8* %44, i32 %45)
%47 = load i32, i32* %14
%48 = icmp slt i32 %46, %47
br i1 %48, label %49, label %55
49: ; preds = %41
%50 = load i32, i32* @stderr
%51 = load i8*, i8** %7
%52 = load i32, i32* @errno
%53 = call i8* @strerror(i32 %52)
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %51, i8* %53)
store i32 -1, i32* %6
br label %87
55: ; preds = %41
store i32 0, i32* %13
br label %56
56: ; preds = %79, %55
%57 = load i32, i32* %13
%58 = load i32, i32* %14
%59 = icmp slt i32 %57, %58
br i1 %59, label %60, label %82
60: ; preds = %56
%61 = load i8*, i8** %9
%62 = load i32, i32* %13
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %61, i64 %63
%65 = load i8, i8* %64
%66 = zext i8 %65 to i32
%67 = icmp ne i32 %66, 0
br i1 %67, label %68, label %78
68: ; preds = %60
%69 = load i32, i32* @stderr
%70 = load i32, i32* %13
%71 = load i8*, i8** %9
%72 = load i32, i32* %13
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i8, i8* %71, i64 %73
%75 = load i8, i8* %74
%76 = zext i8 %75 to i32
%77 = call i32 (i32, i8*, ...) @fprintf(i32 %69, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 %70, i32 0, i32 %76)
store i32 -1, i32* %6
br label %87
78: ; preds = %60
br label %79
79: ; preds = %78
%80 = load i32, i32* %13
%81 = add nsw i32 %80, 1
store i32 %81, i32* %13
br label %56
82: ; preds = %56
%83 = load i32, i32* %14
%84 = load i32, i32* %12
%85 = sub nsw i32 %84, %83
store i32 %85, i32* %12
br label %32
86: ; preds = %32
store i32 0, i32* %6
br label %87
87: ; preds = %86, %68, %49, %20
%88 = load i32, i32* %6
ret i32 %88
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"seek failed: %s: %s\0A\00"
@errno = global i32 0
@.str.1 = [20 x i8] c"read failed: %s %s\0A\00"
@.str.2 = [49 x i8] c"Byte %d different from expected value: %d vs %d\0A\00"
define i32 @test_read_rest(i8* %0, i32 %1, i8* %2, i32 %3, i64 %4) {
%6 = load i32, i32* @SEEK_SET
%7 = tail call i64 @lseek(i32 %1, i32 %3, i32 %6)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %5
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = tail call i8* @strerror(i32 %11)
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %12)
br label %47
14: ; preds = %5
%15 = trunc i64 %4 to i32
%16 = sub i32 %15, %3
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %47
18: ; preds = %14, %44
%19 = phi i32 [ %45, %44 ], [ %16, %14 ]
%20 = icmp ult i32 %19, 65536
%21 = select i1 %20, i32 %19, i32 65536
%22 = tail call i32 @read_buff(i32 %1, i8* %2, i32 %21)
%23 = icmp slt i32 %22, %21
br i1 %23, label %26, label %24
24: ; preds = %18
%25 = zext i32 %21 to i64
br label %31
26: ; preds = %18
%27 = load i32, i32* @stderr
%28 = load i32, i32* @errno
%29 = tail call i8* @strerror(i32 %28)
%30 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %29)
br label %47
31: ; preds = %24, %41
%32 = phi i64 [ 0, %24 ], [ %42, %41 ]
%33 = getelementptr inbounds i8, i8* %2, i64 %32
%34 = load i8, i8* %33
%35 = icmp eq i8 %34, 0
br i1 %35, label %41, label %36
36: ; preds = %31
%37 = trunc i64 %32 to i32
%38 = zext i8 %34 to i32
%39 = load i32, i32* @stderr
%40 = tail call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 %37, i32 0, i32 %38)
br label %47
41: ; preds = %31
%42 = add nuw nsw i64 %32, 1
%43 = icmp eq i64 %42, %25
br i1 %43, label %44, label %31
44: ; preds = %41
%45 = sub nsw i32 %19, %21
%46 = icmp sgt i32 %45, 0
br i1 %46, label %18, label %47
47: ; preds = %44, %14, %36, %26, %9
%48 = phi i32 [ -1, %9 ], [ -1, %26 ], [ -1, %36 ], [ 0, %14 ], [ 0, %44 ]
ret i32 %48
}