#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 *strchr(char *, char const);
int str_split(char *const str, const char separator, char **const fields,
              const int max_fields) {
  fields[0] = str;
  for (int i = 1; i < max_fields; ++i)
    fields[i] = NULL;
  char *search_pos = str;
  int count = 1;
  while (count < max_fields) {
    char *sep = strchr(search_pos, separator);
    if (sep == NULL)
      break;
    *sep = '\0';
    fields[count] = search_pos = (sep + 1);
    count += 1;
  }
  return count;
}


x86 assembly (clang, -O0):
<str_split>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1165 <str_split+0x55>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    113b <str_split+0x2b>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11ce <str_split+0xbe>
mov    -0x28(%rbp),%rdi
movsbl -0x9(%rbp),%esi
call   1030 <strchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11a1 <str_split+0x91>
jmp    11ce <str_split+0xbe>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x38(%rbp),%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1174 <str_split+0x64>
mov    -0x2c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_split>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,(%rdx)
mov    $0x1,%r15d
cmp    $0x2,%ecx
jl     1189 <str_split+0x69>
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %ecx,%r12d
lea    0x8(%rdx),%rdi
add    $0xffffffff,%ecx
shl    $0x3,%rcx
xor    %esi,%esi
mov    %rcx,%rdx
call   1040 <memset@plt>
mov    $0x1,%r15d
movsbl %bpl,%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     1189 <str_split+0x69>
mov    %rax,%rbx
movb   $0x0,(%rax)
add    $0x1,%rbx
mov    %rbx,(%r14,%r15,8)
add    $0x1,%r15
cmp    %r15,%r12
jne    1160 <str_split+0x40>
mov    %r12d,%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @str_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
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i8*
store i8* %0, i8** %5
store i8 %1, i8* %6
store i8** %2, i8*** %7
store i32 %3, i32* %8
%13 = load i8*, i8** %5
%14 = load i8**, i8*** %7
%15 = getelementptr inbounds i8*, i8** %14, i64 0
store i8* %13, i8** %15
store i32 1, i32* %9
br label %16
16: ; preds = %25, %4
%17 = load i32, i32* %9
%18 = load i32, i32* %8
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %28
20: ; preds = %16
%21 = load i8**, i8*** %7
%22 = load i32, i32* %9
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8*, i8** %21, i64 %23
store i8* null, i8** %24
br label %25
25: ; preds = %20
%26 = load i32, i32* %9
%27 = add nsw i32 %26, 1
store i32 %27, i32* %9
br label %16
28: ; preds = %16
%29 = load i8*, i8** %5
store i8* %29, i8** %10
store i32 1, i32* %11
br label %30
30: ; preds = %41, %28
%31 = load i32, i32* %11
%32 = load i32, i32* %8
%33 = icmp slt i32 %31, %32
br i1 %33, label %34, label %51
34: ; preds = %30
%35 = load i8*, i8** %10
%36 = load i8, i8* %6
%37 = call i8* @strchr(i8* %35, i8 signext %36)
store i8* %37, i8** %12
%38 = load i8*, i8** %12
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %41
40: ; preds = %34
br label %51
41: ; preds = %34
%42 = load i8*, i8** %12
store i8 0, i8* %42
%43 = load i8*, i8** %12
%44 = getelementptr inbounds i8, i8* %43, i64 1
store i8* %44, i8** %10
%45 = load i8**, i8*** %7
%46 = load i32, i32* %11
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8*, i8** %45, i64 %47
store i8* %44, i8** %48
%49 = load i32, i32* %11
%50 = add nsw i32 %49, 1
store i32 %50, i32* %11
br label %30
51: ; preds = %40, %30
%52 = load i32, i32* %11
ret i32 %52
}

LLVM IR (-O2):
define i32 @str_split(i8* %0, i8 signext %1, i8** nocapture writeonly %2, i32 %3) {
store i8* %0, i8** %2
%5 = icmp sgt i32 %3, 1
br i1 %5, label %6, label %25
6: ; preds = %4
%7 = getelementptr i8*, i8** %2, i64 1
%8 = bitcast i8** %7 to i8*
%9 = add nsw i32 %3, -1
%10 = zext i32 %9 to i64
%11 = shl nuw nsw i64 %10, 3
%12 = zext i32 %3 to i64
br label %13
13: ; preds = %6, %18
%14 = phi i64 [ 1, %6 ], [ %21, %18 ]
%15 = phi i8* [ %0, %6 ], [ %19, %18 ]
%16 = tail call i8* @strchr(i8* dereferenceable(1) %15, i8 signext %1)
%17 = icmp eq i8* %16, null
br i1 %17, label %23, label %18
18: ; preds = %13
store i8 0, i8* %16
%19 = getelementptr inbounds i8, i8* %16, i64 1
%20 = getelementptr inbounds i8*, i8** %2, i64 %14
store i8* %19, i8** %20
%21 = add nuw nsw i64 %14, 1
%22 = icmp eq i64 %21, %12
br i1 %22, label %25, label %13
23: ; preds = %13
%24 = trunc i64 %14 to i32
br label %25
25: ; preds = %18, %23, %4
%26 = phi i32 [ 1, %4 ], [ %24, %23 ], [ %3, %18 ]
ret i32 %26
}