#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*/ snprintf(char *, int, char *, char *, ...);
int stat(char *, struct stat *);
char *strrchr(char *, char);
int /*<<< orphan*/ zstrncpy(char *, char *, int);
int MIME_test_uniquename(char *path, char *fname, int method) {
  struct stat buf;
  char newname[1023 + 1];
  char scratch[1023 + 1];
  char *frontname, *extention;
  int cleared = 0;
  int count = 1;
  if (!scratch) {
    return -1;
  }
  frontname = extention = NULL;
  if (method == 1) {
    zstrncpy(scratch, fname, 1023);
    frontname = scratch;
    extention = strrchr(scratch, '.');
    if (extention) {
      *extention = '\0';
      extention++;
    } else {
      method = 3;
    }
  }
  snprintf(newname, 1023, "%s/%s", path, fname);
  while (!cleared) {
    if ((stat(newname, &buf) == -1)) {
      cleared++;
    } else {
      if (method == 2) {
        snprintf(newname, 1023, "%s/%d_%s", path, count, fname);
      } else if (method == 1) {
        snprintf(newname, 1023, "%s/%s_%d.%s", path, frontname, count,
                 extention);
      } else if (method == 3) {
        snprintf(newname, 1023, "%s/%s_%d", path, fname, count);
      }
      count++;
    }
  }
  if (count > 1) {
    frontname = strrchr(newname, '/');
    if (frontname)
      frontname++;
    else
      frontname = newname;
    zstrncpy(fname, frontname, 128);
  }
  return 0;
}


x86 assembly (clang, -O0):
<MIME_test_uniquename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x834(%rbp)
movl   $0x1,-0x838(%rbp)
movq   $0x0,-0x830(%rbp)
movq   $0x0,-0x828(%rbp)
cmpl   $0x1,-0x14(%rbp)
jne    1200 <MIME_test_uniquename+0xc0>
lea    -0x820(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x3ff,%edx
call   1060 <zstrncpy@plt>
lea    -0x820(%rbp),%rax
mov    %rax,-0x828(%rbp)
lea    -0x820(%rbp),%rdi
mov    $0x2e,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x830(%rbp)
cmpq   $0x0,-0x830(%rbp)
je     11f4 <MIME_test_uniquename+0xb4>
mov    -0x830(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x830(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x830(%rbp)
jmp    11fb <MIME_test_uniquename+0xbb>
movl   $0x3,-0x14(%rbp)
jmp    1200 <MIME_test_uniquename+0xc0>
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    $0x3ff,%esi
lea    0xde5(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
cmpl   $0x0,-0x834(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    123b <MIME_test_uniquename+0xfb>
jmp    1337 <MIME_test_uniquename+0x1f7>
lea    -0x420(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
jne    1268 <MIME_test_uniquename+0x128>
mov    -0x834(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x834(%rbp)
jmp    1332 <MIME_test_uniquename+0x1f2>
cmpl   $0x2,-0x14(%rbp)
jne    12a0 <MIME_test_uniquename+0x160>
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    -0x838(%rbp),%r8d
mov    -0x10(%rbp),%r9
mov    $0x3ff,%esi
lea    0xd72(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    1323 <MIME_test_uniquename+0x1e3>
cmpl   $0x1,-0x14(%rbp)
jne    12e6 <MIME_test_uniquename+0x1a6>
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    -0x828(%rbp),%r8
mov    -0x838(%rbp),%r9d
mov    -0x830(%rbp),%rax
mov    $0x3ff,%esi
lea    0xd39(%rip),%rdx
mov    %rax,(%rsp)
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    131e <MIME_test_uniquename+0x1de>
cmpl   $0x3,-0x14(%rbp)
jne    1319 <MIME_test_uniquename+0x1d9>
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    -0x838(%rbp),%r9d
mov    $0x3ff,%esi
lea    0xd09(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    131e <MIME_test_uniquename+0x1de>
jmp    1323 <MIME_test_uniquename+0x1e3>
mov    -0x838(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x838(%rbp)
jmp    1222 <MIME_test_uniquename+0xe2>
cmpl   $0x1,-0x838(%rbp)
jle    13a4 <MIME_test_uniquename+0x264>
lea    -0x420(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x828(%rbp)
cmpq   $0x0,-0x828(%rbp)
je     1381 <MIME_test_uniquename+0x241>
mov    -0x828(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x828(%rbp)
jmp    138f <MIME_test_uniquename+0x24f>
lea    -0x420(%rbp),%rax
mov    %rax,-0x828(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x828(%rbp),%rsi
mov    $0x80,%edx
call   1060 <zstrncpy@plt>
xor    %eax,%eax
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<MIME_test_uniquename>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x828,%rsp
mov    %rsi,%r14
mov    %rdi,%r13
cmp    $0x1,%edx
jne    119c <MIME_test_uniquename+0x5c>
lea    0x420(%rsp),%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x3ff,%edx
call   1060 <zstrncpy@plt>
mov    %rbx,0x10(%rsp)
mov    %rbx,%rdi
mov    $0x2e,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     11a7 <MIME_test_uniquename+0x67>
mov    %rax,%rbx
movb   $0x0,(%rax)
add    $0x1,%rbx
mov    $0x1,%ebp
jmp    11ae <MIME_test_uniquename+0x6e>
mov    %edx,%ebp
xor    %eax,%eax
mov    %rax,0x10(%rsp)
jmp    11ac <MIME_test_uniquename+0x6c>
mov    $0x3,%ebp
xor    %ebx,%ebx
lea    0xe4b(%rip),%rdx
lea    0x20(%rsp),%r15
mov    %r15,%rdi
mov    $0x3ff,%esi
mov    %r13,%rcx
mov    %r14,%r8
xor    %eax,%eax
call   1030 <snprintf@plt>
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
je     12be <MIME_test_uniquename+0x17e>
mov    %eax,0xc(%rsp)
mov    %rbx,0x18(%rsp)
mov    $0x1,%r15d
lea    0x20(%rsp),%rbx
lea    0x8(%rsp),%r12
jmp    1233 <MIME_test_uniquename+0xf3>
mov    %rbx,%rdi
mov    $0x3ff,%esi
lea    0xe0c(%rip),%rdx
mov    %r13,%rcx
mov    %r14,%r8
mov    %r15d,%r9d
xor    %eax,%eax
call   1030 <snprintf@plt>
add    $0x1,%r15d
mov    %rbx,%rdi
mov    %r12,%rsi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
je     1292 <MIME_test_uniquename+0x152>
cmp    $0x1,%ebp
je     1270 <MIME_test_uniquename+0x130>
cmp    $0x3,%ebp
je     1200 <MIME_test_uniquename+0xc0>
cmp    $0x2,%ebp
jne    121f <MIME_test_uniquename+0xdf>
mov    %rbx,%rdi
mov    $0x3ff,%esi
lea    0xdb5(%rip),%rdx
mov    %r13,%rcx
mov    %r15d,%r8d
mov    %r14,%r9
xor    %eax,%eax
call   1030 <snprintf@plt>
jmp    121f <MIME_test_uniquename+0xdf>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x18(%rsp),%rax
mov    %rax,(%rsp)
mov    %rbx,%rdi
mov    $0x3ff,%esi
lea    0xd87(%rip),%rdx
mov    %r13,%rcx
mov    0x10(%rsp),%r8
jmp    1215 <MIME_test_uniquename+0xd5>
cmpl   $0xffffffff,0xc(%rsp)
je     12be <MIME_test_uniquename+0x17e>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
lea    0x1(%rax),%rsi
test   %rax,%rax
cmove  %rbx,%rsi
mov    %r14,%rdi
mov    $0x80,%edx
call   1060 <zstrncpy@plt>
xor    %eax,%eax
add    $0x828,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [9 x i8] c"%s/%d_%s\00"
@.str.2 = [12 x i8] c"%s/%s_%d.%s\00"
@.str.3 = [9 x i8] c"%s/%s_%d\00"
define i32 @MIME_test_uniquename(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca [1024 x i8]
%9 = alloca [1024 x i8]
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %12
store i32 1, i32* %13
store i8* null, i8** %11
store i8* null, i8** %10
%14 = load i32, i32* %6
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %31
16: ; preds = %3
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%18 = load i8*, i8** %5
%19 = call i32 @zstrncpy(i8* %17, i8* %18, i32 1023)
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
store i8* %20, i8** %10
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%22 = call i8* @strrchr(i8* %21, i8 signext 46)
store i8* %22, i8** %11
%23 = load i8*, i8** %11
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %29
25: ; preds = %16
%26 = load i8*, i8** %11
store i8 0, i8* %26
%27 = load i8*, i8** %11
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %11
br label %30
29: ; preds = %16
store i32 3, i32* %6
br label %30
30: ; preds = %29, %25
br label %31
31: ; preds = %30, %3
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%33 = load i8*, i8** %4
%34 = load i8*, i8** %5
%35 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %32, i32 1023, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %33, i8* %34)
br label %36
36: ; preds = %80, %31
%37 = load i32, i32* %12
%38 = icmp ne i32 %37, 0
%39 = xor i1 %38, true
br i1 %39, label %40, label %81
40: ; preds = %36
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%42 = call i32 @stat(i8* %41, %struct.stat* %7)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = load i32, i32* %12
%46 = add nsw i32 %45, 1
store i32 %46, i32* %12
br label %80
47: ; preds = %40
%48 = load i32, i32* %6
%49 = icmp eq i32 %48, 2
br i1 %49, label %50, label %56
50: ; preds = %47
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%52 = load i8*, i8** %4
%53 = load i32, i32* %13
%54 = load i8*, i8** %5
%55 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %51, i32 1023, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %52, i32 %53, i8* %54)
br label %77
56: ; preds = %47
%57 = load i32, i32* %6
%58 = icmp eq i32 %57, 1
br i1 %58, label %59, label %66
59: ; preds = %56
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%61 = load i8*, i8** %4
%62 = load i8*, i8** %10
%63 = load i32, i32* %13
%64 = load i8*, i8** %11
%65 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %60, i32 1023, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %61, i8* %62, i32 %63, i8* %64)
br label %76
66: ; preds = %56
%67 = load i32, i32* %6
%68 = icmp eq i32 %67, 3
br i1 %68, label %69, label %75
69: ; preds = %66
%70 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%71 = load i8*, i8** %4
%72 = load i8*, i8** %5
%73 = load i32, i32* %13
%74 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %70, i32 1023, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %71, i8* %72, i32 %73)
br label %75
75: ; preds = %69, %66
br label %76
76: ; preds = %75, %59
br label %77
77: ; preds = %76, %50
%78 = load i32, i32* %13
%79 = add nsw i32 %78, 1
store i32 %79, i32* %13
br label %80
80: ; preds = %77, %44
br label %36
81: ; preds = %36
%82 = load i32, i32* %13
%83 = icmp sgt i32 %82, 1
br i1 %83, label %84, label %98
84: ; preds = %81
%85 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%86 = call i8* @strrchr(i8* %85, i8 signext 47)
store i8* %86, i8** %10
%87 = load i8*, i8** %10
%88 = icmp ne i8* %87, null
br i1 %88, label %89, label %92
89: ; preds = %84
%90 = load i8*, i8** %10
%91 = getelementptr inbounds i8, i8* %90, i32 1
store i8* %91, i8** %10
br label %94
92: ; preds = %84
%93 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
store i8* %93, i8** %10
br label %94
94: ; preds = %92, %89
%95 = load i8*, i8** %5
%96 = load i8*, i8** %10
%97 = call i32 @zstrncpy(i8* %95, i8* %96, i32 128)
br label %98
98: ; preds = %94, %81
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [9 x i8] c"%s/%d_%s\00"
@.str.2 = [12 x i8] c"%s/%s_%d.%s\00"
@.str.3 = [9 x i8] c"%s/%s_%d\00"
define i32 @MIME_test_uniquename(i8* %0, i8* %1, i32 %2) {
%4 = alloca %struct.stat
%5 = alloca [1024 x i8]
%6 = alloca [1024 x i8]
%7 = bitcast %struct.stat* %4 to i8*
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%10 = icmp eq i32 %2, 1
br i1 %10, label %11, label %17
11: ; preds = %3
%12 = call i32 @zstrncpy(i8* %9, i8* %1, i32 1023)
%13 = call i8* @strrchr(i8* %9, i8 signext 46)
%14 = icmp eq i8* %13, null
br i1 %14, label %17, label %15
15: ; preds = %11
store i8 0, i8* %13
%16 = getelementptr inbounds i8, i8* %13, i64 1
br label %17
17: ; preds = %11, %15, %3
%18 = phi i32 [ 1, %15 ], [ %2, %3 ], [ 3, %11 ]
%19 = phi i8* [ %9, %15 ], [ null, %3 ], [ %9, %11 ]
%20 = phi i8* [ %16, %15 ], [ null, %3 ], [ null, %11 ]
%21 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %8, i32 1023, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%22 = call i32 @stat(i8* %8, %struct.stat* %4)
%23 = icmp eq i32 %22, -1
br i1 %23, label %43, label %24
24: ; preds = %17, %32
%25 = phi i32 [ %33, %32 ], [ 1, %17 ]
switch i32 %18, label %32 [
i32 2, label %26
i32 1, label %28
i32 3, label %30
]
26: ; preds = %24
%27 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %8, i32 1023, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %25, i8* %1)
br label %32
28: ; preds = %24
%29 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %8, i32 1023, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %19, i32 %25, i8* %20)
br label %32
30: ; preds = %24
%31 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %8, i32 1023, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %1, i32 %25)
br label %32
32: ; preds = %26, %30, %28, %24
%33 = add nuw nsw i32 %25, 1
%34 = call i32 @stat(i8* %8, %struct.stat* %4)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %24
36: ; preds = %32
br i1 %23, label %43, label %37
37: ; preds = %36
%38 = call i8* @strrchr(i8* %8, i8 signext 47)
%39 = icmp eq i8* %38, null
%40 = getelementptr inbounds i8, i8* %38, i64 1
%41 = select i1 %39, i8* %8, i8* %40
%42 = call i32 @zstrncpy(i8* %1, i8* %41, i32 128)
br label %43
43: ; preds = %17, %37, %36
ret i32 0
}