#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 int /*<<< orphan*/ DIR;

/* Variables and functions */
char *alloca(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
char *malloc(size_t);
int /*<<< orphan*/ *opendir(char *);
float pow(int, int);
char *realloc(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *unique_dir(const char *name) {
  char *new_name = malloc(strlen(name + 3));
  strncpy(new_name, name, strlen(name));
  new_name[strlen(name)] = '\0';
  DIR *d;
  if (!(d = opendir(new_name))) {
    closedir(d);
    return new_name;
  }
  int count = 1;
  int place = 1;
  float res = 10;
  int pos = strlen(name) + 1;
  new_name[pos - 1] = '_';
  new_name[pos + 1] = '\0';
  do {
    if (d)
      closedir(d);
    if (++count == res) {
      new_name = realloc(new_name, strlen(new_name) + 2);
      new_name[strlen(new_name)] = '\0';
      res = pow(10, ++place);
    }
    char *num = alloca(place + 1);
    sprintf(num, "%d", count);
    num[place] = '\0';
    strncpy(new_name + pos, num, place + 1);
  } while (d = opendir(new_name));
  return new_name;
}


x86 assembly (clang, -O0):
<unique_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x3,%rdi
call   1050 <strlen@plt>
mov    %rax,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1226 <unique_dir+0x96>
mov    -0x20(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1363 <unique_dir+0x1d3>
movl   $0x1,-0x24(%rbp)
movl   $0x1,-0x28(%rbp)
movss  0xdc4(%rip),%xmm0
movss  %xmm0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%rax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x5f,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
cmpq   $0x0,-0x20(%rbp)
je     1287 <unique_dir+0xf7>
mov    -0x20(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
cvtsi2ss %eax,%xmm0
ucomiss -0x2c(%rbp),%xmm0
jne    12fc <unique_dir+0x16c>
jp     12fc <unique_dir+0x16c>
mov    -0x18(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x60(%rbp),%rdi
add    $0x2,%rax
mov    %eax,%esi
call   1090 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x58(%rbp),%rax
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%esi
add    $0x1,%esi
mov    %esi,-0x28(%rbp)
mov    $0xa,%edi
call   10a0 <pow@plt>
movss  %xmm0,-0x2c(%rbp)
mov    -0x28(%rbp),%edi
add    $0x1,%edi
call   1070 <alloca@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x24(%rbp),%edx
lea    0xceb(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x38(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
movslq -0x30(%rbp),%rax
add    %rax,%rdi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%edx
add    $0x1,%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1273 <unique_dir+0xe3>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unique_dir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbp
add    $0x3,%rdi
call   1050 <strlen@plt>
mov    %rax,%rdi
call   1080 <malloc@plt>
mov    %rax,%r14
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12e7 <unique_dir+0x157>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1050 <strlen@plt>
movslq %eax,%rcx
shl    $0x20,%rax
movb   $0x5f,(%r14,%rcx,1)
movabs $0x200000000,%rcx
add    %rax,%rcx
sar    $0x20,%rcx
movb   $0x0,(%r14,%rcx,1)
movabs $0x100000000,%r15
add    %rax,%r15
sar    $0x20,%r15
movss  0xdd5(%rip),%xmm0
movss  %xmm0,0x4(%rsp)
mov    $0x1,%r12d
mov    $0x2,%r13d
jmp    128b <unique_dir+0xfb>
nop
lea    0x1(%r12),%ebx
mov    %ebx,%edi
call   1070 <alloca@plt>
mov    %rax,%rbp
mov    %rax,%rdi
lea    0xdab(%rip),%rsi
mov    %r13d,%edx
call   10b0 <sprintf@plt>
movslq %r12d,%rax
movb   $0x0,0x0(%rbp,%rax,1)
lea    (%r14,%r15,1),%rdi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
mov    %r14,%rdi
call   1040 <opendir@plt>
mov    %rax,%rbx
add    $0x1,%r13d
test   %rax,%rax
je     12ee <unique_dir+0x15e>
mov    %rbx,%rdi
call   1060 <closedir@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %r13d,%xmm0
movss  0x4(%rsp),%xmm1
ucomiss %xmm0,%xmm1
jne    1240 <unique_dir+0xb0>
jp     1240 <unique_dir+0xb0>
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x2(%rax),%esi
mov    %r14,%rdi
call   1090 <realloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1050 <strlen@plt>
movb   $0x0,(%r14,%rax,1)
add    $0x1,%r12d
mov    $0xa,%edi
mov    %r12d,%esi
call   10a0 <pow@plt>
movss  %xmm0,0x4(%rsp)
jmp    1240 <unique_dir+0xb0>
xor    %edi,%edi
call   1060 <closedir@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
define i8* @unique_dir(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca float
%9 = alloca i32
%10 = alloca i8*
store i8* %0, i8** %3
%11 = load i8*, i8** %3
%12 = getelementptr inbounds i8, i8* %11, i64 3
%13 = call i64 @strlen(i8* %12)
%14 = call i8* @malloc(i64 %13)
store i8* %14, i8** %4
%15 = load i8*, i8** %4
%16 = load i8*, i8** %3
%17 = load i8*, i8** %3
%18 = call i64 @strlen(i8* %17)
%19 = trunc i64 %18 to i32
%20 = call i32 @strncpy(i8* %15, i8* %16, i32 %19)
%21 = load i8*, i8** %4
%22 = load i8*, i8** %3
%23 = call i64 @strlen(i8* %22)
%24 = getelementptr inbounds i8, i8* %21, i64 %23
store i8 0, i8* %24
%25 = load i8*, i8** %4
%26 = call i32* @opendir(i8* %25)
store i32* %26, i32** %5
%27 = icmp ne i32* %26, null
br i1 %27, label %32, label %28
28: ; preds = %1
%29 = load i32*, i32** %5
%30 = call i32 @closedir(i32* %29)
%31 = load i8*, i8** %4
store i8* %31, i8** %2
br label %98
32: ; preds = %1
store i32 1, i32* %6
store i32 1, i32* %7
store float 1.000000e+01, float* %8
%33 = load i8*, i8** %3
%34 = call i64 @strlen(i8* %33)
%35 = add i64 %34, 1
%36 = trunc i64 %35 to i32
store i32 %36, i32* %9
%37 = load i8*, i8** %4
%38 = load i32, i32* %9
%39 = sub nsw i32 %38, 1
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %37, i64 %40
store i8 95, i8* %41
%42 = load i8*, i8** %4
%43 = load i32, i32* %9
%44 = add nsw i32 %43, 1
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %42, i64 %45
store i8 0, i8* %46
br label %47
47: ; preds = %92, %32
%48 = load i32*, i32** %5
%49 = icmp ne i32* %48, null
br i1 %49, label %50, label %53
50: ; preds = %47
%51 = load i32*, i32** %5
%52 = call i32 @closedir(i32* %51)
br label %53
53: ; preds = %50, %47
%54 = load i32, i32* %6
%55 = add nsw i32 %54, 1
store i32 %55, i32* %6
%56 = sitofp i32 %55 to float
%57 = load float, float* %8
%58 = fcmp oeq float %56, %57
br i1 %58, label %59, label %73
59: ; preds = %53
%60 = load i8*, i8** %4
%61 = load i8*, i8** %4
%62 = call i64 @strlen(i8* %61)
%63 = add i64 %62, 2
%64 = trunc i64 %63 to i32
%65 = call i8* @realloc(i8* %60, i32 %64)
store i8* %65, i8** %4
%66 = load i8*, i8** %4
%67 = load i8*, i8** %4
%68 = call i64 @strlen(i8* %67)
%69 = getelementptr inbounds i8, i8* %66, i64 %68
store i8 0, i8* %69
%70 = load i32, i32* %7
%71 = add nsw i32 %70, 1
store i32 %71, i32* %7
%72 = call float @pow(i32 10, i32 %71)
store float %72, float* %8
br label %73
73: ; preds = %59, %53
%74 = load i32, i32* %7
%75 = add nsw i32 %74, 1
%76 = call i8* @alloca(i32 %75)
store i8* %76, i8** %10
%77 = load i8*, i8** %10
%78 = load i32, i32* %6
%79 = call i32 @sprintf(i8* %77, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %78)
%80 = load i8*, i8** %10
%81 = load i32, i32* %7
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds i8, i8* %80, i64 %82
store i8 0, i8* %83
%84 = load i8*, i8** %4
%85 = load i32, i32* %9
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds i8, i8* %84, i64 %86
%88 = load i8*, i8** %10
%89 = load i32, i32* %7
%90 = add nsw i32 %89, 1
%91 = call i32 @strncpy(i8* %87, i8* %88, i32 %90)
br label %92
92: ; preds = %73
%93 = load i8*, i8** %4
%94 = call i32* @opendir(i8* %93)
store i32* %94, i32** %5
%95 = icmp ne i32* %94, null
br i1 %95, label %47, label %96
96: ; preds = %92
%97 = load i8*, i8** %4
store i8* %97, i8** %2
br label %98
98: ; preds = %96, %28
%99 = load i8*, i8** %2
ret i8* %99
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
define i8* @unique_dir(i8* %0) {
%2 = getelementptr inbounds i8, i8* %0, i64 3
%3 = tail call i64 @strlen(i8* %2)
%4 = tail call i8* @malloc(i64 %3)
%5 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%6 = trunc i64 %5 to i32
%7 = tail call i32 @strncpy(i8* %4, i8* %0, i32 %6)
%8 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%9 = getelementptr inbounds i8, i8* %4, i64 %8
store i8 0, i8* %9
%10 = tail call i32* @opendir(i8* %4)
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = tail call i32 @closedir(i32* null)
br label %56
14: ; preds = %1
%15 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%16 = shl i64 %15, 32
%17 = ashr exact i64 %16, 32
%18 = getelementptr inbounds i8, i8* %4, i64 %17
store i8 95, i8* %18
%19 = add i64 %16, 8589934592
%20 = ashr exact i64 %19, 32
%21 = getelementptr inbounds i8, i8* %4, i64 %20
store i8 0, i8* %21
%22 = add i64 %16, 4294967296
%23 = ashr exact i64 %22, 32
br label %24
24: ; preds = %43, %14
%25 = phi i32* [ %10, %14 ], [ %54, %43 ]
%26 = phi i32 [ 1, %14 ], [ %31, %43 ]
%27 = phi i32 [ 1, %14 ], [ %44, %43 ]
%28 = phi float [ 1.000000e+01, %14 ], [ %45, %43 ]
%29 = phi i8* [ %4, %14 ], [ %46, %43 ]
%30 = tail call i32 @closedir(i32* %25)
%31 = add nuw nsw i32 %26, 1
%32 = sitofp i32 %31 to float
%33 = fcmp oeq float %28, %32
br i1 %33, label %34, label %43
34: ; preds = %24
%35 = tail call i64 @strlen(i8* dereferenceable(1) %29)
%36 = trunc i64 %35 to i32
%37 = add i32 %36, 2
%38 = tail call i8* @realloc(i8* %29, i32 %37)
%39 = tail call i64 @strlen(i8* dereferenceable(1) %38)
%40 = getelementptr inbounds i8, i8* %38, i64 %39
store i8 0, i8* %40
%41 = add nsw i32 %27, 1
%42 = tail call float @pow(i32 10, i32 %41)
br label %43
43: ; preds = %34, %24
%44 = phi i32 [ %41, %34 ], [ %27, %24 ]
%45 = phi float [ %42, %34 ], [ %28, %24 ]
%46 = phi i8* [ %38, %34 ], [ %29, %24 ]
%47 = add nsw i32 %44, 1
%48 = tail call i8* @alloca(i32 %47)
%49 = tail call i32 @sprintf(i8* dereferenceable(1) %48, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %31)
%50 = sext i32 %44 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
store i8 0, i8* %51
%52 = getelementptr inbounds i8, i8* %46, i64 %23
%53 = tail call i32 @strncpy(i8* %52, i8* %48, i32 %47)
%54 = tail call i32* @opendir(i8* %46)
%55 = icmp eq i32* %54, null
br i1 %55, label %56, label %24
56: ; preds = %43, %12
%57 = phi i8* [ %4, %12 ], [ %46, %43 ]
ret i8* %57
}