#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 *index(char *, char);
int split(char *text, char separator, char **field, int field_capacity) {
  int fields = 1;
  field[0] = text;
  while (1) {
    text = index(text, separator);
    if (text != NULL) {
      *text = '\0';
      ++text;
      if (fields < field_capacity) {
        field[fields] = text;
        ++fields;
      }
    } else
      break;
  }
  return fields;
}


x86 assembly (clang, -O0):
<split>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x8(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rdi
movsbl -0x9(%rbp),%esi
call   1030 <index@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1194 <split+0x84>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    118f <split+0x7f>
mov    -0x8(%rbp),%rdx
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1199 <split+0x89>
jmp    119e <split+0x8e>
jmp    113b <split+0x2b>
mov    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<split>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r15d
mov    %rdx,%r14
mov    %esi,%ebx
mov    %rdi,(%rdx)
call   1030 <index@plt>
mov    $0x1,%ebp
test   %rax,%rax
je     1167 <split+0x57>
movsbl %bl,%ebx
jmp    114f <split+0x3f>
cs nopw 0x0(%rax,%rax,1)
mov    %rax,%rdi
mov    %ebx,%esi
call   1030 <index@plt>
test   %rax,%rax
je     1167 <split+0x57>
movb   $0x0,(%rax)
add    $0x1,%rax
cmp    %r15d,%ebp
jge    1140 <split+0x30>
movslq %ebp,%rcx
mov    %rax,(%r14,%rcx,8)
add    $0x1,%ebp
jmp    1140 <split+0x30>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @split(i8* %0, i8 signext %1, i8** %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i8 %1, i8* %6
store i8** %2, i8*** %7
store i32 %3, i32* %8
store i32 1, i32* %9
%10 = load i8*, i8** %5
%11 = load i8**, i8*** %7
%12 = getelementptr inbounds i8*, i8** %11, i64 0
store i8* %10, i8** %12
br label %13
13: ; preds = %4, %36
%14 = load i8*, i8** %5
%15 = load i8, i8* %6
%16 = call i8* @index(i8* %14, i8 signext %15)
store i8* %16, i8** %5
%17 = load i8*, i8** %5
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %35
19: ; preds = %13
%20 = load i8*, i8** %5
store i8 0, i8* %20
%21 = load i8*, i8** %5
%22 = getelementptr inbounds i8, i8* %21, i32 1
store i8* %22, i8** %5
%23 = load i32, i32* %9
%24 = load i32, i32* %8
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %34
26: ; preds = %19
%27 = load i8*, i8** %5
%28 = load i8**, i8*** %7
%29 = load i32, i32* %9
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8*, i8** %28, i64 %30
store i8* %27, i8** %31
%32 = load i32, i32* %9
%33 = add nsw i32 %32, 1
store i32 %33, i32* %9
br label %34
34: ; preds = %26, %19
br label %36
35: ; preds = %13
br label %37
36: ; preds = %34
br label %13
37: ; preds = %35
%38 = load i32, i32* %9
ret i32 %38
}

LLVM IR (-O2):
define i32 @split(i8* %0, i8 signext %1, i8** nocapture writeonly %2, i32 %3) {
store i8* %0, i8** %2
%5 = tail call i8* @index(i8* %0, i8 signext %1)
%6 = icmp eq i8* %5, null
br i1 %6, label %20, label %7
7: ; preds = %4, %16
%8 = phi i8* [ %18, %16 ], [ %5, %4 ]
%9 = phi i32 [ %17, %16 ], [ 1, %4 ]
store i8 0, i8* %8
%10 = getelementptr inbounds i8, i8* %8, i64 1
%11 = icmp slt i32 %9, %3
br i1 %11, label %12, label %16
12: ; preds = %7
%13 = sext i32 %9 to i64
%14 = getelementptr inbounds i8*, i8** %2, i64 %13
store i8* %10, i8** %14
%15 = add nsw i32 %9, 1
br label %16
16: ; preds = %7, %12
%17 = phi i32 [ %15, %12 ], [ %9, %7 ]
%18 = tail call i8* @index(i8* %10, i8 signext %1)
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %7
20: ; preds = %16, %4
%21 = phi i32 [ 1, %4 ], [ %17, %16 ]
ret i32 %21
}