#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*/ free(char *);
char **realloc(char const **, int);
char *strdup(char const *);
char *strtok(char *, char *);
char *uniquestr(char *);
const char **comma_separate_values(const char *value, int *num_elems) {
  const char **result = NULL;
  *num_elems = 0;
  if (value != NULL) {
    char *comma_string = strdup(value);
    char *current = strtok(comma_string, ",");
    while (current != NULL) {
      do {
        (*num_elems)++;
        (result) = realloc((result), sizeof(*(result)) * (*num_elems));
        (result)[((*num_elems) - 1)] = (uniquestr(current));
      } while (0);
      current = strtok(NULL, ",");
    }
    free(comma_string);
  }
  do {
    (*num_elems)++;
    (result) = realloc((result), sizeof(*(result)) * (*num_elems));
    (result)[((*num_elems) - 1)] = (NULL);
  } while (0);
  (*num_elems)--;
  return result;
}


x86 assembly (clang, -O0):
<comma_separate_values>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
cmpq   $0x0,-0x8(%rbp)
je     1215 <comma_separate_values+0xc5>
mov    -0x8(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe6b(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     120c <comma_separate_values+0xbc>
jmp    11ae <comma_separate_values+0x5e>
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq (%rax),%rax
shl    $0x3,%rax
mov    %eax,%esi
call   1050 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <uniquestr@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe02(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x28(%rbp)
jmp    119e <comma_separate_values+0x4e>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    121a <comma_separate_values+0xca>
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq (%rax),%rax
shl    $0x3,%rax
mov    %eax,%esi
call   1050 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
add    $0xffffffff,%ecx
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<comma_separate_values>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
movl   $0x0,(%rsi)
test   %rdi,%rdi
je     11cf <comma_separate_values+0x7f>
call   1070 <strdup@plt>
mov    %rax,%r15
lea    0xe8a(%rip),%rsi
mov    %rax,%rdi
call   1060 <strtok@plt>
test   %rax,%rax
je     11d9 <comma_separate_values+0x89>
mov    %rax,%rbx
xor    %r13d,%r13d
lea    0xe70(%rip),%r12
mov    (%r14),%eax
lea    0x1(%rax),%ecx
mov    %ecx,(%r14)
lea    0x8(,%rax,8),%esi
mov    %r13,%rdi
call   1050 <realloc@plt>
mov    %rax,%r13
mov    %rbx,%rdi
call   1040 <uniquestr@plt>
movslq (%r14),%rcx
mov    %rax,-0x8(%r13,%rcx,8)
xor    %edi,%edi
mov    %r12,%rsi
call   1060 <strtok@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1190 <comma_separate_values+0x40>
jmp    11dc <comma_separate_values+0x8c>
mov    $0x1,%esi
xor    %r13d,%r13d
jmp    11ea <comma_separate_values+0x9a>
xor    %r13d,%r13d
mov    %r15,%rdi
call   1030 <free@plt>
mov    (%r14),%esi
add    $0x1,%esi
mov    %esi,(%r14)
shl    $0x3,%esi
mov    %r13,%rdi
call   1050 <realloc@plt>
movslq (%r14),%rcx
movq   $0x0,-0x8(%rax,%rcx,8)
add    $0xffffffffffffffff,%rcx
mov    %ecx,(%r14)
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c",\00"
define i8** @comma_separate_values(i8* %0, i32* nocapture %1) {
store i32 0, i32* %1
%3 = icmp eq i8* %0, null
br i1 %3, label %27, label %4
4: ; preds = %2
%5 = tail call i8* @strdup(i8* %0)
%6 = tail call i8* @strtok(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i8* %6, null
br i1 %7, label %22, label %8
8: ; preds = %4, %8
%9 = phi i8* [ %20, %8 ], [ %6, %4 ]
%10 = phi i8** [ %14, %8 ], [ null, %4 ]
%11 = load i32, i32* %1
%12 = add nsw i32 %11, 1
store i32 %12, i32* %1
%13 = shl i32 %12, 3
%14 = tail call i8** @realloc(i8** %10, i32 %13)
%15 = tail call i8* @uniquestr(i8* %9)
%16 = load i32, i32* %1
%17 = add nsw i32 %16, -1
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8*, i8** %14, i64 %18
store i8* %15, i8** %19
%20 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %8
22: ; preds = %8, %4
%23 = phi i8** [ null, %4 ], [ %14, %8 ]
%24 = tail call i32 @free(i8* %5)
%25 = load i32, i32* %1
%26 = add i32 %25, 1
br label %27
27: ; preds = %2, %22
%28 = phi i32 [ %26, %22 ], [ 1, %2 ]
%29 = phi i8** [ %23, %22 ], [ null, %2 ]
store i32 %28, i32* %1
%30 = shl i32 %28, 3
%31 = tail call i8** @realloc(i8** %29, i32 %30)
%32 = load i32, i32* %1
%33 = add nsw i32 %32, -1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8*, i8** %31, i64 %34
store i8* null, i8** %35
store i32 %33, i32* %1
ret i8** %31
}