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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void checkread(const char *d, const char *f, int start, int n, char c) {
  int fd;
  char name[512];
  sleep(1);
  sprintf(name, "%s/%s", d, f);
  fd = open(name, 0);
  if (fd < 0) {
    fprintf(stderr, "test-lab-3-b: open(%s): %s\n", name, strerror(errno));
    exit(1);
  }
  if (lseek(fd, start, 0) != (off_t)start) {
    fprintf(stderr, "test-lab-3-b: lseek(%s, %d): %s\n", name, start,
            strerror(errno));
    exit(1);
  }
  for (int i = 0; i < n; i++) {
    char xc;
    if (read(fd, &xc, 1) != 1) {
      fprintf(stderr, "test-lab-3-b: read(%s): %s\n", name, strerror(errno));
      exit(1);
    }
    if (xc != c) {
      fprintf(stderr, "test-lab-3-b: checkread off %d %02x != %02x\n",
              start + i, xc, c);
      exit(1);
    }
  }
  close(fd);
}


x86 assembly (clang, -O0):
<checkread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x260,%rsp
mov    %r8b,%al
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %al,-0x19(%rbp)
mov    $0x1,%edi
call   10b0 <sleep@plt>
lea    -0x220(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe31(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1242 <checkread+0xb2>
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x234(%rbp)
lea    -0x220(%rbp),%rax
mov    %rax,-0x230(%rbp)
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x234(%rbp),%edi
mov    -0x230(%rbp),%rdx
mov    %rax,%rcx
lea    0xdd5(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%edi
mov    -0x14(%rbp),%esi
xor    %edx,%edx
call   1030 <lseek@plt>
movslq -0x14(%rbp),%rcx
cmp    %rcx,%rax
je     12be <checkread+0x12e>
mov    0x2d7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x244(%rbp)
lea    -0x220(%rbp),%rax
mov    %rax,-0x240(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x238(%rbp)
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x244(%rbp),%edi
mov    -0x240(%rbp),%rdx
mov    -0x238(%rbp),%ecx
mov    %rax,%r8
lea    0xd75(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movl   $0x0,-0x224(%rbp)
mov    -0x224(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    13a9 <checkread+0x219>
mov    -0x20(%rbp),%edi
lea    -0x225(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
je     1347 <checkread+0x1b7>
mov    0x2ce5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x254(%rbp)
lea    -0x220(%rbp),%rax
mov    %rax,-0x250(%rbp)
mov    0x2cc0(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x254(%rbp),%edi
mov    -0x250(%rbp),%rdx
mov    %rax,%rcx
lea    0xd0d(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movsbl -0x225(%rbp),%eax
movsbl -0x19(%rbp),%ecx
cmp    %ecx,%eax
je     1390 <checkread+0x200>
mov    0x2c7f(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
add    -0x224(%rbp),%edx
movsbl -0x225(%rbp),%ecx
movsbl -0x19(%rbp),%r8d
lea    0xce0(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1395 <checkread+0x205>
mov    -0x224(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x224(%rbp)
jmp    12c8 <checkread+0x138>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
add    $0x260,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checkread>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %r8d,%r15d
mov    %ecx,%r14d
mov    %edx,%ebx
mov    %rsi,%r12
mov    %rdi,%rbp
mov    $0x1,%edi
call   10b0 <sleep@plt>
lea    0xe40(%rip),%rsi
lea    0x10(%rsp),%r13
mov    %r13,%rdi
mov    %rbp,%rdx
mov    %r12,%rcx
call   1080 <sprintf@plt>
mov    %r13,%rdi
xor    %esi,%esi
call   1070 <open@plt>
test   %eax,%eax
js     12b2 <checkread+0x122>
mov    %eax,%ebp
mov    %eax,%edi
mov    %ebx,%esi
xor    %edx,%edx
call   1030 <lseek@plt>
mov    %ebx,%r13d
movslq %ebx,%rcx
cmp    %rcx,%rax
jne    12d2 <checkread+0x142>
test   %r14d,%r14d
jle    1236 <checkread+0xa6>
neg    %r14d
xor    %ebx,%ebx
lea    0xf(%rsp),%r12
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
jne    124e <checkread+0xbe>
movsbl 0xf(%rsp),%ecx
cmp    %r15b,%cl
jne    1287 <checkread+0xf7>
add    $0xffffffff,%ebx
cmp    %ebx,%r14d
jne    1210 <checkread+0x80>
mov    %ebp,%edi
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <close@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%ebx
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdd7(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movsbl %r15b,%r8d
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
sub    %ebx,%r13d
lea    0xdc1(%rip),%rsi
mov    %r13d,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d27(%rip),%rax
mov    (%rax),%ebx
mov    0x2d16(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd36(%rip),%rsi
jmp    126c <checkread+0xdc>
mov    0x2d07(%rip),%rax
mov    (%rax),%ebx
mov    0x2cf6(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd32(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
mov    %r13d,%ecx
mov    %rax,%r8
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"%s/%s\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"test-lab-3-b: open(%s): %s\0A\00"
@errno = global i32 0
@.str.2 = [33 x i8] c"test-lab-3-b: lseek(%s, %d): %s\0A\00"
@.str.3 = [28 x i8] c"test-lab-3-b: read(%s): %s\0A\00"
@.str.4 = [45 x i8] c"test-lab-3-b: checkread off %d %02x != %02x\0A\00"
define void @checkread(i8* %0, i8* %1, i32 %2, i32 %3, i8 signext %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8
%11 = alloca i32
%12 = alloca [512 x i8]
%13 = alloca i32
%14 = alloca i8
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i8 %4, i8* %10
%15 = call i32 @sleep(i32 1)
%16 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%17 = load i8*, i8** %6
%18 = load i8*, i8** %7
%19 = call i32 @sprintf(i8* %16, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %17, i8* %18)
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%21 = call i32 @open(i8* %20, i32 0)
store i32 %21, i32* %11
%22 = load i32, i32* %11
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %5
%25 = load i32, i32* @stderr
%26 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %26, i8* %28)
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %5
%32 = load i32, i32* %11
%33 = load i32, i32* %8
%34 = call i64 @lseek(i32 %32, i32 %33, i32 0)
%35 = load i32, i32* %8
%36 = sext i32 %35 to i64
%37 = icmp ne i64 %34, %36
br i1 %37, label %38, label %46
38: ; preds = %31
%39 = load i32, i32* @stderr
%40 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%41 = load i32, i32* %8
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %40, i32 %41, i8* %43)
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %31
store i32 0, i32* %13
br label %47
47: ; preds = %80, %46
%48 = load i32, i32* %13
%49 = load i32, i32* %9
%50 = icmp slt i32 %48, %49
br i1 %50, label %51, label %83
51: ; preds = %47
%52 = load i32, i32* %11
%53 = call i32 @read(i32 %52, i8* %14, i32 1)
%54 = icmp ne i32 %53, 1
br i1 %54, label %55, label %62
55: ; preds = %51
%56 = load i32, i32* @stderr
%57 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%58 = load i32, i32* @errno
%59 = call i8* @strerror(i32 %58)
%60 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %57, i8* %59)
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %51
%63 = load i8, i8* %14
%64 = sext i8 %63 to i32
%65 = load i8, i8* %10
%66 = sext i8 %65 to i32
%67 = icmp ne i32 %64, %66
br i1 %67, label %68, label %79
68: ; preds = %62
%69 = load i32, i32* @stderr
%70 = load i32, i32* %8
%71 = load i32, i32* %13
%72 = add nsw i32 %70, %71
%73 = load i8, i8* %14
%74 = sext i8 %73 to i32
%75 = load i8, i8* %10
%76 = sext i8 %75 to i32
%77 = call i32 (i32, i8*, ...) @fprintf(i32 %69, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0), i32 %72, i32 %74, i32 %76)
%78 = call i32 @exit(i32 1)
unreachable
79: ; preds = %62
br label %80
80: ; preds = %79
%81 = load i32, i32* %13
%82 = add nsw i32 %81, 1
store i32 %82, i32* %13
br label %47
83: ; preds = %47
%84 = load i32, i32* %11
%85 = call i32 @close(i32 %84)
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s/%s\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"test-lab-3-b: open(%s): %s\0A\00"
@errno = global i32 0
@.str.2 = [33 x i8] c"test-lab-3-b: lseek(%s, %d): %s\0A\00"
@.str.3 = [28 x i8] c"test-lab-3-b: read(%s): %s\0A\00"
@.str.4 = [45 x i8] c"test-lab-3-b: checkread off %d %02x != %02x\0A\00"
define void @checkread(i8* %0, i8* %1, i32 %2, i32 %3, i8 signext %4) {
%6 = alloca [512 x i8]
%7 = alloca i8
%8 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%9 = tail call i32 @sleep(i32 1)
%10 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%11 = call i32 @open(i8* %8, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %5
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %8, i8* %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %5
%20 = tail call i64 @lseek(i32 %11, i32 %2, i32 0)
%21 = sext i32 %2 to i64
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = icmp sgt i32 %3, 0
br i1 %24, label %33, label %31
25: ; preds = %19
%26 = load i32, i32* @stderr
%27 = load i32, i32* @errno
%28 = tail call i8* @strerror(i32 %27)
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %8, i32 %2, i8* %28)
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %53, %23
%32 = tail call i32 @close(i32 %11)
ret void
33: ; preds = %23, %53
%34 = phi i32 [ %54, %53 ], [ 0, %23 ]
%35 = call i32 @read(i32 %11, i8* %7, i32 1)
%36 = icmp eq i32 %35, 1
br i1 %36, label %43, label %37
37: ; preds = %33
%38 = load i32, i32* @stderr
%39 = load i32, i32* @errno
%40 = tail call i8* @strerror(i32 %39)
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %8, i8* %40)
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %33
%44 = load i8, i8* %7
%45 = icmp eq i8 %44, %4
br i1 %45, label %53, label %46
46: ; preds = %43
%47 = sext i8 %4 to i32
%48 = sext i8 %44 to i32
%49 = load i32, i32* @stderr
%50 = add nsw i32 %34, %2
%51 = tail call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0), i32 %50, i32 %48, i32 %47)
%52 = tail call i32 @exit(i32 1)
unreachable
53: ; preds = %43
%54 = add nuw nsw i32 %34, 1
%55 = icmp eq i32 %54, %3
br i1 %55, label %31, label %33
}