#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*/ asprintf(char **, char *, char *, int);
int /*<<< orphan*/ free(char *);
char *replaceall(char *, char *, char *);
char *strdup(char *);
int replace_tokens(char **target, char **tokens, int toknum, char *tokmark,
                   int starttok) {
  int i;
  char *tmp;
  char *var;
  for (i = 0; i <= toknum; i++) {
    asprintf(&var, "%s%i", tokmark, i - starttok);
    tmp = replaceall(*target, var, tokens[i]);
    free(var);
    free(*target);
    *target = strdup(tmp);
    free(tmp);
  }
  return i;
}


x86 assembly (clang, -O0):
<replace_tokens>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x14(%rbp),%eax
jg     11e7 <replace_tokens+0xa7>
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%ecx
sub    -0x24(%rbp),%ecx
lea    -0x38(%rbp),%rdi
lea    0xe7d(%rip),%rsi
call   1050 <asprintf@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
call   1040 <replaceall@plt>
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1162 <replace_tokens+0x22>
mov    -0x28(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_tokens>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,0x8(%rsp)
test   %edx,%edx
js     11e0 <replace_tokens+0xa0>
mov    %rcx,%r12
mov    %edx,%r13d
mov    %rsi,%r14
mov    %rdi,%rbx
add    $0x1,%r13d
mov    0x8(%rsp),%rcx
neg    %ecx
mov    %rcx,0x8(%rsp)
xor    %r15d,%r15d
nopw   0x0(%rax,%rax,1)
mov    0x8(%rsp),%rcx
add    %r15d,%ecx
lea    0x10(%rsp),%rdi
lea    0xe6c(%rip),%rsi
mov    %r12,%rdx
call   1050 <asprintf@plt>
mov    (%rbx),%rdi
mov    0x10(%rsp),%rsi
mov    (%r14,%r15,8),%rdx
call   1040 <replaceall@plt>
mov    %rax,%rbp
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    (%rbx),%rdi
call   1030 <free@plt>
mov    %rbp,%rdi
call   1060 <strdup@plt>
mov    %rax,(%rbx)
mov    %rbp,%rdi
call   1030 <free@plt>
add    $0x1,%r15
cmp    %r15,%r13
jne    1180 <replace_tokens+0x40>
jmp    11e3 <replace_tokens+0xa3>
xor    %r13d,%r13d
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%s%i\00"
define i32 @replace_tokens(i8** %0, i8** %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
store i8** %0, i8*** %6
store i8** %1, i8*** %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i32 %4, i32* %10
store i32 0, i32* %11
br label %14
14: ; preds = %43, %5
%15 = load i32, i32* %11
%16 = load i32, i32* %8
%17 = icmp sle i32 %15, %16
br i1 %17, label %18, label %46
18: ; preds = %14
%19 = load i8*, i8** %9
%20 = load i32, i32* %11
%21 = load i32, i32* %10
%22 = sub nsw i32 %20, %21
%23 = call i32 @asprintf(i8** %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %19, i32 %22)
%24 = load i8**, i8*** %6
%25 = load i8*, i8** %24
%26 = load i8*, i8** %13
%27 = load i8**, i8*** %7
%28 = load i32, i32* %11
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8*, i8** %27, i64 %29
%31 = load i8*, i8** %30
%32 = call i8* @replaceall(i8* %25, i8* %26, i8* %31)
store i8* %32, i8** %12
%33 = load i8*, i8** %13
%34 = call i32 @free(i8* %33)
%35 = load i8**, i8*** %6
%36 = load i8*, i8** %35
%37 = call i32 @free(i8* %36)
%38 = load i8*, i8** %12
%39 = call i8* @strdup(i8* %38)
%40 = load i8**, i8*** %6
store i8* %39, i8** %40
%41 = load i8*, i8** %12
%42 = call i32 @free(i8* %41)
br label %43
43: ; preds = %18
%44 = load i32, i32* %11
%45 = add nsw i32 %44, 1
store i32 %45, i32* %11
br label %14
46: ; preds = %14
%47 = load i32, i32* %11
ret i32 %47
}

LLVM IR (-O2):
@.str = [5 x i8] c"%s%i\00"
define i32 @replace_tokens(i8** nocapture %0, i8** nocapture %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i8*
%7 = bitcast i8** %6 to i8*
%8 = icmp slt i32 %2, 0
br i1 %8, label %30, label %9
9: ; preds = %5
%10 = add nuw i32 %2, 1
%11 = zext i32 %10 to i64
br label %12
12: ; preds = %9, %12
%13 = phi i64 [ 0, %9 ], [ %28, %12 ]
%14 = trunc i64 %13 to i32
%15 = sub nsw i32 %14, %4
%16 = call i32 @asprintf(i8** %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %3, i32 %15)
%17 = load i8*, i8** %0
%18 = load i8*, i8** %6
%19 = getelementptr inbounds i8*, i8** %1, i64 %13
%20 = load i8*, i8** %19
%21 = call i8* @replaceall(i8* %17, i8* %18, i8* %20)
%22 = load i8*, i8** %6
%23 = call i32 @free(i8* %22)
%24 = load i8*, i8** %0
%25 = call i32 @free(i8* %24)
%26 = call i8* @strdup(i8* %21)
store i8* %26, i8** %0
%27 = call i32 @free(i8* %21)
%28 = add nuw nsw i64 %13, 1
%29 = icmp eq i64 %28, %11
br i1 %29, label %30, label %12
30: ; preds = %12, %5
%31 = phi i32 [ 0, %5 ], [ %10, %12 ]
ret i32 %31
}