#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;
};
typedef int ssize_t;
typedef int off_t;

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(void *);
void *malloc(int);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ remove(char *);
int stat(char *, struct stat *);
int write(int, void *, int);
void shred(char *path) {
  struct stat stat_buf;
  if (stat(path, &stat_buf) == -1)
    return;
  off_t fsize = stat_buf.st_size;
  int fd = open(path, O_WRONLY);
  if (fd == -1)
    return;
  void *buf = malloc(4096);
  memset(buf, 0, 4096);
  ssize_t ret = 0;
  off_t shift = 0;
  while ((ret = write(fd, buf,
                      ((fsize - shift > 4096) ? 4096 : (fsize - shift)))) > 0)
    shift += ret;
  close(fd);
  free(buf);
  if (ret == -1)
    return;
  remove(path);
}


x86 assembly (clang, -O0):
<shred>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
jne    11a7 <shred+0x27>
jmp    1284 <shred+0x104>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11d1 <shred+0x51>
jmp    1284 <shred+0x104>
mov    $0x1000,%edi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1060 <memset@plt>
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
sub    -0x28(%rbp),%eax
cmp    $0x1000,%eax
jle    1229 <shred+0xa9>
mov    $0x1000,%eax
mov    %eax,-0x38(%rbp)
jmp    1232 <shred+0xb2>
mov    -0x14(%rbp),%eax
sub    -0x28(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%edi
mov    -0x38(%rbp),%edx
call   1050 <write@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jle    125b <shred+0xdb>
mov    -0x24(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    11fd <shred+0x7d>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0xffffffff,-0x24(%rbp)
jne    127b <shred+0xfb>
jmp    1284 <shred+0x104>
mov    -0x8(%rbp),%rdi
call   1040 <remove@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shred>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
je     1241 <shred+0xc1>
mov    0x8(%rsp),%r12d
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     1241 <shred+0xc1>
mov    %eax,%r15d
mov    %rbx,0x10(%rsp)
mov    $0x1000,%r13d
mov    $0x1000,%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
xor    %r14d,%r14d
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1060 <memset@plt>
mov    %r12d,%edx
sub    %r14d,%edx
cmp    $0x1000,%edx
cmovge %r13d,%edx
mov    %r15d,%edi
mov    %rbx,%rsi
call   1050 <write@plt>
add    %eax,%r14d
test   %eax,%eax
jg     11f0 <shred+0x70>
mov    %eax,%ebp
mov    %r15d,%edi
call   1070 <close@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
cmp    $0xffffffff,%ebp
mov    0x10(%rsp),%rdi
je     1241 <shred+0xc1>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <remove@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_WRONLY = global i32 0
define void @shred(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.stat
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %2
%9 = load i8*, i8** %2
%10 = call i32 @stat(i8* %9, %struct.stat* %3)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %1
br label %57
13: ; preds = %1
%14 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%15 = load i32, i32* %14
store i32 %15, i32* %4
%16 = load i8*, i8** %2
%17 = load i32, i32* @O_WRONLY
%18 = call i32 @open(i8* %16, i32 %17)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %22
21: ; preds = %13
br label %57
22: ; preds = %13
%23 = call i8* @malloc(i32 4096)
store i8* %23, i8** %6
%24 = load i8*, i8** %6
%25 = call i32 @memset(i8* %24, i32 0, i32 4096)
store i32 0, i32* %7
store i32 0, i32* %8
br label %26
26: ; preds = %42, %22
%27 = load i32, i32* %5
%28 = load i8*, i8** %6
%29 = load i32, i32* %4
%30 = load i32, i32* %8
%31 = sub nsw i32 %29, %30
%32 = icmp sgt i32 %31, 4096
br i1 %32, label %33, label %34
33: ; preds = %26
br label %38
34: ; preds = %26
%35 = load i32, i32* %4
%36 = load i32, i32* %8
%37 = sub nsw i32 %35, %36
br label %38
38: ; preds = %34, %33
%39 = phi i32 [ 4096, %33 ], [ %37, %34 ]
%40 = call i32 @write(i32 %27, i8* %28, i32 %39)
store i32 %40, i32* %7
%41 = icmp sgt i32 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %38
%43 = load i32, i32* %7
%44 = load i32, i32* %8
%45 = add nsw i32 %44, %43
store i32 %45, i32* %8
br label %26
46: ; preds = %38
%47 = load i32, i32* %5
%48 = call i32 @close(i32 %47)
%49 = load i8*, i8** %6
%50 = call i32 @free(i8* %49)
%51 = load i32, i32* %7
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %54
53: ; preds = %46
br label %57
54: ; preds = %46
%55 = load i8*, i8** %2
%56 = call i32 @remove(i8* %55)
br label %57
57: ; preds = %54, %53, %21, %12
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_WRONLY = global i32 0
define void @shred(i8* nocapture %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i32 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i32 %4, -1
br i1 %5, label %29, label %6
6: ; preds = %1
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%8 = load i32, i32* %7
%9 = load i32, i32* @O_WRONLY
%10 = tail call i32 @open(i8* %0, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %29, label %12
12: ; preds = %6
%13 = tail call dereferenceable_or_null(4096) i8* @malloc(i32 4096)
%14 = tail call i32 @memset(i8* %13, i32 0, i32 4096)
br label %15
15: ; preds = %15, %12
%16 = phi i32 [ 0, %12 ], [ %22, %15 ]
%17 = sub nsw i32 %8, %16
%18 = icmp slt i32 %17, 4096
%19 = select i1 %18, i32 %17, i32 4096
%20 = tail call i32 @write(i32 %10, i8* %13, i32 %19)
%21 = icmp sgt i32 %20, 0
%22 = add nsw i32 %20, %16
br i1 %21, label %15, label %23
23: ; preds = %15
%24 = tail call i32 @close(i32 %10)
%25 = tail call i32 @free(i8* %13)
%26 = icmp eq i32 %20, -1
br i1 %26, label %29, label %27
27: ; preds = %23
%28 = tail call i32 @remove(i8* %0)
br label %29
29: ; preds = %6, %23, %27, %1
ret void
}