#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 BUFSIZ;
int O_CREAT;
int O_RDWR;
void *alloca(int);
int /*<<< orphan*/ memset(void *, char, int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int write(int, void *, int);
int create_file(char *fname, int size) {
  int i;
  int fd;
  int wsize;
  void *buffer = alloca(BUFSIZ);
  if (buffer == NULL)
    return -1;
  memset(buffer, 'a', BUFSIZ);
  fd = open(fname, O_RDWR | O_CREAT, 0644);
  if (fd < 0) {
    perror("open");
    return -1;
  }
  wsize = BUFSIZ;
  for (i = size; i > 0; i -= BUFSIZ) {
    if (i < BUFSIZ)
      wsize = i;
    if (write(fd, buffer, wsize) != wsize) {
      perror("write");
      return -1;
    }
  }
  return fd;
}


x86 assembly (clang, -O0):
<create_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
call   1050 <alloca@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1188 <create_file+0x38>
movl   $0xffffffff,-0x4(%rbp)
jmp    1269 <create_file+0x119>
mov    -0x28(%rbp),%rdi
mov    0x2e3d(%rip),%rax
mov    (%rax),%edx
mov    $0x61,%esi
call   1040 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
mov    0x2e45(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11e4 <create_file+0x94>
lea    0xe2d(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1269 <create_file+0x119>
mov    0x2de5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    1263 <create_file+0x113>
mov    -0x18(%rbp),%eax
mov    0x2dc6(%rip),%rcx
cmp    (%rcx),%eax
jge    1218 <create_file+0xc8>
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1030 <write@plt>
cmp    -0x20(%rbp),%eax
je     1248 <create_file+0xf8>
lea    0xdce(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1269 <create_file+0x119>
jmp    124d <create_file+0xfd>
mov    0x2d7c(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
jmp    11f6 <create_file+0xa6>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    0x2e69(%rip),%r12
mov    (%r12),%edi
call   1050 <alloca@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     11e1 <create_file+0x91>
mov    %rax,%r15
mov    (%r12),%edx
mov    %rax,%rdi
mov    $0x61,%esi
call   1040 <memset@plt>
mov    0x2e4a(%rip),%rax
mov    0x2e5b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1a4,%edx
call   1060 <open@plt>
test   %eax,%eax
js     11f3 <create_file+0xa3>
mov    %eax,%r13d
test   %ebp,%ebp
jle    11de <create_file+0x8e>
mov    (%r12),%eax
mov    %eax,%ebx
nop
cmp    %eax,%ebp
cmovl  %ebp,%ebx
mov    %r13d,%edi
mov    %r15,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
jne    1201 <create_file+0xb1>
mov    (%r12),%eax
sub    %eax,%ebp
jg     11c0 <create_file+0x70>
mov    %r13d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe06(%rip),%rdi
call   1070 <perror@plt>
jmp    11e1 <create_file+0x91>
lea    0xdfd(%rip),%rdi
call   1070 <perror@plt>
jmp    11e1 <create_file+0x91>

LLVM IR (-O0):
@BUFSIZ = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str = [5 x i8] c"open\00"
@.str.1 = [6 x i8] c"write\00"
define i32 @create_file(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = load i32, i32* @BUFSIZ
%11 = call i8* @alloca(i32 %10)
store i8* %11, i8** %9
%12 = load i8*, i8** %9
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 -1, i32* %3
br label %56
15: ; preds = %2
%16 = load i8*, i8** %9
%17 = load i32, i32* @BUFSIZ
%18 = call i32 @memset(i8* %16, i8 signext 97, i32 %17)
%19 = load i8*, i8** %4
%20 = load i32, i32* @O_RDWR
%21 = load i32, i32* @O_CREAT
%22 = or i32 %20, %21
%23 = call i32 @open(i8* %19, i32 %22, i32 420)
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %15
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %56
28: ; preds = %15
%29 = load i32, i32* @BUFSIZ
store i32 %29, i32* %8
%30 = load i32, i32* %5
store i32 %30, i32* %6
br label %31
31: ; preds = %50, %28
%32 = load i32, i32* %6
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %54
34: ; preds = %31
%35 = load i32, i32* %6
%36 = load i32, i32* @BUFSIZ
%37 = icmp slt i32 %35, %36
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = load i32, i32* %6
store i32 %39, i32* %8
br label %40
40: ; preds = %38, %34
%41 = load i32, i32* %7
%42 = load i8*, i8** %9
%43 = load i32, i32* %8
%44 = call i32 @write(i32 %41, i8* %42, i32 %43)
%45 = load i32, i32* %8
%46 = icmp ne i32 %44, %45
br i1 %46, label %47, label %49
47: ; preds = %40
%48 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %56
49: ; preds = %40
br label %50
50: ; preds = %49
%51 = load i32, i32* @BUFSIZ
%52 = load i32, i32* %6
%53 = sub nsw i32 %52, %51
store i32 %53, i32* %6
br label %31
54: ; preds = %31
%55 = load i32, i32* %7
store i32 %55, i32* %3
br label %56
56: ; preds = %54, %47, %26, %14
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str = [5 x i8] c"open\00"
@.str.1 = [6 x i8] c"write\00"
define i32 @create_file(i8* nocapture %0, i32 %1) {
%3 = load i32, i32* @BUFSIZ
%4 = tail call i8* @alloca(i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %32, label %6
6: ; preds = %2
%7 = load i32, i32* @BUFSIZ
%8 = tail call i32 @memset(i8* %4, i8 signext 97, i32 %7)
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_CREAT
%11 = or i32 %10, %9
%12 = tail call i32 @open(i8* %0, i32 %11, i32 420)
%13 = icmp slt i32 %12, 0
br i1 %13, label %29, label %14
14: ; preds = %6
%15 = load i32, i32* @BUFSIZ
%16 = icmp sgt i32 %1, 0
br i1 %16, label %17, label %32
17: ; preds = %14, %25
%18 = phi i32 [ %26, %25 ], [ %15, %14 ]
%19 = phi i32 [ %22, %25 ], [ %15, %14 ]
%20 = phi i32 [ %27, %25 ], [ %1, %14 ]
%21 = icmp slt i32 %20, %18
%22 = select i1 %21, i32 %20, i32 %19
%23 = tail call i32 @write(i32 %12, i8* %4, i32 %22)
%24 = icmp eq i32 %23, %22
br i1 %24, label %25, label %29
25: ; preds = %17
%26 = load i32, i32* @BUFSIZ
%27 = sub nsw i32 %20, %26
%28 = icmp sgt i32 %27, 0
br i1 %28, label %17, label %32
29: ; preds = %17, %6
%30 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %6 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), %17 ]
%31 = tail call i32 @perror(i8* %30)
br label %32
32: ; preds = %25, %29, %14, %2
%33 = phi i32 [ -1, %2 ], [ %12, %14 ], [ -1, %29 ], [ %12, %25 ]
ret i32 %33
}