#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 st_size;
};

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ __NR_unlink;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ memset(int *, int /*<<< orphan*/, int);
int open(char const *, int /*<<< orphan*/);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
char *strerror(int /*<<< orphan*/);
int syscall(int /*<<< orphan*/, char const *);
int write(int, int *, int);
int unlink(const char *pathname) {
  struct stat f_info;
  if (stat(pathname, &f_info) != 0) {
    fprintf(stdout, "Failed to stat file (%s): %s\n", pathname,
            strerror(errno));
    return -1;
  }
  int f_size = f_info.st_size;
  int to_Write[f_size];
  memset(to_Write, 0, f_size);
  int fd = open(pathname, O_WRONLY);
  if (fd < 0) {
    fprintf(stderr, "Failed to open file (%s) for writing: %s", pathname,
            strerror(errno));
    return -1;
  }
  int written;
  written = write(fd, to_Write, f_size);
  if (written < f_size)
    fprintf(stderr, "Warning, wrote %d bytes but expected to write %d!\n",
            written, f_size);
  return syscall(__NR_unlink, pathname);
}


x86 assembly (clang, -O0):
<unlink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     11d9 <unlink+0x69>
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xe3a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12da <unlink+0x16a>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
lea    0xf(,%rax,4),%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x58(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    127c <unlink+0x10c>
mov    0x2da2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rdx
mov    %rax,%rcx
lea    0xdbc(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    12d3 <unlink+0x163>
mov    -0x58(%rbp),%rsi
mov    -0x34(%rbp),%edi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12b7 <unlink+0x147>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
mov    -0x3c(%rbp),%edx
mov    -0x1c(%rbp),%ecx
lea    0xd97(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
call   1060 <syscall@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unlink>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r15
lea    -0x28(%rbp),%rsi
call   1070 <stat@plt>
test   %rax,%rax
je     11c7 <unlink+0x57>
mov    0x2e31(%rip),%rax
mov    (%rax),%ebx
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe52(%rip),%rsi
mov    %ebx,%edi
mov    %r15,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    127b <unlink+0x10b>
mov    %rsp,%r12
mov    -0x28(%rbp),%r14d
mov    %rsp,%rbx
lea    0xf(,%r14,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r14d,%edx
call   1040 <memset@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1246 <unlink+0xd6>
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <write@plt>
cmp    %r14d,%eax
jge    1233 <unlink+0xc3>
mov    0x2dba(%rip),%rcx
mov    (%rcx),%edi
lea    0xe20(%rip),%rsi
mov    %eax,%edx
mov    %r14d,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
mov    %r15,%rsi
call   1060 <syscall@plt>
jmp    1278 <unlink+0x108>
mov    0x2d8b(%rip),%rax
mov    (%rax),%ebx
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdba(%rip),%rsi
mov    %ebx,%edi
mov    %r15,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
mov    %r12,%rsp
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@stdout = global i32 0
@.str = [30 x i8] c"Failed to stat file (%s): %s\0A\00"
@errno = global i32 0
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str.1 = [41 x i8] c"Failed to open file (%s) for writing: %s\00"
@.str.2 = [51 x i8] c"Warning, wrote %d bytes but expected to write %d!\0A\00"
@__NR_unlink = global i32 0
define i32 @unlink(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %3
%11 = load i8*, i8** %3
%12 = call i64 @stat(i8* %11, %struct.stat* %4)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %1
%15 = load i32, i32* @stdout
%16 = load i8*, i8** %3
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %16, i8* %18)
store i32 -1, i32* %2
br label %58
20: ; preds = %1
%21 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%22 = load i32, i32* %21
store i32 %22, i32* %5
%23 = load i32, i32* %5
%24 = zext i32 %23 to i64
%25 = call i8* @llvm.stacksave()
store i8* %25, i8** %6
%26 = alloca i32, i64 %24
store i64 %24, i64* %7
%27 = load i32, i32* %5
%28 = call i32 @memset(i32* %26, i32 0, i32 %27)
%29 = load i8*, i8** %3
%30 = load i32, i32* @O_WRONLY
%31 = call i32 @open(i8* %29, i32 %30)
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %20
%35 = load i32, i32* @stderr
%36 = load i8*, i8** %3
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %36, i8* %38)
store i32 -1, i32* %2
store i32 1, i32* %9
br label %56
40: ; preds = %20
%41 = load i32, i32* %8
%42 = load i32, i32* %5
%43 = call i32 @write(i32 %41, i32* %26, i32 %42)
store i32 %43, i32* %10
%44 = load i32, i32* %10
%45 = load i32, i32* %5
%46 = icmp slt i32 %44, %45
br i1 %46, label %47, label %52
47: ; preds = %40
%48 = load i32, i32* @stderr
%49 = load i32, i32* %10
%50 = load i32, i32* %5
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i32 %49, i32 %50)
br label %52
52: ; preds = %47, %40
%53 = load i32, i32* @__NR_unlink
%54 = load i8*, i8** %3
%55 = call i32 @syscall(i32 %53, i8* %54)
store i32 %55, i32* %2
store i32 1, i32* %9
br label %56
56: ; preds = %52, %34
%57 = load i8*, i8** %6
br label %58
58: ; preds = %56, %14
%59 = load i32, i32* %2
ret i32 %59
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stdout = global i32 0
@.str = [30 x i8] c"Failed to stat file (%s): %s\0A\00"
@errno = global i32 0
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str.1 = [41 x i8] c"Failed to open file (%s) for writing: %s\00"
@.str.2 = [51 x i8] c"Warning, wrote %d bytes but expected to write %d!\0A\00"
@__NR_unlink = global i32 0
define i32 @unlink(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %11, label %6
6: ; preds = %1
%7 = load i32, i32* @stdout
%8 = load i32, i32* @errno
%9 = call i8* @strerror(i32 %8)
%10 = call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %9)
br label %37
11: ; preds = %1
%12 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%13 = load i32, i32* %12
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
%16 = alloca i32, i64 %14
%17 = call i32 @memset(i32* %16, i32 0, i32 %13)
%18 = load i32, i32* @O_WRONLY
%19 = call i32 @open(i8* %0, i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %11
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %24)
br label %35
26: ; preds = %11
%27 = call i32 @write(i32 %19, i32* %16, i32 %13)
%28 = icmp slt i32 %27, %13
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i32 %27, i32 %13)
br label %32
32: ; preds = %29, %26
%33 = load i32, i32* @__NR_unlink
%34 = call i32 @syscall(i32 %33, i8* %0)
br label %35
35: ; preds = %32, %21
%36 = phi i32 [ -1, %21 ], [ %34, %32 ]
br label %37
37: ; preds = %35, %6
%38 = phi i32 [ -1, %6 ], [ %36, %35 ]
ret i32 %38
}