#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;
scalar_t__ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ memset(char *, char, int);
int open(char *, int, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char *);
int write(int, char *, size_t);
int mk_file(char *filename, size_t sz) {
  int fd;
  char buffer[(4096)];
  (void)unlink(filename);
  if ((fd = open(filename, O_CREAT | O_RDWR, (S_IRUSR | S_IWUSR))) < 0) {
    fprintf(stderr, "Cannot create %s: %s\n", filename, strerror(errno));
    return -1;
  }
  memset(buffer, 'x', (4096));
  while (sz > 0) {
    size_t n = (sz > (4096)) ? (4096) : sz;
    int ret;
    if ((ret = write(fd, buffer, n)) < 0) {
      fprintf(stderr, "Error writing to file %s: %s\n", filename,
              strerror(errno));
      close(fd);
      return -1;
    }
    sz -= ret;
  }
  if (close(fd) < 0) {
    fprintf(stderr, "Cannot close %s: %s\n", filename, strerror(errno));
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<mk_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1070,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
mov    0x2e40(%rip),%rax
or     (%rax),%esi
mov    0x2e0f(%rip),%rax
mov    (%rax),%edx
mov    0x2e26(%rip),%rax
or     (%rax),%edx
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1215 <mk_file+0xa5>
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x103c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x1038(%rbp)
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x103c(%rbp),%edi
mov    -0x1038(%rbp),%rdx
mov    %rax,%rcx
lea    0xdfc(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136b <mk_file+0x1fb>
lea    -0x1020(%rbp),%rdi
mov    $0x78,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
cmpq   $0x0,-0x18(%rbp)
jbe    1302 <mk_file+0x192>
cmpq   $0x1000,-0x18(%rbp)
jbe    1255 <mk_file+0xe5>
mov    $0x1000,%eax
mov    %rax,-0x1048(%rbp)
jmp    1260 <mk_file+0xf0>
mov    -0x18(%rbp),%rax
mov    %rax,-0x1048(%rbp)
mov    -0x1048(%rbp),%rax
mov    %rax,-0x1028(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x1028(%rbp),%rdx
call   1040 <write@plt>
mov    %eax,-0x102c(%rbp)
cmp    $0x0,%eax
jge    12eb <mk_file+0x17b>
mov    0x2d2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1054(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x1050(%rbp)
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x1054(%rbp),%edi
mov    -0x1050(%rbp),%rdx
mov    %rax,%rcx
lea    0xd44(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136b <mk_file+0x1fb>
movslq -0x102c(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    122b <mk_file+0xbb>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
jge    1364 <mk_file+0x1f4>
mov    0x2cad(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1064(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x1060(%rbp)
mov    0x2c8b(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x1064(%rbp),%edi
mov    -0x1060(%rbp),%rdx
mov    %rax,%rcx
lea    0xce1(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136b <mk_file+0x1fb>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1070,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mk_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
call   1030 <unlink@plt>
mov    0x2e69(%rip),%rax
mov    0x2e4a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e17(%rip),%rax
mov    0x2e30(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %r14,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     123a <mk_file+0xca>
mov    %eax,%ebp
mov    %rsp,%rdi
mov    $0x78,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
test   %rbx,%rbx
je     1205 <mk_file+0x95>
mov    %rsp,%r15
nopl   0x0(%rax,%rax,1)
cmp    $0x1000,%rbx
mov    $0x1000,%edx
cmovb  %rbx,%rdx
mov    %ebp,%edi
mov    %r15,%rsi
call   1040 <write@plt>
test   %eax,%eax
js     1267 <mk_file+0xf7>
mov    %eax,%eax
sub    %rax,%rbx
jne    11e0 <mk_file+0x70>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jns    129e <mk_file+0x12e>
mov    0x2da7(%rip),%rax
mov    (%rax),%ebx
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdfc(%rip),%rsi
jmp    1258 <mk_file+0xe8>
mov    0x2d87(%rip),%rax
mov    (%rax),%ebx
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xda8(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1070 <fprintf@plt>
jmp    1299 <mk_file+0x129>
mov    0x2d5a(%rip),%rax
mov    (%rax),%ebx
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd91(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1070 <fprintf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0xffffffff,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"Cannot create %s: %s\0A\00"
@errno = global i32 0
@.str.1 = [30 x i8] c"Error writing to file %s: %s\0A\00"
@.str.2 = [21 x i8] c"Cannot close %s: %s\0A\00"
define i32 @mk_file(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca [4096 x i8]
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
%10 = load i8*, i8** %4
%11 = call i32 @unlink(i8* %10)
%12 = load i8*, i8** %4
%13 = load i32, i32* @O_CREAT
%14 = load i32, i32* @O_RDWR
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IRUSR
%17 = load i32, i32* @S_IWUSR
%18 = or i32 %16, %17
%19 = call i32 @open(i8* %12, i32 %15, i32 %18)
store i32 %19, i32* %6
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %2
%22 = load i32, i32* @stderr
%23 = load i8*, i8** %4
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %23, i8* %25)
store i32 -1, i32* %3
br label %70
27: ; preds = %2
%28 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%29 = call i32 @memset(i8* %28, i8 signext 120, i32 4096)
br label %30
30: ; preds = %54, %27
%31 = load i64, i64* %5
%32 = icmp ugt i64 %31, 0
br i1 %32, label %33, label %59
33: ; preds = %30
%34 = load i64, i64* %5
%35 = icmp ugt i64 %34, 4096
br i1 %35, label %36, label %37
36: ; preds = %33
br label %39
37: ; preds = %33
%38 = load i64, i64* %5
br label %39
39: ; preds = %37, %36
%40 = phi i64 [ 4096, %36 ], [ %38, %37 ]
store i64 %40, i64* %8
%41 = load i32, i32* %6
%42 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%43 = load i64, i64* %8
%44 = call i32 @write(i32 %41, i8* %42, i64 %43)
store i32 %44, i32* %9
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %54
46: ; preds = %39
%47 = load i32, i32* @stderr
%48 = load i8*, i8** %4
%49 = load i32, i32* @errno
%50 = call i8* @strerror(i32 %49)
%51 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %48, i8* %50)
%52 = load i32, i32* %6
%53 = call i64 @close(i32 %52)
store i32 -1, i32* %3
br label %70
54: ; preds = %39
%55 = load i32, i32* %9
%56 = sext i32 %55 to i64
%57 = load i64, i64* %5
%58 = sub i64 %57, %56
store i64 %58, i64* %5
br label %30
59: ; preds = %30
%60 = load i32, i32* %6
%61 = call i64 @close(i32 %60)
%62 = icmp slt i64 %61, 0
br i1 %62, label %63, label %69
63: ; preds = %59
%64 = load i32, i32* @stderr
%65 = load i8*, i8** %4
%66 = load i32, i32* @errno
%67 = call i8* @strerror(i32 %66)
%68 = call i32 @fprintf(i32 %64, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %65, i8* %67)
store i32 -1, i32* %3
br label %70
69: ; preds = %59
store i32 0, i32* %3
br label %70
70: ; preds = %69, %63, %46, %21
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"Cannot create %s: %s\0A\00"
@errno = global i32 0
@.str.1 = [30 x i8] c"Error writing to file %s: %s\0A\00"
@.str.2 = [21 x i8] c"Cannot close %s: %s\0A\00"
define i32 @mk_file(i8* %0, i64 %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @unlink(i8* %0)
%6 = load i32, i32* @O_CREAT
%7 = load i32, i32* @O_RDWR
%8 = or i32 %7, %6
%9 = load i32, i32* @S_IRUSR
%10 = load i32, i32* @S_IWUSR
%11 = or i32 %10, %9
%12 = tail call i32 @open(i8* %0, i32 %8, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = tail call i8* @strerror(i32 %16)
%18 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %17)
br label %46
19: ; preds = %2
%20 = call i32 @memset(i8* %4, i8 signext 120, i32 4096)
%21 = icmp eq i64 %1, 0
br i1 %21, label %38, label %24
22: ; preds = %24
%23 = icmp eq i64 %31, 0
br i1 %23, label %38, label %24
24: ; preds = %19, %22
%25 = phi i64 [ %31, %22 ], [ %1, %19 ]
%26 = icmp ult i64 %25, 4096
%27 = select i1 %26, i64 %25, i64 4096
%28 = call i32 @write(i32 %12, i8* %4, i64 %27)
%29 = icmp slt i32 %28, 0
%30 = zext i32 %28 to i64
%31 = sub i64 %25, %30
br i1 %29, label %32, label %22
32: ; preds = %24
%33 = load i32, i32* @stderr
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %35)
%37 = call i64 @close(i32 %12)
br label %46
38: ; preds = %22, %19
%39 = call i64 @close(i32 %12)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %38
%42 = load i32, i32* @stderr
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %44)
br label %46
46: ; preds = %32, %38, %41, %14
%47 = phi i32 [ -1, %14 ], [ -1, %41 ], [ 0, %38 ], [ -1, %32 ]
ret i32 %47
}