#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_EXCL;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fillbuf_rand(unsigned int *, int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, char *);
int write(int, char *, int);
void generate_file(char *name, unsigned filesize) {
  char buf[1024];
  unsigned b;
  int fd;
  fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0644);
  if (fd < 0) {
    printf("ERROR: unable to open file <%s>\n", name);
    exit(1);
  }
  for (b = 0; b < filesize; b++) {
    fillbuf_rand((unsigned *)buf, 1024 / sizeof(unsigned));
    if (write(fd, buf, 1024) != 1024) {
      printf("ERROR: unable to write file <%s>\n", name);
      exit(1);
    }
  }
  close(fd);
}


x86 assembly (clang, -O0):
<generate_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
mov    0x2e72(%rip),%rax
or     (%rax),%esi
mov    0x2e49(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
mov    %eax,-0x418(%rbp)
cmpl   $0x0,-0x418(%rbp)
jge    11c8 <generate_file+0x68>
mov    -0x8(%rbp),%rsi
lea    0xe47(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movl   $0x0,-0x414(%rbp)
mov    -0x414(%rbp),%eax
cmp    -0xc(%rbp),%eax
jae    1247 <generate_file+0xe7>
lea    -0x410(%rbp),%rdi
mov    $0x100,%esi
call   1060 <fillbuf_rand@plt>
mov    -0x418(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1030 <write@plt>
cmp    $0x400,%eax
je     122e <generate_file+0xce>
mov    -0x8(%rbp),%rsi
lea    0xe02(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1233 <generate_file+0xd3>
mov    -0x414(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x414(%rbp)
jmp    11d2 <generate_file+0x72>
mov    -0x418(%rbp),%edi
call   1050 <close@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    0x2e54(%rip),%rcx
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e4d(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
test   %eax,%eax
js     1208 <generate_file+0xa8>
mov    %eax,%r15d
test   %ebp,%ebp
je     11d9 <generate_file+0x79>
mov    %rsp,%rbx
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x100,%esi
call   1060 <fillbuf_rand@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1030 <write@plt>
cmp    $0x400,%eax
jne    11ef <generate_file+0x8f>
add    $0xffffffff,%ebp
jne    11b0 <generate_file+0x50>
mov    %r15d,%edi
call   1050 <close@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe2b(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xdf1(%rip),%rdi
jmp    11f6 <generate_file+0x96>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str = [33 x i8] c"ERROR: unable to open file <%s>\0A\00"
@.str.1 = [34 x i8] c"ERROR: unable to write file <%s>\0A\00"
define void @generate_file(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_WRONLY
%10 = load i32, i32* @O_CREAT
%11 = or i32 %9, %10
%12 = load i32, i32* @O_EXCL
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %8, i32 %13, i32 420)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = load i8*, i8** %3
%19 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
store i32 0, i32* %6
br label %22
22: ; preds = %39, %21
%23 = load i32, i32* %6
%24 = load i32, i32* %4
%25 = icmp ult i32 %23, %24
br i1 %25, label %26, label %42
26: ; preds = %22
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%28 = bitcast i8* %27 to i32*
%29 = call i32 @fillbuf_rand(i32* %28, i32 256)
%30 = load i32, i32* %7
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%32 = call i32 @write(i32 %30, i8* %31, i32 1024)
%33 = icmp ne i32 %32, 1024
br i1 %33, label %34, label %38
34: ; preds = %26
%35 = load i8*, i8** %3
%36 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %35)
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %26
br label %39
39: ; preds = %38
%40 = load i32, i32* %6
%41 = add i32 %40, 1
store i32 %41, i32* %6
br label %22
42: ; preds = %22
%43 = load i32, i32* %7
%44 = call i32 @close(i32 %43)
ret void
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str = [33 x i8] c"ERROR: unable to open file <%s>\0A\00"
@.str.1 = [34 x i8] c"ERROR: unable to write file <%s>\0A\00"
define void @generate_file(i8* %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = load i32, i32* @O_EXCL
%9 = or i32 %7, %8
%10 = tail call i32 @open(i8* %0, i32 %9, i32 420)
%11 = icmp slt i32 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %2
%13 = icmp eq i32 %1, 0
br i1 %13, label %30, label %14
14: ; preds = %12
%15 = bitcast [1024 x i8]* %3 to i32*
br label %21
16: ; preds = %2
%17 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %0)
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %21
%20 = icmp eq i32 %26, %1
br i1 %20, label %30, label %21
21: ; preds = %14, %19
%22 = phi i32 [ 0, %14 ], [ %26, %19 ]
%23 = call i32 @fillbuf_rand(i32* %15, i32 256)
%24 = call i32 @write(i32 %10, i8* %4, i32 1024)
%25 = icmp eq i32 %24, 1024
%26 = add nuw i32 %22, 1
br i1 %25, label %19, label %27
27: ; preds = %21
%28 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %19, %12
%31 = call i32 @close(i32 %10)
ret void
}