#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*/ F_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
char *calloc(int, int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ perror(char *);
int rename(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, ...);
void wrename(char *path, int pathlen, int begin) {
  int count = begin + 1;
  char *newpath = calloc(1, pathlen + 2);
  char *tmppath = calloc(1, pathlen + 2);
  sprintf(newpath, "%s.%d", path, count);
  if (access(newpath, F_OK)) {
    int i = count;
    for (i; i > 0; i--) {
      sprintf(newpath, "%s.%d", path, i);
      int t = i - 1;
      if (t) {
        sprintf(tmppath, "%s.%d", path, i - 1);
      } else {
        sprintf(tmppath, "%s", path);
      }
      int re = rename(tmppath, newpath);
      if (re)
        perror("rename");
    }
  } else {
    wrename(path, pathlen, count);
  }
  free(tmppath);
  free(newpath);
}


x86 assembly (clang, -O0):
<wrename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%esi
add    $0x2,%esi
mov    $0x1,%edi
call   1050 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%esi
add    $0x2,%esi
mov    $0x1,%edi
call   1050 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xe3b(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
call   1060 <access@plt>
cmp    $0x0,%rax
je     1299 <wrename+0x129>
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jle    1294 <wrename+0x124>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
lea    0xdf6(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
mov    -0x2c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
je     1245 <wrename+0xd5>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
sub    $0x1,%ecx
lea    0xdc7(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
jmp    125b <wrename+0xeb>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xdb2(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <rename@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     1281 <wrename+0x111>
lea    0xd8d(%rip),%rdi
call   1070 <perror@plt>
jmp    1286 <wrename+0x116>
mov    -0x2c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x2c(%rbp)
jmp    11ee <wrename+0x7e>
jmp    12a8 <wrename+0x138>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1030 <wrename@plt>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wrename>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rdi,%r14
lea    0x1(%rbp),%r13d
mov    %rsi,(%rsp)
lea    0x2(%rsi),%ebx
mov    $0x1,%edi
mov    %ebx,%esi
call   1060 <calloc@plt>
mov    %rax,%r15
mov    $0x1,%edi
mov    %ebx,%esi
call   1060 <calloc@plt>
mov    %rax,%r12
lea    0xe40(%rip),%rsi
mov    %r15,%rdi
mov    %r14,%rdx
mov    %r13d,%ecx
xor    %eax,%eax
call   10a0 <sprintf@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1070 <access@plt>
test   %rax,%rax
je     125b <wrename+0xdb>
test   %ebp,%ebp
js     126a <wrename+0xea>
lea    0xe0b(%rip),%r13
mov    %ebp,%ebx
jmp    1221 <wrename+0xa1>
nopl   0x0(%rax)
mov    %r14,%rsi
call   1050 <strcpy@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   1090 <rename@plt>
test   %eax,%eax
jne    124d <wrename+0xcd>
lea    0x2(%rbx),%eax
mov    %ebx,%ebp
cmp    $0x1,%eax
jbe    126a <wrename+0xea>
lea    0x1(%rbp),%ecx
mov    %r15,%rdi
mov    %r13,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   10a0 <sprintf@plt>
mov    %r12,%rdi
sub    $0x1,%ebx
jb     1200 <wrename+0x80>
mov    %r13,%rsi
mov    %r14,%rdx
mov    %ebp,%ecx
xor    %eax,%eax
call   10a0 <sprintf@plt>
jmp    1208 <wrename+0x88>
lea    0xdb2(%rip),%rdi
call   1080 <perror@plt>
jmp    1217 <wrename+0x97>
mov    %r14,%rdi
mov    (%rsp),%rsi
mov    %r13d,%edx
call   1030 <wrename@plt>
mov    %r12,%rdi
call   1040 <free@plt>
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <free@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"%s.%d\00"
@F_OK = global i32 0
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [7 x i8] c"rename\00"
define void @wrename(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%13 = load i32, i32* %6
%14 = add nsw i32 %13, 1
store i32 %14, i32* %7
%15 = load i32, i32* %5
%16 = add nsw i32 %15, 2
%17 = call i8* @calloc(i32 1, i32 %16)
store i8* %17, i8** %8
%18 = load i32, i32* %5
%19 = add nsw i32 %18, 2
%20 = call i8* @calloc(i32 1, i32 %19)
store i8* %20, i8** %9
%21 = load i8*, i8** %8
%22 = load i8*, i8** %4
%23 = load i32, i32* %7
%24 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %21, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %22, i32 %23)
%25 = load i8*, i8** %8
%26 = load i32, i32* @F_OK
%27 = call i64 @access(i8* %25, i32 %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %67
29: ; preds = %3
%30 = load i32, i32* %7
store i32 %30, i32* %10
%31 = load i32, i32* %10
br label %32
32: ; preds = %63, %29
%33 = load i32, i32* %10
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %66
35: ; preds = %32
%36 = load i8*, i8** %8
%37 = load i8*, i8** %4
%38 = load i32, i32* %10
%39 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %36, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %37, i32 %38)
%40 = load i32, i32* %10
%41 = sub nsw i32 %40, 1
store i32 %41, i32* %11
%42 = load i32, i32* %11
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %35
%45 = load i8*, i8** %9
%46 = load i8*, i8** %4
%47 = load i32, i32* %10
%48 = sub nsw i32 %47, 1
%49 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %45, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %46, i32 %48)
br label %54
50: ; preds = %35
%51 = load i8*, i8** %9
%52 = load i8*, i8** %4
%53 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %51, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %52)
br label %54
54: ; preds = %50, %44
%55 = load i8*, i8** %9
%56 = load i8*, i8** %8
%57 = call i32 @rename(i8* %55, i8* %56)
store i32 %57, i32* %12
%58 = load i32, i32* %12
%59 = icmp ne i32 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %54
%61 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %62
62: ; preds = %60, %54
br label %63
63: ; preds = %62
%64 = load i32, i32* %10
%65 = add nsw i32 %64, -1
store i32 %65, i32* %10
br label %32
66: ; preds = %32
br label %71
67: ; preds = %3
%68 = load i8*, i8** %4
%69 = load i32, i32* %5
%70 = load i32, i32* %7
br label %71
71: ; preds = %67, %66
%72 = load i8*, i8** %9
%73 = call i32 @free(i8* %72)
%74 = load i8*, i8** %8
%75 = call i32 @free(i8* %74)
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s.%d\00"
@F_OK = global i32 0
@.str.2 = [7 x i8] c"rename\00"
define void @wrename(i8* %0, i32 %1, i32 %2) {
%4 = add nsw i32 %2, 1
%5 = add nsw i32 %1, 2
%6 = tail call i8* @calloc(i32 1, i32 %5)
%7 = tail call i8* @calloc(i32 1, i32 %5)
%8 = tail call i32 (i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %6, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %4)
%9 = load i32, i32* @F_OK
%10 = tail call i64 @access(i8* %6, i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %30, label %12
12: ; preds = %3
%13 = icmp sgt i32 %2, -1
br i1 %13, label %14, label %31
14: ; preds = %12, %28
%15 = phi i32 [ %17, %28 ], [ %4, %12 ]
%16 = tail call i32 (i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %6, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %15)
%17 = add nsw i32 %15, -1
%18 = icmp eq i32 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %14
%20 = tail call i32 (i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %7, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %17)
br label %23
21: ; preds = %14
%22 = tail call i8* @strcpy(i8* dereferenceable(1) %7, i8* dereferenceable(1) %0)
br label %23
23: ; preds = %21, %19
%24 = tail call i32 @rename(i8* %7, i8* %6)
%25 = icmp eq i32 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %26, %23
%29 = icmp ugt i32 %15, 1
br i1 %29, label %14, label %31
30: ; preds = %3
tail call void @wrename(i8* %0, i32 %1, i32 %4)
br label %31
31: ; preds = %28, %12, %30
%32 = tail call i32 @free(i8* %7)
%33 = tail call i32 @free(i8* %6)
ret void
}