#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 O_CREAT;
int O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int main(int argc, char *argv[]) {
  if (argc != 4) {
    fprintf(stderr, "Usage: %s <filename> <blocks> <cycles>\n", argv[0]);
    return 1;
  }
  int fd = open(argv[1], O_RDWR | O_CREAT, 0600);
  if (fd == -1) {
    perror("Unable to open file");
    return 1;
  }
  int blocks = atoi(argv[2]);
  int cycles = atoi(argv[3]);
  char buf[4096] = {0};
  int cycle;
  for (cycle = 0; cycle < cycles; ++cycle) {
    lseek(fd, 0, SEEK_SET);
    int block;
    for (block = 0; block <= blocks; ++block) {
      if (write(fd, buf, 4096) != 4096) {
        fprintf(stderr, "Short write\n");
        return 1;
      }
    }
    lseek(fd, 0, SEEK_SET);
    for (block = 0; block <= blocks; ++block) {
      if (read(fd, buf, 4096) != 4096) {
        fprintf(stderr, "Short read\n");
        return 1;
      }
    }
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     11dd <main+0x4d>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe36(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13a8 <main+0x218>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
mov    0x2e03(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1226 <main+0x96>
lea    0xe13(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13a8 <main+0x218>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
movl   $0x0,-0x1024(%rbp)
mov    -0x1024(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1399 <main+0x209>
mov    -0x14(%rbp),%edi
mov    0x2d4c(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
movl   $0x0,-0x1028(%rbp)
mov    -0x1028(%rbp),%eax
cmp    -0x18(%rbp),%eax
jg     12f9 <main+0x169>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
cmp    $0x1000,%eax
je     12e0 <main+0x150>
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
lea    0xd6f(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13a8 <main+0x218>
jmp    12e5 <main+0x155>
mov    -0x1028(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1028(%rbp)
jmp    128f <main+0xff>
mov    -0x14(%rbp),%edi
mov    0x2cc5(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
movl   $0x0,-0x1028(%rbp)
mov    -0x1028(%rbp),%eax
cmp    -0x18(%rbp),%eax
jg     1380 <main+0x1f0>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
cmp    $0x1000,%eax
je     1367 <main+0x1d7>
mov    0x2c85(%rip),%rax
mov    (%rax),%edi
lea    0xcf5(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13a8 <main+0x218>
jmp    136c <main+0x1dc>
mov    -0x1028(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1028(%rbp)
jmp    1316 <main+0x186>
jmp    1385 <main+0x1f5>
mov    -0x1024(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1024(%rbp)
jmp    1263 <main+0xd3>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rsi,%r14
cmp    $0x4,%edi
jne    12cf <main+0x13f>
mov    0x8(%r14),%rdi
mov    0x2e28(%rip),%rax
mov    0x2e39(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x180,%edx
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     132b <main+0x19b>
mov    %eax,%ebp
mov    0x10(%r14),%rdi
call   10b0 <atoi@plt>
mov    %eax,%r13d
mov    0x18(%r14),%rdi
call   10b0 <atoi@plt>
mov    %eax,%ebx
lea    0x10(%rsp),%rdi
mov    $0x1000,%edx
xor    %esi,%esi
call   1050 <memset@plt>
test   %ebx,%ebx
jle    12c4 <main+0x134>
lea    0x1(%r13),%eax
mov    %eax,0x8(%rsp)
xor    %r14d,%r14d
mov    0x2dae(%rip),%r15
lea    0x10(%rsp),%r12
mov    %ebx,0xc(%rsp)
mov    (%r15),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
test   %r13d,%r13d
js     12a0 <main+0x110>
mov    $0xffffffff,%r15d
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
cmp    $0x1000,%eax
jne    12eb <main+0x15b>
add    $0x1,%r15d
cmp    %r15d,%r13d
jne    1240 <main+0xb0>
mov    0x2d5e(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
mov    0x8(%rsp),%ebx
test   %r13d,%r13d
js     12ac <main+0x11c>
xchg   %ax,%ax
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
cmp    $0x1000,%eax
jne    12fd <main+0x16d>
add    $0xffffffff,%ebx
jne    1280 <main+0xf0>
jmp    12ac <main+0x11c>
nopl   (%rax)
mov    (%r15),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
add    $0x1,%r14d
mov    0xc(%rsp),%ebx
cmp    %ebx,%r14d
mov    0x2d0a(%rip),%r15
jne    1223 <main+0x93>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    1319 <main+0x189>
mov    0x2cfa(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xd1e(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    1314 <main+0x184>
mov    0x2cde(%rip),%rax
mov    (%rax),%edi
lea    0xd41(%rip),%rsi
jmp    130d <main+0x17d>
mov    0x2ccc(%rip),%rax
mov    (%rax),%edi
lea    0xd3c(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcf6(%rip),%rdi
call   10a0 <perror@plt>
jmp    1314 <main+0x184>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [40 x i8] c"Usage: %s <filename> <blocks> <cycles>\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [20 x i8] c"Unable to open file\00"
@SEEK_SET = global i32 0
@.str.2 = [13 x i8] c"Short write\0A\00"
@.str.3 = [12 x i8] c"Short read\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [4096 x i8]
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 4
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %18)
store i32 1, i32* %3
br label %93
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = load i32, i32* @O_RDWR
%25 = load i32, i32* @O_CREAT
%26 = or i32 %24, %25
%27 = call i32 @open(i8* %23, i32 %26, i32 384)
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %20
%31 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %93
32: ; preds = %20
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 2
%35 = load i8*, i8** %34
%36 = call i32 @atoi(i8* %35)
store i32 %36, i32* %7
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 3
%39 = load i8*, i8** %38
%40 = call i32 @atoi(i8* %39)
store i32 %40, i32* %8
%41 = bitcast [4096 x i8]* %9 to i8*
store i32 0, i32* %10
br label %42
42: ; preds = %87, %32
%43 = load i32, i32* %10
%44 = load i32, i32* %8
%45 = icmp slt i32 %43, %44
br i1 %45, label %46, label %90
46: ; preds = %42
%47 = load i32, i32* %6
%48 = load i32, i32* @SEEK_SET
%49 = call i32 @lseek(i32 %47, i32 0, i32 %48)
store i32 0, i32* %11
br label %50
50: ; preds = %63, %46
%51 = load i32, i32* %11
%52 = load i32, i32* %7
%53 = icmp sle i32 %51, %52
br i1 %53, label %54, label %66
54: ; preds = %50
%55 = load i32, i32* %6
%56 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%57 = call i32 @write(i32 %55, i8* %56, i32 4096)
%58 = icmp ne i32 %57, 4096
br i1 %58, label %59, label %62
59: ; preds = %54
%60 = load i32, i32* @stderr
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %93
62: ; preds = %54
br label %63
63: ; preds = %62
%64 = load i32, i32* %11
%65 = add nsw i32 %64, 1
store i32 %65, i32* %11
br label %50
66: ; preds = %50
%67 = load i32, i32* %6
%68 = load i32, i32* @SEEK_SET
%69 = call i32 @lseek(i32 %67, i32 0, i32 %68)
store i32 0, i32* %11
br label %70
70: ; preds = %83, %66
%71 = load i32, i32* %11
%72 = load i32, i32* %7
%73 = icmp sle i32 %71, %72
br i1 %73, label %74, label %86
74: ; preds = %70
%75 = load i32, i32* %6
%76 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%77 = call i32 @read(i32 %75, i8* %76, i32 4096)
%78 = icmp ne i32 %77, 4096
br i1 %78, label %79, label %82
79: ; preds = %74
%80 = load i32, i32* @stderr
%81 = call i32 (i32, i8*, ...) @fprintf(i32 %80, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %93
82: ; preds = %74
br label %83
83: ; preds = %82
%84 = load i32, i32* %11
%85 = add nsw i32 %84, 1
store i32 %85, i32* %11
br label %70
86: ; preds = %70
br label %87
87: ; preds = %86
%88 = load i32, i32* %10
%89 = add nsw i32 %88, 1
store i32 %89, i32* %10
br label %42
90: ; preds = %42
%91 = load i32, i32* %6
%92 = call i32 @close(i32 %91)
store i32 0, i32* %3
br label %93
93: ; preds = %90, %79, %59, %30, %14
%94 = load i32, i32* %3
ret i32 %94
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [40 x i8] c"Usage: %s <filename> <blocks> <cycles>\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [20 x i8] c"Unable to open file\00"
@SEEK_SET = global i32 0
@.str.2 = [13 x i8] c"Short write\0A\00"
@.str.3 = [12 x i8] c"Short read\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i8]
%4 = icmp eq i32 %0, 4
br i1 %4, label %9, label %5
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i8*, i8** %1
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %67
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_CREAT
%14 = or i32 %13, %12
%15 = tail call i32 @open(i8* %11, i32 %14, i32 384)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %9
%18 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %67
19: ; preds = %9
%20 = getelementptr inbounds i8*, i8** %1, i64 2
%21 = load i8*, i8** %20
%22 = tail call i32 @atoi(i8* %21)
%23 = getelementptr inbounds i8*, i8** %1, i64 3
%24 = load i8*, i8** %23
%25 = tail call i32 @atoi(i8* %24)
%26 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%27 = icmp sgt i32 %25, 0
br i1 %27, label %28, label %63
28: ; preds = %19
%29 = icmp slt i32 %22, 0
br label %30
30: ; preds = %28, %60
%31 = phi i32 [ 0, %28 ], [ %61, %60 ]
%32 = load i32, i32* @SEEK_SET
%33 = tail call i32 @lseek(i32 %15, i32 0, i32 %32)
br i1 %29, label %34, label %40
34: ; preds = %30
%35 = load i32, i32* @SEEK_SET
%36 = tail call i32 @lseek(i32 %15, i32 0, i32 %35)
br label %60
37: ; preds = %40
%38 = add nuw i32 %41, 1
%39 = icmp eq i32 %41, %22
br i1 %39, label %47, label %40
40: ; preds = %30, %37
%41 = phi i32 [ %38, %37 ], [ 0, %30 ]
%42 = call i32 @write(i32 %15, i8* %26, i32 4096)
%43 = icmp eq i32 %42, 4096
br i1 %43, label %37, label %44
44: ; preds = %40
%45 = load i32, i32* @stderr
%46 = tail call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %65
47: ; preds = %37
%48 = load i32, i32* @SEEK_SET
%49 = tail call i32 @lseek(i32 %15, i32 0, i32 %48)
br i1 %29, label %60, label %53
50: ; preds = %53
%51 = add nuw i32 %54, 1
%52 = icmp eq i32 %54, %22
br i1 %52, label %60, label %53
53: ; preds = %47, %50
%54 = phi i32 [ %51, %50 ], [ 0, %47 ]
%55 = call i32 @read(i32 %15, i8* %26, i32 4096)
%56 = icmp eq i32 %55, 4096
br i1 %56, label %50, label %57
57: ; preds = %53
%58 = load i32, i32* @stderr
%59 = tail call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %65
60: ; preds = %50, %34, %47
%61 = add nuw nsw i32 %31, 1
%62 = icmp eq i32 %61, %25
br i1 %62, label %63, label %30
63: ; preds = %60, %19
%64 = tail call i32 @close(i32 %15)
br label %65
65: ; preds = %44, %57, %63
%66 = phi i32 [ 0, %63 ], [ 1, %57 ], [ 1, %44 ]
br label %67
67: ; preds = %17, %65, %5
%68 = phi i32 [ 1, %5 ], [ 1, %17 ], [ %66, %65 ]
ret i32 %68
}