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

/* Variables and functions */
int /*<<< orphan*/ ENOMEM;
int /*<<< orphan*/ ENOTEMPTY;
int /*<<< orphan*/ EXIT_FAILURE;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
scalar_t__ alloca(size_t);
int /*<<< orphan*/ assert(char const *);
scalar_t__ empty_directory(char const *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ isalnum(int);
scalar_t__ lstat(char const *, struct stat *);
int /*<<< orphan*/ perror(char *);
unsigned char random_char();
scalar_t__ rename(char const *, char *);
int rmdir(char *);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
char *strrchr(char *, char);
int /*<<< orphan*/ sync();
int unlink(char *);
int rename_unlink(const char *path) {
  char *new_name, *p;
  struct stat statbuf;
  size_t new_name_size;
  int i = 0;
  assert(path);
  if (lstat(path, &statbuf) < 0)
    return -1;
  if (S_ISDIR(statbuf.st_mode) && (empty_directory(path) < 0)) {
    errno = ENOTEMPTY;
    return -1;
  }
  new_name_size = strlen(path) + 15;
  if ((new_name = (char *)alloca(new_name_size)) == NULL) {
    errno = ENOMEM;
    return -1;
  }
  strncpy(new_name, path, new_name_size);
  if ((p = strrchr(new_name, '/')) != NULL) {
    p++;
    *p = '\0';
  } else {
    p = new_name;
  }
  do {
    i = 0;
    while (i < 14) {
      unsigned char c = random_char();
      if (isalnum((int)c)) {
        p[i] = c;
        i++;
      }
    }
    p[i] = '\0';
  } while (lstat(new_name, &statbuf) == 0);
  if (rename(path, new_name) < 0) {
    perror("could not rename");
    return -1;
  }
  sync();
  if (lstat(new_name, &statbuf) < 0) {
    perror("Fatal error in rename_unlink()");
    exit(EXIT_FAILURE);
  }
  if (S_ISDIR(statbuf.st_mode))
    return rmdir(new_name);
  return unlink(new_name);
}


x86 assembly (clang, -O0):
<rename_unlink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
call   10c0 <assert@plt>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10a0 <lstat@plt>
cmp    $0x0,%rax
jge    123f <rename_unlink+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    141e <rename_unlink+0x21e>
mov    -0x28(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1282 <rename_unlink+0x82>
mov    -0x10(%rbp),%rdi
call   10b0 <empty_directory@plt>
cmp    $0x0,%rax
jge    1282 <rename_unlink+0x82>
mov    0x2d55(%rip),%rax
mov    (%rax),%ecx
mov    0x2d64(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    141e <rename_unlink+0x21e>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
add    $0xf,%eax
cltq
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   10d0 <alloca@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    12c9 <rename_unlink+0xc9>
mov    0x2d2e(%rip),%rax
mov    (%rax),%ecx
mov    0x2d1d(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    141e <rename_unlink+0x21e>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1040 <strncpy@plt>
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1090 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     130e <rename_unlink+0x10e>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
jmp    1316 <rename_unlink+0x116>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    131b <rename_unlink+0x11b>
movl   $0x0,-0x34(%rbp)
cmpl   $0xe,-0x34(%rbp)
jge    1365 <rename_unlink+0x165>
mov    $0x0,%al
call   10e0 <random_char@plt>
mov    %al,-0x35(%rbp)
movzbl -0x35(%rbp),%edi
call   1080 <isalnum@plt>
cmp    $0x0,%rax
je     1360 <rename_unlink+0x160>
mov    -0x35(%rbp),%dl
mov    -0x20(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1322 <rename_unlink+0x122>
mov    -0x20(%rbp),%rax
movslq -0x34(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10a0 <lstat@plt>
cmp    $0x0,%rax
je     131b <rename_unlink+0x11b>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1110 <rename@plt>
cmp    $0x0,%rax
jge    13b7 <rename_unlink+0x1b7>
lea    0xc5a(%rip),%rdi
call   1100 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    141e <rename_unlink+0x21e>
mov    $0x0,%al
call   10f0 <sync@plt>
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10a0 <lstat@plt>
cmp    $0x0,%rax
jge    13ef <rename_unlink+0x1ef>
lea    0xc35(%rip),%rdi
call   1100 <perror@plt>
mov    0x2be8(%rip),%rax
mov    (%rax),%edi
call   1120 <exit@plt>
mov    -0x28(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1412 <rename_unlink+0x212>
mov    -0x18(%rbp),%rdi
call   1050 <rmdir@plt>
mov    %eax,-0x4(%rbp)
jmp    141e <rename_unlink+0x21e>
mov    -0x18(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rename_unlink>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   10c0 <assert@plt>
mov    %rsp,%rsi
mov    %r14,%rdi
call   10a0 <lstat@plt>
test   %rax,%rax
js     12a7 <rename_unlink+0xa7>
mov    (%rsp),%edi
call   1070 <S_ISDIR@plt>
test   %rax,%rax
je     1241 <rename_unlink+0x41>
mov    %r14,%rdi
call   10b0 <empty_directory@plt>
test   %rax,%rax
js     1295 <rename_unlink+0x95>
mov    %r14,%rdi
call   1060 <strlen@plt>
add    $0xf,%eax
movslq %eax,%r15
mov    %r15,%rdi
call   10d0 <alloca@plt>
test   %rax,%rax
je     128c <rename_unlink+0x8c>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <strncpy@plt>
mov    %r12,%rdi
mov    $0x2f,%esi
call   1090 <strrchr@plt>
test   %rax,%rax
je     12bb <rename_unlink+0xbb>
mov    %rax,%r13
add    $0x1,%r13
movb   $0x0,0x1(%rax)
jmp    12be <rename_unlink+0xbe>
mov    0x2d4d(%rip),%rax
jmp    129c <rename_unlink+0x9c>
mov    0x2d24(%rip),%rax
mov    (%rax),%eax
mov    0x2d33(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12,%r13
mov    %rsp,%r15
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   10e0 <random_char@plt>
mov    %eax,%ebx
movzbl %al,%edi
call   1080 <isalnum@plt>
test   %rax,%rax
je     12f1 <rename_unlink+0xf1>
movslq %ebp,%rbp
mov    %bl,0x0(%r13,%rbp,1)
add    $0x1,%ebp
cmp    $0xe,%ebp
jl     12d0 <rename_unlink+0xd0>
mov    %ebp,%eax
movb   $0x0,0x0(%r13,%rax,1)
mov    %r12,%rdi
mov    %r15,%rsi
call   10a0 <lstat@plt>
mov    $0x0,%ebp
test   %rax,%rax
je     12d0 <rename_unlink+0xd0>
mov    %r14,%rdi
mov    %r12,%rsi
call   1110 <rename@plt>
test   %rax,%rax
js     136c <rename_unlink+0x16c>
xor    %eax,%eax
call   10f0 <sync@plt>
mov    %rsp,%rsi
mov    %r12,%rdi
call   10a0 <lstat@plt>
test   %rax,%rax
js     137d <rename_unlink+0x17d>
mov    (%rsp),%edi
call   1070 <S_ISDIR@plt>
mov    %r12,%rdi
add    $0x8,%rsp
test   %rax,%rax
je     135d <rename_unlink+0x15d>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <rmdir@plt>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <unlink@plt>
lea    0xc8d(%rip),%rdi
call   1100 <perror@plt>
jmp    12a7 <rename_unlink+0xa7>
lea    0xc8d(%rip),%rdi
call   1100 <perror@plt>
mov    0x2c40(%rip),%rax
mov    (%rax),%edi
call   1120 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@ENOTEMPTY = global i32 0
@errno = global i32 0
@ENOMEM = global i32 0
@.str = [17 x i8] c"could not rename\00"
@.str.1 = [31 x i8] c"Fatal error in rename_unlink()\00"
@EXIT_FAILURE = global i32 0
define i32 @rename_unlink(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.stat
%7 = alloca i64
%8 = alloca i32
%9 = alloca i8
store i8* %0, i8** %3
store i32 0, i32* %8
%10 = load i8*, i8** %3
%11 = call i32 @assert(i8* %10)
%12 = load i8*, i8** %3
%13 = call i64 @lstat(i8* %12, %struct.stat* %6)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %1
store i32 -1, i32* %2
br label %108
16: ; preds = %1
%17 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%18 = load i32, i32* %17
%19 = call i64 @S_ISDIR(i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %16
%22 = load i8*, i8** %3
%23 = call i64 @empty_directory(i8* %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = load i32, i32* @ENOTEMPTY
store i32 %26, i32* @errno
store i32 -1, i32* %2
br label %108
27: ; preds = %21, %16
%28 = load i8*, i8** %3
%29 = call i32 @strlen(i8* %28)
%30 = add nsw i32 %29, 15
%31 = sext i32 %30 to i64
store i64 %31, i64* %7
%32 = load i64, i64* %7
%33 = call i64 @alloca(i64 %32)
%34 = inttoptr i64 %33 to i8*
store i8* %34, i8** %4
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %38
36: ; preds = %27
%37 = load i32, i32* @ENOMEM
store i32 %37, i32* @errno
store i32 -1, i32* %2
br label %108
38: ; preds = %27
%39 = load i8*, i8** %4
%40 = load i8*, i8** %3
%41 = load i64, i64* %7
%42 = call i32 @strncpy(i8* %39, i8* %40, i64 %41)
%43 = load i8*, i8** %4
%44 = call i8* @strrchr(i8* %43, i8 signext 47)
store i8* %44, i8** %5
%45 = icmp ne i8* %44, null
br i1 %45, label %46, label %50
46: ; preds = %38
%47 = load i8*, i8** %5
%48 = getelementptr inbounds i8, i8* %47, i32 1
store i8* %48, i8** %5
%49 = load i8*, i8** %5
store i8 0, i8* %49
br label %52
50: ; preds = %38
%51 = load i8*, i8** %4
store i8* %51, i8** %5
br label %52
52: ; preds = %50, %46
br label %53
53: ; preds = %77, %52
store i32 0, i32* %8
br label %54
54: ; preds = %71, %53
%55 = load i32, i32* %8
%56 = icmp slt i32 %55, 14
br i1 %56, label %57, label %72
57: ; preds = %54
%58 = call zeroext i8 (...) @random_char()
store i8 %58, i8* %9
%59 = load i8, i8* %9
%60 = zext i8 %59 to i32
%61 = call i64 @isalnum(i32 %60)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %71
63: ; preds = %57
%64 = load i8, i8* %9
%65 = load i8*, i8** %5
%66 = load i32, i32* %8
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds i8, i8* %65, i64 %67
store i8 %64, i8* %68
%69 = load i32, i32* %8
%70 = add nsw i32 %69, 1
store i32 %70, i32* %8
br label %71
71: ; preds = %63, %57
br label %54
72: ; preds = %54
%73 = load i8*, i8** %5
%74 = load i32, i32* %8
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i8, i8* %73, i64 %75
store i8 0, i8* %76
br label %77
77: ; preds = %72
%78 = load i8*, i8** %4
%79 = call i64 @lstat(i8* %78, %struct.stat* %6)
%80 = icmp eq i64 %79, 0
br i1 %80, label %53, label %81
81: ; preds = %77
%82 = load i8*, i8** %3
%83 = load i8*, i8** %4
%84 = call i64 @rename(i8* %82, i8* %83)
%85 = icmp slt i64 %84, 0
br i1 %85, label %86, label %88
86: ; preds = %81
%87 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %108
88: ; preds = %81
%89 = call i32 (...) @sync()
%90 = load i8*, i8** %4
%91 = call i64 @lstat(i8* %90, %struct.stat* %6)
%92 = icmp slt i64 %91, 0
br i1 %92, label %93, label %97
93: ; preds = %88
%94 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%95 = load i32, i32* @EXIT_FAILURE
%96 = call i32 @exit(i32 %95)
unreachable
97: ; preds = %88
%98 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%99 = load i32, i32* %98
%100 = call i64 @S_ISDIR(i32 %99)
%101 = icmp ne i64 %100, 0
br i1 %101, label %102, label %105
102: ; preds = %97
%103 = load i8*, i8** %4
%104 = call i32 @rmdir(i8* %103)
store i32 %104, i32* %2
br label %108
105: ; preds = %97
%106 = load i8*, i8** %4
%107 = call i32 @unlink(i8* %106)
store i32 %107, i32* %2
br label %108
108: ; preds = %105, %102, %86, %36, %25, %15
%109 = load i32, i32* %2
ret i32 %109
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@ENOTEMPTY = global i32 0
@errno = global i32 0
@ENOMEM = global i32 0
@.str = [17 x i8] c"could not rename\00"
@.str.1 = [31 x i8] c"Fatal error in rename_unlink()\00"
@EXIT_FAILURE = global i32 0
define i32 @rename_unlink(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = tail call i32 @assert(i8* %0)
%5 = call i64 @lstat(i8* %0, %struct.stat* %2)
%6 = icmp slt i64 %5, 0
br i1 %6, label %75, label %7
7: ; preds = %1
%8 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%9 = load i32, i32* %8
%10 = tail call i64 @S_ISDIR(i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %7
%13 = tail call i64 @empty_directory(i8* %0)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = load i32, i32* @ENOTEMPTY
store i32 %16, i32* @errno
br label %75
17: ; preds = %12, %7
%18 = tail call i32 @strlen(i8* %0)
%19 = add nsw i32 %18, 15
%20 = sext i32 %19 to i64
%21 = tail call i64 @alloca(i64 %20)
%22 = inttoptr i64 %21 to i8*
%23 = icmp eq i64 %21, 0
br i1 %23, label %24, label %26
24: ; preds = %17
%25 = load i32, i32* @ENOMEM
store i32 %25, i32* @errno
br label %75
26: ; preds = %17
%27 = tail call i32 @strncpy(i8* %22, i8* %0, i64 %20)
%28 = tail call i8* @strrchr(i8* %22, i8 signext 47)
%29 = icmp eq i8* %28, null
br i1 %29, label %32, label %30
30: ; preds = %26
%31 = getelementptr inbounds i8, i8* %28, i64 1
store i8 0, i8* %31
br label %32
32: ; preds = %26, %30
%33 = phi i8* [ %31, %30 ], [ %22, %26 ]
br label %34
34: ; preds = %47, %32
%35 = phi i32 [ 0, %32 ], [ %48, %47 ]
%36 = tail call zeroext i8 (...) @random_char()
%37 = zext i8 %36 to i32
%38 = tail call i64 @isalnum(i32 %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %44, label %40
40: ; preds = %34
%41 = sext i32 %35 to i64
%42 = getelementptr inbounds i8, i8* %33, i64 %41
store i8 %36, i8* %42
%43 = add nsw i32 %35, 1
br label %44
44: ; preds = %40, %34
%45 = phi i32 [ %43, %40 ], [ %35, %34 ]
%46 = icmp slt i32 %45, 14
br i1 %46, label %47, label %49
47: ; preds = %44, %49
%48 = phi i32 [ %45, %44 ], [ 0, %49 ]
br label %34
49: ; preds = %44
%50 = zext i32 %45 to i64
%51 = getelementptr inbounds i8, i8* %33, i64 %50
store i8 0, i8* %51
%52 = call i64 @lstat(i8* %22, %struct.stat* %2)
%53 = icmp eq i64 %52, 0
br i1 %53, label %47, label %54
54: ; preds = %49
%55 = tail call i64 @rename(i8* %0, i8* %22)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %54
%58 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %75
59: ; preds = %54
%60 = tail call i32 (...) @sync()
%61 = call i64 @lstat(i8* %22, %struct.stat* %2)
%62 = icmp slt i64 %61, 0
br i1 %62, label %63, label %67
63: ; preds = %59
%64 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%65 = load i32, i32* @EXIT_FAILURE
%66 = tail call i32 @exit(i32 %65)
unreachable
67: ; preds = %59
%68 = load i32, i32* %8
%69 = tail call i64 @S_ISDIR(i32 %68)
%70 = icmp eq i64 %69, 0
br i1 %70, label %73, label %71
71: ; preds = %67
%72 = tail call i32 @rmdir(i8* %22)
br label %75
73: ; preds = %67
%74 = tail call i32 @unlink(i8* %22)
br label %75
75: ; preds = %1, %73, %71, %57, %24, %15
%76 = phi i32 [ -1, %15 ], [ -1, %24 ], [ -1, %57 ], [ %72, %71 ], [ %74, %73 ], [ -1, %1 ]
ret i32 %76
}