#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 empty_directory(char const *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ isalnum(int);
int lstat(char const *, struct stat *);
int /*<<< orphan*/ perror(char *);
char random_char();
int 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, c;
  struct stat statbuf;
  size_t new_name_size = strlen(path) + 15;
  int i = 0;
  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) {
      c = random_char();
      if (isalnum((int)c)) {
        p[i] = c;
        i++;
      }
    }
    p[i] = '\0';
  } while (lstat(new_name, &statbuf) == 0);
  if (lstat(path, &statbuf) == -1)
    return -1;
  if (S_ISDIR(statbuf.st_mode) && (empty_directory(path) == -1)) {
    errno = ENOTEMPTY;
    return -1;
  }
  if (rename(path, new_name) == -1)
    return -1;
  sync();
  if (lstat(new_name, &statbuf) == -1) {
    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)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
add    $0xf,%eax
cltq
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
call   10c0 <alloca@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    124a <rename_unlink+0x5a>
mov    0x2dad(%rip),%rax
mov    (%rax),%ecx
mov    0x2d9c(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13f4 <rename_unlink+0x204>
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     128f <rename_unlink+0x9f>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
jmp    1297 <rename_unlink+0xa7>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    129c <rename_unlink+0xac>
movl   $0x0,-0x34(%rbp)
cmpl   $0xe,-0x34(%rbp)
jge    12e6 <rename_unlink+0xf6>
mov    $0x0,%al
call   10d0 <random_char@plt>
mov    %al,-0x21(%rbp)
movsbl -0x21(%rbp),%edi
call   1080 <isalnum@plt>
cmp    $0x0,%rax
je     12e1 <rename_unlink+0xf1>
mov    -0x21(%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    12a3 <rename_unlink+0xb3>
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,%eax
je     129c <rename_unlink+0xac>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10a0 <lstat@plt>
cmp    $0xffffffff,%eax
jne    132a <rename_unlink+0x13a>
movl   $0xffffffff,-0x4(%rbp)
jmp    13f4 <rename_unlink+0x204>
mov    -0x28(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%rax
je     136c <rename_unlink+0x17c>
mov    -0x10(%rbp),%rdi
call   10b0 <empty_directory@plt>
cmp    $0xffffffff,%eax
jne    136c <rename_unlink+0x17c>
mov    0x2c6b(%rip),%rax
mov    (%rax),%ecx
mov    0x2c7a(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13f4 <rename_unlink+0x204>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1100 <rename@plt>
cmp    $0xffffffff,%eax
jne    138e <rename_unlink+0x19e>
movl   $0xffffffff,-0x4(%rbp)
jmp    13f4 <rename_unlink+0x204>
mov    $0x0,%al
call   10e0 <sync@plt>
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10a0 <lstat@plt>
cmp    $0xffffffff,%eax
jne    13c5 <rename_unlink+0x1d5>
lea    0xc4e(%rip),%rdi
call   10f0 <perror@plt>
mov    0x2c12(%rip),%rax
mov    (%rax),%edi
call   1110 <exit@plt>
mov    -0x28(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%rax
je     13e8 <rename_unlink+0x1f8>
mov    -0x18(%rbp),%rdi
call   1050 <rmdir@plt>
mov    %eax,-0x4(%rbp)
jmp    13f4 <rename_unlink+0x204>
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   1060 <strlen@plt>
add    $0xf,%eax
movslq %eax,%rbx
mov    %rbx,%rdi
call   10c0 <alloca@plt>
test   %rax,%rax
je     1246 <rename_unlink+0x56>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <strncpy@plt>
mov    %r15,%rdi
mov    $0x2f,%esi
call   1090 <strrchr@plt>
test   %rax,%rax
je     125d <rename_unlink+0x6d>
mov    %rax,%r13
add    $0x1,%r13
movb   $0x0,0x1(%rax)
jmp    1260 <rename_unlink+0x70>
mov    0x2d93(%rip),%rax
mov    (%rax),%eax
mov    0x2d82(%rip),%rcx
mov    %eax,(%rcx)
jmp    1326 <rename_unlink+0x136>
mov    %r15,%r13
mov    %rsp,%r12
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   10d0 <random_char@plt>
mov    %eax,%ebx
movsbl %al,%edi
call   1080 <isalnum@plt>
test   %rax,%rax
je     1291 <rename_unlink+0xa1>
movslq %ebp,%rbp
mov    %bl,0x0(%r13,%rbp,1)
add    $0x1,%ebp
cmp    $0xe,%ebp
jl     1270 <rename_unlink+0x80>
mov    %ebp,%eax
movb   $0x0,0x0(%r13,%rax,1)
mov    %r15,%rdi
mov    %r12,%rsi
call   10a0 <lstat@plt>
mov    $0x0,%ebp
test   %eax,%eax
je     1270 <rename_unlink+0x80>
mov    %rsp,%rsi
mov    %r14,%rdi
call   10a0 <lstat@plt>
cmp    $0xffffffff,%eax
je     1326 <rename_unlink+0x136>
mov    (%rsp),%edi
call   1070 <S_ISDIR@plt>
test   %rax,%rax
je     12dc <rename_unlink+0xec>
mov    %r14,%rdi
call   10b0 <empty_directory@plt>
cmp    $0xffffffff,%eax
je     133a <rename_unlink+0x14a>
mov    %r14,%rdi
mov    %r15,%rsi
call   1100 <rename@plt>
cmp    $0xffffffff,%eax
je     1326 <rename_unlink+0x136>
xor    %eax,%eax
call   10e0 <sync@plt>
mov    %rsp,%rsi
mov    %r15,%rdi
call   10a0 <lstat@plt>
cmp    $0xffffffff,%eax
je     1355 <rename_unlink+0x165>
mov    (%rsp),%edi
call   1070 <S_ISDIR@plt>
mov    %r15,%rdi
add    $0x8,%rsp
test   %rax,%rax
je     1346 <rename_unlink+0x156>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <rmdir@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c7f(%rip),%rax
jmp    124d <rename_unlink+0x5d>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <unlink@plt>
lea    0xca4(%rip),%rdi
call   10f0 <perror@plt>
mov    0x2c68(%rip),%rax
mov    (%rax),%edi
call   1110 <exit@plt>

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

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