#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_WRONLY;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fsync(int);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, char, int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int main(int argc, char **argv) {
  int ret = -1, fd = -1;
  int buf_size = 2 * 1024 * 1024;
  char *buffer = NULL;
  if (argc != 2) {
    fprintf(stderr, "usage: %s <filename>\n", argv[0]);
    return (-1);
  }
  buffer = (char *)malloc(buf_size);
  if (!buffer) {
    perror("malloc");
    return (-1);
  }
  printf("Using testfile %s\n", argv[1]);
  fd = open(argv[1], (O_CREAT | O_WRONLY), 0666);
  if (fd < 0) {
    perror("open");
    return (-1);
  }
  memset(buffer, 'Z', buf_size);
  ret = write(fd, buffer, buf_size);
  if (ret < 0) {
    fprintf(stderr, "errno is %x\n", errno);
    perror("write");
    return (-1);
  }
  fsync(fd);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
movl   $0x200000,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11f7 <main+0x67>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe1c(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ff <main+0x16f>
mov    -0x1c(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1226 <main+0x96>
lea    0xe01(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ff <main+0x16f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xde8(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
mov    0x2d6e(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   10a0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1283 <main+0xf3>
lea    0xdbe(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ff <main+0x16f>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    $0x5a,%esi
call   1050 <memset@plt>
mov    -0x18(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12e8 <main+0x158>
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
mov    0x2d10(%rip),%rax
mov    (%rax),%edx
lea    0xd6c(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
lea    0xd6b(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ff <main+0x16f>
mov    -0x18(%rbp),%edi
call   1090 <fsync@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbp
cmp    $0x2,%edi
jne    1222 <main+0x92>
mov    $0x200000,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1249 <main+0xb9>
mov    %rax,%rbx
mov    0x8(%rbp),%rsi
lea    0xe5d(%rip),%rdi
call   1040 <printf@plt>
mov    0x8(%rbp),%rdi
mov    0x2e28(%rip),%rax
mov    0x2de9(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1b6,%edx
call   10a0 <open@plt>
test   %eax,%eax
js     1252 <main+0xc2>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    $0x5a,%esi
mov    $0x200000,%edx
call   1050 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200000,%edx
call   1030 <write@plt>
test   %eax,%eax
js     125b <main+0xcb>
mov    %ebp,%edi
call   1090 <fsync@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    1242 <main+0xb2>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rdx
lea    0xdca(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdc6(%rip),%rdi
jmp    1282 <main+0xf2>
lea    0xdd7(%rip),%rdi
jmp    1282 <main+0xf2>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
mov    0x2d65(%rip),%rax
mov    (%rax),%edx
lea    0xdc1(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
lea    0xdc0(%rip),%rdi
call   10b0 <perror@plt>
jmp    123d <main+0xad>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [22 x i8] c"usage: %s <filename>\0A\00"
@.str.1 = [7 x i8] c"malloc\00"
@.str.2 = [19 x i8] c"Using testfile %s\0A\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.3 = [5 x i8] c"open\00"
@.str.4 = [13 x i8] c"errno is %x\0A\00"
@errno = global i32 0
@.str.5 = [6 x i8] c"write\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 i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
store i32 -1, i32* %7
store i32 2097152, i32* %8
store i8* null, i8** %9
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 -1, i32* %3
br label %62
18: ; preds = %2
%19 = load i32, i32* %8
%20 = call i64 @malloc(i32 %19)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %9
%22 = load i8*, i8** %9
%23 = icmp ne i8* %22, null
br i1 %23, label %26, label %24
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %62
26: ; preds = %18
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = load i32, i32* @O_CREAT
%35 = load i32, i32* @O_WRONLY
%36 = or i32 %34, %35
%37 = call i32 @open(i8* %33, i32 %36, i32 438)
store i32 %37, i32* %7
%38 = load i32, i32* %7
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %26
%41 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %62
42: ; preds = %26
%43 = load i8*, i8** %9
%44 = load i32, i32* %8
%45 = call i32 @memset(i8* %43, i8 signext 90, i32 %44)
%46 = load i32, i32* %7
%47 = load i8*, i8** %9
%48 = load i32, i32* %8
%49 = call i32 @write(i32 %46, i8* %47, i32 %48)
store i32 %49, i32* %6
%50 = load i32, i32* %6
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %57
52: ; preds = %42
%53 = load i32, i32* @stderr
%54 = load i32, i32* @errno
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %54)
%56 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %3
br label %62
57: ; preds = %42
%58 = load i32, i32* %7
%59 = call i32 @fsync(i32 %58)
%60 = load i32, i32* %7
%61 = call i32 @close(i32 %60)
store i32 0, i32* %3
br label %62
62: ; preds = %57, %52, %40, %24, %12
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [22 x i8] c"usage: %s <filename>\0A\00"
@.str.1 = [7 x i8] c"malloc\00"
@.str.2 = [19 x i8] c"Using testfile %s\0A\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.3 = [5 x i8] c"open\00"
@.str.4 = [13 x i8] c"errno is %x\0A\00"
@errno = global i32 0
@.str.5 = [6 x i8] c"write\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %38
8: ; preds = %2
%9 = tail call i64 @malloc(i32 2097152)
%10 = inttoptr i64 %9 to i8*
%11 = icmp eq i64 %9, 0
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %38
14: ; preds = %8
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %16)
%18 = load i8*, i8** %15
%19 = load i32, i32* @O_CREAT
%20 = load i32, i32* @O_WRONLY
%21 = or i32 %20, %19
%22 = tail call i32 @open(i8* %18, i32 %21, i32 438)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %14
%25 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %38
26: ; preds = %14
%27 = tail call i32 @memset(i8* %10, i8 signext 90, i32 2097152)
%28 = tail call i32 @write(i32 %22, i8* %10, i32 2097152)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %26
%31 = load i32, i32* @stderr
%32 = load i32, i32* @errno
%33 = tail call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %32)
%34 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %38
35: ; preds = %26
%36 = tail call i32 @fsync(i32 %22)
%37 = tail call i32 @close(i32 %22)
br label %38
38: ; preds = %35, %30, %24, %12, %4
%39 = phi i32 [ -1, %4 ], [ -1, %24 ], [ -1, %30 ], [ 0, %35 ], [ -1, %12 ]
ret i32 %39
}