#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_error(char *);
int /*<<< orphan*/ coco_file_exists(char *);
int /*<<< orphan*/ coco_free_memory(char *);
char *coco_strdupf(char *, int, char *);
void coco_create_unique_filename(char **file_name) {
  int counter = 1;
  char *new_file_name;
  if (!coco_file_exists(*file_name)) {
    return;
  }
  while (counter < 99) {
    new_file_name = coco_strdupf("%02d-%s", counter, *file_name);
    if (!coco_file_exists(new_file_name)) {
      coco_free_memory(*file_name);
      *file_name = new_file_name;
      return;
    } else {
      counter++;
      coco_free_memory(new_file_name);
    }
  }
  coco_free_memory(new_file_name);
  coco_error(
      "coco_create_unique_filename(): could not create a unique file name");
  return;
}


x86 assembly (clang, -O0):
<coco_create_unique_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x1,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <coco_file_exists@plt>
cmp    $0x0,%eax
jne    116d <coco_create_unique_filename+0x2d>
jmp    11f0 <coco_create_unique_filename+0xb0>
jmp    1172 <coco_create_unique_filename+0x32>
cmpl   $0x63,-0xc(%rbp)
jge    11db <coco_create_unique_filename+0x9b>
mov    -0xc(%rbp),%esi
mov    -0x8(%rbp),%rax
mov    (%rax),%rdx
lea    0xe73(%rip),%rdi
call   1050 <coco_strdupf@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <coco_file_exists@plt>
cmp    $0x0,%eax
jne    11c4 <coco_create_unique_filename+0x84>
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    11f0 <coco_create_unique_filename+0xb0>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <coco_free_memory@plt>
jmp    1172 <coco_create_unique_filename+0x32>
mov    -0x18(%rbp),%rdi
call   1060 <coco_free_memory@plt>
lea    0xe1d(%rip),%rdi
call   1040 <coco_error@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<coco_create_unique_filename>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
mov    (%rdi),%rdi
call   1030 <coco_file_exists@plt>
test   %eax,%eax
je     11c5 <coco_create_unique_filename+0x85>
mov    $0x1,%ebp
lea    0xe9e(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r15),%rdx
mov    %r14,%rdi
mov    %ebp,%esi
call   1050 <coco_strdupf@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1030 <coco_file_exists@plt>
test   %eax,%eax
je     11ba <coco_create_unique_filename+0x7a>
add    $0x1,%ebp
mov    %rbx,%rdi
call   1060 <coco_free_memory@plt>
cmp    $0x63,%ebp
jne    1170 <coco_create_unique_filename+0x30>
mov    %rbx,%rdi
call   1060 <coco_free_memory@plt>
lea    0xe5d(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <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"%02d-%s\00"
@.str.1 = [67 x i8] c"coco_create_unique_filename(): could not create a unique file name\00"
define void @coco_create_unique_filename(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_file_exists(i8* %6)
%8 = icmp ne i32 %7, 0
br i1 %8, label %10, label %9
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, 99
br i1 %13, label %14, label %34
14: ; preds = %11
%15 = load i32, i32* %3
%16 = load i8**, i8*** %2
%17 = load i8*, i8** %16
%18 = call i8* @coco_strdupf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %15, i8* %17)
store i8* %18, i8** %4
%19 = load i8*, i8** %4
%20 = call i32 @coco_file_exists(i8* %19)
%21 = icmp ne i32 %20, 0
br i1 %21, label %28, label %22
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** %4
%36 = call i32 @coco_free_memory(i8* %35)
%37 = call i32 @coco_error(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.1, i64 0, i64 0))
br label %38
38: ; preds = %34, %22, %9
ret void
}

LLVM IR (-O2):
@.str = [8 x i8] c"%02d-%s\00"
@.str.1 = [67 x i8] c"coco_create_unique_filename(): could not create a unique file name\00"
define void @coco_create_unique_filename(i8** nocapture %0) {
%2 = load i8*, i8** %0
%3 = tail call i32 @coco_file_exists(i8* %2)
%4 = icmp eq i32 %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), i32 %6, i8* %7)
%9 = tail call i32 @coco_file_exists(i8* %8)
%10 = icmp eq i32 %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, 99
br i1 %17, label %18, label %5
18: ; preds = %14
%19 = tail call i32 @coco_free_memory(i8* %8)
%20 = tail call i32 @coco_error(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %1, %18, %11
ret void
}