#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 */
struct stat {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ assert(char *);
char *emalloc(size_t);
int /*<<< orphan*/ fname_is_avail(char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char *, unsigned int);
char *strdup(char *);
size_t const strlen(char *);
char *unused_fname(char *fname) {
  const size_t MAX_NUMBER_LEN = 100;
  char *new_fname = fname;
  struct stat stat_buf;
  unsigned i;
  int test;
  assert(fname);
  for (i = 2; new_fname && !fname_is_avail(new_fname); i++) {
    size_t new_buflen = strlen(fname) + MAX_NUMBER_LEN + 1;
    if (new_fname != fname)
      free(new_fname);
    new_fname = emalloc(new_buflen);
    snprintf(new_fname, new_buflen, "%s.%u", fname, i);
  }
  return new_fname == fname ? strdup(fname) : new_fname;
}


x86 assembly (clang, -O0):
<unused_fname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x64,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <assert@plt>
movl   $0x2,-0x24(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x31(%rbp)
je     11c0 <unused_fname+0x50>
mov    -0x18(%rbp),%rdi
call   1060 <fname_is_avail@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    11d0 <unused_fname+0x60>
jmp    1233 <unused_fname+0xc3>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x64,%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x8(%rbp),%rax
je     11fc <unused_fname+0x8c>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1080 <emalloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x8(%rbp),%rcx
mov    -0x24(%rbp),%r8d
lea    0xde0(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    119c <unused_fname+0x2c>
mov    -0x18(%rbp),%rax
cmp    -0x8(%rbp),%rax
jne    1253 <unused_fname+0xe3>
mov    -0x8(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x40(%rbp)
jmp    125b <unused_fname+0xeb>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unused_fname>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
call   1070 <assert@plt>
test   %r14,%r14
je     11f2 <unused_fname+0x82>
mov    $0x2,%r12d
lea    0xe6e(%rip),%r15
mov    %r14,%rbx
jmp    11c8 <unused_fname+0x58>
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1080 <emalloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %r12d,%r8d
call   1050 <snprintf@plt>
add    $0x1,%r12d
test   %rbx,%rbx
je     11f2 <unused_fname+0x82>
mov    %rbx,%rdi
call   1060 <fname_is_avail@plt>
test   %eax,%eax
jne    11f4 <unused_fname+0x84>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbp
add    $0x65,%rbp
cmp    %r14,%rbx
je     11a0 <unused_fname+0x30>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    11a0 <unused_fname+0x30>
xor    %ebx,%ebx
cmp    %r14,%rbx
je     1205 <unused_fname+0x95>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <strdup@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [6 x i8] c"%s.%u\00"
define i8* @unused_fname(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca %struct.stat
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
store i8* %0, i8** %2
store i64 100, i64* %3
%9 = load i8*, i8** %2
store i8* %9, i8** %4
%10 = load i8*, i8** %2
%11 = call i32 @assert(i8* %10)
store i32 2, i32* %6
br label %12
12: ; preds = %41, %1
%13 = load i8*, i8** %4
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = load i8*, i8** %4
%17 = call i32 @fname_is_avail(i8* %16)
%18 = icmp ne i32 %17, 0
%19 = xor i1 %18, true
br label %20
20: ; preds = %15, %12
%21 = phi i1 [ false, %12 ], [ %19, %15 ]
br i1 %21, label %22, label %44
22: ; preds = %20
%23 = load i8*, i8** %2
%24 = call i64 @strlen(i8* %23)
%25 = add i64 %24, 100
%26 = add i64 %25, 1
store i64 %26, i64* %8
%27 = load i8*, i8** %4
%28 = load i8*, i8** %2
%29 = icmp ne i8* %27, %28
br i1 %29, label %30, label %33
30: ; preds = %22
%31 = load i8*, i8** %4
%32 = call i32 @free(i8* %31)
br label %33
33: ; preds = %30, %22
%34 = load i64, i64* %8
%35 = call i8* @emalloc(i64 %34)
store i8* %35, i8** %4
%36 = load i8*, i8** %4
%37 = load i64, i64* %8
%38 = load i8*, i8** %2
%39 = load i32, i32* %6
%40 = call i32 @snprintf(i8* %36, i64 %37, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %38, i32 %39)
br label %41
41: ; preds = %33
%42 = load i32, i32* %6
%43 = add i32 %42, 1
store i32 %43, i32* %6
br label %12
44: ; preds = %20
%45 = load i8*, i8** %4
%46 = load i8*, i8** %2
%47 = icmp eq i8* %45, %46
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = load i8*, i8** %2
%50 = call i8* @strdup(i8* %49)
br label %53
51: ; preds = %44
%52 = load i8*, i8** %4
br label %53
53: ; preds = %51, %48
%54 = phi i8* [ %50, %48 ], [ %52, %51 ]
ret i8* %54
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s.%u\00"
define i8* @unused_fname(i8* %0) {
%2 = tail call i32 @assert(i8* %0)
%3 = icmp eq i8* %0, null
br i1 %3, label %20, label %4
4: ; preds = %1, %15
%5 = phi i8* [ %16, %15 ], [ %0, %1 ]
%6 = phi i32 [ %18, %15 ], [ 2, %1 ]
%7 = tail call i32 @fname_is_avail(i8* %5)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %20
9: ; preds = %4
%10 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%11 = add i64 %10, 101
%12 = icmp eq i8* %5, %0
br i1 %12, label %15, label %13
13: ; preds = %9
%14 = tail call i32 @free(i8* %5)
br label %15
15: ; preds = %13, %9
%16 = tail call i8* @emalloc(i64 %11)
%17 = tail call i32 @snprintf(i8* %16, i64 %11, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %6)
%18 = add i32 %6, 1
%19 = icmp eq i8* %16, null
br i1 %19, label %20, label %4
20: ; preds = %4, %15, %1
%21 = phi i8* [ null, %1 ], [ null, %15 ], [ %5, %4 ]
%22 = icmp eq i8* %21, %0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = tail call i8* @strdup(i8* %0)
br label %25
25: ; preds = %20, %23
%26 = phi i8* [ %24, %23 ], [ %21, %20 ]
ret i8* %26
}