#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 */
scalar_t__ coco_create_directory(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_create_directory(*path) == 0) {
    return;
  }
  while (counter < 999) {
    new_path = coco_strdupf("%s-%03d", *path, counter);
    if (coco_create_directory(new_path) == 0) {
      coco_free_memory(*path);
      *path = new_path;
      return;
    } else {
      counter++;
      coco_free_memory(new_path);
    }
  }
  coco_error(
      "coco_create_unique_directory(): unable to create unique directory %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_create_directory@plt>
cmp    $0x0,%rax
jne    116e <coco_create_unique_directory+0x2e>
jmp    11f3 <coco_create_unique_directory+0xb3>
jmp    1173 <coco_create_unique_directory+0x33>
cmpl   $0x3e7,-0xc(%rbp)
jge    11e0 <coco_create_unique_directory+0xa0>
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
mov    -0xc(%rbp),%edx
lea    0xe6f(%rip),%rdi
call   1040 <coco_strdupf@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <coco_create_directory@plt>
cmp    $0x0,%rax
jne    11c9 <coco_create_unique_directory+0x89>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1060 <coco_free_memory@plt>
mov    -0x18(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
jmp    11f3 <coco_create_unique_directory+0xb3>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <coco_free_memory@plt>
jmp    1173 <coco_create_unique_directory+0x33>
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
lea    0xe1a(%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_create_directory@plt>
test   %rax,%rax
je     11c4 <coco_create_unique_directory+0x84>
mov    $0x1,%ebp
lea    0xe9d(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%r15),%rsi
mov    %r14,%rdi
mov    %ebp,%edx
call   1040 <coco_strdupf@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <coco_create_directory@plt>
test   %rax,%rax
je     11b9 <coco_create_unique_directory+0x79>
add    $0x1,%ebp
mov    %rbx,%rdi
call   1060 <coco_free_memory@plt>
cmp    $0x3e7,%ebp
jne    1170 <coco_create_unique_directory+0x30>
mov    (%r15),%rsi
lea    0xe5e(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <coco_error@plt>
mov    (%r15),%rdi
call   1060 <coco_free_memory@plt>
mov    %rbx,(%r15)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"%s-%03d\00"
@.str.1 = [69 x i8] c"coco_create_unique_directory(): unable to create unique directory %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 i64 @coco_create_directory(i8* %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %1
br label %38
10: ; preds = %1
br label %11
11: ; preds = %33, %10
%12 = load i32, i32* %3
%13 = icmp slt i32 %12, 999
br i1 %13, label %14, label %34
14: ; preds = %11
%15 = load i8**, i8*** %2
%16 = load i8*, i8** %15
%17 = load i32, i32* %3
%18 = call i8* @coco_strdupf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %16, i32 %17)
store i8* %18, i8** %4
%19 = load i8*, i8** %4
%20 = call i64 @coco_create_directory(i8* %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %14
%23 = load i8**, i8*** %2
%24 = load i8*, i8** %23
%25 = call i32 @coco_free_memory(i8* %24)
%26 = load i8*, i8** %4
%27 = load i8**, i8*** %2
store i8* %26, i8** %27
br label %38
28: ; preds = %14
%29 = load i32, i32* %3
%30 = add nsw i32 %29, 1
store i32 %30, i32* %3
%31 = load i8*, i8** %4
%32 = call i32 @coco_free_memory(i8* %31)
br label %33
33: ; preds = %28
br label %11
34: ; preds = %11
%35 = load i8**, i8*** %2
%36 = load i8*, i8** %35
%37 = call i32 @coco_error(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.1, i64 0, i64 0), i8* %36)
br label %38
38: ; preds = %34, %22, %9
ret void
}

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