#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 uint64_t;
typedef int /*<<< orphan*/ i;

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *);
int write(int, int *, int);
void write_pattern() {
  printf("writing pattern\n");
  int fd = open("foo", O_CREAT | O_WRONLY, 0644);
  uint64_t i;
  int r;
  for (i = 0; i < 1048576 * sizeof(i); i += sizeof(i)) {
    r = write(fd, &i, sizeof(i));
  }
  close(fd);
}


x86 assembly (clang, -O0):
<write_pattern>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xeb1(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e9d(%rip),%rax
mov    (%rax),%esi
mov    0x2e6c(%rip),%rax
or     (%rax),%esi
lea    0xea4(%rip),%rdi
mov    $0x1a4,%edx
call   1060 <open@plt>
mov    %eax,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movslq -0x8(%rbp),%rax
cmp    $0x400000,%rax
jae    11b5 <write_pattern+0x75>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %eax,-0xc(%rbp)
movslq -0x8(%rbp),%rax
add    $0x4,%rax
mov    %eax,-0x8(%rbp)
jmp    1181 <write_pattern+0x41>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_pattern>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xeb5(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e9d(%rip),%rax
mov    0x2e6e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe93(%rip),%rdi
mov    $0x1a4,%edx
call   1060 <open@plt>
mov    %eax,%ebp
movl   $0x0,0xc(%rsp)
mov    $0xfffffffc,%ebx
lea    0xc(%rsp),%r14
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1040 <write@plt>
lea    0x8(%rbx),%eax
mov    %eax,0xc(%rsp)
add    $0x4,%ebx
cmp    $0x3ffffc,%ebx
jb     1190 <write_pattern+0x50>
mov    %ebp,%edi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <close@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"writing pattern\0A\00"
@.str.1 = [4 x i8] c"foo\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
define void @write_pattern() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_WRONLY
%7 = or i32 %5, %6
%8 = call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %7, i32 420)
store i32 %8, i32* %1
store i32 0, i32* %2
br label %9
9: ; preds = %16, %0
%10 = load i32, i32* %2
%11 = sext i32 %10 to i64
%12 = icmp ult i64 %11, 4194304
br i1 %12, label %13, label %21
13: ; preds = %9
%14 = load i32, i32* %1
%15 = call i32 @write(i32 %14, i32* %2, i32 4)
store i32 %15, i32* %3
br label %16
16: ; preds = %13
%17 = load i32, i32* %2
%18 = sext i32 %17 to i64
%19 = add i64 %18, 4
%20 = trunc i64 %19 to i32
store i32 %20, i32* %2
br label %9
21: ; preds = %9
%22 = load i32, i32* %1
%23 = call i32 @close(i32 %22)
ret void
}

LLVM IR (-O2):
@.str.1 = [4 x i8] c"foo\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@str = [16 x i8] c"writing pattern\00"
define void @write_pattern() {
%1 = alloca i32
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_WRONLY
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %5, i32 420)
%7 = bitcast i32* %1 to i8*
store i32 0, i32* %1
br label %8
8: ; preds = %0, %8
%9 = phi i32 [ 0, %0 ], [ %11, %8 ]
%10 = call i32 @write(i32 %6, i32* %1, i32 4)
%11 = add nuw nsw i32 %9, 4
store i32 %11, i32* %1
%12 = icmp ult i32 %9, 4194300
br i1 %12, label %8, label %13
13: ; preds = %8
%14 = tail call i32 @close(i32 %6)
ret void
}