#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*/);
char *malloc(unsigned int);
scalar_t__ memcmp(char *, int *, unsigned int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
unsigned int read(int, char *, unsigned int);
unsigned int write(int, int *, unsigned int);
void do_it(int data[], int fd, unsigned len) {
  char *buf = malloc(len);
  int i;
  for (i = 0; i < 100; 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);
    }
    if (read(fd, buf, len) != 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    $0x20,%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,-0x1c(%rbp)
cmpl   $0x64,-0x1c(%rbp)
jge    12b6 <do_it+0x126>
mov    -0xc(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1060 <lseek@plt>
cmp    $0x0,%rax
jge    11eb <do_it+0x5b>
lea    0xe24(%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    1219 <do_it+0x89>
lea    0xdfc(%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    1245 <do_it+0xb5>
lea    0xdca(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1070 <read@plt>
cmp    -0x10(%rbp),%eax
je     1273 <do_it+0xe3>
lea    0xda8(%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     12a3 <do_it+0x113>
lea    0xd7d(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    12a8 <do_it+0x118>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11b5 <do_it+0x25>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_it>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%ebx
mov    %esi,%r12d
mov    %rdi,%r14
mov    %edx,%edi
call   1090 <malloc@plt>
mov    %rax,%r15
mov    $0x64,%ebp
nop
mov    %r12d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <lseek@plt>
test   %rax,%rax
js     121b <do_it+0x8b>
mov    %r12d,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1050 <write@plt>
cmp    %ebx,%eax
jb     1224 <do_it+0x94>
mov    %r12d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <lseek@plt>
test   %rax,%rax
js     121b <do_it+0x8b>
mov    %r12d,%edi
mov    %r15,%rsi
mov    %ebx,%edx
call   1070 <read@plt>
cmp    %ebx,%eax
jne    122d <do_it+0x9d>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1080 <memcmp@plt>
test   %rax,%rax
jne    1243 <do_it+0xb3>
add    $0xffffffff,%ebp
jne    11b0 <do_it+0x20>
mov    %r15,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
lea    0xdde(%rip),%rdi
jmp    1234 <do_it+0xa4>
lea    0xddb(%rip),%rdi
jmp    1234 <do_it+0xa4>
lea    0xdd8(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xdc7(%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 i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%9 = load i32, i32* %6
%10 = call i8* @malloc(i32 %9)
store i8* %10, i8** %7
store i32 0, i32* %8
br label %11
11: ; preds = %58, %3
%12 = load i32, i32* %8
%13 = icmp slt i32 %12, 100
br i1 %13, label %14, label %61
14: ; preds = %11
%15 = load i32, i32* %5
%16 = call i64 @lseek(i32 %15, i32 0, i32 0)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %14
%22 = load i32, i32* %5
%23 = load i32*, i32** %4
%24 = load i32, i32* %6
%25 = call i32 @write(i32 %22, i32* %23, i32 %24)
%26 = load i32, i32* %6
%27 = icmp ult i32 %25, %26
br i1 %27, label %28, label %31
28: ; preds = %21
%29 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %21
%32 = load i32, i32* %5
%33 = call i64 @lseek(i32 %32, i32 0, i32 0)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %31
%39 = load i32, i32* %5
%40 = load i8*, i8** %7
%41 = load i32, i32* %6
%42 = call i32 @read(i32 %39, i8* %40, i32 %41)
%43 = load i32, i32* %6
%44 = icmp ne i32 %42, %43
br i1 %44, label %45, label %48
45: ; preds = %38
%46 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %38
%49 = load i8*, i8** %7
%50 = load i32*, i32** %4
%51 = load i32, i32* %6
%52 = call i64 @memcmp(i8* %49, i32* %50, i32 %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %48
%55 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %48
br label %58
58: ; preds = %57
%59 = load i32, i32* %8
%60 = add nsw i32 %59, 1
store i32 %60, i32* %8
br label %11
61: ; preds = %11
%62 = load i8*, i8** %7
%63 = call i32 @free(i8* %62)
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 i8* @malloc(i32 %2)
br label %8
5: ; preds = %33
%6 = add nuw nsw i32 %9, 1
%7 = icmp eq i32 %6, 100
br i1 %7, label %39, label %8
8: ; preds = %3, %5
%9 = phi i32 [ 0, %3 ], [ %6, %5 ]
%10 = tail call i64 @lseek(i32 %1, i32 0, i32 0)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %8
%16 = tail call i32 @write(i32 %1, i32* %0, i32 %2)
%17 = icmp ult i32 %16, %2
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %15
%22 = tail call i64 @lseek(i32 %1, i32 0, i32 0)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %21
%28 = tail call i32 @read(i32 %1, i8* %4, i32 %2)
%29 = icmp eq i32 %28, %2
br i1 %29, label %33, label %30
30: ; preds = %27
%31 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%32 = tail call i32 @exit(i32 1)
unreachable
33: ; preds = %27
%34 = tail call i64 @memcmp(i8* %4, i32* %0, i32 %2)
%35 = icmp eq i64 %34, 0
br i1 %35, label %5, label %36
36: ; preds = %33
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%38 = tail call i32 @exit(i32 1)
unreachable
39: ; preds = %5
%40 = tail call i32 @free(i8* %4)
ret void
}