#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 atoi(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *strrchr(char *, char);
char *filename2template(char *filename, int *startindex) {
  int l = strlen(filename);
  char *newname = (char *)malloc(l + 5);
  while (1) {
    l--;
    if (l < 0 || strchr("0123456789", filename[l]))
      break;
  };
  if (l >= 0) {
    int lastdigit = l;
    int firstdigit = l;
    while (firstdigit && strchr("0123456789", filename[firstdigit - 1]))
      firstdigit--;
    *startindex = atoi(filename + firstdigit);
    memcpy(newname, filename, firstdigit);
    sprintf(newname + firstdigit, "%%%dd", lastdigit + 1 - firstdigit);
    strcat(newname + firstdigit, filename + lastdigit + 1);
    return newname;
  }
  char *dot = strrchr(filename, '.');
  if (dot) {
    int pos = dot - filename;
    memcpy(newname, filename, pos);
    newname[pos++] = '.';
    newname[pos++] = '%';
    newname[pos++] = 'd';
    strcpy(newname + pos, dot);
    *startindex = 1;
    return newname;
  }
  strcpy(newname, filename);
  strcat(newname, ".%d");
  *startindex = 1;
  return newname;
}


x86 assembly (clang, -O0):
<filename2template>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
add    $0x5,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     11f0 <filename2template+0x60>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
lea    0xe23(%rip),%rdi
movsbl (%rax,%rcx,1),%esi
call   1050 <strchr@plt>
cmp    $0x0,%rax
je     11f5 <filename2template+0x65>
jmp    11fa <filename2template+0x6a>
jmp    11bb <filename2template+0x2b>
cmpl   $0x0,-0x1c(%rbp)
jl     12d8 <filename2template+0x148>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x30(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x30(%rbp)
mov    %al,-0x3d(%rbp)
je     1246 <filename2template+0xb6>
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
lea    0xdcd(%rip),%rdi
movsbl (%rax,%rcx,1),%esi
call   1050 <strchr@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x3d(%rbp)
mov    -0x3d(%rbp),%al
test   $0x1,%al
jne    1256 <filename2template+0xc6>
jmp    1264 <filename2template+0xd4>
mov    -0x30(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x30(%rbp)
jmp    1210 <filename2template+0x80>
mov    -0x10(%rbp),%rdi
movslq -0x30(%rbp),%rax
add    %rax,%rdi
call   1090 <atoi@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1070 <memcpy@plt>
mov    -0x28(%rbp),%rdi
movslq -0x30(%rbp),%rax
add    %rax,%rdi
mov    -0x2c(%rbp),%edx
add    $0x1,%edx
sub    -0x30(%rbp),%edx
lea    0xd64(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x28(%rbp),%rdi
movslq -0x30(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
movslq -0x2c(%rbp),%rax
add    %rax,%rsi
add    $0x1,%rsi
call   10a0 <strcat@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    13af <filename2template+0x21f>
mov    -0x10(%rbp),%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1380 <filename2template+0x1f0>
mov    -0x38(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x3c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1070 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x3c(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x3c(%rbp)
movslq %ecx,%rcx
movb   $0x2e,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    -0x3c(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x3c(%rbp)
movslq %ecx,%rcx
movb   $0x25,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    -0x3c(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x3c(%rbp)
movslq %ecx,%rcx
movb   $0x64,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
movslq -0x3c(%rbp),%rax
add    %rax,%rdi
mov    -0x38(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    13af <filename2template+0x21f>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rdi
lea    0xc79(%rip),%rsi
call   10a0 <strcat@plt>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filename2template>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,0x10(%rsp)
mov    %rdi,%r12
call   1040 <strlen@plt>
mov    %eax,%ebx
lea    0x5(%rbx),%edi
call   1080 <malloc@plt>
mov    %rax,0x8(%rsp)
mov    %ebx,%ebp
add    $0x1,%ebx
mov    %rbp,%r15
shl    $0x20,%r15
lea    0xe33(%rip),%r13
movabs $0xffffffff00000000,%r14
ff ff ff
nopw   0x0(%rax,%rax,1)
add    $0xffffffff,%ebx
test   %ebx,%ebx
jle    1237 <filename2template+0xa7>
movsbl -0x1(%r12,%rbp,1),%esi
mov    %r13,%rdi
call   1050 <strchr@plt>
add    %r14,%r15
add    $0xffffffffffffffff,%rbp
test   %rax,%rax
je     11e0 <filename2template+0x50>
mov    %ebp,%ebp
lea    0xdf6(%rip),%r13
mov    %ebx,%r14d
nopl   (%rax)
sub    $0x1,%rbp
jb     12c4 <filename2template+0x134>
mov    %ebp,%eax
movsbl (%r12,%rax,1),%esi
mov    %r13,%rdi
call   1050 <strchr@plt>
add    $0xffffffff,%r14d
test   %rax,%rax
jne    1210 <filename2template+0x80>
jmp    12c7 <filename2template+0x137>
mov    %r12,%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     131d <filename2template+0x18d>
mov    %rax,%r13
mov    %rax,%rbx
sub    %r12,%rbx
mov    0x8(%rsp),%rbp
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   1070 <memcpy@plt>
mov    %rbx,%rax
shl    $0x20,%rax
movslq %ebx,%rcx
movb   $0x2e,0x0(%rbp,%rcx,1)
movabs $0x100000000,%rcx
add    %rax,%rcx
sar    $0x20,%rcx
movb   $0x25,0x0(%rbp,%rcx,1)
movabs $0x200000000,%rcx
add    %rax,%rcx
sar    $0x20,%rcx
movb   $0x64,0x0(%rbp,%rcx,1)
movabs $0x300000000,%rcx
add    %rax,%rcx
sar    $0x20,%rcx
mov    %rbp,%rdi
add    %rcx,%rdi
mov    %r13,%rsi
call   1030 <strcpy@plt>
jmp    133c <filename2template+0x1ac>
xor    %r14d,%r14d
movslq %r14d,%r13
lea    (%r12,%r13,1),%rdi
call   1090 <atoi@plt>
mov    0x10(%rsp),%rcx
mov    %eax,(%rcx)
mov    0x8(%rsp),%rbp
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %r13d,%edx
call   1070 <memcpy@plt>
mov    %rbp,%r14
add    %r13,%r14
sub    %r13d,%ebx
lea    0xd0e(%rip),%rsi
mov    %r14,%rdi
mov    %ebx,%edx
call   10b0 <sprintf@plt>
sar    $0x20,%r15
lea    (%r12,%r15,1),%rsi
add    $0x1,%rsi
mov    %r14,%rdi
call   10a0 <strcat@plt>
jmp    1347 <filename2template+0x1b7>
mov    0x8(%rsp),%rbp
mov    %rbp,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
lea    0xcdd(%rip),%rsi
mov    %rbp,%rdi
call   10a0 <strcat@plt>
mov    0x10(%rsp),%rax
movl   $0x1,(%rax)
mov    %rbp,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"0123456789\00"
@.str.1 = [6 x i8] c"%%%dd\00"
@.str.2 = [4 x i8] c".%d\00"
define i8* @filename2template(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%12 = load i8*, i8** %4
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = add nsw i32 %14, 5
%16 = call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %7
br label %18
18: ; preds = %2, %32
%19 = load i32, i32* %6
%20 = add nsw i32 %19, -1
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp slt i32 %21, 0
br i1 %22, label %31, label %23
23: ; preds = %18
%24 = load i8*, i8** %4
%25 = load i32, i32* %6
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i8, i8* %27
%29 = call i64 @strchr(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8 signext %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %23, %18
br label %33
32: ; preds = %23
br label %18
33: ; preds = %31
%34 = load i32, i32* %6
%35 = icmp sge i32 %34, 0
br i1 %35, label %36, label %87
36: ; preds = %33
%37 = load i32, i32* %6
store i32 %37, i32* %8
%38 = load i32, i32* %6
store i32 %38, i32* %9
br label %39
39: ; preds = %53, %36
%40 = load i32, i32* %9
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %51
42: ; preds = %39
%43 = load i8*, i8** %4
%44 = load i32, i32* %9
%45 = sub nsw i32 %44, 1
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %43, i64 %46
%48 = load i8, i8* %47
%49 = call i64 @strchr(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8 signext %48)
%50 = icmp ne i64 %49, 0
br label %51
51: ; preds = %42, %39
%52 = phi i1 [ false, %39 ], [ %50, %42 ]
br i1 %52, label %53, label %56
53: ; preds = %51
%54 = load i32, i32* %9
%55 = add nsw i32 %54, -1
store i32 %55, i32* %9
br label %39
56: ; preds = %51
%57 = load i8*, i8** %4
%58 = load i32, i32* %9
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %57, i64 %59
%61 = call i32 @atoi(i8* %60)
%62 = load i32*, i32** %5
store i32 %61, i32* %62
%63 = load i8*, i8** %7
%64 = load i8*, i8** %4
%65 = load i32, i32* %9
%66 = call i32 @memcpy(i8* %63, i8* %64, i32 %65)
%67 = load i8*, i8** %7
%68 = load i32, i32* %9
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds i8, i8* %67, i64 %69
%71 = load i32, i32* %8
%72 = add nsw i32 %71, 1
%73 = load i32, i32* %9
%74 = sub nsw i32 %72, %73
%75 = call i32 @sprintf(i8* %70, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %74)
%76 = load i8*, i8** %7
%77 = load i32, i32* %9
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i8, i8* %76, i64 %78
%80 = load i8*, i8** %4
%81 = load i32, i32* %8
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds i8, i8* %80, i64 %82
%84 = getelementptr inbounds i8, i8* %83, i64 1
%85 = call i32 @strcat(i8* %79, i8* %84)
%86 = load i8*, i8** %7
store i8* %86, i8** %3
br label %134
87: ; preds = %33
%88 = load i8*, i8** %4
%89 = call i8* @strrchr(i8* %88, i8 signext 46)
store i8* %89, i8** %10
%90 = load i8*, i8** %10
%91 = icmp ne i8* %90, null
br i1 %91, label %92, label %126
92: ; preds = %87
%93 = load i8*, i8** %10
%94 = load i8*, i8** %4
%95 = ptrtoint i8* %93 to i64
%96 = ptrtoint i8* %94 to i64
%97 = sub i64 %95, %96
%98 = trunc i64 %97 to i32
store i32 %98, i32* %11
%99 = load i8*, i8** %7
%100 = load i8*, i8** %4
%101 = load i32, i32* %11
%102 = call i32 @memcpy(i8* %99, i8* %100, i32 %101)
%103 = load i8*, i8** %7
%104 = load i32, i32* %11
%105 = add nsw i32 %104, 1
store i32 %105, i32* %11
%106 = sext i32 %104 to i64
%107 = getelementptr inbounds i8, i8* %103, i64 %106
store i8 46, i8* %107
%108 = load i8*, i8** %7
%109 = load i32, i32* %11
%110 = add nsw i32 %109, 1
store i32 %110, i32* %11
%111 = sext i32 %109 to i64
%112 = getelementptr inbounds i8, i8* %108, i64 %111
store i8 37, i8* %112
%113 = load i8*, i8** %7
%114 = load i32, i32* %11
%115 = add nsw i32 %114, 1
store i32 %115, i32* %11
%116 = sext i32 %114 to i64
%117 = getelementptr inbounds i8, i8* %113, i64 %116
store i8 100, i8* %117
%118 = load i8*, i8** %7
%119 = load i32, i32* %11
%120 = sext i32 %119 to i64
%121 = getelementptr inbounds i8, i8* %118, i64 %120
%122 = load i8*, i8** %10
%123 = call i32 @strcpy(i8* %121, i8* %122)
%124 = load i32*, i32** %5
store i32 1, i32* %124
%125 = load i8*, i8** %7
store i8* %125, i8** %3
br label %134
126: ; preds = %87
%127 = load i8*, i8** %7
%128 = load i8*, i8** %4
%129 = call i32 @strcpy(i8* %127, i8* %128)
%130 = load i8*, i8** %7
%131 = call i32 @strcat(i8* %130, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%132 = load i32*, i32** %5
store i32 1, i32* %132
%133 = load i8*, i8** %7
store i8* %133, i8** %3
br label %134
134: ; preds = %126, %92, %56
%135 = load i8*, i8** %3
ret i8* %135
}

LLVM IR (-O2):
@.str = [11 x i8] c"0123456789\00"
@.str.1 = [6 x i8] c"%%%dd\00"
@.str.2 = [4 x i8] c".%d\00"
define i8* @filename2template(i8* %0, i32* nocapture writeonly %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = add nsw i32 %3, 5
%5 = tail call i64 @malloc(i32 %4)
%6 = inttoptr i64 %5 to i8*
%7 = zext i32 %3 to i64
br label %8
8: ; preds = %13, %2
%9 = phi i64 [ %10, %13 ], [ %7, %2 ]
%10 = add nsw i64 %9, -1
%11 = trunc i64 %9 to i32
%12 = icmp slt i32 %11, 1
br i1 %12, label %47, label %13
13: ; preds = %8
%14 = getelementptr inbounds i8, i8* %0, i64 %10
%15 = load i8, i8* %14
%16 = tail call i64 @strchr(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8 signext %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %8, label %18
18: ; preds = %13
%19 = trunc i64 %9 to i32
%20 = and i64 %10, 4294967295
br label %21
21: ; preds = %18, %24
%22 = phi i64 [ %20, %18 ], [ %25, %24 ]
%23 = icmp eq i64 %22, 0
br i1 %23, label %33, label %24
24: ; preds = %21
%25 = add nsw i64 %22, -1
%26 = and i64 %25, 4294967295
%27 = getelementptr inbounds i8, i8* %0, i64 %26
%28 = load i8, i8* %27
%29 = tail call i64 @strchr(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8 signext %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %21
31: ; preds = %24
%32 = trunc i64 %22 to i32
br label %33
33: ; preds = %21, %31
%34 = phi i32 [ %32, %31 ], [ 0, %21 ]
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %0, i64 %35
%37 = tail call i32 @atoi(i8* %36)
store i32 %37, i32* %1
%38 = tail call i32 @memcpy(i8* %6, i8* %0, i32 %34)
%39 = getelementptr inbounds i8, i8* %6, i64 %35
%40 = sub nsw i32 %19, %34
%41 = tail call i32 @sprintf(i8* dereferenceable(1) %39, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %40)
%42 = shl i64 %10, 32
%43 = ashr exact i64 %42, 32
%44 = getelementptr inbounds i8, i8* %0, i64 %43
%45 = getelementptr inbounds i8, i8* %44, i64 1
%46 = tail call i32 @strcat(i8* %39, i8* %45)
br label %73
47: ; preds = %8
%48 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 46)
%49 = icmp eq i8* %48, null
br i1 %49, label %69, label %50
50: ; preds = %47
%51 = ptrtoint i8* %48 to i64
%52 = ptrtoint i8* %0 to i64
%53 = sub i64 %51, %52
%54 = trunc i64 %53 to i32
%55 = tail call i32 @memcpy(i8* %6, i8* %0, i32 %54)
%56 = shl i64 %53, 32
%57 = ashr exact i64 %56, 32
%58 = getelementptr inbounds i8, i8* %6, i64 %57
store i8 46, i8* %58
%59 = add i64 %56, 4294967296
%60 = ashr exact i64 %59, 32
%61 = getelementptr inbounds i8, i8* %6, i64 %60
store i8 37, i8* %61
%62 = add i64 %56, 8589934592
%63 = ashr exact i64 %62, 32
%64 = getelementptr inbounds i8, i8* %6, i64 %63
store i8 100, i8* %64
%65 = add i64 %56, 12884901888
%66 = ashr exact i64 %65, 32
%67 = getelementptr inbounds i8, i8* %6, i64 %66
%68 = tail call i32 @strcpy(i8* %67, i8* %48)
br label %72
69: ; preds = %47
%70 = tail call i32 @strcpy(i8* %6, i8* %0)
%71 = tail call i32 @strcat(i8* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
br label %72
72: ; preds = %69, %50
store i32 1, i32* %1
br label %73
73: ; preds = %72, %33
ret i8* %6
}