#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;
};
typedef int /*<<< orphan*/ single_pats;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int fstat(int, struct stat *);
int /*<<< orphan*/ memset(unsigned char *, unsigned char, int);
int pattern_pass(int, unsigned char *, int, int /*<<< orphan*/);
int spc_fd_wipe(int fd) {
  int count, i, pass, patternsz;
  struct stat st;
  unsigned char buf[4096], *pattern;
  static unsigned char single_pats[1] = {0xff};
  if (fstat(fd, &st) == -1)
    return -1;
  if (!st.st_size)
    return 0;
  for (pass = 0; pass < sizeof(single_pats); pass++) {
    memset(buf, single_pats[pass], sizeof(buf));
    if (pattern_pass(fd, buf, sizeof(buf), st.st_size) == -1)
      return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<spc_fd_wipe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1050 <fstat@plt>
cmp    $0xffffffff,%eax
jne    115f <spc_fd_wipe+0x2f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f2 <spc_fd_wipe+0xc2>
cmpl   $0x0,-0x20(%rbp)
jne    1175 <spc_fd_wipe+0x45>
movl   $0x0,-0x4(%rbp)
jmp    11f2 <spc_fd_wipe+0xc2>
movl   $0x0,-0x14(%rbp)
movslq -0x14(%rbp),%rax
cmp    $0x1,%rax
jae    11eb <spc_fd_wipe+0xbb>
lea    -0x1020(%rbp),%rdi
movslq -0x14(%rbp),%rcx
lea    0x2e9c(%rip),%rax
mov    (%rax,%rcx,1),%al
movzbl %al,%esi
mov    $0x1000,%edx
call   1030 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x20(%rbp),%ecx
mov    $0x1000,%edx
call   1040 <pattern_pass@plt>
cmp    $0xffffffff,%eax
jne    11d8 <spc_fd_wipe+0xa8>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f2 <spc_fd_wipe+0xc2>
jmp    11dd <spc_fd_wipe+0xad>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    117c <spc_fd_wipe+0x4c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spc_fd_wipe>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1010,%rsp
mov    %edi,%ebx
lea    0x8(%rsp),%rsi
call   1050 <fstat@plt>
cmp    $0xffffffff,%eax
je     118a <spc_fd_wipe+0x5a>
mov    0x8(%rsp),%ebp
test   %ebp,%ebp
je     1191 <spc_fd_wipe+0x61>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0xff,%esi
mov    $0x1000,%edx
call   1030 <memset@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
mov    %ebp,%ecx
call   1040 <pattern_pass@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
sete   %al
neg    %eax
jmp    1193 <spc_fd_wipe+0x63>
mov    $0xffffffff,%eax
jmp    1193 <spc_fd_wipe+0x63>
xor    %eax,%eax
add    $0x1010,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@spc_fd_wipe.single_pats = internal global [1 x i8] c"\FF"
define i32 @spc_fd_wipe(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca [4096 x i8]
%10 = alloca i8*
store i32 %0, i32* %3
%11 = load i32, i32* %3
%12 = call i32 @fstat(i32 %11, %struct.stat* %8)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %1
store i32 -1, i32* %2
br label %44
15: ; preds = %1
%16 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%17 = load i32, i32* %16
%18 = icmp ne i32 %17, 0
br i1 %18, label %20, label %19
19: ; preds = %15
store i32 0, i32* %2
br label %44
20: ; preds = %15
store i32 0, i32* %6
br label %21
21: ; preds = %40, %20
%22 = load i32, i32* %6
%23 = sext i32 %22 to i64
%24 = icmp ult i64 %23, 1
br i1 %24, label %25, label %43
25: ; preds = %21
%26 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%27 = load i32, i32* %6
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [1 x i8], [1 x i8]* @spc_fd_wipe.single_pats, i64 0, i64 %28
%30 = load i8, i8* %29
%31 = call i32 @memset(i8* %26, i8 zeroext %30, i32 4096)
%32 = load i32, i32* %3
%33 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%34 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @pattern_pass(i32 %32, i8* %33, i32 4096, i32 %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %39
38: ; preds = %25
store i32 -1, i32* %2
br label %44
39: ; preds = %25
br label %40
40: ; preds = %39
%41 = load i32, i32* %6
%42 = add nsw i32 %41, 1
store i32 %42, i32* %6
br label %21
43: ; preds = %21
store i32 0, i32* %2
br label %44
44: ; preds = %43, %38, %19, %14
%45 = load i32, i32* %2
ret i32 %45
}

LLVM IR (-O2):
%struct.stat = type { i32 }
define i32 @spc_fd_wipe(i32 %0) {
%2 = alloca %struct.stat
%3 = alloca [4096 x i8]
%4 = bitcast %struct.stat* %2 to i8*
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%6 = call i32 @fstat(i32 %0, %struct.stat* %2)
%7 = icmp eq i32 %6, -1
br i1 %7, label %17, label %8
8: ; preds = %1
%9 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%10 = load i32, i32* %9
%11 = icmp eq i32 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %8
%13 = call i32 @memset(i8* %5, i8 zeroext -1, i32 4096)
%14 = call i32 @pattern_pass(i32 %0, i8* %5, i32 4096, i32 %10)
%15 = icmp eq i32 %14, -1
%16 = sext i1 %15 to i32
br label %17
17: ; preds = %12, %8, %1
%18 = phi i32 [ -1, %1 ], [ 0, %8 ], [ %16, %12 ]
ret i32 %18
}