#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 */
char *insert_string(char *, char *, int);
int instr(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int strlen(char *);
char *piece_insert(char *source_destination, char delimiter, char *insert_this,
                   int piece_offset) {
  int here;
  char delimiter_string[2];
  char insert_buffer[1024];
  if (!piece_offset) {
    if (delimiter) {
      sprintf(insert_buffer, "%s%c", insert_this, delimiter);
    } else {
      sprintf(insert_buffer, "%s", insert_this);
    }
    return insert_string(source_destination, insert_buffer, 0);
  } else {
    *delimiter_string = delimiter;
    *(delimiter_string + 1) = '\0';
    here = instr(delimiter_string, source_destination, piece_offset);
    if (here == -1)
      here = strlen(source_destination);
    sprintf(insert_buffer, "%c%s", delimiter, insert_this);
    return insert_string(source_destination, insert_buffer, here);
  }
}


x86 assembly (clang, -O0):
<piece_insert>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %sil,%al
mov    %rdi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    11c6 <piece_insert+0x86>
cmpb   $0x0,-0x11(%rbp)
je     1192 <piece_insert+0x52>
lea    -0x430(%rbp),%rdi
mov    -0x20(%rbp),%rdx
movsbl -0x11(%rbp),%ecx
lea    0xe7a(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
jmp    11ab <piece_insert+0x6b>
lea    -0x430(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe63(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x430(%rbp),%rsi
xor    %edx,%edx
call   1050 <insert_string@plt>
mov    %rax,-0x8(%rbp)
jmp    122d <piece_insert+0xed>
mov    -0x11(%rbp),%al
mov    %al,-0x2a(%rbp)
movb   $0x0,-0x29(%rbp)
lea    -0x2a(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1040 <instr@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    11f9 <piece_insert+0xb9>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x28(%rbp)
lea    -0x430(%rbp),%rdi
movsbl -0x11(%rbp),%edx
mov    -0x20(%rbp),%rcx
lea    0xdf6(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x430(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1050 <insert_string@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<piece_insert>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x418,%rsp
mov    %rdx,%r14
mov    %esi,%ebx
mov    %rdi,%r15
test   %ecx,%ecx
je     11ad <piece_insert+0x5d>
mov    %bl,0xe(%rsp)
movb   $0x0,0xf(%rsp)
lea    0xe(%rsp),%rdi
mov    %r15,%rsi
mov    %ecx,%edx
call   1050 <instr@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1192 <piece_insert+0x42>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
movsbl %bl,%edx
lea    0xe69(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    %r14,%rcx
xor    %eax,%eax
call   1070 <sprintf@plt>
jmp    11dd <piece_insert+0x8d>
test   %bl,%bl
je     11ce <piece_insert+0x7e>
movsbl %bl,%ecx
lea    0xe45(%rip),%rsi
xor    %ebp,%ebp
lea    0x10(%rsp),%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <sprintf@plt>
jmp    11dd <piece_insert+0x8d>
lea    0x10(%rsp),%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
xor    %ebp,%ebp
lea    0x10(%rsp),%rsi
mov    %r15,%rdi
mov    %ebp,%edx
call   1060 <insert_string@plt>
add    $0x418,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%s%c\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [5 x i8] c"%c%s\00"
define i8* @piece_insert(i8* %0, i8 signext %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca [2 x i8]
%12 = alloca [1024 x i8]
store i8* %0, i8** %6
store i8 %1, i8* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%13 = load i32, i32* %9
%14 = icmp ne i32 %13, 0
br i1 %14, label %32, label %15
15: ; preds = %4
%16 = load i8, i8* %7
%17 = icmp ne i8 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %15
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%20 = load i8*, i8** %8
%21 = load i8, i8* %7
%22 = sext i8 %21 to i32
%23 = call i32 (i8*, i8*, ...) @sprintf(i8* %19, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %20, i32 %22)
br label %28
24: ; preds = %15
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%26 = load i8*, i8** %8
%27 = call i32 (i8*, i8*, ...) @sprintf(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %26)
br label %28
28: ; preds = %24, %18
%29 = load i8*, i8** %6
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%31 = call i8* @insert_string(i8* %29, i8* %30, i32 0)
store i8* %31, i8** %5
br label %56
32: ; preds = %4
%33 = load i8, i8* %7
%34 = getelementptr inbounds [2 x i8], [2 x i8]* %11, i64 0, i64 0
store i8 %33, i8* %34
%35 = getelementptr inbounds [2 x i8], [2 x i8]* %11, i64 0, i64 0
%36 = getelementptr inbounds i8, i8* %35, i64 1
store i8 0, i8* %36
%37 = getelementptr inbounds [2 x i8], [2 x i8]* %11, i64 0, i64 0
%38 = load i8*, i8** %6
%39 = load i32, i32* %9
%40 = call i32 @instr(i8* %37, i8* %38, i32 %39)
store i32 %40, i32* %10
%41 = load i32, i32* %10
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %46
43: ; preds = %32
%44 = load i8*, i8** %6
%45 = call i32 @strlen(i8* %44)
store i32 %45, i32* %10
br label %46
46: ; preds = %43, %32
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%48 = load i8, i8* %7
%49 = sext i8 %48 to i32
%50 = load i8*, i8** %8
%51 = call i32 (i8*, i8*, ...) @sprintf(i8* %47, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %49, i8* %50)
%52 = load i8*, i8** %6
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%54 = load i32, i32* %10
%55 = call i8* @insert_string(i8* %52, i8* %53, i32 %54)
store i8* %55, i8** %5
br label %56
56: ; preds = %46, %28
%57 = load i8*, i8** %5
ret i8* %57
}

LLVM IR (-O2):
@.str = [5 x i8] c"%s%c\00"
@.str.2 = [5 x i8] c"%c%s\00"
define i8* @piece_insert(i8* %0, i8 signext %1, i8* %2, i32 %3) {
%5 = alloca [2 x i8]
%6 = alloca [1024 x i8]
%7 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%9 = icmp eq i32 %3, 0
br i1 %9, label %10, label %17
10: ; preds = %4
%11 = icmp eq i8 %1, 0
br i1 %11, label %15, label %12
12: ; preds = %10
%13 = sext i8 %1 to i32
%14 = call i32 (i8*, i8*, ...) @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %2, i32 %13)
br label %27
15: ; preds = %10
%16 = call i8* @strcpy(i8* %8, i8* dereferenceable(1) %2)
br label %27
17: ; preds = %4
store i8 %1, i8* %7
%18 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 1
store i8 0, i8* %18
%19 = call i32 @instr(i8* %7, i8* %0, i32 %3)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @strlen(i8* %0)
br label %23
23: ; preds = %21, %17
%24 = phi i32 [ %22, %21 ], [ %19, %17 ]
%25 = sext i8 %1 to i32
%26 = call i32 (i8*, i8*, ...) @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %25, i8* %2)
br label %27
27: ; preds = %12, %15, %23
%28 = phi i32 [ %24, %23 ], [ 0, %15 ], [ 0, %12 ]
%29 = call i8* @insert_string(i8* %0, i8* %8, i32 %28)
ret i8* %29
}