#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*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ *find_buffer(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char const *, int);
scalar_t__ strlen(char const *);
char *strrchr(char const *, char);
scalar_t__ zmalloc(scalar_t__);
char *zstrdup(char const *);
char *make_buffer_name(const char *filename) {
  const char *p;
  char *name;
  int i;
  if ((p = strrchr(filename, '/')) == NULL)
    p = filename;
  else
    ++p;
  if (find_buffer(p, 0) == NULL)
    return zstrdup(p);
  name = (char *)zmalloc(strlen(p) + 6);
  for (i = 2; i <= 999; i++) {
    sprintf(name, "%s<%d>", p, i);
    if (find_buffer(name, 0) == NULL)
      return name;
  }
  assert(0);
  return NULL;
}


x86 assembly (clang, -O0):
<make_buffer_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11a5 <make_buffer_name+0x35>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    11b1 <make_buffer_name+0x41>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
call   1060 <find_buffer@plt>
cmp    $0x0,%rax
jne    11d8 <make_buffer_name+0x68>
mov    -0x18(%rbp),%rdi
call   1090 <zstrdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1260 <make_buffer_name+0xf0>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rdi
add    $0x6,%rdi
call   1050 <zmalloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x2,-0x24(%rbp)
cmpl   $0x3e7,-0x24(%rbp)
jg     1251 <make_buffer_name+0xe1>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x24(%rbp),%ecx
lea    0xde9(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
call   1060 <find_buffer@plt>
cmp    $0x0,%rax
jne    123e <make_buffer_name+0xce>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1260 <make_buffer_name+0xf0>
jmp    1243 <make_buffer_name+0xd3>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11f8 <make_buffer_name+0x88>
xor    %edi,%edi
call   1070 <assert@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_buffer_name>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    $0x2f,%esi
call   1040 <strrchr@plt>
lea    0x1(%rax),%r14
test   %rax,%rax
cmove  %rbx,%r14
mov    %r14,%rdi
xor    %esi,%esi
call   1060 <find_buffer@plt>
test   %rax,%rax
je     1201 <make_buffer_name+0x91>
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    0x6(%rax),%rdi
call   1050 <zmalloc@plt>
mov    %rax,%rbx
mov    $0x2,%ebp
lea    0xe42(%rip),%r15
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    %ebp,%ecx
call   1080 <sprintf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <find_buffer@plt>
test   %rax,%rax
je     11f3 <make_buffer_name+0x83>
add    $0x1,%ebp
cmp    $0x3e8,%ebp
jne    11c0 <make_buffer_name+0x50>
xor    %ebx,%ebx
xor    %edi,%edi
call   1070 <assert@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <zstrdup@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"%s<%d>\00"
define i8* @make_buffer_name(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i8* @strrchr(i8* %7, i8 signext 47)
store i8* %8, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = load i8*, i8** %3
store i8* %11, i8** %4
br label %15
12: ; preds = %1
%13 = load i8*, i8** %4
%14 = getelementptr inbounds i8, i8* %13, i32 1
store i8* %14, i8** %4
br label %15
15: ; preds = %12, %10
%16 = load i8*, i8** %4
%17 = call i32* @find_buffer(i8* %16, i32 0)
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i8*, i8** %4
%21 = call i8* @zstrdup(i8* %20)
store i8* %21, i8** %2
br label %47
22: ; preds = %15
%23 = load i8*, i8** %4
%24 = call i64 @strlen(i8* %23)
%25 = add nsw i64 %24, 6
%26 = call i64 @zmalloc(i64 %25)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %5
store i32 2, i32* %6
br label %28
28: ; preds = %42, %22
%29 = load i32, i32* %6
%30 = icmp sle i32 %29, 999
br i1 %30, label %31, label %45
31: ; preds = %28
%32 = load i8*, i8** %5
%33 = load i8*, i8** %4
%34 = load i32, i32* %6
%35 = call i32 @sprintf(i8* %32, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %33, i32 %34)
%36 = load i8*, i8** %5
%37 = call i32* @find_buffer(i8* %36, i32 0)
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %41
39: ; preds = %31
%40 = load i8*, i8** %5
store i8* %40, i8** %2
br label %47
41: ; preds = %31
br label %42
42: ; preds = %41
%43 = load i32, i32* %6
%44 = add nsw i32 %43, 1
store i32 %44, i32* %6
br label %28
45: ; preds = %28
%46 = call i32 @assert(i32 0)
store i8* null, i8** %2
br label %47
47: ; preds = %45, %39, %19
%48 = load i8*, i8** %2
ret i8* %48
}

LLVM IR (-O2):
@.str = [7 x i8] c"%s<%d>\00"
define i8* @make_buffer_name(i8* %0) {
%2 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%3 = icmp eq i8* %2, null
%4 = getelementptr inbounds i8, i8* %2, i64 1
%5 = select i1 %3, i8* %0, i8* %4
%6 = tail call i32* @find_buffer(i8* %5, i32 0)
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i8* @zstrdup(i8* %5)
br label %25
10: ; preds = %1
%11 = tail call i64 @strlen(i8* dereferenceable(1) %5)
%12 = add nsw i64 %11, 6
%13 = tail call i64 @zmalloc(i64 %12)
%14 = inttoptr i64 %13 to i8*
br label %18
15: ; preds = %18
%16 = add nuw nsw i32 %19, 1
%17 = icmp eq i32 %16, 1000
br i1 %17, label %23, label %18
18: ; preds = %10, %15
%19 = phi i32 [ 2, %10 ], [ %16, %15 ]
%20 = tail call i32 @sprintf(i8* dereferenceable(1) %14, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %5, i32 %19)
%21 = tail call i32* @find_buffer(i8* %14, i32 0)
%22 = icmp eq i32* %21, null
br i1 %22, label %25, label %15
23: ; preds = %15
%24 = tail call i32 @assert(i32 0)
br label %25
25: ; preds = %18, %23, %8
%26 = phi i8* [ %9, %8 ], [ null, %23 ], [ %14, %18 ]
ret i8* %26
}