#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*/ cleanup(char **, size_t);
size_t ft_countwords(char *, char);
scalar_t__ ft_memalloc(size_t);
char *ft_strsub(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
char *next_word(char const *, char);
int /*<<< orphan*/ word_len(char const *, char);
char **ft_strsplit(char const *s, char c) {
  char **split;
  size_t cur;
  size_t wordcount;
  wordcount = ft_countwords((char *)s, c);
  split = (char **)ft_memalloc((wordcount + 1) * sizeof(char *));
  if (split == NULL)
    return (NULL);
  cur = 0;
  while (cur < wordcount) {
    s = next_word(s, c);
    split[cur] = ft_strsub(s, 0, word_len(s, c));
    if (split[cur] == NULL) {
      cleanup(split, cur);
      return (NULL);
    }
    cur++;
    s += word_len(s, c);
  }
  split[wordcount] = NULL;
  return (split);
}


x86 assembly (clang, -O0):
<ft_strsplit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %sil,%al
mov    %rdi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    -0x10(%rbp),%rdi
movsbl -0x11(%rbp),%esi
call   1060 <ft_countwords@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
shl    $0x3,%rdi
call   1050 <ft_memalloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b0 <ft_strsplit+0x50>
movq   $0x0,-0x8(%rbp)
jmp    127b <ft_strsplit+0x11b>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    1263 <ft_strsplit+0x103>
mov    -0x10(%rbp),%rdi
movsbl -0x11(%rbp),%esi
call   1030 <next_word@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
movsbl -0x11(%rbp),%esi
call   1070 <word_len@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%edx
xor    %esi,%esi
call   1040 <ft_strsub@plt>
mov    %rax,%rdx
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
jne    1235 <ft_strsplit+0xd5>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1080 <cleanup@plt>
movq   $0x0,-0x8(%rbp)
jmp    127b <ft_strsplit+0x11b>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
movsbl -0x11(%rbp),%esi
call   1070 <word_len@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    11b8 <ft_strsplit+0x58>
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ft_strsplit>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%rbp
call   1060 <ft_countwords@plt>
mov    %rax,%r14
lea    0x8(,%rax,8),%rdi
call   1050 <ft_memalloc@plt>
test   %rax,%rax
je     11fd <ft_strsplit+0x9d>
mov    %rax,%r15
test   %r14,%r14
je     11e8 <ft_strsplit+0x88>
xor    %r13d,%r13d
movsbl %bl,%r12d
nopl   0x0(%rax)
mov    %rbp,%rdi
mov    %r12d,%esi
call   1030 <next_word@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12d,%esi
call   1070 <word_len@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %eax,%edx
call   1040 <ft_strsub@plt>
mov    %rax,(%r15,%r13,8)
test   %rax,%rax
je     11f2 <ft_strsplit+0x92>
add    $0x1,%r13
mov    %rbx,%rdi
mov    %r12d,%esi
call   1070 <word_len@plt>
movslq %eax,%rbp
add    %rbx,%rbp
cmp    %r13,%r14
jne    11a0 <ft_strsplit+0x40>
movq   $0x0,(%r15,%r14,8)
jmp    1200 <ft_strsplit+0xa0>
mov    %r15,%rdi
mov    %r13,%rsi
call   1080 <cleanup@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8** @ft_strsplit(i8* %0, i8 signext %1) {
%3 = alloca i8**
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i8**
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %4
store i8 %1, i8* %5
%9 = load i8*, i8** %4
%10 = load i8, i8* %5
%11 = call i64 @ft_countwords(i8* %9, i8 signext %10)
store i64 %11, i64* %8
%12 = load i64, i64* %8
%13 = add i64 %12, 1
%14 = mul i64 %13, 8
%15 = call i64 @ft_memalloc(i64 %14)
%16 = inttoptr i64 %15 to i8**
store i8** %16, i8*** %6
%17 = load i8**, i8*** %6
%18 = icmp eq i8** %17, null
br i1 %18, label %19, label %20
19: ; preds = %2
store i8** null, i8*** %3
br label %60
20: ; preds = %2
store i64 0, i64* %7
br label %21
21: ; preds = %46, %20
%22 = load i64, i64* %7
%23 = load i64, i64* %8
%24 = icmp ult i64 %22, %23
br i1 %24, label %25, label %55
25: ; preds = %21
%26 = load i8*, i8** %4
%27 = load i8, i8* %5
%28 = call i8* @next_word(i8* %26, i8 signext %27)
store i8* %28, i8** %4
%29 = load i8*, i8** %4
%30 = load i8*, i8** %4
%31 = load i8, i8* %5
%32 = call i32 @word_len(i8* %30, i8 signext %31)
%33 = call i8* @ft_strsub(i8* %29, i32 0, i32 %32)
%34 = load i8**, i8*** %6
%35 = load i64, i64* %7
%36 = getelementptr inbounds i8*, i8** %34, i64 %35
store i8* %33, i8** %36
%37 = load i8**, i8*** %6
%38 = load i64, i64* %7
%39 = getelementptr inbounds i8*, i8** %37, i64 %38
%40 = load i8*, i8** %39
%41 = icmp eq i8* %40, null
br i1 %41, label %42, label %46
42: ; preds = %25
%43 = load i8**, i8*** %6
%44 = load i64, i64* %7
%45 = call i32 @cleanup(i8** %43, i64 %44)
store i8** null, i8*** %3
br label %60
46: ; preds = %25
%47 = load i64, i64* %7
%48 = add i64 %47, 1
store i64 %48, i64* %7
%49 = load i8*, i8** %4
%50 = load i8, i8* %5
%51 = call i32 @word_len(i8* %49, i8 signext %50)
%52 = load i8*, i8** %4
%53 = sext i32 %51 to i64
%54 = getelementptr inbounds i8, i8* %52, i64 %53
store i8* %54, i8** %4
br label %21
55: ; preds = %21
%56 = load i8**, i8*** %6
%57 = load i64, i64* %8
%58 = getelementptr inbounds i8*, i8** %56, i64 %57
store i8* null, i8** %58
%59 = load i8**, i8*** %6
store i8** %59, i8*** %3
br label %60
60: ; preds = %55, %42, %19
%61 = load i8**, i8*** %3
ret i8** %61
}

LLVM IR (-O2):
define i8** @ft_strsplit(i8* %0, i8 signext %1) {
%3 = tail call i64 @ft_countwords(i8* %0, i8 signext %1)
%4 = shl i64 %3, 3
%5 = add i64 %4, 8
%6 = tail call i64 @ft_memalloc(i64 %5)
%7 = inttoptr i64 %6 to i8**
%8 = icmp eq i64 %6, 0
br i1 %8, label %29, label %9
9: ; preds = %2
%10 = icmp eq i64 %3, 0
br i1 %10, label %27, label %11
11: ; preds = %9, %21
%12 = phi i64 [ %22, %21 ], [ 0, %9 ]
%13 = phi i8* [ %25, %21 ], [ %0, %9 ]
%14 = tail call i8* @next_word(i8* %13, i8 signext %1)
%15 = tail call i32 @word_len(i8* %14, i8 signext %1)
%16 = tail call i8* @ft_strsub(i8* %14, i32 0, i32 %15)
%17 = getelementptr inbounds i8*, i8** %7, i64 %12
store i8* %16, i8** %17
%18 = icmp eq i8* %16, null
br i1 %18, label %19, label %21
19: ; preds = %11
%20 = tail call i32 @cleanup(i8** %7, i64 %12)
br label %29
21: ; preds = %11
%22 = add nuw i64 %12, 1
%23 = tail call i32 @word_len(i8* %14, i8 signext %1)
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %14, i64 %24
%26 = icmp eq i64 %22, %3
br i1 %26, label %27, label %11
27: ; preds = %21, %9
%28 = getelementptr inbounds i8*, i8** %7, i64 %3
store i8* null, i8** %28
br label %29
29: ; preds = %2, %27, %19
%30 = phi i8** [ null, %19 ], [ %7, %27 ], [ null, %2 ]
ret i8** %30
}