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

/* Variables and functions */
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ __sdel_random_filename(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int lstat(char *, struct stat *);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int rename(char *, char *);
int rmdir(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strlen(char *);
int unlink(char *);
int sdel_unlink(char *filename, int directory, int truncate, int slow) {
  int fd;
  int turn = 0;
  int result;
  char newname[strlen(filename) + 1];
  struct stat filestat;
  if (!directory && truncate)
    if ((fd = open(filename, O_WRONLY | O_TRUNC | slow)) >= 0)
      close(fd);
  strcpy(newname, filename);
  do {
    __sdel_random_filename(newname);
    if ((result = lstat(newname, &filestat)) >= 0)
      turn++;
  } while ((result >= 0) && (turn <= 100));
  if (turn <= 100) {
    result = rename(filename, newname);
    if (result != 0) {
      fprintf(stderr, "Warning: Couldn't rename %s - ", filename);
      perror("");
      strcpy(newname, filename);
    }
  } else {
    fprintf(stderr, "Warning: Couldn't find a free filename for %s!\n",
            filename);
    strcpy(newname, filename);
  }
  if (directory) {
    result = rmdir(newname);
    if (result) {
      printf("Warning: Unable to remove directory %s - ", filename);
      perror("");
      (void)rename(newname, filename);
    }
  } else {
    result = unlink(newname);
    if (result) {
      printf("Warning: Unable to unlink file %s - ", filename);
      perror("");
      (void)rename(newname, filename);
    }
  }
  if (result != 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<sdel_unlink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <strlen@plt>
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1256 <sdel_unlink+0x96>
cmpl   $0x0,-0x18(%rbp)
je     1256 <sdel_unlink+0x96>
mov    -0x10(%rbp),%rdi
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
mov    0x2dc5(%rip),%rax
or     (%rax),%esi
or     -0x1c(%rbp),%esi
call   10c0 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jl     1251 <sdel_unlink+0x91>
mov    -0x20(%rbp),%edi
call   1090 <close@plt>
jmp    1256 <sdel_unlink+0x96>
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x50(%rbp),%rdi
call   1050 <__sdel_random_filename@plt>
mov    -0x50(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   10a0 <lstat@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jl     128e <sdel_unlink+0xce>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1293 <sdel_unlink+0xd3>
xor    %eax,%eax
cmpl   $0x0,-0x28(%rbp)
mov    %al,-0x51(%rbp)
jl     12ac <sdel_unlink+0xec>
cmpl   $0x64,-0x24(%rbp)
setle  %al
mov    %al,-0x51(%rbp)
mov    -0x51(%rbp),%al
test   $0x1,%al
jne    1263 <sdel_unlink+0xa3>
cmpl   $0x64,-0x24(%rbp)
jg     1312 <sdel_unlink+0x152>
mov    -0x50(%rbp),%rsi
mov    -0x10(%rbp),%rdi
call   10e0 <rename@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     130d <sdel_unlink+0x14d>
mov    0x2cf6(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd11(%rip),%rsi
call   10b0 <fprintf@plt>
lea    0xd53(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    1338 <sdel_unlink+0x178>
mov    0x2cbf(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xcf9(%rip),%rsi
call   10b0 <fprintf@plt>
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
cmpl   $0x0,-0x14(%rbp)
je     1386 <sdel_unlink+0x1c6>
mov    -0x50(%rbp),%rdi
call   1060 <rmdir@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     1381 <sdel_unlink+0x1c1>
mov    -0x10(%rbp),%rsi
lea    0xcec(%rip),%rdi
call   1080 <printf@plt>
lea    0xcdf(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10e0 <rename@plt>
jmp    13ca <sdel_unlink+0x20a>
mov    -0x50(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     13c5 <sdel_unlink+0x205>
mov    -0x10(%rbp),%rsi
lea    0xcd2(%rip),%rdi
call   1080 <printf@plt>
lea    0xc9b(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10e0 <rename@plt>
jmp    13ca <sdel_unlink+0x20a>
cmpl   $0x0,-0x28(%rbp)
je     13e7 <sdel_unlink+0x227>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    13f5 <sdel_unlink+0x235>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sdel_unlink>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r12d
mov    %edx,%r13d
mov    %esi,%ebx
mov    %rdi,%r14
call   1070 <strlen@plt>
add    $0x1,%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    %ebx,-0x2c(%rbp)
test   %ebx,%ebx
jne    1228 <sdel_unlink+0x68>
test   %r13d,%r13d
je     1228 <sdel_unlink+0x68>
mov    0x2dc3(%rip),%rax
mov    0x2dec(%rip),%rcx
or     (%rax),%r12d
or     (%rcx),%r12d
mov    %r14,%rdi
mov    %r12d,%esi
call   10c0 <open@plt>
test   %eax,%eax
js     1228 <sdel_unlink+0x68>
mov    %eax,%edi
call   1090 <close@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
xor    %ecx,%ecx
lea    -0x30(%rbp),%r12
nopl   0x0(%rax)
mov    %ecx,%ebx
mov    %r15,%rdi
call   1050 <__sdel_random_filename@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   10a0 <lstat@plt>
mov    %eax,%ecx
not    %ecx
shr    $0x1f,%ecx
add    %ebx,%ecx
test   %eax,%eax
js     1267 <sdel_unlink+0xa7>
cmp    $0x65,%ecx
jb     1240 <sdel_unlink+0x80>
cmp    $0x64,%ecx
ja     129a <sdel_unlink+0xda>
mov    %r14,%rdi
mov    %r15,%rsi
call   10e0 <rename@plt>
test   %eax,%eax
jne    12e2 <sdel_unlink+0x122>
cmpl   $0x0,-0x2c(%rbp)
je     12c3 <sdel_unlink+0x103>
mov    %r15,%rdi
call   1060 <rmdir@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
je     12d3 <sdel_unlink+0x113>
lea    0xdb7(%rip),%rdi
jmp    130f <sdel_unlink+0x14f>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
lea    0xd75(%rip),%rsi
mov    %r14,%rdx
call   10b0 <fprintf@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
cmpl   $0x0,-0x2c(%rbp)
jne    1281 <sdel_unlink+0xc1>
mov    %r15,%rdi
call   1030 <unlink@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jne    1308 <sdel_unlink+0x148>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
lea    0xd0e(%rip),%rsi
mov    %r14,%rdx
call   10b0 <fprintf@plt>
lea    0xd4d(%rip),%rdi
call   10d0 <perror@plt>
jmp    12b2 <sdel_unlink+0xf2>
lea    0xd6a(%rip),%rdi
mov    %r14,%rsi
call   1080 <printf@plt>
lea    0xd30(%rip),%rdi
call   10d0 <perror@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   10e0 <rename@plt>
mov    $0xffffffff,%eax
jmp    12d3 <sdel_unlink+0x113>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"Warning: Couldn't rename %s - \00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [48 x i8] c"Warning: Couldn't find a free filename for %s!\0A\00"
@.str.3 = [42 x i8] c"Warning: Unable to remove directory %s - \00"
@.str.4 = [37 x i8] c"Warning: Unable to unlink file %s - \00"
define i32 @sdel_unlink(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i64
%15 = alloca %struct.stat
%16 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 0, i32* %11
%17 = load i8*, i8** %6
%18 = call i32 @strlen(i8* %17)
%19 = add nsw i32 %18, 1
%20 = zext i32 %19 to i64
%21 = call i8* @llvm.stacksave()
store i8* %21, i8** %13
%22 = alloca i8, i64 %20
store i64 %20, i64* %14
%23 = load i32, i32* %7
%24 = icmp ne i32 %23, 0
br i1 %24, label %41, label %25
25: ; preds = %4
%26 = load i32, i32* %8
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %41
28: ; preds = %25
%29 = load i8*, i8** %6
%30 = load i32, i32* @O_WRONLY
%31 = load i32, i32* @O_TRUNC
%32 = or i32 %30, %31
%33 = load i32, i32* %9
%34 = or i32 %32, %33
%35 = call i32 @open(i8* %29, i32 %34)
store i32 %35, i32* %10
%36 = icmp sge i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %28
%38 = load i32, i32* %10
%39 = call i32 @close(i32 %38)
br label %40
40: ; preds = %37, %28
br label %41
41: ; preds = %40, %25, %4
%42 = load i8*, i8** %6
%43 = call i32 @strcpy(i8* %22, i8* %42)
br label %44
44: ; preds = %58, %41
%45 = call i32 @__sdel_random_filename(i8* %22)
%46 = call i32 @lstat(i8* %22, %struct.stat* %15)
store i32 %46, i32* %12
%47 = icmp sge i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = load i32, i32* %11
%50 = add nsw i32 %49, 1
store i32 %50, i32* %11
br label %51
51: ; preds = %48, %44
br label %52
52: ; preds = %51
%53 = load i32, i32* %12
%54 = icmp sge i32 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %52
%56 = load i32, i32* %11
%57 = icmp sle i32 %56, 100
br label %58
58: ; preds = %55, %52
%59 = phi i1 [ false, %52 ], [ %57, %55 ]
br i1 %59, label %44, label %60
60: ; preds = %58
%61 = load i32, i32* %11
%62 = icmp sle i32 %61, 100
br i1 %62, label %63, label %76
63: ; preds = %60
%64 = load i8*, i8** %6
%65 = call i32 @rename(i8* %64, i8* %22)
store i32 %65, i32* %12
%66 = load i32, i32* %12
%67 = icmp ne i32 %66, 0
br i1 %67, label %68, label %75
68: ; preds = %63
%69 = load i32, i32* @stderr
%70 = load i8*, i8** %6
%71 = call i32 @fprintf(i32 %69, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %70)
%72 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
%73 = load i8*, i8** %6
%74 = call i32 @strcpy(i8* %22, i8* %73)
br label %75
75: ; preds = %68, %63
br label %82
76: ; preds = %60
%77 = load i32, i32* @stderr
%78 = load i8*, i8** %6
%79 = call i32 @fprintf(i32 %77, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i8* %78)
%80 = load i8*, i8** %6
%81 = call i32 @strcpy(i8* %22, i8* %80)
br label %82
82: ; preds = %76, %75
%83 = load i32, i32* %7
%84 = icmp ne i32 %83, 0
br i1 %84, label %85, label %96
85: ; preds = %82
%86 = call i32 @rmdir(i8* %22)
store i32 %86, i32* %12
%87 = load i32, i32* %12
%88 = icmp ne i32 %87, 0
br i1 %88, label %89, label %95
89: ; preds = %85
%90 = load i8*, i8** %6
%91 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i8* %90)
%92 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
%93 = load i8*, i8** %6
%94 = call i32 @rename(i8* %22, i8* %93)
br label %95
95: ; preds = %89, %85
br label %107
96: ; preds = %82
%97 = call i32 @unlink(i8* %22)
store i32 %97, i32* %12
%98 = load i32, i32* %12
%99 = icmp ne i32 %98, 0
br i1 %99, label %100, label %106
100: ; preds = %96
%101 = load i8*, i8** %6
%102 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i8* %101)
%103 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
%104 = load i8*, i8** %6
%105 = call i32 @rename(i8* %22, i8* %104)
br label %106
106: ; preds = %100, %96
br label %107
107: ; preds = %106, %95
%108 = load i32, i32* %12
%109 = icmp ne i32 %108, 0
br i1 %109, label %110, label %111
110: ; preds = %107
store i32 -1, i32* %5
store i32 1, i32* %16
br label %112
111: ; preds = %107
store i32 0, i32* %5
store i32 1, i32* %16
br label %112
112: ; preds = %111, %110
%113 = load i8*, i8** %13
%114 = load i32, i32* %5
ret i32 %114
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"Warning: Couldn't rename %s - \00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [48 x i8] c"Warning: Couldn't find a free filename for %s!\0A\00"
@.str.3 = [42 x i8] c"Warning: Unable to remove directory %s - \00"
@.str.4 = [37 x i8] c"Warning: Unable to unlink file %s - \00"
define i32 @sdel_unlink(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct.stat
%6 = tail call i32 @strlen(i8* %0)
%7 = add nsw i32 %6, 1
%8 = zext i32 %7 to i64
%9 = alloca i8, i64 %8
%10 = bitcast %struct.stat* %5 to i8*
%11 = icmp eq i32 %1, 0
%12 = icmp ne i32 %2, 0
%13 = and i1 %11, %12
br i1 %13, label %14, label %23
14: ; preds = %4
%15 = load i32, i32* @O_WRONLY
%16 = load i32, i32* @O_TRUNC
%17 = or i32 %15, %3
%18 = or i32 %17, %16
%19 = tail call i32 @open(i8* %0, i32 %18)
%20 = icmp sgt i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = tail call i32 @close(i32 %19)
br label %23
23: ; preds = %14, %21, %4
%24 = call i32 @strcpy(i8* %9, i8* %0)
br label %25
25: ; preds = %25, %23
%26 = phi i32 [ 0, %23 ], [ %32, %25 ]
%27 = call i32 @__sdel_random_filename(i8* %9)
%28 = call i32 @lstat(i8* %9, %struct.stat* %5)
%29 = icmp sgt i32 %28, -1
%30 = xor i32 %28, -1
%31 = lshr i32 %30, 31
%32 = add nuw nsw i32 %31, %26
%33 = icmp ult i32 %32, 101
%34 = select i1 %29, i1 %33, i1 false
br i1 %34, label %25, label %35
35: ; preds = %25
br i1 %33, label %36, label %43
36: ; preds = %35
%37 = call i32 @rename(i8* %0, i8* %9)
%38 = icmp eq i32 %37, 0
br i1 %38, label %48, label %39
39: ; preds = %36
%40 = load i32, i32* @stderr
%41 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %0)
%42 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %46
43: ; preds = %35
%44 = load i32, i32* @stderr
%45 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %46
46: ; preds = %43, %39
%47 = call i32 @strcpy(i8* %9, i8* %0)
br label %48
48: ; preds = %46, %36
br i1 %11, label %52, label %49
49: ; preds = %48
%50 = call i32 @rmdir(i8* %9)
%51 = icmp eq i32 %50, 0
br i1 %51, label %60, label %55
52: ; preds = %48
%53 = call i32 @unlink(i8* %9)
%54 = icmp eq i32 %53, 0
br i1 %54, label %60, label %55
55: ; preds = %52, %49
%56 = phi i8* [ getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), %49 ], [ getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), %52 ]
%57 = call i32 @printf(i8* %56, i8* %0)
%58 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
%59 = call i32 @rename(i8* %9, i8* %0)
br label %60
60: ; preds = %55, %49, %52
%61 = phi i32 [ 0, %52 ], [ 0, %49 ], [ -1, %55 ]
ret i32 %61
}