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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(char *);
scalar_t__ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ malloc(unsigned int);
scalar_t__ memcmp(char *, int *, unsigned int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, unsigned int);
unsigned int write(int, int *, unsigned int);
void do_it(int data[], int fd, unsigned len) {
  char *buf = (char *)malloc(len), *ptr;
  int i, bytes_read;
  unsigned count;
  for (i = 0; i < 1000; i++) {
    if (lseek(fd, 0, 0) < 0) {
      perror("lseek");
      exit(1);
    }
    if (write(fd, data, len) < len) {
      perror("write");
      exit(1);
    }
    if (lseek(fd, 0, 0) < 0) {
      perror("lseek");
      exit(1);
    }
    ptr = buf;
    count = 0;
    while ((bytes_read = read(fd, ptr, len - count)) > 0) {
      ptr += bytes_read;
      count += bytes_read;
    }
    if (count < len) {
      perror("read");
      exit(1);
    }
    if (memcmp(buf, data, len) != 0) {
      printf("Data Mismatch\n");
      exit(1);
    }
  }
  free(buf);
}


x86 assembly (clang, -O0):
<do_it>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x10(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x3e8,-0x24(%rbp)
jge    12f9 <do_it+0x169>
mov    -0xc(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1060 <lseek@plt>
cmp    $0x0,%rax
jge    11ee <do_it+0x5e>
lea    0xe21(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1040 <write@plt>
cmp    -0x10(%rbp),%eax
jae    121c <do_it+0x8c>
lea    0xdf9(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0xc(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1060 <lseek@plt>
cmp    $0x0,%rax
jge    1248 <do_it+0xb8>
lea    0xdc7(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%edx
sub    -0x2c(%rbp),%edx
call   1070 <read@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jle    1294 <do_it+0x104>
mov    -0x28(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    1257 <do_it+0xc7>
mov    -0x2c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jae    12b6 <do_it+0x126>
lea    0xd65(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1080 <memcmp@plt>
cmp    $0x0,%rax
je     12e6 <do_it+0x156>
lea    0xd3a(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    12eb <do_it+0x15b>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11b5 <do_it+0x25>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_it>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %esi,%r12d
mov    %rdi,%r15
mov    %edx,%edi
call   1090 <malloc@plt>
mov    %rax,%r14
xor    %r13d,%r13d
mov    %rax,(%rsp)
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r12d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <lseek@plt>
test   %rax,%rax
js     1295 <do_it+0x105>
mov    %r12d,%edi
mov    %r15,%rsi
mov    %ebx,%edx
call   1050 <write@plt>
cmp    %ebx,%eax
jb     129e <do_it+0x10e>
mov    %r12d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <lseek@plt>
test   %rax,%rax
js     1295 <do_it+0x105>
mov    %r12d,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1070 <read@plt>
test   %eax,%eax
jle    1250 <do_it+0xc0>
mov    %r14,%rbp
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
mov    %eax,%ecx
add    %rcx,%rbp
add    %eax,%r14d
mov    %ebx,%edx
sub    %r14d,%edx
mov    %r12d,%edi
mov    %rbp,%rsi
call   1070 <read@plt>
test   %eax,%eax
jg     1220 <do_it+0x90>
cmp    %ebx,%r14d
jae    1258 <do_it+0xc8>
jmp    12a7 <do_it+0x117>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %r14d,%r14d
cmp    %ebx,%r14d
jb     12a7 <do_it+0x117>
mov    (%rsp),%r14
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1080 <memcmp@plt>
test   %rax,%rax
jne    12bd <do_it+0x12d>
add    $0x1,%r13d
cmp    $0x3e8,%r13d
jne    11c0 <do_it+0x30>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
lea    0xd64(%rip),%rdi
jmp    12ae <do_it+0x11e>
lea    0xd61(%rip),%rdi
jmp    12ae <do_it+0x11e>
lea    0xd5e(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xd4d(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"lseek\00"
@.str.1 = [6 x i8] c"write\00"
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [15 x i8] c"Data Mismatch\0A\00"
define void @do_it(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%12 = load i32, i32* %6
%13 = call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %7
store i32 0, i32* %9
br label %15
15: ; preds = %77, %3
%16 = load i32, i32* %9
%17 = icmp slt i32 %16, 1000
br i1 %17, label %18, label %80
18: ; preds = %15
%19 = load i32, i32* %5
%20 = call i64 @lseek(i32 %19, i32 0, i32 0)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %18
%26 = load i32, i32* %5
%27 = load i32*, i32** %4
%28 = load i32, i32* %6
%29 = call i32 @write(i32 %26, i32* %27, i32 %28)
%30 = load i32, i32* %6
%31 = icmp ult i32 %29, %30
br i1 %31, label %32, label %35
32: ; preds = %25
%33 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %25
%36 = load i32, i32* %5
%37 = call i64 @lseek(i32 %36, i32 0, i32 0)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %35
%43 = load i8*, i8** %7
store i8* %43, i8** %8
store i32 0, i32* %11
br label %44
44: ; preds = %52, %42
%45 = load i32, i32* %5
%46 = load i8*, i8** %8
%47 = load i32, i32* %6
%48 = load i32, i32* %11
%49 = sub i32 %47, %48
%50 = call i32 @read(i32 %45, i8* %46, i32 %49)
store i32 %50, i32* %10
%51 = icmp sgt i32 %50, 0
br i1 %51, label %52, label %60
52: ; preds = %44
%53 = load i32, i32* %10
%54 = load i8*, i8** %8
%55 = sext i32 %53 to i64
%56 = getelementptr inbounds i8, i8* %54, i64 %55
store i8* %56, i8** %8
%57 = load i32, i32* %10
%58 = load i32, i32* %11
%59 = add i32 %58, %57
store i32 %59, i32* %11
br label %44
60: ; preds = %44
%61 = load i32, i32* %11
%62 = load i32, i32* %6
%63 = icmp ult i32 %61, %62
br i1 %63, label %64, label %67
64: ; preds = %60
%65 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%66 = call i32 @exit(i32 1)
unreachable
67: ; preds = %60
%68 = load i8*, i8** %7
%69 = load i32*, i32** %4
%70 = load i32, i32* %6
%71 = call i64 @memcmp(i8* %68, i32* %69, i32 %70)
%72 = icmp ne i64 %71, 0
br i1 %72, label %73, label %76
73: ; preds = %67
%74 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%75 = call i32 @exit(i32 1)
unreachable
76: ; preds = %67
br label %77
77: ; preds = %76
%78 = load i32, i32* %9
%79 = add nsw i32 %78, 1
store i32 %79, i32* %9
br label %15
80: ; preds = %15
%81 = load i8*, i8** %7
%82 = call i32 @free(i8* %81)
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"lseek\00"
@.str.1 = [6 x i8] c"write\00"
@.str.2 = [5 x i8] c"read\00"
@str = [14 x i8] c"Data Mismatch\00"
define void @do_it(i32* %0, i32 %1, i32 %2) {
%4 = tail call i64 @malloc(i32 %2)
%5 = inttoptr i64 %4 to i8*
br label %9
6: ; preds = %47
%7 = add nuw nsw i32 %10, 1
%8 = icmp eq i32 %7, 1000
br i1 %8, label %53, label %9
9: ; preds = %3, %6
%10 = phi i32 [ 0, %3 ], [ %7, %6 ]
%11 = tail call i64 @lseek(i32 %1, i32 0, i32 0)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %9
%17 = tail call i32 @write(i32 %1, i32* %0, i32 %2)
%18 = icmp ult i32 %17, %2
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %16
%23 = tail call i64 @lseek(i32 %1, i32 0, i32 0)
%24 = icmp slt i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = tail call i32 @read(i32 %1, i8* %5, i32 %2)
%27 = icmp sgt i32 %26, 0
br i1 %27, label %31, label %41
28: ; preds = %22
%29 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %25, %31
%32 = phi i32 [ %39, %31 ], [ %26, %25 ]
%33 = phi i32 [ %37, %31 ], [ 0, %25 ]
%34 = phi i8* [ %36, %31 ], [ %5, %25 ]
%35 = zext i32 %32 to i64
%36 = getelementptr inbounds i8, i8* %34, i64 %35
%37 = add i32 %32, %33
%38 = sub i32 %2, %37
%39 = tail call i32 @read(i32 %1, i8* %36, i32 %38)
%40 = icmp sgt i32 %39, 0
br i1 %40, label %31, label %41
41: ; preds = %31, %25
%42 = phi i32 [ 0, %25 ], [ %37, %31 ]
%43 = icmp ult i32 %42, %2
br i1 %43, label %44, label %47
44: ; preds = %41
%45 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%46 = tail call i32 @exit(i32 1)
unreachable
47: ; preds = %41
%48 = tail call i64 @memcmp(i8* %5, i32* %0, i32 %2)
%49 = icmp eq i64 %48, 0
br i1 %49, label %6, label %50
50: ; preds = %47
%51 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%52 = tail call i32 @exit(i32 1)
unreachable
53: ; preds = %6
%54 = tail call i32 @free(i8* %5)
ret void
}