#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 */
typedef int /*<<< orphan*/ scratch;

/* Variables and functions */
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strcat(char *, char *);
size_t strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int ee_replace(char *eqn, int num_chars, int replace_with) {
  char scratch[1024];
  if (strlen(eqn + num_chars) >= sizeof(scratch))
    return 17;
  strncpy(scratch, eqn + num_chars, sizeof(scratch) - 1);
  scratch[sizeof(scratch) - 1] = '\0';
  eqn[0] = '[';
  sprintf(eqn + 1, "%d", replace_with);
  eqn[strlen(eqn) + 1] = '\0';
  eqn[strlen(eqn)] = ']';
  strcat(eqn, scratch);
  return 0;
}


x86 assembly (clang, -O0):
<ee_replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
call   1040 <strlen@plt>
cmp    $0x400,%rax
jb     117d <ee_replace+0x3d>
movl   $0x11,-0x4(%rbp)
jmp    1217 <ee_replace+0xd7>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
mov    $0x3ff,%edx
call   1030 <strncpy@plt>
movb   $0x0,-0x21(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x5b,(%rax)
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
mov    -0x18(%rbp),%edx
lea    0xe4a(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x430(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x430(%rbp),%rax
movb   $0x0,0x1(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x428(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x428(%rbp),%rax
movb   $0x5d,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
lea    -0x420(%rbp),%rsi
call   1050 <strcat@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ee_replace>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %edx,%r15d
mov    %rdi,%rbx
movslq %esi,%rbp
add    %rdi,%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    $0x11,%eax
cmp    $0x3ff,%rcx
ja     11c9 <ee_replace+0x89>
mov    %rsp,%r14
mov    %r14,%rdi
mov    %rbp,%rsi
mov    $0x3ff,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x3ff(%rsp)
movb   $0x5b,(%rbx)
lea    0x1(%rbx),%rdi
lea    0xe65(%rip),%rsi
mov    %r15d,%edx
call   1060 <sprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
movb   $0x0,0x1(%rax,%rbx,1)
mov    %rbx,%rdi
call   1040 <strlen@plt>
movb   $0x5d,(%rbx,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <strcat@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
define i32 @ee_replace(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [1024 x i8]
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%9 = load i8*, i8** %5
%10 = load i32, i32* %6
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %9, i64 %11
%13 = call i64 @strlen(i8* %12)
%14 = icmp uge i64 %13, 1024
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 17, i32* %4
br label %42
16: ; preds = %3
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%18 = load i8*, i8** %5
%19 = load i32, i32* %6
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = call i32 @strncpy(i8* %17, i8* %21, i32 1023)
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 1023
store i8 0, i8* %23
%24 = load i8*, i8** %5
%25 = getelementptr inbounds i8, i8* %24, i64 0
store i8 91, i8* %25
%26 = load i8*, i8** %5
%27 = getelementptr inbounds i8, i8* %26, i64 1
%28 = load i32, i32* %7
%29 = call i32 @sprintf(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %28)
%30 = load i8*, i8** %5
%31 = load i8*, i8** %5
%32 = call i64 @strlen(i8* %31)
%33 = add i64 %32, 1
%34 = getelementptr inbounds i8, i8* %30, i64 %33
store i8 0, i8* %34
%35 = load i8*, i8** %5
%36 = load i8*, i8** %5
%37 = call i64 @strlen(i8* %36)
%38 = getelementptr inbounds i8, i8* %35, i64 %37
store i8 93, i8* %38
%39 = load i8*, i8** %5
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%41 = call i32 @strcat(i8* %39, i8* %40)
store i32 0, i32* %4
br label %42
42: ; preds = %16, %15
%43 = load i32, i32* %4
ret i32 %43
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
define i32 @ee_replace(i8* %0, i32 %1, i32 %2) {
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%6 = sext i32 %1 to i64
%7 = getelementptr inbounds i8, i8* %0, i64 %6
%8 = tail call i64 @strlen(i8* dereferenceable(1) %7)
%9 = icmp ugt i64 %8, 1023
br i1 %9, label %21, label %10
10: ; preds = %3
%11 = call i32 @strncpy(i8* %5, i8* %7, i32 1023)
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 1023
store i8 0, i8* %12
store i8 91, i8* %0
%13 = getelementptr inbounds i8, i8* %0, i64 1
%14 = call i32 @sprintf(i8* %13, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %2)
%15 = call i64 @strlen(i8* %0)
%16 = add i64 %15, 1
%17 = getelementptr inbounds i8, i8* %0, i64 %16
store i8 0, i8* %17
%18 = call i64 @strlen(i8* %0)
%19 = getelementptr inbounds i8, i8* %0, i64 %18
store i8 93, i8* %19
%20 = call i32 @strcat(i8* %0, i8* %5)
br label %21
21: ; preds = %3, %10
%22 = phi i32 [ 0, %10 ], [ 17, %3 ]
ret i32 %22
}