#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 st_nlink;
  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*/ 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) && (statbuf.st_nlink > 2)) {
    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,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x38(%rbp),%rdi
call   10b0 <alloca@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    123a <rename_unlink+0x5a>
mov    0x2dbd(%rip),%rax
mov    (%rax),%ecx
mov    0x2dac(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13dc <rename_unlink+0x1fc>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x38(%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     127f <rename_unlink+0x9f>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
jmp    1287 <rename_unlink+0xa7>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    128c <rename_unlink+0xac>
movl   $0x0,-0x3c(%rbp)
cmpl   $0xe,-0x3c(%rbp)
jge    12d6 <rename_unlink+0xf6>
mov    $0x0,%al
call   10c0 <random_char@plt>
mov    %al,-0x21(%rbp)
movsbl -0x21(%rbp),%edi
call   1080 <isalnum@plt>
cmp    $0x0,%rax
je     12d1 <rename_unlink+0xf1>
mov    -0x21(%rbp),%dl
mov    -0x20(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1293 <rename_unlink+0xb3>
mov    -0x20(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   10a0 <lstat@plt>
cmp    $0x0,%eax
je     128c <rename_unlink+0xac>
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   10a0 <lstat@plt>
cmp    $0xffffffff,%eax
jne    131a <rename_unlink+0x13a>
movl   $0xffffffff,-0x4(%rbp)
jmp    13dc <rename_unlink+0x1fc>
mov    -0x2c(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1354 <rename_unlink+0x174>
cmpl   $0x2,-0x30(%rbp)
jle    1354 <rename_unlink+0x174>
mov    0x2c83(%rip),%rax
mov    (%rax),%ecx
mov    0x2c92(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13dc <rename_unlink+0x1fc>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10f0 <rename@plt>
cmp    $0xffffffff,%eax
jne    1376 <rename_unlink+0x196>
movl   $0xffffffff,-0x4(%rbp)
jmp    13dc <rename_unlink+0x1fc>
mov    $0x0,%al
call   10d0 <sync@plt>
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   10a0 <lstat@plt>
cmp    $0xffffffff,%eax
jne    13ad <rename_unlink+0x1cd>
lea    0xc66(%rip),%rdi
call   10e0 <perror@plt>
mov    0x2c2a(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
mov    -0x2c(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%rax
je     13d0 <rename_unlink+0x1f0>
mov    -0x18(%rbp),%rdi
call   1050 <rmdir@plt>
mov    %eax,-0x4(%rbp)
jmp    13dc <rename_unlink+0x1fc>
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   10b0 <alloca@plt>
test   %rax,%rax
je     1236 <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     123f <rename_unlink+0x5f>
mov    %rax,%r13
add    $0x1,%r13
movb   $0x0,0x1(%rax)
jmp    1242 <rename_unlink+0x62>
mov    0x2da3(%rip),%rax
jmp    12bd <rename_unlink+0xdd>
mov    %r15,%r13
mov    %rsp,%r12
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   10c0 <random_char@plt>
mov    %eax,%ebx
movsbl %al,%edi
call   1080 <isalnum@plt>
test   %rax,%rax
je     1271 <rename_unlink+0x91>
movslq %ebp,%rbp
mov    %bl,0x0(%r13,%rbp,1)
add    $0x1,%ebp
cmp    $0xe,%ebp
jl     1250 <rename_unlink+0x70>
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     1250 <rename_unlink+0x70>
mov    %rsp,%rsi
mov    %r14,%rdi
call   10a0 <lstat@plt>
cmp    $0xffffffff,%eax
je     12c8 <rename_unlink+0xe8>
mov    0x4(%rsp),%edi
call   1070 <S_ISDIR@plt>
test   %rax,%rax
je     12dc <rename_unlink+0xfc>
cmpl   $0x3,(%rsp)
jl     12dc <rename_unlink+0xfc>
mov    0x2d03(%rip),%rax
mov    (%rax),%eax
mov    0x2d12(%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    %r14,%rdi
mov    %r15,%rsi
call   10f0 <rename@plt>
cmp    $0xffffffff,%eax
je     12c8 <rename_unlink+0xe8>
xor    %eax,%eax
call   10d0 <sync@plt>
mov    %rsp,%rsi
mov    %r15,%rdi
call   10a0 <lstat@plt>
cmp    $0xffffffff,%eax
je     1336 <rename_unlink+0x156>
mov    0x4(%rsp),%edi
call   1070 <S_ISDIR@plt>
mov    %r15,%rdi
add    $0x8,%rsp
test   %rax,%rax
je     1327 <rename_unlink+0x147>
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    0xcc3(%rip),%rdi
call   10e0 <perror@plt>
mov    0x2c87(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32, 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 1
%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 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%75 = load i32, i32* %74
%76 = icmp sgt i32 %75, 2
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 1
%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, 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 %73
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 %73, label %43
43: ; preds = %40
%44 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%45 = load i32, i32* %44
%46 = tail call i64 @S_ISDIR(i32 %45)
%47 = icmp ne i64 %46, 0
%48 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%49 = load i32, i32* %48
%50 = icmp sgt i32 %49, 2
%51 = select i1 %47, i1 %50, i1 false
br i1 %51, label %52, label %54
52: ; preds = %43
%53 = load i32, i32* @ENOTEMPTY
store i32 %53, i32* @errno
br label %73
54: ; preds = %43
%55 = tail call i32 @rename(i8* %0, i8* %8)
%56 = icmp eq i32 %55, -1
br i1 %56, label %73, label %57
57: ; preds = %54
%58 = tail call i32 (...) @sync()
%59 = call i32 @lstat(i8* %8, %struct.stat* %2)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %65
61: ; preds = %57
%62 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%63 = load i32, i32* @EXIT_FAILURE
%64 = tail call i32 @exit(i32 %63)
unreachable
65: ; preds = %57
%66 = load i32, i32* %44
%67 = tail call i64 @S_ISDIR(i32 %66)
%68 = icmp eq i64 %67, 0
br i1 %68, label %71, label %69
69: ; preds = %65
%70 = tail call i32 @rmdir(i8* %8)
br label %73
71: ; preds = %65
%72 = tail call i32 @unlink(i8* %8)
br label %73
73: ; preds = %54, %40, %71, %69, %52, %10
%74 = phi i32 [ -1, %10 ], [ -1, %52 ], [ %70, %69 ], [ %72, %71 ], [ -1, %40 ], [ -1, %54 ]
ret i32 %74
}