#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 load_next_text(char *, size_t);
int /*<<< orphan*/ *strchr(char const *, char);
int strlen(char *);
char *strtok(char *, char const *);
int tokenizer(const char *delimiter, char *buffer, size_t buffer_size,
              void (*partial_fn)(char *), void (*complete_fn)(void)) {
  char *f;
  int has_text;
  has_text = load_next_text(buffer, buffer_size);
  if (!has_text) {
    return 0;
  }
  do {
    int token_exists = 0;
    int not_truncated = 0;
    if (strchr(delimiter, buffer[strlen(buffer) - 1]) != NULL) {
      not_truncated = 1;
    }
    f = strtok(buffer, delimiter);
    while (f != NULL) {
      partial_fn(f);
      f = strtok(NULL, delimiter);
      if (f != NULL) {
        complete_fn();
      } else {
        if (not_truncated) {
          complete_fn();
        } else {
          token_exists = 1;
        }
      }
    }
    has_text = load_next_text(buffer, buffer_size);
    if (token_exists && strchr(delimiter, buffer[0]) != NULL) {
      complete_fn();
    }
  } while (has_text);
  return 1;
}


x86 assembly (clang, -O0):
<tokenizer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <load_next_text@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jne    1182 <tokenizer+0x42>
movl   $0x0,-0x4(%rbp)
jmp    1292 <tokenizer+0x152>
jmp    1187 <tokenizer+0x47>
movl   $0x0,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    %eax,%ecx
mov    -0x50(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
je     11d8 <tokenizer+0x98>
movl   $0x1,-0x44(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strtok@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1245 <tokenizer+0x105>
mov    -0x28(%rbp),%rax
mov    -0x38(%rbp),%rdi
call   *%rax
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1050 <strtok@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1222 <tokenizer+0xe2>
call   *-0x30(%rbp)
jmp    1240 <tokenizer+0x100>
cmpl   $0x0,-0x44(%rbp)
je     1234 <tokenizer+0xf4>
call   *-0x30(%rbp)
jmp    123b <tokenizer+0xfb>
movl   $0x1,-0x40(%rbp)
jmp    1240 <tokenizer+0x100>
jmp    11e9 <tokenizer+0xa9>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <load_next_text@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x40(%rbp)
je     127c <tokenizer+0x13c>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
movsbl (%rax),%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
je     127c <tokenizer+0x13c>
call   *-0x30(%rbp)
jmp    1281 <tokenizer+0x141>
cmpl   $0x0,-0x3c(%rbp)
jne    1187 <tokenizer+0x47>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tokenizer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r14
mov    %rcx,%r13
mov    %rsi,%r12
mov    %rdi,%rbx
mov    %rsi,%rdi
mov    %rdx,(%rsp)
mov    %rdx,%rsi
call   1060 <load_next_text@plt>
test   %eax,%eax
jne    119a <tokenizer+0x5a>
xor    %eax,%eax
jmp    121a <tokenizer+0xda>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
test   %rbp,%rbp
je     11f0 <tokenizer+0xb0>
call   *%r14
mov    %r12,%rdi
mov    (%rsp),%rsi
call   1060 <load_next_text@plt>
mov    %eax,%ebp
test   %ebp,%ebp
je     1215 <tokenizer+0xd5>
mov    %r12,%rdi
call   1030 <strlen@plt>
cltq
movsbl -0x1(%rax,%r12,1),%esi
mov    %rbx,%rdi
call   1040 <strchr@plt>
mov    %rax,%rbp
mov    %r12,%rdi
mov    %rbx,%rsi
call   1050 <strtok@plt>
test   %rax,%rax
je     1188 <tokenizer+0x48>
mov    %rax,%rdi
nopl   0x0(%rax,%rax,1)
call   *%r13
xor    %edi,%edi
mov    %rbx,%rsi
call   1050 <strtok@plt>
mov    %rax,%r15
test   %rax,%rax
je     1180 <tokenizer+0x40>
call   *%r14
mov    %r15,%rdi
jmp    11d0 <tokenizer+0x90>
nopl   (%rax)
mov    %r12,%rdi
mov    (%rsp),%rsi
call   1060 <load_next_text@plt>
mov    %eax,%ebp
movsbl (%r12),%esi
mov    %rbx,%rdi
call   1040 <strchr@plt>
test   %rax,%rax
je     1196 <tokenizer+0x56>
call   *%r14
jmp    1196 <tokenizer+0x56>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @tokenizer(i8* %0, i8* %1, i64 %2, void (i8*)* %3, void ()* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca void (i8*)*
%11 = alloca void ()*
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i8* %0, i8** %7
store i8* %1, i8** %8
store i64 %2, i64* %9
store void (i8*)* %3, void (i8*)** %10
store void ()* %4, void ()** %11
%16 = load i8*, i8** %8
%17 = load i64, i64* %9
%18 = call i32 @load_next_text(i8* %16, i64 %17)
store i32 %18, i32* %13
%19 = load i32, i32* %13
%20 = icmp ne i32 %19, 0
br i1 %20, label %22, label %21
21: ; preds = %5
store i32 0, i32* %6
br label %79
22: ; preds = %5
br label %23
23: ; preds = %75, %22
store i32 0, i32* %14
store i32 0, i32* %15
%24 = load i8*, i8** %7
%25 = load i8*, i8** %8
%26 = load i8*, i8** %8
%27 = call i32 @strlen(i8* %26)
%28 = sub nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %25, i64 %29
%31 = load i8, i8* %30
%32 = call i32* @strchr(i8* %24, i8 signext %31)
%33 = icmp ne i32* %32, null
br i1 %33, label %34, label %35
34: ; preds = %23
store i32 1, i32* %15
br label %35
35: ; preds = %34, %23
%36 = load i8*, i8** %8
%37 = load i8*, i8** %7
%38 = call i8* @strtok(i8* %36, i8* %37)
store i8* %38, i8** %12
br label %39
39: ; preds = %58, %35
%40 = load i8*, i8** %12
%41 = icmp ne i8* %40, null
br i1 %41, label %42, label %59
42: ; preds = %39
%43 = load void (i8*)*, void (i8*)** %10
%44 = load i8*, i8** %12
%45 = load i8*, i8** %7
%46 = call i8* @strtok(i8* null, i8* %45)
store i8* %46, i8** %12
%47 = load i8*, i8** %12
%48 = icmp ne i8* %47, null
br i1 %48, label %49, label %51
49: ; preds = %42
%50 = load void ()*, void ()** %11
br label %58
51: ; preds = %42
%52 = load i32, i32* %15
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %51
%55 = load void ()*, void ()** %11
br label %57
56: ; preds = %51
store i32 1, i32* %14
br label %57
57: ; preds = %56, %54
br label %58
58: ; preds = %57, %49
br label %39
59: ; preds = %39
%60 = load i8*, i8** %8
%61 = load i64, i64* %9
%62 = call i32 @load_next_text(i8* %60, i64 %61)
store i32 %62, i32* %13
%63 = load i32, i32* %14
%64 = icmp ne i32 %63, 0
br i1 %64, label %65, label %74
65: ; preds = %59
%66 = load i8*, i8** %7
%67 = load i8*, i8** %8
%68 = getelementptr inbounds i8, i8* %67, i64 0
%69 = load i8, i8* %68
%70 = call i32* @strchr(i8* %66, i8 signext %69)
%71 = icmp ne i32* %70, null
br i1 %71, label %72, label %74
72: ; preds = %65
%73 = load void ()*, void ()** %11
br label %74
74: ; preds = %72, %65, %59
br label %75
75: ; preds = %74
%76 = load i32, i32* %13
%77 = icmp ne i32 %76, 0
br i1 %77, label %23, label %78
78: ; preds = %75
store i32 1, i32* %6
br label %79
79: ; preds = %78, %21
%80 = load i32, i32* %6
ret i32 %80
}

LLVM IR (-O2):
define i32 @tokenizer(i8* %0, i8* %1, i64 %2, void (i8*)* nocapture %3, void ()* nocapture %4) {
%6 = tail call i32 @load_next_text(i8* %1, i64 %2)
%7 = icmp eq i32 %6, 0
br i1 %7, label %38, label %8
8: ; preds = %5, %35
%9 = tail call i32 @strlen(i8* %1)
%10 = add nsw i32 %9, -1
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %1, i64 %11
%13 = load i8, i8* %12
%14 = tail call i32* @strchr(i8* %0, i8 signext %13)
%15 = icmp eq i32* %14, null
%16 = tail call i8* @strtok(i8* %1, i8* %0)
%17 = icmp eq i8* %16, null
br i1 %17, label %27, label %18
18: ; preds = %8
tail call void %3(i8* %16)
%19 = tail call i8* @strtok(i8* null, i8* %0)
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %23
21: ; preds = %23, %18
br i1 %15, label %29, label %22
22: ; preds = %21
tail call void %4()
br label %27
23: ; preds = %18, %23
%24 = phi i8* [ %25, %23 ], [ %19, %18 ]
tail call void %4()
tail call void %3(i8* %24)
%25 = tail call i8* @strtok(i8* null, i8* %0)
%26 = icmp eq i8* %25, null
br i1 %26, label %21, label %23
27: ; preds = %8, %22
%28 = tail call i32 @load_next_text(i8* %1, i64 %2)
br label %35
29: ; preds = %21
%30 = tail call i32 @load_next_text(i8* %1, i64 %2)
%31 = load i8, i8* %1
%32 = tail call i32* @strchr(i8* %0, i8 signext %31)
%33 = icmp eq i32* %32, null
br i1 %33, label %35, label %34
34: ; preds = %29
tail call void %4()
br label %35
35: ; preds = %27, %34, %29
%36 = phi i32 [ %28, %27 ], [ %30, %34 ], [ %30, %29 ]
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %8
38: ; preds = %35, %5
%39 = phi i32 [ 0, %5 ], [ 1, %35 ]
ret i32 %39
}