#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 */
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
char *strdup(char *);
char *strtok(char *, char *);
int split(const char *str, char *delim, char **list, int max_list_length) {
  char buffer[BUFSIZ];
  int list_length;
  char *ptr;
  snprintf(buffer, sizeof(buffer), "%s", str);
  list_length = 0;
  for (ptr = strtok(buffer, delim); ptr != NULL; ptr = strtok(NULL, delim)) {
    if (list_length < max_list_length) {
      list[list_length++] = strdup(ptr);
    }
  }
  return list_length;
}


x86 assembly (clang, -O0):
<split>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x48(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
mov    %eax,%esi
mov    -0x8(%rbp),%rcx
lea    0xe80(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x48(%rbp),%rdi
movl   $0x0,-0x34(%rbp)
mov    -0x10(%rbp),%rsi
call   1040 <strtok@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     11f1 <split+0xc1>
mov    -0x34(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11d6 <split+0xa6>
mov    -0x40(%rbp),%rdi
call   1050 <strdup@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
mov    -0x34(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x34(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
jmp    11db <split+0xab>
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1040 <strtok@plt>
mov    %rax,-0x40(%rbp)
jmp    119d <split+0x6d>
mov    -0x34(%rbp),%eax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<split>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r15d
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%rcx
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe95(%rip),%rdx
mov    %rbx,%rdi
call   1030 <snprintf@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1040 <strtok@plt>
xor    %ebx,%ebx
jmp    119a <split+0x6a>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %edi,%edi
mov    %r12,%rsi
call   1040 <strtok@plt>
test   %rax,%rax
je     11b8 <split+0x88>
cmp    %r15d,%ebx
jge    1190 <split+0x60>
mov    %rax,%rdi
call   1050 <strdup@plt>
movslq %ebx,%rcx
add    $0x1,%ebx
mov    %rax,(%r14,%rcx,8)
jmp    1190 <split+0x60>
mov    %ebx,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [3 x i8] c"%s\00"
define i32 @split(i8* %0, i8* %1, i8** %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%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 i32, i32* @BUFSIZ
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %9
%16 = alloca i8, i64 %14
store i64 %14, i64* %10
%17 = trunc i64 %14 to i32
%18 = load i8*, i8** %5
%19 = call i32 @snprintf(i8* %16, i32 %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %18)
store i32 0, i32* %11
%20 = load i8*, i8** %6
%21 = call i8* @strtok(i8* %16, i8* %20)
store i8* %21, i8** %12
br label %22
22: ; preds = %38, %4
%23 = load i8*, i8** %12
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %41
25: ; preds = %22
%26 = load i32, i32* %11
%27 = load i32, i32* %8
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %37
29: ; preds = %25
%30 = load i8*, i8** %12
%31 = call i8* @strdup(i8* %30)
%32 = load i8**, i8*** %7
%33 = load i32, i32* %11
%34 = add nsw i32 %33, 1
store i32 %34, i32* %11
%35 = sext i32 %33 to i64
%36 = getelementptr inbounds i8*, i8** %32, i64 %35
store i8* %31, i8** %36
br label %37
37: ; preds = %29, %25
br label %38
38: ; preds = %37
%39 = load i8*, i8** %6
%40 = call i8* @strtok(i8* null, i8* %39)
store i8* %40, i8** %12
br label %22
41: ; preds = %22
%42 = load i32, i32* %11
%43 = load i8*, i8** %9
ret i32 %42
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [3 x i8] c"%s\00"
define i32 @split(i8* %0, i8* nocapture %1, i8** nocapture writeonly %2, i32 %3) {
%5 = load i32, i32* @BUFSIZ
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = call i32 @snprintf(i8* %7, i32 %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%9 = call i8* @strtok(i8* %7, i8* %1)
%10 = icmp eq i8* %9, null
br i1 %10, label %24, label %11
11: ; preds = %4, %20
%12 = phi i8* [ %22, %20 ], [ %9, %4 ]
%13 = phi i32 [ %21, %20 ], [ 0, %4 ]
%14 = icmp slt i32 %13, %3
br i1 %14, label %15, label %20
15: ; preds = %11
%16 = call i8* @strdup(i8* %12)
%17 = add nsw i32 %13, 1
%18 = sext i32 %13 to i64
%19 = getelementptr inbounds i8*, i8** %2, i64 %18
store i8* %16, i8** %19
br label %20
20: ; preds = %11, %15
%21 = phi i32 [ %17, %15 ], [ %13, %11 ]
%22 = call i8* @strtok(i8* null, i8* %1)
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %11
24: ; preds = %20, %4
%25 = phi i32 [ 0, %4 ], [ %21, %20 ]
ret i32 %25
}