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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ lstat(char const *, struct stat *);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ unlink(char const *);
scalar_t__ writeOver(int, int /*<<< orphan*/, int);
int deleteFile(const char *filepath) {
  int fd = open(filepath, O_RDWR);
  if (fd < 0) {
    fprintf(stderr, "Failed opening file.\n");
    return 2;
  }
  struct stat st = {};
  lstat(filepath, &st);
  if (writeOver(fd, st.st_size, 131072)) {
    fprintf(stderr, "Error overwriting file.\n");
    return 3;
  }
  close(fd);
  unlink(filepath);
  return 0;
}


x86 assembly (clang, -O0):
<deleteFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11bc <deleteFile+0x4c>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
lea    0xe55(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    122c <deleteFile+0xbc>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <lstat@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    $0x20000,%edx
call   1080 <writeOver@plt>
cmp    $0x0,%rax
je     1214 <deleteFile+0xa4>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
lea    0xe13(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x3,-0x4(%rbp)
jmp    122c <deleteFile+0xbc>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<deleteFile>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     11bc <deleteFile+0x5c>
mov    %eax,%ebp
movl   $0x0,(%rsp)
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1050 <lstat@plt>
mov    (%rsp),%esi
mov    %ebp,%edi
mov    $0x20000,%edx
call   1070 <writeOver@plt>
test   %rax,%rax
je     11d8 <deleteFile+0x78>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
lea    0xe66(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x3,%eax
jmp    11e9 <deleteFile+0x89>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
lea    0xe34(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x2,%eax
jmp    11e9 <deleteFile+0x89>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDWR = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"Failed opening file.\0A\00"
@.str.1 = [25 x i8] c"Error overwriting file.\0A\00"
define i32 @deleteFile(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.stat
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* %6, i32 %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %2
br label %31
14: ; preds = %1
%15 = bitcast %struct.stat* %5 to i8*
%16 = load i8*, i8** %3
%17 = call i32 @lstat(i8* %16, %struct.stat* %5)
%18 = load i32, i32* %4
%19 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i64 @writeOver(i32 %18, i32 %20, i32 131072)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %14
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 3, i32* %2
br label %31
26: ; preds = %14
%27 = load i32, i32* %4
%28 = call i32 @close(i32 %27)
%29 = load i8*, i8** %3
%30 = call i32 @unlink(i8* %29)
store i32 0, i32* %2
br label %31
31: ; preds = %26, %23, %11
%32 = load i32, i32* %2
ret i32 %32
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDWR = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"Failed opening file.\0A\00"
@.str.1 = [25 x i8] c"Error overwriting file.\0A\00"
define i32 @deleteFile(i8* nocapture %0) {
%2 = alloca %struct.stat
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %24
9: ; preds = %1
%10 = bitcast %struct.stat* %2 to i8*
%11 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
store i32 0, i32* %11
%12 = call i32 @lstat(i8* %0, %struct.stat* %2)
%13 = load i32, i32* %11
%14 = tail call i64 @writeOver(i32 %4, i32 %13, i32 131072)
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %9
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %22
19: ; preds = %9
%20 = tail call i32 @close(i32 %4)
%21 = tail call i32 @unlink(i8* %0)
br label %22
22: ; preds = %19, %16
%23 = phi i32 [ 3, %16 ], [ 0, %19 ]
br label %24
24: ; preds = %22, %6
%25 = phi i32 [ 2, %6 ], [ %23, %22 ]
ret i32 %25
}