#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 *, ...);
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, i = 0;
  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);
  }
  while (1) {
    memset(buffer, (char)i, buf_size);
    ret = write(fd, buffer, buf_size);
    if (ret < 0) {
      fprintf(stderr, "errno is %x\n", errno);
      perror("write");
      return (-1);
    }
    if (i++ > 50) {
      break;
    }
  }
  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   $0x0,-0x1c(%rbp)
movl   $0x200000,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11ee <main+0x6e>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe25(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1312 <main+0x192>
mov    -0x20(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    121d <main+0x9d>
lea    0xe0a(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1312 <main+0x192>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdf1(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
mov    0x2d77(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    127a <main+0xfa>
lea    0xdc7(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1312 <main+0x192>
jmp    127f <main+0xff>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%eax
mov    -0x20(%rbp),%edx
movsbl %al,%esi
call   1050 <memset@plt>
mov    -0x18(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12e5 <main+0x165>
mov    0x2d24(%rip),%rax
mov    (%rax),%edi
mov    0x2d13(%rip),%rax
mov    (%rax),%edx
lea    0xd6f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
lea    0xd6e(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1312 <main+0x192>
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x1c(%rbp)
cmp    $0x32,%eax
jle    12fe <main+0x17e>
jmp    1303 <main+0x183>
jmp    127f <main+0xff>
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   %r14
push   %rbx
mov    %rsi,%rbp
cmp    $0x2,%edi
jne    1219 <main+0x99>
mov    $0x200000,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     123e <main+0xbe>
mov    %rax,%rbx
mov    0x8(%rbp),%rsi
lea    0xe6c(%rip),%rdi
call   1040 <printf@plt>
mov    0x8(%rbp),%rdi
mov    0x2e37(%rip),%rax
mov    0x2df8(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1b6,%edx
call   1090 <open@plt>
test   %eax,%eax
js     1247 <main+0xc7>
mov    %eax,%r14d
xor    %ebp,%ebp
nop
movsbl %bpl,%esi
mov    %rbx,%rdi
mov    $0x200000,%edx
call   1050 <memset@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x200000,%edx
call   1030 <write@plt>
test   %eax,%eax
js     1250 <main+0xd0>
add    $0x1,%ebp
cmp    $0x34,%ebp
jne    11e0 <main+0x60>
mov    %r14d,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    1239 <main+0xb9>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rdx
lea    0xdd3(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdd1(%rip),%rdi
jmp    1277 <main+0xf7>
lea    0xde2(%rip),%rdi
jmp    1277 <main+0xf7>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
mov    0x2d70(%rip),%rax
mov    (%rax),%edx
lea    0xdcc(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
lea    0xdcb(%rip),%rdi
call   10a0 <perror@plt>
jmp    1234 <main+0xb4>

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

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 %42
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 %42
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 %42
26: ; preds = %14, %37
%27 = phi i32 [ %38, %37 ], [ 0, %14 ]
%28 = trunc i32 %27 to i8
%29 = tail call i32 @memset(i8* %10, i8 signext %28, i32 2097152)
%30 = tail call i32 @write(i32 %22, i8* %10, i32 2097152)
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %26
%33 = load i32, i32* @stderr
%34 = load i32, i32* @errno
%35 = tail call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %34)
%36 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %42
37: ; preds = %26
%38 = add nuw nsw i32 %27, 1
%39 = icmp eq i32 %38, 52
br i1 %39, label %40, label %26
40: ; preds = %37
%41 = tail call i32 @close(i32 %22)
br label %42
42: ; preds = %40, %32, %24, %12, %4
%43 = phi i32 [ -1, %4 ], [ -1, %24 ], [ -1, %32 ], [ 0, %40 ], [ -1, %12 ]
ret i32 %43
}