#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*/ EINVAL;
int /*<<< orphan*/ ENOMEM;
int /*<<< orphan*/ ENOTEMPTY;
int /*<<< orphan*/ EXIT_FAILURE;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
scalar_t__ alloca(size_t);
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;
  if (!path) {
    errno = EINVAL;
    return -1;
  }
  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)
    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)
cmpq   $0x0,-0x10(%rbp)
jne    122c <rename_unlink+0x3c>
mov    0x2db3(%rip),%rax
mov    (%rax),%ecx
mov    0x2dba(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1422 <rename_unlink+0x232>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10a0 <lstat@plt>
cmp    $0x0,%rax
jge    124f <rename_unlink+0x5f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1422 <rename_unlink+0x232>
mov    -0x28(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1292 <rename_unlink+0xa2>
mov    -0x10(%rbp),%rdi
call   10b0 <empty_directory@plt>
cmp    $0x0,%rax
jge    1292 <rename_unlink+0xa2>
mov    0x2d3d(%rip),%rax
mov    (%rax),%ecx
mov    0x2d54(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1422 <rename_unlink+0x232>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
add    $0xf,%eax
cltq
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   10c0 <alloca@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    12d9 <rename_unlink+0xe9>
mov    0x2d1e(%rip),%rax
mov    (%rax),%ecx
mov    0x2d0d(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1422 <rename_unlink+0x232>
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     131e <rename_unlink+0x12e>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
jmp    1326 <rename_unlink+0x136>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    132b <rename_unlink+0x13b>
movl   $0x0,-0x34(%rbp)
cmpl   $0xe,-0x34(%rbp)
jge    1375 <rename_unlink+0x185>
mov    $0x0,%al
call   10d0 <random_char@plt>
mov    %al,-0x35(%rbp)
movzbl -0x35(%rbp),%edi
call   1080 <isalnum@plt>
cmp    $0x0,%rax
je     1370 <rename_unlink+0x180>
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    1332 <rename_unlink+0x142>
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     132b <rename_unlink+0x13b>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1100 <rename@plt>
cmp    $0x0,%rax
jge    13bb <rename_unlink+0x1cb>
movl   $0xffffffff,-0x4(%rbp)
jmp    1422 <rename_unlink+0x232>
mov    $0x0,%al
call   10e0 <sync@plt>
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10a0 <lstat@plt>
cmp    $0x0,%rax
jge    13f3 <rename_unlink+0x203>
lea    0xc20(%rip),%rdi
call   10f0 <perror@plt>
mov    0x2be4(%rip),%rax
mov    (%rax),%edi
call   1110 <exit@plt>
mov    -0x28(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1416 <rename_unlink+0x226>
mov    -0x18(%rbp),%rdi
call   1050 <rmdir@plt>
mov    %eax,-0x4(%rbp)
jmp    1422 <rename_unlink+0x232>
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
test   %rdi,%rdi
je     1279 <rename_unlink+0x89>
mov    %rdi,%r14
mov    %rsp,%rsi
call   10a0 <lstat@plt>
test   %rax,%rax
js     129d <rename_unlink+0xad>
mov    (%rsp),%edi
call   1070 <S_ISDIR@plt>
test   %rax,%rax
je     122e <rename_unlink+0x3e>
mov    %r14,%rdi
call   10b0 <empty_directory@plt>
test   %rax,%rax
js     128b <rename_unlink+0x9b>
mov    %r14,%rdi
call   1060 <strlen@plt>
add    $0xf,%eax
movslq %eax,%r15
mov    %r15,%rdi
call   10c0 <alloca@plt>
test   %rax,%rax
je     1282 <rename_unlink+0x92>
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     12b1 <rename_unlink+0xc1>
mov    %rax,%r13
add    $0x1,%r13
movb   $0x0,0x1(%rax)
jmp    12b4 <rename_unlink+0xc4>
mov    0x2d48(%rip),%rax
jmp    1292 <rename_unlink+0xa2>
mov    0x2d57(%rip),%rax
jmp    1292 <rename_unlink+0xa2>
mov    0x2d26(%rip),%rax
mov    (%rax),%eax
mov    0x2d3d(%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
nopl   0x0(%rax)
xor    %eax,%eax
call   10d0 <random_char@plt>
mov    %eax,%ebx
movzbl %al,%edi
call   1080 <isalnum@plt>
test   %rax,%rax
je     12e1 <rename_unlink+0xf1>
movslq %ebp,%rbp
mov    %bl,0x0(%r13,%rbp,1)
add    $0x1,%ebp
cmp    $0xe,%ebp
jl     12c0 <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     12c0 <rename_unlink+0xd0>
mov    %r14,%rdi
mov    %r12,%rsi
call   1100 <rename@plt>
test   %rax,%rax
js     129d <rename_unlink+0xad>
xor    %eax,%eax
call   10e0 <sync@plt>
mov    %rsp,%rsi
mov    %r12,%rdi
call   10a0 <lstat@plt>
test   %rax,%rax
js     135c <rename_unlink+0x16c>
mov    (%rsp),%edi
call   1070 <S_ISDIR@plt>
mov    %r12,%rdi
add    $0x8,%rsp
test   %rax,%rax
je     134d <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    0xc9d(%rip),%rdi
call   10f0 <perror@plt>
mov    0x2c61(%rip),%rax
mov    (%rax),%edi
call   1110 <exit@plt>

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

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