#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*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
char *strdupa(char *);
char *strpbrk(char *, char const *);
char *strtok(char *, char const *);
int main(int argc, char *argv[]) {
  size_t token_no = 0;
  const char *delim = "-:";
  if (argc < 2) {
    printf("Usage: %s <string> [<delim>]\n", argv[0]);
    return 1;
  }
  if (argc >= 3) {
    delim = argv[2];
  }
  printf("input string: %s, delimiter: %s\n", argv[1], delim);
  printf("--- strtok ---\n");
  char *s = strdupa(argv[1]);
  if (!s) {
    fprintf(stderr, "unable to strdup\n");
    return 2;
  }
  s = strtok(s, delim);
  if (!s) {
    return 0;
  }
  do {
    token_no++;
    printf("token#%zu: %s\n", token_no, s);
  } while ((s = strtok(NULL, delim)));
  printf("--- strpbrk ---\n");
  token_no = 0;
  char *end, *start = strdupa(argv[1]);
  if (!start) {
    fprintf(stderr, "unable to strdup\n");
    return 2;
  }
  do {
    char d;
    token_no++;
    end = strpbrk(start, delim);
    if (end) {
      d = *end;
      *end = '\0';
    } else {
      d = '\0';
    }
    printf("delim: %c, token#%zu: %s\n", d, token_no, start);
    start = end;
  } while (start++);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    0xe8b(%rip),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11a4 <main+0x54>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1364 <main+0x214>
cmpl   $0x3,-0x8(%rbp)
jl     11ba <main+0x6a>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x20(%rbp),%rdx
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <strdupa@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    121f <main+0xcf>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
lea    0xe44(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1364 <main+0x214>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1247 <main+0xf7>
movl   $0x0,-0x4(%rbp)
jmp    1364 <main+0x214>
jmp    124c <main+0xfc>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1070 <strtok@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    124c <main+0xfc>
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <strdupa@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12dc <main+0x18c>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
lea    0xd87(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1364 <main+0x214>
jmp    12e1 <main+0x191>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <strpbrk@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     131e <main+0x1ce>
mov    -0x30(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x39(%rbp)
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
jmp    1322 <main+0x1d2>
movb   $0x0,-0x39(%rbp)
movsbl -0x39(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x38(%rbp),%rcx
lea    0xd40(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
cmp    $0x0,%rax
jne    12e1 <main+0x191>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
cmp    $0x1,%edi
jg     118f <main+0x2f>
mov    (%r14),%rsi
lea    0xe86(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%r12d
jmp    12d4 <main+0x174>
cmp    $0x2,%edi
jne    119d <main+0x3d>
lea    0xe65(%rip),%r15
jmp    11a1 <main+0x41>
mov    0x10(%r14),%r15
mov    0x8(%r14),%rsi
lea    0xe75(%rip),%rdi
xor    %r12d,%r12d
mov    %r15,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xeae(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%r14),%rdi
call   1070 <strdupa@plt>
test   %rax,%rax
je     1298 <main+0x138>
mov    %rax,%rdi
mov    %r15,%rsi
call   1080 <strtok@plt>
test   %rax,%rax
je     12d4 <main+0x174>
mov    $0x1,%ebx
lea    0xe68(%rip),%rbp
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
mov    %r15,%rsi
call   1080 <strtok@plt>
add    $0x1,%rbx
test   %rax,%rax
jne    1200 <main+0xa0>
lea    0xe53(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%r14),%rdi
call   1070 <strdupa@plt>
test   %rax,%rax
je     1298 <main+0x138>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   1060 <strpbrk@plt>
test   %rax,%rax
je     12b5 <main+0x155>
mov    %rax,%rbp
mov    $0x1,%r13d
lea    0xdf4(%rip),%r14
movsbl 0x0(%rbp),%esi
movb   $0x0,0x0(%rbp)
mov    %r14,%rdi
mov    %r13,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbp,%rbx
add    $0x1,%rbx
add    $0x1,%r13
mov    %rbx,%rdi
mov    %r15,%rsi
call   1060 <strpbrk@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1260 <main+0x100>
jmp    12bb <main+0x15b>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
lea    0xd9a(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x2,%r12d
jmp    12d4 <main+0x174>
mov    $0x1,%r13d
lea    0xd92(%rip),%rdi
xor    %r12d,%r12d
xor    %esi,%esi
mov    %r13,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"-:\00"
@.str.1 = [30 x i8] c"Usage: %s <string> [<delim>]\0A\00"
@.str.2 = [33 x i8] c"input string: %s, delimiter: %s\0A\00"
@.str.3 = [16 x i8] c"--- strtok ---\0A\00"
@stderr = global i32 0
@.str.4 = [18 x i8] c"unable to strdup\0A\00"
@.str.5 = [15 x i8] c"token#%zu: %s\0A\00"
@.str.6 = [17 x i8] c"--- strpbrk ---\0A\00"
@.str.7 = [26 x i8] c"delim: %c, token#%zu: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 0, i64* %6
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %7
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %17)
store i32 1, i32* %3
br label %97
19: ; preds = %2
%20 = load i32, i32* %4
%21 = icmp sge i32 %20, 3
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
store i8* %25, i8** %7
br label %26
26: ; preds = %22, %19
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = load i8*, i8** %7
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %29, i8* %30)
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = load i8*, i8** %34
%36 = call i8* @strdupa(i8* %35)
store i8* %36, i8** %8
%37 = load i8*, i8** %8
%38 = icmp ne i8* %37, null
br i1 %38, label %42, label %39
39: ; preds = %26
%40 = load i32, i32* @stderr
%41 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
store i32 2, i32* %3
br label %97
42: ; preds = %26
%43 = load i8*, i8** %8
%44 = load i8*, i8** %7
%45 = call i8* @strtok(i8* %43, i8* %44)
store i8* %45, i8** %8
%46 = load i8*, i8** %8
%47 = icmp ne i8* %46, null
br i1 %47, label %49, label %48
48: ; preds = %42
store i32 0, i32* %3
br label %97
49: ; preds = %42
br label %50
50: ; preds = %56, %49
%51 = load i64, i64* %6
%52 = add i64 %51, 1
store i64 %52, i64* %6
%53 = load i64, i64* %6
%54 = load i8*, i8** %8
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i64 %53, i8* %54)
br label %56
56: ; preds = %50
%57 = load i8*, i8** %7
%58 = call i8* @strtok(i8* null, i8* %57)
store i8* %58, i8** %8
%59 = icmp ne i8* %58, null
br i1 %59, label %50, label %60
60: ; preds = %56
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0))
store i64 0, i64* %6
%62 = load i8**, i8*** %5
%63 = getelementptr inbounds i8*, i8** %62, i64 1
%64 = load i8*, i8** %63
%65 = call i8* @strdupa(i8* %64)
store i8* %65, i8** %10
%66 = load i8*, i8** %10
%67 = icmp ne i8* %66, null
br i1 %67, label %71, label %68
68: ; preds = %60
%69 = load i32, i32* @stderr
%70 = call i32 @fprintf(i32 %69, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
store i32 2, i32* %3
br label %97
71: ; preds = %60
br label %72
72: ; preds = %92, %71
%73 = load i64, i64* %6
%74 = add i64 %73, 1
store i64 %74, i64* %6
%75 = load i8*, i8** %10
%76 = load i8*, i8** %7
%77 = call i8* @strpbrk(i8* %75, i8* %76)
store i8* %77, i8** %9
%78 = load i8*, i8** %9
%79 = icmp ne i8* %78, null
br i1 %79, label %80, label %84
80: ; preds = %72
%81 = load i8*, i8** %9
%82 = load i8, i8* %81
store i8 %82, i8* %11
%83 = load i8*, i8** %9
store i8 0, i8* %83
br label %85
84: ; preds = %72
store i8 0, i8* %11
br label %85
85: ; preds = %84, %80
%86 = load i8, i8* %11
%87 = sext i8 %86 to i32
%88 = load i64, i64* %6
%89 = load i8*, i8** %10
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0), i32 %87, i64 %88, i8* %89)
%91 = load i8*, i8** %9
store i8* %91, i8** %10
br label %92
92: ; preds = %85
%93 = load i8*, i8** %10
%94 = getelementptr inbounds i8, i8* %93, i32 1
store i8* %94, i8** %10
%95 = icmp ne i8* %93, null
br i1 %95, label %72, label %96
96: ; preds = %92
store i32 0, i32* %3
br label %97
97: ; preds = %96, %68, %48, %39, %14
%98 = load i32, i32* %3
ret i32 %98
}

LLVM IR (-O2):
@.str = [3 x i8] c"-:\00"
@.str.1 = [30 x i8] c"Usage: %s <string> [<delim>]\0A\00"
@.str.2 = [33 x i8] c"input string: %s, delimiter: %s\0A\00"
@stderr = global i32 0
@.str.4 = [18 x i8] c"unable to strdup\0A\00"
@.str.5 = [15 x i8] c"token#%zu: %s\0A\00"
@.str.7 = [26 x i8] c"delim: %c, token#%zu: %s\0A\00"
@str = [15 x i8] c"--- strtok ---\00"
@str.8 = [16 x i8] c"--- strpbrk ---\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %5)
br label %60
7: ; preds = %2
%8 = icmp eq i32 %0, 2
br i1 %8, label %12, label %9
9: ; preds = %7
%10 = getelementptr inbounds i8*, i8** %1, i64 2
%11 = load i8*, i8** %10
br label %12
12: ; preds = %9, %7
%13 = phi i8* [ %11, %9 ], [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), %7 ]
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %15, i8* %13)
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%18 = load i8*, i8** %14
%19 = tail call i8* @strdupa(i8* %18)
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %24
21: ; preds = %12
%22 = load i32, i32* @stderr
%23 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %60
24: ; preds = %12
%25 = tail call i8* @strtok(i8* %19, i8* %13)
%26 = icmp eq i8* %25, null
br i1 %26, label %60, label %27
27: ; preds = %24, %27
%28 = phi i64 [ %30, %27 ], [ 0, %24 ]
%29 = phi i8* [ %32, %27 ], [ %25, %24 ]
%30 = add i64 %28, 1
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i64 %30, i8* %29)
%32 = tail call i8* @strtok(i8* null, i8* %13)
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %27
34: ; preds = %27
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.8, i64 0, i64 0))
%36 = load i8*, i8** %14
%37 = tail call i8* @strdupa(i8* %36)
%38 = icmp eq i8* %37, null
br i1 %38, label %42, label %39
39: ; preds = %34
%40 = tail call i8* @strpbrk(i8* %37, i8* %13)
%41 = icmp eq i8* %40, null
br i1 %41, label %45, label %49
42: ; preds = %34
%43 = load i32, i32* @stderr
%44 = tail call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %60
45: ; preds = %49, %39
%46 = phi i8* [ %37, %39 ], [ %56, %49 ]
%47 = phi i64 [ 1, %39 ], [ %57, %49 ]
%48 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0), i32 0, i64 %47, i8* %46)
br label %60
49: ; preds = %39, %49
%50 = phi i8* [ %58, %49 ], [ %40, %39 ]
%51 = phi i64 [ %57, %49 ], [ 1, %39 ]
%52 = phi i8* [ %56, %49 ], [ %37, %39 ]
%53 = load i8, i8* %50
store i8 0, i8* %50
%54 = sext i8 %53 to i32
%55 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0), i32 %54, i64 %51, i8* %52)
%56 = getelementptr inbounds i8, i8* %50, i64 1
%57 = add i64 %51, 1
%58 = tail call i8* @strpbrk(i8* %56, i8* %13)
%59 = icmp eq i8* %58, null
br i1 %59, label %45, label %49
60: ; preds = %45, %21, %24, %42, %4
%61 = phi i32 [ 1, %4 ], [ 2, %21 ], [ 0, %24 ], [ 2, %42 ], [ 0, %45 ]
ret i32 %61
}