#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 */
struct stat {
  int dummy;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ buff;

/* Variables and functions */
scalar_t__ ENOSPC;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int open(char *, int, int);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int /*<<< orphan*/ unlink(char *);
int write(int, unsigned char *, int);
int test_exercise(char *filename, ssize_t size) {
  int fd;
  ssize_t i;
  ssize_t n;
  struct stat statbuf;
  ssize_t nbytes = size;
  int ret = (1);
  unsigned char buff[(65536)];
  unlink(filename);
  if ((fd = open(filename, O_RDWR | O_CREAT, 0600)) < 0) {
    fprintf(stderr, "Failed to open %s: %s\n", filename, strerror(errno));
    return (1);
  }
  memset(buff, 0, sizeof(buff));
  while (nbytes > 0) {
    int rc;
    ssize_t n = (nbytes > (65536)) ? (65536) : nbytes;
    rc = write(fd, buff, n);
    if (rc < 0) {
      if (errno == ENOSPC)
        ret = (0);
      break;
    }
    nbytes -= n;
  }
  close(fd);
  unlink(filename);
  return ret;
}


x86 assembly (clang, -O0):
<test_exercise>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10050,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
movl   $0x1,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
mov    0x2e4c(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1215 <test_exercise+0xa5>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10044(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x10040(%rbp)
mov    0x2de3(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x10044(%rbp),%edi
mov    -0x10040(%rbp),%rdx
mov    %rax,%rcx
lea    0xdfc(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12dc <test_exercise+0x16c>
lea    -0x10030(%rbp),%rdi
xor    %esi,%esi
mov    $0x10000,%edx
call   1050 <memset@plt>
cmpl   $0x0,-0x2c(%rbp)
jle    12c5 <test_exercise+0x155>
cmpl   $0x10000,-0x2c(%rbp)
jle    124f <test_exercise+0xdf>
mov    $0x10000,%eax
mov    %eax,-0x10048(%rbp)
jmp    1258 <test_exercise+0xe8>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x10048(%rbp)
mov    -0x10048(%rbp),%eax
mov    %eax,-0x10038(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x10030(%rbp),%rsi
mov    -0x10038(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x10034(%rbp)
cmpl   $0x0,-0x10034(%rbp)
jge    12b2 <test_exercise+0x142>
mov    0x2d35(%rip),%rax
mov    (%rax),%rax
mov    0x2d23(%rip),%rcx
cmp    (%rcx),%rax
jne    12ad <test_exercise+0x13d>
movl   $0x0,-0x30(%rbp)
jmp    12c5 <test_exercise+0x155>
mov    -0x10038(%rbp),%ecx
mov    -0x2c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x2c(%rbp)
jmp    1228 <test_exercise+0xb8>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_exercise>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10008,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
call   1030 <unlink@plt>
mov    0x2e4e(%rip),%rax
mov    0x2e5f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r14,%rdi
mov    $0x180,%edx
call   1080 <open@plt>
test   %eax,%eax
js     11f7 <test_exercise+0x87>
mov    %eax,%r13d
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x10000,%edx
call   1050 <memset@plt>
mov    $0x1,%r15d
test   %ebx,%ebx
jle    1246 <test_exercise+0xd6>
mov    %rsp,%r12
nopl   (%rax)
cmp    $0x10000,%ebx
mov    $0x10000,%ebp
cmovb  %ebx,%ebp
mov    %r13d,%edi
mov    %r12,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
test   %eax,%eax
js     122b <test_exercise+0xbb>
sub    %ebp,%ebx
test   %ebx,%ebx
jg     11d0 <test_exercise+0x60>
jmp    1246 <test_exercise+0xd6>
mov    0x2dd2(%rip),%rax
mov    (%rax),%ebx
mov    0x2dc1(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
lea    0xdea(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1070 <fprintf@plt>
mov    $0x1,%r15d
jmp    1256 <test_exercise+0xe6>
mov    0x2d96(%rip),%rax
mov    (%rax),%rax
mov    0x2d84(%rip),%rcx
xor    %r15d,%r15d
cmp    (%rcx),%rax
setne  %r15b
mov    %r13d,%edi
call   1060 <close@plt>
mov    %r14,%rdi
call   1030 <unlink@plt>
mov    %r15d,%eax
add    $0x10008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@stderr = global i32 0
@.str = [23 x i8] c"Failed to open %s: %s\0A\00"
@errno = global i64 0
@ENOSPC = global i64 0
define i32 @test_exercise(i8* %0, i32 %1) {
%3 = alloca [65536 x i8]
%4 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @unlink(i8* %0)
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_CREAT
%8 = or i32 %7, %6
%9 = tail call i32 @open(i8* %0, i32 %8, i32 384)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i64, i64* @errno
%14 = tail call i8* @strerror(i64 %13)
%15 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %14)
br label %37
16: ; preds = %2
%17 = call i32 @memset(i8* %4, i32 0, i32 65536)
%18 = icmp sgt i32 %1, 0
br i1 %18, label %21, label %33
19: ; preds = %21
%20 = icmp sgt i32 %27, 0
br i1 %20, label %21, label %33
21: ; preds = %16, %19
%22 = phi i32 [ %27, %19 ], [ %1, %16 ]
%23 = icmp ult i32 %22, 65536
%24 = select i1 %23, i32 %22, i32 65536
%25 = call i32 @write(i32 %9, i8* %4, i32 %24)
%26 = icmp slt i32 %25, 0
%27 = sub nsw i32 %22, %24
br i1 %26, label %28, label %19
28: ; preds = %21
%29 = load i64, i64* @errno
%30 = load i64, i64* @ENOSPC
%31 = icmp ne i64 %29, %30
%32 = zext i1 %31 to i32
br label %33
33: ; preds = %19, %16, %28
%34 = phi i32 [ %32, %28 ], [ 1, %16 ], [ 1, %19 ]
%35 = call i32 @close(i32 %9)
%36 = call i32 @unlink(i8* %0)
br label %37
37: ; preds = %33, %11
%38 = phi i32 [ 1, %11 ], [ %34, %33 ]
ret i32 %38
}