#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_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int O_TRUNC;
int S_IRUSR;
int S_IWUSR;
size_t atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  if (argc < 3) {
    fprintf(stderr, "usage: read_write <repeats>\n");
    exit(EXIT_FAILURE);
  }
  size_t repeats = atoi(argv[1]);
  size_t chunk_size = atoi(argv[2]);
  for (int i = 0; i < repeats; ++i) {
    int src = open("test.dat", O_RDONLY);
    if (src == -1)
      do {
        perror("fstat");
        exit(EXIT_FAILURE);
      } while (0);
    int dst = open("output.dat", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
    if (dst == -1)
      do {
        perror("open");
        exit(EXIT_FAILURE);
      } while (0);
    char *buffer = malloc(chunk_size);
    close(src);
    close(dst);
    free(buffer);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11c3 <main+0x43>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atoi@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    12d8 <main+0x158>
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    123d <main+0xbd>
jmp    1223 <main+0xa3>
lea    0xdfc(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
mov    0x2dab(%rip),%rax
or     (%rax),%esi
mov    0x2d9a(%rip),%rax
or     (%rax),%esi
mov    0x2d49(%rip),%rax
mov    (%rax),%edx
mov    0x2d60(%rip),%rax
or     (%rax),%edx
lea    0xdbb(%rip),%rdi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    12a4 <main+0x124>
jmp    128a <main+0x10a>
lea    0xda6(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d13(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11ec <main+0x6c>
mov    0x2cc1(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jg     11aa <main+0x2a>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
call   1050 <fprintf@plt>
jmp    128a <main+0x10a>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
call   1090 <atoi@plt>
mov    %rax,%r13
mov    0x10(%rbx),%rdi
call   1090 <atoi@plt>
test   %r13,%r13
je     1267 <main+0xe7>
mov    %rax,%r14
lea    0xe57(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
lea    0xe2d(%rip),%rdi
xor    %eax,%eax
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1275 <main+0xf5>
mov    %eax,%ebp
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
mov    0x2dca(%rip),%rax
or     (%rax),%esi
mov    0x2dd9(%rip),%rax
or     (%rax),%esi
mov    0x2da8(%rip),%rax
mov    (%rax),%edx
mov    0x2d7f(%rip),%rax
or     (%rax),%edx
mov    %r12,%rdi
xor    %eax,%eax
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     127e <main+0xfe>
mov    %eax,%ebx
mov    %r14,%rdi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %r15,%rdi
call   1030 <free@plt>
add    $0xffffffffffffffff,%r13
jne    11e0 <main+0x60>
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xdaa(%rip),%rdi
jmp    1285 <main+0x105>
lea    0xdb2(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d1f(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [29 x i8] c"usage: read_write <repeats>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [9 x i8] c"test.dat\00"
@O_RDONLY = global i32 0
@.str.2 = [6 x i8] c"fstat\00"
@.str.3 = [11 x i8] c"output.dat\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.4 = [5 x i8] c"open\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 3
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i64 @atoi(i8* %22)
store i64 %23, i64* %6
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
%27 = call i64 @atoi(i8* %26)
store i64 %27, i64* %7
store i32 0, i32* %8
br label %28
28: ; preds = %71, %19
%29 = load i32, i32* %8
%30 = sext i32 %29 to i64
%31 = load i64, i64* %6
%32 = icmp ult i64 %30, %31
br i1 %32, label %33, label %74
33: ; preds = %28
%34 = load i32, i32* @O_RDONLY
%35 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %34)
store i32 %35, i32* %9
%36 = load i32, i32* %9
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %44
38: ; preds = %33
br label %39
39: ; preds = %38
%40 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%41 = load i32, i32* @EXIT_FAILURE
%42 = call i32 @exit(i32 %41)
unreachable
43: ; No predecessors!
br label %44
44: ; preds = %43, %33
%45 = load i32, i32* @O_RDWR
%46 = load i32, i32* @O_CREAT
%47 = or i32 %45, %46
%48 = load i32, i32* @O_TRUNC
%49 = or i32 %47, %48
%50 = load i32, i32* @S_IRUSR
%51 = load i32, i32* @S_IWUSR
%52 = or i32 %50, %51
%53 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %49, i32 %52)
store i32 %53, i32* %10
%54 = load i32, i32* %10
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %62
56: ; preds = %44
br label %57
57: ; preds = %56
%58 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%59 = load i32, i32* @EXIT_FAILURE
%60 = call i32 @exit(i32 %59)
unreachable
61: ; No predecessors!
br label %62
62: ; preds = %61, %44
%63 = load i64, i64* %7
%64 = call i8* @malloc(i64 %63)
store i8* %64, i8** %11
%65 = load i32, i32* %9
%66 = call i32 @close(i32 %65)
%67 = load i32, i32* %10
%68 = call i32 @close(i32 %67)
%69 = load i8*, i8** %11
%70 = call i32 @free(i8* %69)
br label %71
71: ; preds = %62
%72 = load i32, i32* %8
%73 = add nsw i32 %72, 1
store i32 %73, i32* %8
br label %28
74: ; preds = %28
%75 = load i32, i32* @EXIT_SUCCESS
%76 = call i32 @exit(i32 %75)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [29 x i8] c"usage: read_write <repeats>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [9 x i8] c"test.dat\00"
@O_RDONLY = global i32 0
@.str.2 = [6 x i8] c"fstat\00"
@.str.3 = [11 x i8] c"output.dat\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.4 = [5 x i8] c"open\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %9
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i64 @atoi(i8* %11)
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = tail call i64 @atoi(i8* %14)
%16 = icmp eq i64 %12, 0
br i1 %16, label %17, label %20
17: ; preds = %44, %9
%18 = load i32, i32* @EXIT_SUCCESS
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %9, %44
%21 = phi i64 [ %49, %44 ], [ 0, %9 ]
%22 = load i32, i32* @O_RDONLY
%23 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %20
%26 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = tail call i32 @exit(i32 %27)
unreachable
29: ; preds = %20
%30 = load i32, i32* @O_RDWR
%31 = load i32, i32* @O_CREAT
%32 = or i32 %31, %30
%33 = load i32, i32* @O_TRUNC
%34 = or i32 %32, %33
%35 = load i32, i32* @S_IRUSR
%36 = load i32, i32* @S_IWUSR
%37 = or i32 %36, %35
%38 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %34, i32 %37)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %44
40: ; preds = %29
%41 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%42 = load i32, i32* @EXIT_FAILURE
%43 = tail call i32 @exit(i32 %42)
unreachable
44: ; preds = %29
%45 = tail call i8* @malloc(i64 %15)
%46 = tail call i32 @close(i32 %23)
%47 = tail call i32 @close(i32 %38)
%48 = tail call i32 @free(i8* %45)
%49 = add nuw i64 %21, 1
%50 = icmp eq i64 %49, %12
br i1 %50, label %17, label %20
}