#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 /*<<< orphan*/, int /*<<< orphan*/);
int random();
int read_buff(int, unsigned char *, int);
int /*<<< orphan*/ srandom(unsigned int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int test_read_random(char *filename, int fd, unsigned char *buff,
                     ssize_t size) {
  ssize_t buflen;
  if (lseek(fd, 0, SEEK_SET) < 0) {
    fprintf(stderr, "seek failed: %s: %s\n", filename, strerror(errno));
    return -1;
  }
  srandom((unsigned int)(0xdeadbeef));
  buflen = 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++) {
      unsigned char val = random() & 0xff;
      if (buff[j] != val) {
        fprintf(stderr, "Byte %d different from expected value: %d vs %d\n", j,
                val, buff[j]);
        return -1;
      }
    }
    buflen -= n;
  }
  return 0;
}


x86 assembly (clang, -O0):
<test_read_random>:
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    -0x14(%rbp),%edi
mov    0x2e50(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0x0,%rax
jge    11d9 <test_read_random+0x79>
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xe3a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1312 <test_read_random+0x1b2>
mov    $0xdeadbeef,%edi
call   1030 <srandom@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jle    130b <test_read_random+0x1ab>
cmpl   $0x10000,-0x28(%rbp)
jle    120d <test_read_random+0xad>
mov    $0x10000,%eax
mov    %eax,-0x48(%rbp)
jmp    1213 <test_read_random+0xb3>
mov    -0x28(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x48(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1060 <read_buff@plt>
cmp    -0x30(%rbp),%eax
jge    1277 <test_read_random+0x117>
mov    0x2da8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rdx
mov    %rax,%rcx
lea    0xdb1(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1312 <test_read_random+0x1b2>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    12fb <test_read_random+0x19b>
mov    $0x0,%al
call   1070 <random@plt>
and    $0xff,%eax
mov    %al,-0x31(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
movzbl -0x31(%rbp),%ecx
cmp    %ecx,%eax
je     12e8 <test_read_random+0x188>
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
mov    -0x2c(%rbp),%edx
movzbl -0x31(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rsi
movzbl (%rax,%rsi,1),%r8d
lea    0xd54(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1312 <test_read_random+0x1b2>
jmp    12ed <test_read_random+0x18d>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    127e <test_read_random+0x11e>
mov    -0x30(%rbp),%ecx
mov    -0x28(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x28(%rbp)
jmp    11e9 <test_read_random+0x89>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_read_random>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r12d
mov    %rdx,%r14
mov    %esi,%ebx
mov    %rdi,%rbp
mov    0x2e50(%rip),%rax
mov    (%rax),%edx
xor    %r13d,%r13d
mov    %esi,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
test   %rax,%rax
js     1205 <test_read_random+0xa5>
mov    %rbp,0x10(%rsp)
mov    $0xdeadbeef,%edi
call   1030 <srandom@plt>
test   %r12d,%r12d
jle    1256 <test_read_random+0xf6>
mov    %ebx,0xc(%rsp)
xor    %r13d,%r13d
cmp    $0x10000,%r12d
mov    $0x10000,%r15d
cmovb  %r12d,%r15d
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15d,%edx
call   1060 <read_buff@plt>
cmp    %r15d,%eax
jl     1268 <test_read_random+0x108>
mov    %r15d,%ebp
xor    %ebx,%ebx
xchg   %ax,%ax
xor    %eax,%eax
call   1070 <random@plt>
movzbl (%r14,%rbx,1),%r8d
cmp    %al,%r8b
jne    1234 <test_read_random+0xd4>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    11e0 <test_read_random+0x80>
sub    %r15d,%r12d
mov    0xc(%rsp),%ebx
jg     11b2 <test_read_random+0x52>
jmp    1256 <test_read_random+0xf6>
mov    0x2dd4(%rip),%rax
mov    (%rax),%ebx
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xddd(%rip),%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1250 <test_read_random+0xf0>
movzbl %al,%ecx
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
lea    0xde2(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%r13d
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d71(%rip),%rax
mov    (%rax),%ebx
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xd8f(%rip),%rsi
mov    %ebx,%edi
mov    0x10(%rsp),%rdx
jmp    1228 <test_read_random+0xc8>

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_random(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%14 = load i32, i32* %7
%15 = load i32, i32* @SEEK_SET
%16 = call i64 @lseek(i32 %14, i32 0, i32 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %4
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %6
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %20, i8* %22)
store i32 -1, i32* %5
br label %89
24: ; preds = %4
%25 = call i32 @srandom(i32 -559038737)
%26 = load i32, i32* %9
store i32 %26, i32* %10
br label %27
27: ; preds = %84, %24
%28 = load i32, i32* %10
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %88
30: ; preds = %27
%31 = load i32, i32* %10
%32 = icmp sgt i32 %31, 65536
br i1 %32, label %33, label %34
33: ; preds = %30
br label %36
34: ; preds = %30
%35 = load i32, i32* %10
br label %36
36: ; preds = %34, %33
%37 = phi i32 [ 65536, %33 ], [ %35, %34 ]
store i32 %37, i32* %12
%38 = load i32, i32* %7
%39 = load i8*, i8** %8
%40 = load i32, i32* %12
%41 = call i32 @read_buff(i32 %38, i8* %39, i32 %40)
%42 = load i32, i32* %12
%43 = icmp slt i32 %41, %42
br i1 %43, label %44, label %50
44: ; preds = %36
%45 = load i32, i32* @stderr
%46 = load i8*, i8** %6
%47 = load i32, i32* @errno
%48 = call i8* @strerror(i32 %47)
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %46, i8* %48)
store i32 -1, i32* %5
br label %89
50: ; preds = %36
store i32 0, i32* %11
br label %51
51: ; preds = %81, %50
%52 = load i32, i32* %11
%53 = load i32, i32* %12
%54 = icmp slt i32 %52, %53
br i1 %54, label %55, label %84
55: ; preds = %51
%56 = call i32 (...) @random()
%57 = and i32 %56, 255
%58 = trunc i32 %57 to i8
store i8 %58, i8* %13
%59 = load i8*, i8** %8
%60 = load i32, i32* %11
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i8, i8* %59, i64 %61
%63 = load i8, i8* %62
%64 = zext i8 %63 to i32
%65 = load i8, i8* %13
%66 = zext i8 %65 to i32
%67 = icmp ne i32 %64, %66
br i1 %67, label %68, label %80
68: ; preds = %55
%69 = load i32, i32* @stderr
%70 = load i32, i32* %11
%71 = load i8, i8* %13
%72 = zext i8 %71 to i32
%73 = load i8*, i8** %8
%74 = load i32, i32* %11
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i8, i8* %73, i64 %75
%77 = load i8, i8* %76
%78 = zext i8 %77 to i32
%79 = 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 %72, i32 %78)
store i32 -1, i32* %5
br label %89
80: ; preds = %55
br label %81
81: ; preds = %80
%82 = load i32, i32* %11
%83 = add nsw i32 %82, 1
store i32 %83, i32* %11
br label %51
84: ; preds = %51
%85 = load i32, i32* %12
%86 = load i32, i32* %10
%87 = sub nsw i32 %86, %85
store i32 %87, i32* %10
br label %27
88: ; preds = %27
store i32 0, i32* %5
br label %89
89: ; preds = %88, %68, %44, %18
%90 = load i32, i32* %5
ret i32 %90
}

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_random(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = load i32, i32* @SEEK_SET
%6 = tail call i64 @lseek(i32 %1, i32 0, i32 %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %4
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %11)
br label %48
13: ; preds = %4
%14 = tail call i32 @srandom(i32 -559038737)
%15 = icmp sgt i32 %3, 0
br i1 %15, label %16, label %48
16: ; preds = %13, %45
%17 = phi i32 [ %46, %45 ], [ %3, %13 ]
%18 = icmp ult i32 %17, 65536
%19 = select i1 %18, i32 %17, i32 65536
%20 = tail call i32 @read_buff(i32 %1, i8* %2, i32 %19)
%21 = icmp slt i32 %20, %19
br i1 %21, label %24, label %22
22: ; preds = %16
%23 = zext i32 %19 to i64
br label %29
24: ; preds = %16
%25 = load i32, i32* @stderr
%26 = load i32, i32* @errno
%27 = tail call i8* @strerror(i32 %26)
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %27)
br label %48
29: ; preds = %22, %42
%30 = phi i64 [ 0, %22 ], [ %43, %42 ]
%31 = tail call i32 (...) @random()
%32 = getelementptr inbounds i8, i8* %2, i64 %30
%33 = load i8, i8* %32
%34 = trunc i32 %31 to i8
%35 = icmp eq i8 %33, %34
br i1 %35, label %42, label %36
36: ; preds = %29
%37 = trunc i64 %30 to i32
%38 = and i32 %31, 255
%39 = zext i8 %33 to i32
%40 = load i32, i32* @stderr
%41 = tail call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 %37, i32 %38, i32 %39)
br label %48
42: ; preds = %29
%43 = add nuw nsw i64 %30, 1
%44 = icmp eq i64 %43, %23
br i1 %44, label %45, label %29
45: ; preds = %42
%46 = sub nsw i32 %17, %19
%47 = icmp sgt i32 %46, 0
br i1 %47, label %16, label %48
48: ; preds = %45, %13, %24, %36, %8
%49 = phi i32 [ -1, %8 ], [ -1, %36 ], [ -1, %24 ], [ 0, %13 ], [ 0, %45 ]
ret i32 %49
}