#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 instr(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *, ...);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ timlib_strncpy(char *, char *, int);
char *piece_insert_quote_comma(char *destination, char *source, char *string,
                               int piece_offset) {
  int start_offset;
  char *delimiter = "\",\"";
  if (!source)
    return (char *)0;
  if (!*source) {
    sprintf(destination, "\"%s\"", string);
    return destination;
  }
  if (piece_offset == 0) {
    sprintf(destination, "\"%s\",", string);
    strcat(destination, source);
    return destination;
  }
  start_offset = instr(delimiter, source, piece_offset);
  if (start_offset != -1) {
    timlib_strncpy(destination, source, start_offset);
    strcat(destination, "\",\"");
    strcat(destination, string);
    strcat(destination, source + start_offset);
  } else {
    sprintf(destination, "%s,\"%s\"", source, string);
  }
  return destination;
}


x86 assembly (clang, -O0):
<piece_insert_quote_comma>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
lea    0xea2(%rip),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    117a <piece_insert_quote_comma+0x3a>
movq   $0x0,-0x8(%rbp)
jmp    1269 <piece_insert_quote_comma+0x129>
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
jne    11aa <piece_insert_quote_comma+0x6a>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe77(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1269 <piece_insert_quote_comma+0x129>
cmpl   $0x0,-0x24(%rbp)
jne    11e4 <piece_insert_quote_comma+0xa4>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe41(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcat@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1269 <piece_insert_quote_comma+0x129>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <instr@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
je     1247 <piece_insert_quote_comma+0x107>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1060 <timlib_strncpy@plt>
mov    -0x10(%rbp),%rdi
lea    0xde4(%rip),%rsi
call   1040 <strcat@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcat@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
movslq -0x28(%rbp),%rax
add    %rax,%rsi
call   1040 <strcat@plt>
jmp    1261 <piece_insert_quote_comma+0x121>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xdb0(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<piece_insert_quote_comma>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rsi,%rsi
je     11ad <piece_insert_quote_comma+0x6d>
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
cmpb   $0x0,(%rsi)
je     11b2 <piece_insert_quote_comma+0x72>
test   %ecx,%ecx
je     11c8 <piece_insert_quote_comma+0x88>
lea    0xe9b(%rip),%rdi
mov    %rbx,%rsi
mov    %ecx,%edx
call   1030 <instr@plt>
cmp    $0xffffffff,%eax
je     11e9 <piece_insert_quote_comma+0xa9>
mov    %eax,%ebp
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1060 <timlib_strncpy@plt>
lea    0xe76(%rip),%rsi
mov    %r14,%rdi
call   1040 <strcat@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <strcat@plt>
movslq %ebp,%rsi
add    %rbx,%rsi
mov    %r14,%rdi
call   1040 <strcat@plt>
jmp    1200 <piece_insert_quote_comma+0xc0>
xor    %r14d,%r14d
jmp    1200 <piece_insert_quote_comma+0xc0>
lea    0xe54(%rip),%rsi
mov    %r14,%rdi
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <sprintf@plt>
jmp    1200 <piece_insert_quote_comma+0xc0>
lea    0xe35(%rip),%rsi
mov    %r14,%rdi
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <sprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <strcat@plt>
jmp    1200 <piece_insert_quote_comma+0xc0>
lea    0xe1a(%rip),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
mov    %r15,%rcx
xor    %eax,%eax
call   1050 <sprintf@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"\22,\22\00"
@.str.1 = [5 x i8] c"\22%s\22\00"
@.str.2 = [6 x i8] c"\22%s\22,\00"
@.str.3 = [8 x i8] c"%s,\22%s\22\00"
define i8* @piece_insert_quote_comma(i8* %0, i8* %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 i8*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8** %11
%12 = load i8*, i8** %7
%13 = icmp ne i8* %12, null
br i1 %13, label %15, label %14
14: ; preds = %4
store i8* null, i8** %5
br label %65
15: ; preds = %4
%16 = load i8*, i8** %7
%17 = load i8, i8* %16
%18 = icmp ne i8 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %15
%20 = load i8*, i8** %6
%21 = load i8*, i8** %8
%22 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = load i8*, i8** %6
store i8* %23, i8** %5
br label %65
24: ; preds = %15
%25 = load i32, i32* %9
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %35
27: ; preds = %24
%28 = load i8*, i8** %6
%29 = load i8*, i8** %8
%30 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %28, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%31 = load i8*, i8** %6
%32 = load i8*, i8** %7
%33 = call i32 @strcat(i8* %31, i8* %32)
%34 = load i8*, i8** %6
store i8* %34, i8** %5
br label %65
35: ; preds = %24
%36 = load i8*, i8** %11
%37 = load i8*, i8** %7
%38 = load i32, i32* %9
%39 = call i32 @instr(i8* %36, i8* %37, i32 %38)
store i32 %39, i32* %10
%40 = load i32, i32* %10
%41 = icmp ne i32 %40, -1
br i1 %41, label %42, label %58
42: ; preds = %35
%43 = load i8*, i8** %6
%44 = load i8*, i8** %7
%45 = load i32, i32* %10
%46 = call i32 @timlib_strncpy(i8* %43, i8* %44, i32 %45)
%47 = load i8*, i8** %6
%48 = call i32 @strcat(i8* %47, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%49 = load i8*, i8** %6
%50 = load i8*, i8** %8
%51 = call i32 @strcat(i8* %49, i8* %50)
%52 = load i8*, i8** %6
%53 = load i8*, i8** %7
%54 = load i32, i32* %10
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %53, i64 %55
%57 = call i32 @strcat(i8* %52, i8* %56)
br label %63
58: ; preds = %35
%59 = load i8*, i8** %6
%60 = load i8*, i8** %7
%61 = load i8*, i8** %8
%62 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %59, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %60, i8* %61)
br label %63
63: ; preds = %58, %42
%64 = load i8*, i8** %6
store i8* %64, i8** %5
br label %65
65: ; preds = %63, %27, %19, %14
%66 = load i8*, i8** %5
ret i8* %66
}

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