#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 atoi(char *);
char **calloc(int, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
char *strdup(char *);
char *strsep(char **, char *);
int main(int argc, char **argv) {
  int i = 0;
  char *tok, *p_tok;
  static char **arr;
  int arrsiz = 1000;
  static char *delim;
  static char *str;
  if (argc == 1) {
    printf("automatic test ..\n");
    delim = strdup(" ");
    str = strdup(
        "this is a simple string, which should be extracted to 12 strings");
  } else if (argc != 4) {
    fprintf(stderr, "usage: %s [ARR_SIZ] [DELIM] [STRING]\n", argv[0]);
    exit(1);
  } else {
    arrsiz = atoi(argv[1]);
    delim = strdup(argv[2]);
    str = strdup(argv[3]);
  }
  arr = calloc(arrsiz, sizeof(char *));
  p_tok = str;
  while ((tok = strsep(&p_tok, delim)) != NULL) {
    arr[i] = tok;
    i++;
  }
  i = 0;
  while (arr[i] != NULL) {
    printf("ARRAY[%d]: %s\n", i, arr[i]);
    i++;
  }
  if (argc == 1) {
    if (i == 12) {
      return 0;
    } else
      return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x3e8,-0x2c(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    11d7 <main+0x67>
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe60(%rip),%rdi
call   1090 <strdup@plt>
mov    %rax,0x2ea9(%rip)
lea    0xe4f(%rip),%rdi
call   1090 <strdup@plt>
mov    %rax,0x2e9e(%rip)
jmp    1244 <main+0xd4>
cmpl   $0x4,-0x8(%rbp)
je     1207 <main+0x97>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe5e(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <strdup@plt>
mov    %rax,0x2e3d(%rip)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1090 <strdup@plt>
mov    %rax,0x2e31(%rip)
jmp    1244 <main+0xd4>
mov    -0x2c(%rbp),%edi
mov    $0x8,%esi
call   1040 <calloc@plt>
mov    %rax,0x2e08(%rip)
mov    0x2e11(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2dfe(%rip),%rsi
lea    -0x28(%rbp),%rdi
call   1060 <strsep@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     12a2 <main+0x132>
mov    -0x20(%rbp),%rdx
mov    0x2dd4(%rip),%rax
movslq -0x14(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1263 <main+0xf3>
movl   $0x0,-0x14(%rbp)
mov    0x2db0(%rip),%rax
movslq -0x14(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     12ed <main+0x17d>
mov    -0x14(%rbp),%esi
mov    0x2d97(%rip),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xda4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    12a9 <main+0x139>
cmpl   $0x1,-0x8(%rbp)
jne    1319 <main+0x1a9>
cmpl   $0xc,-0x14(%rbp)
jne    130d <main+0x19d>
movl   $0x0,-0x4(%rbp)
jmp    1320 <main+0x1b0>
movl   $0xffffffff,-0x4(%rbp)
jmp    1320 <main+0x1b0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
cmp    $0x1,%edi
jne    11c8 <main+0x48>
lea    0xee2(%rip),%rdi
call   1030 <puts@plt>
lea    0xe5e(%rip),%rdi
call   10a0 <strdup@plt>
mov    %rax,%rbx
mov    %rax,0x2ebf(%rip)
lea    0xe4a(%rip),%rdi
call   10a0 <strdup@plt>
mov    %rax,%rbp
mov    $0x3e8,%r15d
jmp    1200 <main+0x80>
mov    %rsi,%rbp
cmp    $0x4,%r14d
jne    12c4 <main+0x144>
mov    0x8(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,%r15d
mov    0x10(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,%rbx
mov    %rax,0x2e7c(%rip)
mov    0x18(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,%rbp
mov    %rbp,0x2e71(%rip)
mov    %r15d,%edi
mov    $0x8,%esi
call   1050 <calloc@plt>
mov    %rax,0x2e4d(%rip)
mov    %rbp,(%rsp)
mov    %rsp,%rdi
mov    %rbx,%rsi
call   1070 <strsep@plt>
test   %rax,%rax
je     1263 <main+0xe3>
xor    %ebp,%ebp
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x2e21(%rip),%rcx
mov    %rax,(%rcx,%rbp,1)
mov    0x2e1e(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strsep@plt>
add    $0x8,%rbp
test   %rax,%rax
jne    1240 <main+0xc0>
mov    0x2dfe(%rip),%rax
mov    (%rax),%rdx
test   %rdx,%rdx
je     12a9 <main+0x129>
lea    0xdf0(%rip),%rbp
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2dd5(%rip),%rax
mov    0x8(%rax,%rbx,8),%rdx
add    $0x1,%rbx
test   %rdx,%rdx
jne    1280 <main+0x100>
cmp    $0xc,%ebx
setne  %al
jmp    12ab <main+0x12b>
mov    $0x1,%al
cmp    $0x1,%r14d
sete   %cl
and    %al,%cl
movzbl %cl,%eax
neg    %eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rdx
lea    0xd6b(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@main.arr = internal global i8** null
@main.delim = internal global i8* null
@main.str = internal global i8* null
@.str = [19 x i8] c"automatic test ..\0A\00"
@.str.1 = [2 x i8] c" \00"
@.str.2 = [65 x i8] c"this is a simple string, which should be extracted to 12 strings\00"
@stderr = global i32 0
@.str.3 = [38 x i8] c"usage: %s [ARR_SIZ] [DELIM] [STRING]\0A\00"
@.str.4 = [15 x i8] c"ARRAY[%d]: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 1000, i32* %9
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%14 = call i8* @strdup(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %14, i8** @main.delim
%15 = call i8* @strdup(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.2, i64 0, i64 0))
store i8* %15, i8** @main.str
br label %40
16: ; preds = %2
%17 = load i32, i32* %4
%18 = icmp ne i32 %17, 4
br i1 %18, label %19, label %26
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 0
%23 = load i8*, i8** %22
%24 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %23)
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %16
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32 @atoi(i8* %29)
store i32 %30, i32* %9
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = call i8* @strdup(i8* %33)
store i8* %34, i8** @main.delim
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 3
%37 = load i8*, i8** %36
%38 = call i8* @strdup(i8* %37)
store i8* %38, i8** @main.str
br label %39
39: ; preds = %26
br label %40
40: ; preds = %39, %12
%41 = load i32, i32* %9
%42 = call i8** @calloc(i32 %41, i32 8)
store i8** %42, i8*** @main.arr
%43 = load i8*, i8** @main.str
store i8* %43, i8** %8
br label %44
44: ; preds = %48, %40
%45 = load i8*, i8** @main.delim
%46 = call i8* @strsep(i8** %8, i8* %45)
store i8* %46, i8** %7
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %56
48: ; preds = %44
%49 = load i8*, i8** %7
%50 = load i8**, i8*** @main.arr
%51 = load i32, i32* %6
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8*, i8** %50, i64 %52
store i8* %49, i8** %53
%54 = load i32, i32* %6
%55 = add nsw i32 %54, 1
store i32 %55, i32* %6
br label %44
56: ; preds = %44
store i32 0, i32* %6
br label %57
57: ; preds = %64, %56
%58 = load i8**, i8*** @main.arr
%59 = load i32, i32* %6
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8*, i8** %58, i64 %60
%62 = load i8*, i8** %61
%63 = icmp ne i8* %62, null
br i1 %63, label %64, label %74
64: ; preds = %57
%65 = load i32, i32* %6
%66 = load i8**, i8*** @main.arr
%67 = load i32, i32* %6
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8*, i8** %66, i64 %68
%70 = load i8*, i8** %69
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %65, i8* %70)
%72 = load i32, i32* %6
%73 = add nsw i32 %72, 1
store i32 %73, i32* %6
br label %57
74: ; preds = %57
%75 = load i32, i32* %4
%76 = icmp eq i32 %75, 1
br i1 %76, label %77, label %82
77: ; preds = %74
%78 = load i32, i32* %6
%79 = icmp eq i32 %78, 12
br i1 %79, label %80, label %81
80: ; preds = %77
store i32 0, i32* %3
br label %83
81: ; preds = %77
store i32 -1, i32* %3
br label %83
82: ; preds = %74
store i32 0, i32* %3
br label %83
83: ; preds = %82, %81, %80
%84 = load i32, i32* %3
ret i32 %84
}

LLVM IR (-O2):
@main.arr = internal unnamed_addr global i8** null
@main.delim = internal unnamed_addr global i8* null
@main.str = internal unnamed_addr global i8* null
@.str.1 = [2 x i8] c" \00"
@.str.2 = [65 x i8] c"this is a simple string, which should be extracted to 12 strings\00"
@stderr = global i32 0
@.str.3 = [38 x i8] c"usage: %s [ARR_SIZ] [DELIM] [STRING]\0A\00"
@.str.4 = [15 x i8] c"ARRAY[%d]: %s\0A\00"
@str = [18 x i8] c"automatic test ..\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = icmp eq i32 %0, 1
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%8 = tail call dereferenceable_or_null(2) i8* @strdup(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %8, i8** @main.delim
%9 = tail call dereferenceable_or_null(65) i8* @strdup(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.2, i64 0, i64 0))
br label %27
10: ; preds = %2
%11 = icmp eq i32 %0, 4
br i1 %11, label %17, label %12
12: ; preds = %10
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %1
%15 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %14)
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %10
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = tail call i32 @atoi(i8* %19)
%21 = getelementptr inbounds i8*, i8** %1, i64 2
%22 = load i8*, i8** %21
%23 = tail call i8* @strdup(i8* %22)
store i8* %23, i8** @main.delim
%24 = getelementptr inbounds i8*, i8** %1, i64 3
%25 = load i8*, i8** %24
%26 = tail call i8* @strdup(i8* %25)
br label %27
27: ; preds = %17, %6
%28 = phi i8* [ %23, %17 ], [ %8, %6 ]
%29 = phi i8* [ %26, %17 ], [ %9, %6 ]
%30 = phi i32 [ %20, %17 ], [ 1000, %6 ]
store i8* %29, i8** @main.str
%31 = tail call i8** @calloc(i32 %30, i32 8)
store i8** %31, i8*** @main.arr
store i8* %29, i8** %3
%32 = call i8* @strsep(i8** %3, i8* %28)
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %38
34: ; preds = %38, %27
%35 = load i8**, i8*** @main.arr
%36 = load i8*, i8** %35
%37 = icmp eq i8* %36, null
br i1 %37, label %60, label %47
38: ; preds = %27, %38
%39 = phi i64 [ %43, %38 ], [ 0, %27 ]
%40 = phi i8* [ %45, %38 ], [ %32, %27 ]
%41 = load i8**, i8*** @main.arr
%42 = getelementptr inbounds i8*, i8** %41, i64 %39
store i8* %40, i8** %42
%43 = add nuw i64 %39, 1
%44 = load i8*, i8** @main.delim
%45 = call i8* @strsep(i8** %3, i8* %44)
%46 = icmp eq i8* %45, null
br i1 %46, label %34, label %38
47: ; preds = %34, %47
%48 = phi i64 [ %52, %47 ], [ 0, %34 ]
%49 = phi i8* [ %55, %47 ], [ %36, %34 ]
%50 = trunc i64 %48 to i32
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %50, i8* %49)
%52 = add nuw i64 %48, 1
%53 = load i8**, i8*** @main.arr
%54 = getelementptr inbounds i8*, i8** %53, i64 %52
%55 = load i8*, i8** %54
%56 = icmp eq i8* %55, null
br i1 %56, label %57, label %47
57: ; preds = %47
%58 = trunc i64 %52 to i32
%59 = icmp ne i32 %58, 12
br label %60
60: ; preds = %57, %34
%61 = phi i1 [ true, %34 ], [ %59, %57 ]
%62 = select i1 %5, i1 %61, i1 false
%63 = sext i1 %62 to i32
ret i32 %63
}