#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 int ssize_t;
typedef int mode_t;

/* Variables and functions */
int O_APPEND;
int O_CREAT;
int O_SYNC;
int O_TRUNC;
int O_WRONLY;
int atoi(int /*<<< orphan*/);
scalar_t__ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int getopt(int, char **, char *);
scalar_t__ malloc(int);
int open(char *, int, int);
int /*<<< orphan*/ optarg;
int /*<<< orphan*/ perror(char *);
int rand();
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int main(int argc, char **argv) {
  int i, fd, arg, nchunks, chunksize, async;
  ssize_t n;
  char *chunk;
  mode_t fmode;
  chunksize = 1024;
  nchunks = 1024;
  async = 0;
  while ((arg = getopt(argc, argv, "n:c:a")) != -1) {
    switch (arg) {
    case 'n':
      nchunks = atoi(optarg);
      break;
    case 'c':
      chunksize = atoi(optarg);
      break;
    case 'a':
      async = 1;
      break;
    }
  }
  chunk = (char *)malloc(chunksize);
  if (chunk == NULL) {
    perror("malloc failed");
    exit(2);
  }
  for (i = 0; i < chunksize; i++) {
    chunk[i] = rand() % 256;
  }
  fmode = O_WRONLY | O_APPEND | O_CREAT | O_TRUNC | O_APPEND;
  if (!async) {
    fmode |= O_SYNC;
  }
  fd = open("./chunks.dat", fmode, 0644);
  if (fd < 0) {
    perror("open failed");
    exit(2);
  }
  for (i = 0; i < nchunks; i++) {
    n = write(fd, chunk, chunksize);
    if (n < 0) {
      perror("write failed");
      exit(2);
    }
    if (n != chunksize) {
      fprintf(stderr, "warning: written less than a chunk: %d\n", n);
    }
  }
  if (close(fd) < 0) {
    perror("close failed");
    exit(2);
  }
  return 0;
}


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)
movl   $0x400,-0x24(%rbp)
movl   $0x400,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe27(%rip),%rdx
call   1090 <getopt@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
je     1258 <main+0xb8>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x40(%rbp)
sub    $0x61,%eax
je     124c <main+0xac>
jmp    11fe <main+0x5e>
mov    -0x40(%rbp),%eax
sub    $0x63,%eax
je     1236 <main+0x96>
jmp    120f <main+0x6f>
mov    -0x40(%rbp),%eax
sub    $0x6e,%eax
jne    1253 <main+0xb3>
jmp    1220 <main+0x80>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
call   10a0 <atoi@plt>
mov    %eax,-0x20(%rbp)
jmp    1253 <main+0xb3>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
call   10a0 <atoi@plt>
mov    %eax,-0x24(%rbp)
jmp    1253 <main+0xb3>
movl   $0x1,-0x28(%rbp)
jmp    11cb <main+0x2b>
mov    -0x24(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1285 <main+0xe5>
lea    0xd90(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    12c0 <main+0x120>
mov    $0x0,%al
call   10c0 <rand@plt>
mov    $0x100,%ecx
cltd
idiv   %ecx
mov    -0x38(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    128c <main+0xec>
mov    0x2ce9(%rip),%rax
mov    (%rax),%eax
mov    0x2cd8(%rip),%rcx
or     (%rcx),%eax
mov    0x2d1f(%rip),%rcx
or     (%rcx),%eax
mov    0x2d06(%rip),%rcx
or     (%rcx),%eax
mov    0x2cbd(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    1309 <main+0x169>
mov    0x2ccf(%rip),%rax
mov    (%rax),%eax
or     -0x3c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%esi
lea    0xd01(%rip),%rdi
mov    $0x1a4,%edx
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1340 <main+0x1a0>
lea    0xcf0(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    13bc <main+0x21c>
mov    -0x18(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1385 <main+0x1e5>
lea    0xcb7(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
mov    -0x2c(%rbp),%eax
cmp    -0x24(%rbp),%eax
je     13a9 <main+0x209>
mov    0x2c28(%rip),%rax
mov    (%rax),%edi
mov    -0x2c(%rbp),%edx
lea    0xc96(%rip),%rsi
call   1050 <fprintf@plt>
jmp    13ae <main+0x20e>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1347 <main+0x1a7>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jge    13e4 <main+0x244>
lea    0xc8d(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%ebx
xor    %r13d,%r13d
mov    $0x400,%r14d
lea    0xe40(%rip),%r12
mov    $0x400,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   1090 <getopt@plt>
cmp    $0x62,%eax
jg     1200 <main+0x60>
cmp    $0xffffffff,%eax
je     122f <main+0x8f>
cmp    $0x61,%eax
jne    11d0 <main+0x30>
mov    $0x1,%r13d
jmp    11d0 <main+0x30>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmp    $0x63,%eax
je     121c <main+0x7c>
cmp    $0x6e,%eax
jne    11d0 <main+0x30>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
call   10a0 <atoi@plt>
mov    %eax,%ebp
jmp    11d0 <main+0x30>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   10a0 <atoi@plt>
mov    %eax,%r14d
jmp    11d0 <main+0x30>
mov    %r14d,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     132e <main+0x18e>
mov    %rax,%r15
test   %r14d,%r14d
jle    1264 <main+0xc4>
mov    %r14d,%r12d
xor    %ebx,%ebx
nopl   (%rax)
xor    %eax,%eax
call   10c0 <rand@plt>
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1250 <main+0xb0>
mov    0x2d45(%rip),%rax
mov    0x2d36(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d7b(%rip),%rax
or     (%rax),%esi
mov    0x2d62(%rip),%rax
or     (%rax),%esi
xor    %eax,%eax
test   %r13d,%r13d
jne    1298 <main+0xf8>
mov    0x2d3a(%rip),%rax
mov    (%rax),%eax
or     %eax,%esi
lea    0xd73(%rip),%rdi
mov    $0x1a4,%edx
call   1070 <open@plt>
test   %eax,%eax
js     1337 <main+0x197>
mov    %eax,%ebx
test   %ebp,%ebp
jle    12fb <main+0x15b>
mov    0x2d00(%rip),%r13
lea    0xd73(%rip),%r12
jmp    12d5 <main+0x135>
nopl   0x0(%rax)
add    $0xffffffff,%ebp
je     12fb <main+0x15b>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <write@plt>
test   %eax,%eax
js     1318 <main+0x178>
cmp    %r14d,%eax
je     12d0 <main+0x130>
mov    0x0(%r13),%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1050 <fprintf@plt>
jmp    12d0 <main+0x130>
mov    %ebx,%edi
call   1040 <close@plt>
test   %rax,%rax
js     1340 <main+0x1a0>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd0e(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
lea    0xcd1(%rip),%rdi
jmp    131f <main+0x17f>
lea    0xce3(%rip),%rdi
jmp    131f <main+0x17f>
lea    0xd1b(%rip),%rdi
jmp    131f <main+0x17f>

LLVM IR (-O0):
@.str = [6 x i8] c"n:c:a\00"
@optarg = global i32 0
@.str.1 = [14 x i8] c"malloc failed\00"
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_SYNC = global i32 0
@.str.2 = [13 x i8] c"./chunks.dat\00"
@.str.3 = [12 x i8] c"open failed\00"
@.str.4 = [13 x i8] c"write failed\00"
@stderr = global i32 0
@.str.5 = [40 x i8] c"warning: written less than a chunk: %d\0A\00"
@.str.6 = [13 x i8] c"close failed\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 i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1024, i32* %10
store i32 1024, i32* %9
store i32 0, i32* %11
br label %15
15: ; preds = %29, %2
%16 = load i32, i32* %4
%17 = load i8**, i8*** %5
%18 = call i32 @getopt(i32 %16, i8** %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 %18, i32* %8
%19 = icmp ne i32 %18, -1
br i1 %19, label %20, label %30
20: ; preds = %15
%21 = load i32, i32* %8
switch i32 %21, label %29 [
i32 110, label %22
i32 99, label %25
i32 97, label %28
]
22: ; preds = %20
%23 = load i32, i32* @optarg
%24 = call i32 @atoi(i32 %23)
store i32 %24, i32* %9
br label %29
25: ; preds = %20
%26 = load i32, i32* @optarg
%27 = call i32 @atoi(i32 %26)
store i32 %27, i32* %10
br label %29
28: ; preds = %20
store i32 1, i32* %11
br label %29
29: ; preds = %20, %28, %25, %22
br label %15
30: ; preds = %15
%31 = load i32, i32* %10
%32 = call i64 @malloc(i32 %31)
%33 = inttoptr i64 %32 to i8*
store i8* %33, i8** %13
%34 = load i8*, i8** %13
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %39
36: ; preds = %30
%37 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%38 = call i32 @exit(i32 2)
unreachable
39: ; preds = %30
store i32 0, i32* %6
br label %40
40: ; preds = %52, %39
%41 = load i32, i32* %6
%42 = load i32, i32* %10
%43 = icmp slt i32 %41, %42
br i1 %43, label %44, label %55
44: ; preds = %40
%45 = call i32 (...) @rand()
%46 = srem i32 %45, 256
%47 = trunc i32 %46 to i8
%48 = load i8*, i8** %13
%49 = load i32, i32* %6
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
store i8 %47, i8* %51
br label %52
52: ; preds = %44
%53 = load i32, i32* %6
%54 = add nsw i32 %53, 1
store i32 %54, i32* %6
br label %40
55: ; preds = %40
%56 = load i32, i32* @O_WRONLY
%57 = load i32, i32* @O_APPEND
%58 = or i32 %56, %57
%59 = load i32, i32* @O_CREAT
%60 = or i32 %58, %59
%61 = load i32, i32* @O_TRUNC
%62 = or i32 %60, %61
%63 = load i32, i32* @O_APPEND
%64 = or i32 %62, %63
store i32 %64, i32* %14
%65 = load i32, i32* %11
%66 = icmp ne i32 %65, 0
br i1 %66, label %71, label %67
67: ; preds = %55
%68 = load i32, i32* @O_SYNC
%69 = load i32, i32* %14
%70 = or i32 %69, %68
store i32 %70, i32* %14
br label %71
71: ; preds = %67, %55
%72 = load i32, i32* %14
%73 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %72, i32 420)
store i32 %73, i32* %7
%74 = load i32, i32* %7
%75 = icmp slt i32 %74, 0
br i1 %75, label %76, label %79
76: ; preds = %71
%77 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%78 = call i32 @exit(i32 2)
unreachable
79: ; preds = %71
store i32 0, i32* %6
br label %80
80: ; preds = %103, %79
%81 = load i32, i32* %6
%82 = load i32, i32* %9
%83 = icmp slt i32 %81, %82
br i1 %83, label %84, label %106
84: ; preds = %80
%85 = load i32, i32* %7
%86 = load i8*, i8** %13
%87 = load i32, i32* %10
%88 = call i32 @write(i32 %85, i8* %86, i32 %87)
store i32 %88, i32* %12
%89 = load i32, i32* %12
%90 = icmp slt i32 %89, 0
br i1 %90, label %91, label %94
91: ; preds = %84
%92 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%93 = call i32 @exit(i32 2)
unreachable
94: ; preds = %84
%95 = load i32, i32* %12
%96 = load i32, i32* %10
%97 = icmp ne i32 %95, %96
br i1 %97, label %98, label %102
98: ; preds = %94
%99 = load i32, i32* @stderr
%100 = load i32, i32* %12
%101 = call i32 @fprintf(i32 %99, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i32 %100)
br label %102
102: ; preds = %98, %94
br label %103
103: ; preds = %102
%104 = load i32, i32* %6
%105 = add nsw i32 %104, 1
store i32 %105, i32* %6
br label %80
106: ; preds = %80
%107 = load i32, i32* %7
%108 = call i64 @close(i32 %107)
%109 = icmp slt i64 %108, 0
br i1 %109, label %110, label %113
110: ; preds = %106
%111 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
%112 = call i32 @exit(i32 2)
unreachable
113: ; preds = %106
ret i32 0
}

LLVM IR (-O2):
@.str = [6 x i8] c"n:c:a\00"
@optarg = global i32 0
@.str.1 = [14 x i8] c"malloc failed\00"
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_SYNC = global i32 0
@.str.2 = [13 x i8] c"./chunks.dat\00"
@.str.3 = [12 x i8] c"open failed\00"
@.str.4 = [13 x i8] c"write failed\00"
@stderr = global i32 0
@.str.5 = [40 x i8] c"warning: written less than a chunk: %d\0A\00"
@.str.6 = [13 x i8] c"close failed\00"
define i32 @main(i32 %0, i8** %1) {
br label %3
3: ; preds = %17, %2
%4 = phi i32 [ %19, %17 ], [ 1024, %2 ]
%5 = phi i32 [ %8, %17 ], [ 0, %2 ]
%6 = phi i32 [ %11, %17 ], [ 1024, %2 ]
br label %7
7: ; preds = %12, %3
%8 = phi i32 [ %5, %3 ], [ 1, %12 ]
%9 = phi i32 [ %6, %3 ], [ %11, %12 ]
br label %10
10: ; preds = %7, %14
%11 = phi i32 [ %9, %7 ], [ %16, %14 ]
br label %12
12: ; preds = %10, %12
%13 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
switch i32 %13, label %12 [
i32 -1, label %20
i32 110, label %14
i32 99, label %17
i32 97, label %7
]
14: ; preds = %12
%15 = load i32, i32* @optarg
%16 = tail call i32 @atoi(i32 %15)
br label %10
17: ; preds = %12
%18 = load i32, i32* @optarg
%19 = tail call i32 @atoi(i32 %18)
br label %3
20: ; preds = %12
%21 = tail call i64 @malloc(i32 %4)
%22 = inttoptr i64 %21 to i8*
%23 = icmp eq i64 %21, 0
br i1 %23, label %28, label %24
24: ; preds = %20
%25 = icmp sgt i32 %4, 0
br i1 %25, label %26, label %38
26: ; preds = %24
%27 = zext i32 %4 to i64
br label %31
28: ; preds = %20
%29 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%30 = tail call i32 @exit(i32 2)
unreachable
31: ; preds = %26, %31
%32 = phi i64 [ 0, %26 ], [ %36, %31 ]
%33 = tail call i32 (...) @rand()
%34 = trunc i32 %33 to i8
%35 = getelementptr inbounds i8, i8* %22, i64 %32
store i8 %34, i8* %35
%36 = add nuw nsw i64 %32, 1
%37 = icmp eq i64 %36, %27
br i1 %37, label %38, label %31
38: ; preds = %31, %24
%39 = load i32, i32* @O_WRONLY
%40 = load i32, i32* @O_APPEND
%41 = or i32 %40, %39
%42 = load i32, i32* @O_CREAT
%43 = or i32 %41, %42
%44 = load i32, i32* @O_TRUNC
%45 = or i32 %43, %44
%46 = icmp eq i32 %8, 0
%47 = load i32, i32* @O_SYNC
%48 = select i1 %46, i32 %47, i32 0
%49 = or i32 %45, %48
%50 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %49, i32 420)
%51 = icmp slt i32 %50, 0
br i1 %51, label %54, label %52
52: ; preds = %38
%53 = icmp sgt i32 %11, 0
br i1 %53, label %57, label %72
54: ; preds = %38
%55 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%56 = tail call i32 @exit(i32 2)
unreachable
57: ; preds = %52, %69
%58 = phi i32 [ %70, %69 ], [ 0, %52 ]
%59 = tail call i32 @write(i32 %50, i8* %22, i32 %4)
%60 = icmp slt i32 %59, 0
br i1 %60, label %61, label %64
61: ; preds = %57
%62 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%63 = tail call i32 @exit(i32 2)
unreachable
64: ; preds = %57
%65 = icmp eq i32 %59, %4
br i1 %65, label %69, label %66
66: ; preds = %64
%67 = load i32, i32* @stderr
%68 = tail call i32 @fprintf(i32 %67, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i32 %59)
br label %69
69: ; preds = %64, %66
%70 = add nuw nsw i32 %58, 1
%71 = icmp eq i32 %70, %11
br i1 %71, label %72, label %57
72: ; preds = %69, %52
%73 = tail call i64 @close(i32 %50)
%74 = icmp slt i64 %73, 0
br i1 %74, label %75, label %78
75: ; preds = %72
%76 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
%77 = tail call i32 @exit(i32 2)
unreachable
78: ; preds = %72
ret i32 0
}