#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 */

/* Variables and functions */
int /*<<< orphan*/ coco_create_directory(char *);
int /*<<< orphan*/ coco_directory_exists(char *);
int /*<<< orphan*/ coco_error(char *, char *);
int /*<<< orphan*/ coco_free_memory(char *);
char *coco_strdupf(char *, char *, int);
void coco_create_unique_directory(char **path) {
  int counter = 1;
  char *new_path;
  if (!coco_directory_exists(*path)) {
    coco_create_directory(*path);
    return;
  }
  while (counter < 999) {
    new_path = coco_strdupf("%s-%03d", *path, counter);
    if (!coco_directory_exists(new_path)) {
      coco_free_memory(*path);
      *path = new_path;
      coco_create_directory(*path);
      return;
    } else {
      counter++;
      coco_free_memory(new_path);
    }
  }
  coco_error(
      "coco_create_unique_path(): could not create a unique path with name %s",
      *path);
  return;
}


x86 assembly (clang, -O0):
<coco_create_unique_directory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x1,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1050 <coco_directory_exists@plt>
cmp    $0x0,%eax
jne    1189 <coco_create_unique_directory+0x39>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1060 <coco_create_directory@plt>
jmp    1219 <coco_create_unique_directory+0xc9>
jmp    118e <coco_create_unique_directory+0x3e>
cmpl   $0x3e7,-0xc(%rbp)
jge    1206 <coco_create_unique_directory+0xb6>
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
mov    -0xc(%rbp),%edx
lea    0xe54(%rip),%rdi
call   1040 <coco_strdupf@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <coco_directory_exists@plt>
cmp    $0x0,%eax
jne    11ef <coco_create_unique_directory+0x9f>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1070 <coco_free_memory@plt>
mov    -0x18(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1060 <coco_create_directory@plt>
jmp    1219 <coco_create_unique_directory+0xc9>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <coco_free_memory@plt>
jmp    118e <coco_create_unique_directory+0x3e>
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
lea    0xdf4(%rip),%rdi
call   1030 <coco_error@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<coco_create_unique_directory>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
mov    (%rdi),%rdi
call   1050 <coco_directory_exists@plt>
test   %eax,%eax
je     11c8 <coco_create_unique_directory+0x78>
mov    $0x1,%ebp
lea    0xe8e(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r15),%rsi
mov    %r14,%rdi
mov    %ebp,%edx
call   1040 <coco_strdupf@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <coco_directory_exists@plt>
test   %eax,%eax
je     11cd <coco_create_unique_directory+0x7d>
add    $0x1,%ebp
mov    %rbx,%rdi
call   1070 <coco_free_memory@plt>
cmp    $0x3e7,%ebp
jne    1180 <coco_create_unique_directory+0x30>
mov    (%r15),%rsi
lea    0xe4f(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <coco_error@plt>
mov    (%r15),%rdi
jmp    11db <coco_create_unique_directory+0x8b>
mov    (%r15),%rdi
call   1070 <coco_free_memory@plt>
mov    %rbx,(%r15)
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <coco_create_directory@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"%s-%03d\00"
@.str.1 = [71 x i8] c"coco_create_unique_path(): could not create a unique path with name %s\00"
define void @coco_create_unique_directory(i8** %0) {
%2 = alloca i8**
%3 = alloca i32
%4 = alloca i8*
store i8** %0, i8*** %2
store i32 1, i32* %3
%5 = load i8**, i8*** %2
%6 = load i8*, i8** %5
%7 = call i32 @coco_directory_exists(i8* %6)
%8 = icmp ne i32 %7, 0
br i1 %8, label %13, label %9
9: ; preds = %1
%10 = load i8**, i8*** %2
%11 = load i8*, i8** %10
%12 = call i32 @coco_create_directory(i8* %11)
br label %44
13: ; preds = %1
br label %14
14: ; preds = %39, %13
%15 = load i32, i32* %3
%16 = icmp slt i32 %15, 999
br i1 %16, label %17, label %40
17: ; preds = %14
%18 = load i8**, i8*** %2
%19 = load i8*, i8** %18
%20 = load i32, i32* %3
%21 = call i8* @coco_strdupf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %19, i32 %20)
store i8* %21, i8** %4
%22 = load i8*, i8** %4
%23 = call i32 @coco_directory_exists(i8* %22)
%24 = icmp ne i32 %23, 0
br i1 %24, label %34, label %25
25: ; preds = %17
%26 = load i8**, i8*** %2
%27 = load i8*, i8** %26
%28 = call i32 @coco_free_memory(i8* %27)
%29 = load i8*, i8** %4
%30 = load i8**, i8*** %2
store i8* %29, i8** %30
%31 = load i8**, i8*** %2
%32 = load i8*, i8** %31
%33 = call i32 @coco_create_directory(i8* %32)
br label %44
34: ; preds = %17
%35 = load i32, i32* %3
%36 = add nsw i32 %35, 1
store i32 %36, i32* %3
%37 = load i8*, i8** %4
%38 = call i32 @coco_free_memory(i8* %37)
br label %39
39: ; preds = %34
br label %14
40: ; preds = %14
%41 = load i8**, i8*** %2
%42 = load i8*, i8** %41
%43 = call i32 @coco_error(i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.1, i64 0, i64 0), i8* %42)
br label %44
44: ; preds = %40, %25, %9
ret void
}

LLVM IR (-O2):
@.str = [8 x i8] c"%s-%03d\00"
@.str.1 = [71 x i8] c"coco_create_unique_path(): could not create a unique path with name %s\00"
define void @coco_create_unique_directory(i8** nocapture %0) {
%2 = load i8*, i8** %0
%3 = tail call i32 @coco_directory_exists(i8* %2)
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = load i8*, i8** %0
%7 = tail call i32 @coco_create_directory(i8* %6)
br label %25
8: ; preds = %1, %18
%9 = phi i32 [ %19, %18 ], [ 1, %1 ]
%10 = load i8*, i8** %0
%11 = tail call i8* @coco_strdupf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %10, i32 %9)
%12 = tail call i32 @coco_directory_exists(i8* %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %8
%15 = load i8*, i8** %0
%16 = tail call i32 @coco_free_memory(i8* %15)
store i8* %11, i8** %0
%17 = tail call i32 @coco_create_directory(i8* %11)
br label %25
18: ; preds = %8
%19 = add nuw nsw i32 %9, 1
%20 = tail call i32 @coco_free_memory(i8* %11)
%21 = icmp eq i32 %19, 999
br i1 %21, label %22, label %8
22: ; preds = %18
%23 = load i8*, i8** %0
%24 = tail call i32 @coco_error(i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.1, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %22, %14, %5
ret void
}