#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*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ perror_reply(int, char const *);
int /*<<< orphan*/ reply(int, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *strrchr(char const *, char);
char *gunique(const char *local) {
  static char *string = 0;
  struct stat st;
  int count;
  char *cp;
  cp = strrchr(local, '/');
  if (cp)
    *cp = '\0';
  if (stat(cp ? local : ".", &st) < 0) {
    perror_reply(553, cp ? local : ".");
    return ((char *)0);
  }
  if (cp)
    *cp = '/';
  if (string)
    free(string);
  string = malloc(strlen(local) + 5);
  if (string) {
    strcpy(string, local);
    cp = string + strlen(string);
    *cp++ = '.';
    for (count = 1; count < 100; count++) {
      (void)sprintf(cp, "%d", count);
      if (stat(string, &st) < 0)
        return string;
    }
  }
  reply(452, "Unique file name cannot be created.");
  return NULL;
}


x86 assembly (clang, -O0):
<gunique>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11c0 <gunique+0x30>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
cmpq   $0x0,-0x28(%rbp)
je     11d8 <gunique+0x48>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    11e8 <gunique+0x58>
lea    0xe46(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    11e8 <gunique+0x58>
mov    -0x30(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jge    1242 <gunique+0xb2>
cmpq   $0x0,-0x28(%rbp)
je     1217 <gunique+0x87>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1227 <gunique+0x97>
lea    0xe07(%rip),%rax
mov    %rax,-0x38(%rbp)
jmp    1227 <gunique+0x97>
mov    -0x38(%rbp),%rsi
mov    $0x229,%edi
call   10b0 <perror_reply@plt>
movq   $0x0,-0x8(%rbp)
jmp    135e <gunique+0x1ce>
cmpq   $0x0,-0x28(%rbp)
je     1254 <gunique+0xc4>
mov    -0x28(%rbp),%rax
movb   $0x2f,(%rax)
cmpq   $0x0,0x2e14(%rip)
je     126e <gunique+0xde>
mov    0x2e07(%rip),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%edi
add    $0x5,%edi
call   1090 <malloc@plt>
mov    %rax,0x2de8(%rip)
cmpq   $0x0,0x2de0(%rip)
je     1345 <gunique+0x1b5>
mov    0x2dd3(%rip),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    0x2dc3(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0x2db8(%rip),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movb   $0x2e,(%rax)
movl   $0x1,-0x1c(%rbp)
cmpl   $0x64,-0x1c(%rbp)
jge    1340 <gunique+0x1b0>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0xd02(%rip),%rsi
call   10a0 <sprintf@plt>
mov    0x2d66(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jge    132d <gunique+0x19d>
mov    0x2d4c(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    135e <gunique+0x1ce>
jmp    1332 <gunique+0x1a2>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    12e6 <gunique+0x156>
jmp    1345 <gunique+0x1b5>
mov    $0x1c4,%edi
lea    0xcb2(%rip),%rsi
call   1070 <reply@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gunique>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    $0x2f,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     11c9 <gunique+0x39>
mov    %rax,%rbp
movb   $0x0,(%rax)
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1080 <stat@plt>
test   %rax,%rax
js     128c <gunique+0xfc>
movb   $0x2f,0x0(%rbp)
jmp    11e4 <gunique+0x54>
lea    0xe55(%rip),%rbp
mov    %rsp,%rsi
mov    %rbp,%rdi
call   1080 <stat@plt>
test   %rax,%rax
js     128f <gunique+0xff>
mov    0x2e85(%rip),%rdi
test   %rdi,%rdi
je     11f5 <gunique+0x65>
call   1030 <free@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x5(%rax),%edi
call   1090 <malloc@plt>
mov    %rax,0x2e64(%rip)
test   %rax,%rax
je     1279 <gunique+0xe9>
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    0x2e4d(%rip),%rbp
mov    %rbp,%rdi
call   1050 <strlen@plt>
cltq
lea    (%rax,%rbp,1),%rbx
add    $0x1,%rbx
movb   $0x2e,0x0(%rbp,%rax,1)
mov    $0x1,%ebp
lea    0xdba(%rip),%r14
mov    %rsp,%r15
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   10a0 <sprintf@plt>
mov    0x2e0c(%rip),%rdi
mov    %r15,%rsi
call   1080 <stat@plt>
test   %rax,%rax
js     12a9 <gunique+0x119>
add    $0x1,%ebp
cmp    $0x64,%ebp
jne    1250 <gunique+0xc0>
lea    0xd83(%rip),%rsi
mov    $0x1c4,%edi
call   1070 <reply@plt>
jmp    129c <gunique+0x10c>
mov    %rbx,%rbp
mov    $0x229,%edi
mov    %rbp,%rsi
call   10b0 <perror_reply@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dc0(%rip),%rax
jmp    129e <gunique+0x10e>

LLVM IR (-O0):
%struct.stat = type { i32 }
@gunique.string = internal global i8* null
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [36 x i8] c"Unique file name cannot be created.\00"
define i8* @gunique(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i8* @strrchr(i8* %7, i8 signext 47)
store i8* %8, i8** %6
%9 = load i8*, i8** %6
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = load i8*, i8** %6
store i8 0, i8* %12
br label %13
13: ; preds = %11, %1
%14 = load i8*, i8** %6
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = load i8*, i8** %3
br label %19
18: ; preds = %13
br label %19
19: ; preds = %18, %16
%20 = phi i8* [ %17, %16 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %18 ]
%21 = call i64 @stat(i8* %20, %struct.stat* %4)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %32
23: ; preds = %19
%24 = load i8*, i8** %6
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = load i8*, i8** %3
br label %29
28: ; preds = %23
br label %29
29: ; preds = %28, %26
%30 = phi i8* [ %27, %26 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %28 ]
%31 = call i32 @perror_reply(i32 553, i8* %30)
store i8* null, i8** %2
br label %80
32: ; preds = %19
%33 = load i8*, i8** %6
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = load i8*, i8** %6
store i8 47, i8* %36
br label %37
37: ; preds = %35, %32
%38 = load i8*, i8** @gunique.string
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = load i8*, i8** @gunique.string
%42 = call i32 @free(i8* %41)
br label %43
43: ; preds = %40, %37
%44 = load i8*, i8** %3
%45 = call i32 @strlen(i8* %44)
%46 = add nsw i32 %45, 5
%47 = call i8* @malloc(i32 %46)
store i8* %47, i8** @gunique.string
%48 = load i8*, i8** @gunique.string
%49 = icmp ne i8* %48, null
br i1 %49, label %50, label %78
50: ; preds = %43
%51 = load i8*, i8** @gunique.string
%52 = load i8*, i8** %3
%53 = call i32 @strcpy(i8* %51, i8* %52)
%54 = load i8*, i8** @gunique.string
%55 = load i8*, i8** @gunique.string
%56 = call i32 @strlen(i8* %55)
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %54, i64 %57
store i8* %58, i8** %6
%59 = load i8*, i8** %6
%60 = getelementptr inbounds i8, i8* %59, i32 1
store i8* %60, i8** %6
store i8 46, i8* %59
store i32 1, i32* %5
br label %61
61: ; preds = %74, %50
%62 = load i32, i32* %5
%63 = icmp slt i32 %62, 100
br i1 %63, label %64, label %77
64: ; preds = %61
%65 = load i8*, i8** %6
%66 = load i32, i32* %5
%67 = call i32 @sprintf(i8* %65, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %66)
%68 = load i8*, i8** @gunique.string
%69 = call i64 @stat(i8* %68, %struct.stat* %4)
%70 = icmp slt i64 %69, 0
br i1 %70, label %71, label %73
71: ; preds = %64
%72 = load i8*, i8** @gunique.string
store i8* %72, i8** %2
br label %80
73: ; preds = %64
br label %74
74: ; preds = %73
%75 = load i32, i32* %5
%76 = add nsw i32 %75, 1
store i32 %76, i32* %5
br label %61
77: ; preds = %61
br label %78
78: ; preds = %77, %43
%79 = call i32 @reply(i32 452, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %2
br label %80
80: ; preds = %78, %71, %29
%81 = load i8*, i8** %2
ret i8* %81
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@gunique.string = internal unnamed_addr global i8* null
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [36 x i8] c"Unique file name cannot be created.\00"
define i8* @gunique(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = call i64 @stat(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %struct.stat* %2)
%8 = icmp slt i64 %7, 0
br i1 %8, label %12, label %16
9: ; preds = %1
store i8 0, i8* %4
%10 = call i64 @stat(i8* %0, %struct.stat* %2)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9, %6
%13 = phi i8* [ %0, %9 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %6 ]
%14 = call i32 @perror_reply(i32 553, i8* %13)
br label %46
15: ; preds = %9
store i8 47, i8* %4
br label %16
16: ; preds = %6, %15
%17 = load i8*, i8** @gunique.string
%18 = icmp eq i8* %17, null
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = call i32 @free(i8* %17)
br label %21
21: ; preds = %19, %16
%22 = call i32 @strlen(i8* %0)
%23 = add nsw i32 %22, 5
%24 = call i8* @malloc(i32 %23)
store i8* %24, i8** @gunique.string
%25 = icmp eq i8* %24, null
br i1 %25, label %44, label %26
26: ; preds = %21
%27 = call i32 @strcpy(i8* %24, i8* %0)
%28 = load i8*, i8** @gunique.string
%29 = call i32 @strlen(i8* %28)
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = getelementptr inbounds i8, i8* %31, i64 1
store i8 46, i8* %31
br label %36
33: ; preds = %36
%34 = add nuw nsw i32 %37, 1
%35 = icmp eq i32 %34, 100
br i1 %35, label %44, label %36
36: ; preds = %26, %33
%37 = phi i32 [ 1, %26 ], [ %34, %33 ]
%38 = call i32 @sprintf(i8* %32, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %37)
%39 = load i8*, i8** @gunique.string
%40 = call i64 @stat(i8* %39, %struct.stat* %2)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %33
42: ; preds = %36
%43 = load i8*, i8** @gunique.string
br label %46
44: ; preds = %33, %21
%45 = call i32 @reply(i32 452, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
br label %46
46: ; preds = %44, %42, %12
%47 = phi i8* [ null, %12 ], [ %43, %42 ], [ null, %44 ]
ret i8* %47
}