#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 */
typedef scalar_t__ clock_t;

/* Variables and functions */
scalar_t__ CLOCKS_PER_SEC;
scalar_t__ ENOENT;
scalar_t__ clock();
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(size_t);
int rand();
int /*<<< orphan*/ remove(char const *);
int /*<<< orphan*/ rename(char const *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
size_t strlen(char const *);
int chaz_OS_remove(const char *name) {
  int retval = 0;
  static const size_t num_random_chars = 16;
  size_t name_len = strlen(name);
  size_t i;
  char *temp_name = (char *)malloc(name_len + num_random_chars + 1);
  const char *working_name = name;
  clock_t start, now;
  strcpy(temp_name, name);
  for (i = 0; i < num_random_chars; i++) {
    temp_name[name_len + i] = 'A' + rand() % 26;
  }
  temp_name[name_len + num_random_chars] = '\0';
  start = now = clock();
  while (now - start < CLOCKS_PER_SEC) {
    now = clock();
    if (!rename(name, temp_name)) {
      working_name = temp_name;
      break;
    } else if (errno == ENOENT) {
      free(temp_name);
      return 0;
    }
  }
  start = now = clock();
  while (!retval && now - start < CLOCKS_PER_SEC) {
    now = clock();
    retval = !remove(working_name);
  }
  free(temp_name);
  return retval;
}


x86 assembly (clang, -O0):
<chaz_OS_remove>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x10,%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strcpy@plt>
movq   $0x0,-0x28(%rbp)
cmpq   $0x10,-0x28(%rbp)
jae    120f <chaz_OS_remove+0x8f>
mov    $0x0,%al
call   10a0 <rand@plt>
mov    $0x1a,%ecx
cltd
idiv   %ecx
add    $0x41,%edx
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
add    -0x28(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11d2 <chaz_OS_remove+0x52>
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,0x10(%rax,%rcx,1)
mov    $0x0,%al
call   1060 <clock@plt>
mov    %rax,-0x48(%rbp)
mov    %rax,-0x40(%rbp)
mov    -0x48(%rbp),%rax
sub    -0x40(%rbp),%rax
mov    0x2dae(%rip),%rcx
cmp    (%rcx),%rax
jge    12aa <chaz_OS_remove+0x12a>
mov    $0x0,%al
call   1060 <clock@plt>
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1090 <rename@plt>
cmp    $0x0,%eax
jne    1271 <chaz_OS_remove+0xf1>
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    12aa <chaz_OS_remove+0x12a>
mov    0x2d60(%rip),%rax
mov    (%rax),%rax
mov    0x2d46(%rip),%rcx
cmp    (%rcx),%rax
jne    12a0 <chaz_OS_remove+0x120>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1328 <chaz_OS_remove+0x1a8>
jmp    12a5 <chaz_OS_remove+0x125>
jmp    122b <chaz_OS_remove+0xab>
mov    $0x0,%al
call   1060 <clock@plt>
mov    %rax,-0x48(%rbp)
mov    %rax,-0x40(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x49(%rbp)
jne    12e0 <chaz_OS_remove+0x160>
mov    -0x48(%rbp),%rax
sub    -0x40(%rbp),%rax
mov    0x2d11(%rip),%rcx
cmp    (%rcx),%rax
setl   %al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%al
test   $0x1,%al
jne    12f0 <chaz_OS_remove+0x170>
jmp    1319 <chaz_OS_remove+0x199>
mov    $0x0,%al
call   1060 <clock@plt>
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <remove@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x14(%rbp)
jmp    12b9 <chaz_OS_remove+0x139>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chaz_OS_remove>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
call   1070 <strlen@plt>
mov    %rax,%r15
lea    0x11(%rax),%rdi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
call   1050 <strcpy@plt>
mov    %rbx,%rax
mov    %rbx,(%rsp)
add    %r15,%rbx
xor    %ebp,%ebp
nopl   0x0(%rax)
xor    %eax,%eax
call   10a0 <rand@plt>
cltq
imul   $0x4ec4ec4f,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x23,%rcx
add    %edx,%ecx
lea    (%rcx,%rcx,4),%edx
lea    (%rdx,%rdx,4),%edx
add    %ecx,%edx
sub    %edx,%eax
add    $0x41,%al
mov    %al,(%rbx,%rbp,1)
add    $0x1,%rbp
cmp    $0x10,%rbp
jne    11c0 <chaz_OS_remove+0x40>
mov    (%rsp),%rax
movb   $0x0,0x10(%r15,%rax,1)
xor    %eax,%eax
call   1060 <clock@plt>
mov    %rax,%rbx
mov    0x2dd7(%rip),%r13
mov    0x2dc0(%rip),%r15
mov    0x2da9(%rip),%r14
mov    %rax,%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
sub    %rbx,%rbp
cmp    0x0(%r13),%rbp
jge    1264 <chaz_OS_remove+0xe4>
xor    %eax,%eax
call   1060 <clock@plt>
mov    %rax,%rbp
mov    %r12,%rdi
mov    (%rsp),%rsi
call   1090 <rename@plt>
test   %eax,%eax
je     1260 <chaz_OS_remove+0xe0>
mov    (%r15),%rax
cmp    (%r14),%rax
jne    1230 <chaz_OS_remove+0xb0>
xor    %r15d,%r15d
jmp    12a5 <chaz_OS_remove+0x125>
mov    (%rsp),%r12
xor    %r15d,%r15d
xor    %eax,%eax
call   1060 <clock@plt>
mov    %rax,%rbp
mov    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
sub    %rbp,%rbx
cmp    0x0(%r13),%rbx
jge    12a5 <chaz_OS_remove+0x125>
xor    %eax,%eax
call   1060 <clock@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1040 <remove@plt>
test   %eax,%eax
jne    1280 <chaz_OS_remove+0x100>
mov    $0x1,%r15d
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@chaz_OS_remove.num_random_chars = internal constant i64 16
@CLOCKS_PER_SEC = global i64 0
@errno = global i64 0
@ENOENT = global i64 0
define i32 @chaz_OS_remove(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
store i8* %0, i8** %3
store i32 0, i32* %4
%11 = load i8*, i8** %3
%12 = call i64 @strlen(i8* %11)
store i64 %12, i64* %5
%13 = load i64, i64* %5
%14 = add i64 %13, 16
%15 = add i64 %14, 1
%16 = call i64 @malloc(i64 %15)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %7
%18 = load i8*, i8** %3
store i8* %18, i8** %8
%19 = load i8*, i8** %7
%20 = load i8*, i8** %3
%21 = call i32 @strcpy(i8* %19, i8* %20)
store i64 0, i64* %6
br label %22
22: ; preds = %35, %1
%23 = load i64, i64* %6
%24 = icmp ult i64 %23, 16
br i1 %24, label %25, label %38
25: ; preds = %22
%26 = call i32 (...) @rand()
%27 = srem i32 %26, 26
%28 = add nsw i32 65, %27
%29 = trunc i32 %28 to i8
%30 = load i8*, i8** %7
%31 = load i64, i64* %5
%32 = load i64, i64* %6
%33 = add i64 %31, %32
%34 = getelementptr inbounds i8, i8* %30, i64 %33
store i8 %29, i8* %34
br label %35
35: ; preds = %25
%36 = load i64, i64* %6
%37 = add i64 %36, 1
store i64 %37, i64* %6
br label %22
38: ; preds = %22
%39 = load i8*, i8** %7
%40 = load i64, i64* %5
%41 = add i64 %40, 16
%42 = getelementptr inbounds i8, i8* %39, i64 %41
store i8 0, i8* %42
%43 = call i64 (...) @clock()
store i64 %43, i64* %10
store i64 %43, i64* %9
br label %44
44: ; preds = %66, %38
%45 = load i64, i64* %10
%46 = load i64, i64* %9
%47 = sub nsw i64 %45, %46
%48 = load i64, i64* @CLOCKS_PER_SEC
%49 = icmp slt i64 %47, %48
br i1 %49, label %50, label %67
50: ; preds = %44
%51 = call i64 (...) @clock()
store i64 %51, i64* %10
%52 = load i8*, i8** %3
%53 = load i8*, i8** %7
%54 = call i32 @rename(i8* %52, i8* %53)
%55 = icmp ne i32 %54, 0
br i1 %55, label %58, label %56
56: ; preds = %50
%57 = load i8*, i8** %7
store i8* %57, i8** %8
br label %67
58: ; preds = %50
%59 = load i64, i64* @errno
%60 = load i64, i64* @ENOENT
%61 = icmp eq i64 %59, %60
br i1 %61, label %62, label %65
62: ; preds = %58
%63 = load i8*, i8** %7
%64 = call i32 @free(i8* %63)
store i32 0, i32* %2
br label %91
65: ; preds = %58
br label %66
66: ; preds = %65
br label %44
67: ; preds = %56, %44
%68 = call i64 (...) @clock()
store i64 %68, i64* %10
store i64 %68, i64* %9
br label %69
69: ; preds = %80, %67
%70 = load i32, i32* %4
%71 = icmp ne i32 %70, 0
br i1 %71, label %78, label %72
72: ; preds = %69
%73 = load i64, i64* %10
%74 = load i64, i64* %9
%75 = sub nsw i64 %73, %74
%76 = load i64, i64* @CLOCKS_PER_SEC
%77 = icmp slt i64 %75, %76
br label %78
78: ; preds = %72, %69
%79 = phi i1 [ false, %69 ], [ %77, %72 ]
br i1 %79, label %80, label %87
80: ; preds = %78
%81 = call i64 (...) @clock()
store i64 %81, i64* %10
%82 = load i8*, i8** %8
%83 = call i32 @remove(i8* %82)
%84 = icmp ne i32 %83, 0
%85 = xor i1 %84, true
%86 = zext i1 %85 to i32
store i32 %86, i32* %4
br label %69
87: ; preds = %78
%88 = load i8*, i8** %7
%89 = call i32 @free(i8* %88)
%90 = load i32, i32* %4
store i32 %90, i32* %2
br label %91
91: ; preds = %87, %62
%92 = load i32, i32* %2
ret i32 %92
}

LLVM IR (-O2):
@CLOCKS_PER_SEC = global i64 0
@errno = global i64 0
@ENOENT = global i64 0
define i32 @chaz_OS_remove(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = add i64 %2, 17
%4 = tail call i64 @malloc(i64 %3)
%5 = inttoptr i64 %4 to i8*
%6 = tail call i32 @strcpy(i8* %5, i8* %0)
br label %7
7: ; preds = %1, %7
%8 = phi i64 [ 0, %1 ], [ %15, %7 ]
%9 = tail call i32 (...) @rand()
%10 = srem i32 %9, 26
%11 = trunc i32 %10 to i8
%12 = add nsw i8 %11, 65
%13 = add i64 %8, %2
%14 = getelementptr inbounds i8, i8* %5, i64 %13
store i8 %12, i8* %14
%15 = add nuw nsw i64 %8, 1
%16 = icmp eq i64 %15, 16
br i1 %16, label %17, label %7
17: ; preds = %7
%18 = add i64 %2, 16
%19 = getelementptr inbounds i8, i8* %5, i64 %18
store i8 0, i8* %19
%20 = tail call i64 (...) @clock()
br label %21
21: ; preds = %30, %17
%22 = phi i64 [ %20, %17 ], [ %27, %30 ]
%23 = sub nsw i64 %22, %20
%24 = load i64, i64* @CLOCKS_PER_SEC
%25 = icmp slt i64 %23, %24
br i1 %25, label %26, label %34
26: ; preds = %21
%27 = tail call i64 (...) @clock()
%28 = tail call i32 @rename(i8* %0, i8* %5)
%29 = icmp eq i32 %28, 0
br i1 %29, label %34, label %30
30: ; preds = %26
%31 = load i64, i64* @errno
%32 = load i64, i64* @ENOENT
%33 = icmp eq i64 %31, %32
br i1 %33, label %46, label %21
34: ; preds = %26, %21
%35 = phi i8* [ %0, %21 ], [ %5, %26 ]
%36 = tail call i64 (...) @clock()
br label %37
37: ; preds = %34, %42
%38 = phi i64 [ %36, %34 ], [ %43, %42 ]
%39 = sub nsw i64 %38, %36
%40 = load i64, i64* @CLOCKS_PER_SEC
%41 = icmp slt i64 %39, %40
br i1 %41, label %42, label %46
42: ; preds = %37
%43 = tail call i64 (...) @clock()
%44 = tail call i32 @remove(i8* %35)
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %37
46: ; preds = %30, %37, %42
%47 = phi i32 [ 1, %42 ], [ 0, %37 ], [ 0, %30 ]
%48 = tail call i32 @free(i8* %5)
ret i32 %47
}