#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*/ fatal_error(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, int);
scalar_t__ str_exists(char *);
int str_to_sn(char *, int);
int strlen(char *);
int fresh_symbol(char *prefix, int arity) {
  char name[51 + 20];
  int i = 0;
  if (strlen(prefix) > 51) {
    fatal_error("fresh_symbol, prefix is too big.");
  }
  do {
    sprintf(name, "%s%d", prefix, i);
    i++;
  } while (str_exists(name));
  return str_to_sn(name, arity);
}


x86 assembly (clang, -O0):
<fresh_symbol>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x64(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x33,%eax
jle    1184 <fresh_symbol+0x34>
lea    0xe81(%rip),%rdi
call   1040 <fatal_error@plt>
jmp    1189 <fresh_symbol+0x39>
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x64(%rbp),%ecx
lea    0xe86(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x64(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x64(%rbp)
lea    -0x60(%rbp),%rdi
call   1050 <str_exists@plt>
cmp    $0x0,%rax
jne    1189 <fresh_symbol+0x39>
lea    -0x60(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1060 <str_to_sn@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fresh_symbol>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x50,%rsp
mov    %esi,%r14d
mov    %rdi,%r12
call   1030 <strlen@plt>
cmp    $0x34,%eax
jl     1178 <fresh_symbol+0x28>
lea    0xe8d(%rip),%rdi
call   1040 <fatal_error@plt>
xor    %ebp,%ebp
lea    0xea0(%rip),%r15
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %ebp,%ecx
call   1070 <sprintf@plt>
add    $0x1,%ebp
mov    %rbx,%rdi
call   1050 <str_exists@plt>
test   %rax,%rax
jne    1190 <fresh_symbol+0x40>
mov    %rsp,%rdi
mov    %r14d,%esi
call   1060 <str_to_sn@plt>
add    $0x50,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [33 x i8] c"fresh_symbol, prefix is too big.\00"
@.str.1 = [5 x i8] c"%s%d\00"
define i32 @fresh_symbol(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [71 x i8]
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %6
%7 = load i8*, i8** %3
%8 = call i32 @strlen(i8* %7)
%9 = icmp sgt i32 %8, 51
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @fatal_error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %2
br label %13
13: ; preds = %20, %12
%14 = getelementptr inbounds [71 x i8], [71 x i8]* %5, i64 0, i64 0
%15 = load i8*, i8** %3
%16 = load i32, i32* %6
%17 = call i32 @sprintf(i8* %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %15, i32 %16)
%18 = load i32, i32* %6
%19 = add nsw i32 %18, 1
store i32 %19, i32* %6
br label %20
20: ; preds = %13
%21 = getelementptr inbounds [71 x i8], [71 x i8]* %5, i64 0, i64 0
%22 = call i64 @str_exists(i8* %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %13, label %24
24: ; preds = %20
%25 = getelementptr inbounds [71 x i8], [71 x i8]* %5, i64 0, i64 0
%26 = load i32, i32* %4
%27 = call i32 @str_to_sn(i8* %25, i32 %26)
ret i32 %27
}

LLVM IR (-O2):
@.str = [33 x i8] c"fresh_symbol, prefix is too big.\00"
@.str.1 = [5 x i8] c"%s%d\00"
define i32 @fresh_symbol(i8* %0, i32 %1) {
%3 = alloca [71 x i8]
%4 = getelementptr inbounds [71 x i8], [71 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @strlen(i8* %0)
%6 = icmp sgt i32 %5, 51
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @fatal_error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %2
br label %10
10: ; preds = %9, %10
%11 = phi i32 [ %13, %10 ], [ 0, %9 ]
%12 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %11)
%13 = add nuw nsw i32 %11, 1
%14 = call i64 @str_exists(i8* %4)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %10
16: ; preds = %10
%17 = call i32 @str_to_sn(i8* %4, i32 %1)
ret i32 %17
}