#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 *piece_string(char *, char *, char *, int);
int /*<<< orphan*/ printf(char *, char *);
void test_piece_string(void) {
  char *string;
  char destination[16];
  char *delimiter;
  string = "\"zero\",\"one\",\"two\"";
  printf("got string = (%s)\n", string);
  delimiter = "\",\"";
  printf("should be zero: is (%s)\n",
         piece_string(destination, delimiter, string, 0));
  printf("should be one: is (%s)\n",
         piece_string(destination, delimiter, string, 1));
  printf("should be two: is (%s)\n",
         piece_string(destination, delimiter, string, 2));
  printf("should be null: is (%s)\n",
         piece_string(destination, delimiter, string, 3));
}


x86 assembly (clang, -O0):
<test_piece_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xed1(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xed5(%rip),%rdi
call   1030 <printf@plt>
lea    0xedc(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x8(%rbp),%rdx
xor    %ecx,%ecx
call   1040 <piece_string@plt>
mov    %rax,%rsi
lea    0xebf(%rip),%rdi
call   1030 <printf@plt>
lea    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x8(%rbp),%rdx
mov    $0x1,%ecx
call   1040 <piece_string@plt>
mov    %rax,%rsi
lea    0xeb3(%rip),%rdi
call   1030 <printf@plt>
lea    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x8(%rbp),%rdx
mov    $0x2,%ecx
call   1040 <piece_string@plt>
mov    %rax,%rsi
lea    0xea6(%rip),%rdi
call   1030 <printf@plt>
lea    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x8(%rbp),%rdx
mov    $0x3,%ecx
call   1040 <piece_string@plt>
mov    %rax,%rsi
lea    0xe99(%rip),%rdi
call   1030 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_piece_string>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xee3(%rip),%rdi
lea    0xec9(%rip),%rbx
mov    %rbx,%rsi
call   1030 <printf@plt>
lea    0xee0(%rip),%r14
mov    %rsp,%r15
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   1040 <piece_string@plt>
lea    0xeca(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
mov    $0x1,%ecx
call   1040 <piece_string@plt>
lea    0xec1(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
mov    $0x2,%ecx
call   1040 <piece_string@plt>
lea    0xeb7(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
mov    $0x3,%ecx
call   1040 <piece_string@plt>
lea    0xead(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [19 x i8] c"\22zero\22,\22one\22,\22two\22\00"
@.str.1 = [19 x i8] c"got string = (%s)\0A\00"
@.str.2 = [4 x i8] c"\22,\22\00"
@.str.3 = [25 x i8] c"should be zero: is (%s)\0A\00"
@.str.4 = [24 x i8] c"should be one: is (%s)\0A\00"
@.str.5 = [24 x i8] c"should be two: is (%s)\0A\00"
@.str.6 = [25 x i8] c"should be null: is (%s)\0A\00"
define void @test_piece_string() {
%1 = alloca i8*
%2 = alloca [16 x i8]
%3 = alloca i8*
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8** %1
%4 = load i8*, i8** %1
%5 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %4)
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %3
%6 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%7 = load i8*, i8** %3
%8 = load i8*, i8** %1
%9 = call i8* @piece_string(i8* %6, i8* %7, i8* %8, i32 0)
%10 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %9)
%11 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%12 = load i8*, i8** %3
%13 = load i8*, i8** %1
%14 = call i8* @piece_string(i8* %11, i8* %12, i8* %13, i32 1)
%15 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %14)
%16 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%17 = load i8*, i8** %3
%18 = load i8*, i8** %1
%19 = call i8* @piece_string(i8* %16, i8* %17, i8* %18, i32 2)
%20 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %19)
%21 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%22 = load i8*, i8** %3
%23 = load i8*, i8** %1
%24 = call i8* @piece_string(i8* %21, i8* %22, i8* %23, i32 3)
%25 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i8* %24)
ret void
}

LLVM IR (-O2):
@.str = [19 x i8] c"\22zero\22,\22one\22,\22two\22\00"
@.str.1 = [19 x i8] c"got string = (%s)\0A\00"
@.str.2 = [4 x i8] c"\22,\22\00"
@.str.3 = [25 x i8] c"should be zero: is (%s)\0A\00"
@.str.4 = [24 x i8] c"should be one: is (%s)\0A\00"
@.str.5 = [24 x i8] c"should be two: is (%s)\0A\00"
@.str.6 = [25 x i8] c"should be null: is (%s)\0A\00"
define void @test_piece_string() {
%1 = alloca [16 x i8]
%2 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%4 = call i8* @piece_string(i8* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 0)
%5 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%6 = call i8* @piece_string(i8* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 1)
%7 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %6)
%8 = call i8* @piece_string(i8* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 2)
%9 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %8)
%10 = call i8* @piece_string(i8* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 3)
%11 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i8* %10)
ret void
}