#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 S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ errno;
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int /*<<< orphan*/);
int write(int, int *, int);
int main(int argc, char *argv[]) {
  int i, fd, len, status;
  int buffer[0x1000];
  for (i = 0; i < 0x1000; i++) {
    buffer[i] = 4 * i;
  }
  fd = open("binfile.dat", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
  if (fd < 0) {
    printf("File open failed - %s\n", strerror(errno));
    return -1;
  }
  len = 0x1000 * sizeof(int);
  status = write(fd, buffer, len);
  if (status < 0) {
    printf("File write failed - %s\n", strerror(errno));
    return -1;
  }
  if (status != len) {
    printf("File write was short\n");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4020,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x1000,-0x14(%rbp)
jge    118c <main+0x4c>
mov    -0x14(%rbp),%ecx
shl    $0x2,%ecx
movslq -0x14(%rbp),%rax
mov    %ecx,-0x4020(%rbp,%rax,4)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1160 <main+0x20>
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
mov    0x2e5c(%rip),%rax
or     (%rax),%esi
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
mov    0x2e2a(%rip),%rax
or     (%rax),%edx
lea    0xe49(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11f4 <main+0xb4>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %rax,%rsi
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1272 <main+0x132>
movl   $0x4000,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x4020(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1245 <main+0x105>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %rax,%rsi
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1272 <main+0x132>
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     126b <main+0x12b>
lea    0xde3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1272 <main+0x132>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x4020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x4008,%rsp
movdqa 0xea1(%rip),%xmm0
xor    %eax,%eax
movdqa 0xea7(%rip),%xmm1
movdqa 0xeaf(%rip),%xmm2
movdqa 0xeb7(%rip),%xmm3
nopl   0x0(%rax)
movdqa %xmm0,%xmm4
pslld  $0x2,%xmm4
movdqa %xmm4,%xmm5
paddd  %xmm1,%xmm5
movdqa %xmm4,(%rsp,%rax,4)
movdqa %xmm5,0x10(%rsp,%rax,4)
movdqa %xmm4,%xmm5
paddd  %xmm2,%xmm5
paddd  %xmm3,%xmm4
movdqa %xmm5,0x20(%rsp,%rax,4)
movdqa %xmm4,0x30(%rsp,%rax,4)
add    $0x10,%rax
paddd  %xmm1,%xmm0
cmp    $0x1000,%rax
jne    1180 <main+0x30>
mov    0x2e15(%rip),%rax
mov    0x2e26(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2de3(%rip),%rax
mov    0x2df4(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe51(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     1225 <main+0xd5>
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x4000,%edx
call   1040 <write@plt>
test   %eax,%eax
js     123c <main+0xec>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x4000,%ecx
je     1260 <main+0x110>
lea    0xe5d(%rip),%rdi
call   1030 <puts@plt>
jmp    125b <main+0x10b>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe12(%rip),%rdi
jmp    1251 <main+0x101>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe12(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%eax
add    $0x4008,%rsp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"binfile.dat\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [23 x i8] c"File open failed - %s\0A\00"
@errno = global i32 0
@.str.2 = [24 x i8] c"File write failed - %s\0A\00"
@.str.3 = [22 x i8] c"File write was short\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 i32
%10 = alloca [4096 x i32]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
br label %11
11: ; preds = %20, %2
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 4096
br i1 %13, label %14, label %23
14: ; preds = %11
%15 = load i32, i32* %6
%16 = mul nsw i32 4, %15
%17 = load i32, i32* %6
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [4096 x i32], [4096 x i32]* %10, i64 0, i64 %18
store i32 %16, i32* %19
br label %20
20: ; preds = %14
%21 = load i32, i32* %6
%22 = add nsw i32 %21, 1
store i32 %22, i32* %6
br label %11
23: ; preds = %11
%24 = load i32, i32* @O_RDWR
%25 = load i32, i32* @O_CREAT
%26 = or i32 %24, %25
%27 = load i32, i32* @S_IRUSR
%28 = load i32, i32* @S_IWUSR
%29 = or i32 %27, %28
%30 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %26, i32 %29)
store i32 %30, i32* %7
%31 = load i32, i32* %7
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %23
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %35)
store i32 -1, i32* %3
br label %55
37: ; preds = %23
store i32 16384, i32* %8
%38 = load i32, i32* %7
%39 = getelementptr inbounds [4096 x i32], [4096 x i32]* %10, i64 0, i64 0
%40 = load i32, i32* %8
%41 = call i32 @write(i32 %38, i32* %39, i32 %40)
store i32 %41, i32* %9
%42 = load i32, i32* %9
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %37
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %46)
store i32 -1, i32* %3
br label %55
48: ; preds = %37
%49 = load i32, i32* %9
%50 = load i32, i32* %8
%51 = icmp ne i32 %49, %50
br i1 %51, label %52, label %54
52: ; preds = %48
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %55
54: ; preds = %48
store i32 0, i32* %3
br label %55
55: ; preds = %54, %52, %44, %33
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
@.str = [12 x i8] c"binfile.dat\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [23 x i8] c"File open failed - %s\0A\00"
@errno = global i32 0
@.str.2 = [24 x i8] c"File write failed - %s\0A\00"
@str = [21 x i8] c"File write was short\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [4096 x i32]
%4 = bitcast [4096 x i32]* %3 to i8*
br label %5
5: ; preds = %5, %2
%6 = phi i64 [ 0, %2 ], [ %24, %5 ]
%7 = phi <4 x i32> [ <i32 0, i32 1, i32 2, i32 3>, %2 ], [ %25, %5 ]
%8 = getelementptr inbounds [4096 x i32], [4096 x i32]* %3, i64 0, i64 %6
%9 = shl <4 x i32> %7, <i32 2, i32 2, i32 2, i32 2>
%10 = shl <4 x i32> %7, <i32 2, i32 2, i32 2, i32 2>
%11 = add <4 x i32> %10, <i32 16, i32 16, i32 16, i32 16>
%12 = bitcast i32* %8 to <4 x i32>*
store <4 x i32> %9, <4 x i32>* %12
%13 = getelementptr inbounds i32, i32* %8, i64 4
%14 = bitcast i32* %13 to <4 x i32>*
store <4 x i32> %11, <4 x i32>* %14
%15 = or i64 %6, 8
%16 = add <4 x i32> %7, <i32 8, i32 8, i32 8, i32 8>
%17 = getelementptr inbounds [4096 x i32], [4096 x i32]* %3, i64 0, i64 %15
%18 = shl <4 x i32> %16, <i32 2, i32 2, i32 2, i32 2>
%19 = shl <4 x i32> %16, <i32 2, i32 2, i32 2, i32 2>
%20 = add <4 x i32> %19, <i32 16, i32 16, i32 16, i32 16>
%21 = bitcast i32* %17 to <4 x i32>*
store <4 x i32> %18, <4 x i32>* %21
%22 = getelementptr inbounds i32, i32* %17, i64 4
%23 = bitcast i32* %22 to <4 x i32>*
store <4 x i32> %20, <4 x i32>* %23
%24 = add nuw nsw i64 %6, 16
%25 = add <4 x i32> %7, <i32 16, i32 16, i32 16, i32 16>
%26 = icmp eq i64 %24, 4096
br i1 %26, label %27, label %5
27: ; preds = %5
%28 = load i32, i32* @O_RDWR
%29 = load i32, i32* @O_CREAT
%30 = or i32 %29, %28
%31 = load i32, i32* @S_IRUSR
%32 = load i32, i32* @S_IWUSR
%33 = or i32 %32, %31
%34 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %30, i32 %33)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %27
%37 = load i32, i32* @errno
%38 = tail call i8* @strerror(i32 %37)
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %38)
br label %52
40: ; preds = %27
%41 = getelementptr inbounds [4096 x i32], [4096 x i32]* %3, i64 0, i64 0
%42 = call i32 @write(i32 %34, i32* %41, i32 16384)
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %40
%45 = load i32, i32* @errno
%46 = tail call i8* @strerror(i32 %45)
%47 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %46)
br label %52
48: ; preds = %40
%49 = icmp eq i32 %42, 16384
br i1 %49, label %52, label %50
50: ; preds = %48
%51 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %52
52: ; preds = %48, %50, %44, %36
%53 = phi i32 [ -1, %36 ], [ -1, %44 ], [ -1, %50 ], [ 0, %48 ]
ret i32 %53
}