#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 */
char *ShareMallocM1c(int);
char **ShareMallocM2c(int, int /*<<< orphan*/);
char **ShareReallocM2c(char **, int, int /*<<< orphan*/);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *strstr(char *, char const *);
char **ShareSplitStr(const char *buf0, const char *sepa, int *len, int flag) {
  char buf[4096];
  char *str0, *str1, *str2;
  int num = 0;
  char *s = NULL;
  char **strarr = NULL;
  int n;
  n = (strlen(buf0) + 1 > 4096) ? 4096 - 1 : strlen(buf0);
  strncpy(buf, buf0, n);
  buf[n] = 0;
  str0 = str1 = buf;
  str2 = buf + strlen(buf);
  strarr = ShareMallocM2c(1, 0);
  while (str0 <= str2) {
    str1 = strstr(str0, sepa);
    if (str1 == NULL)
      str1 = str2;
    if (((str1 >= str0) && (flag == 0)) || ((str1 > str0) && (flag == 1))) {
      strarr = ShareReallocM2c(strarr, num + 1, 0);
      s = ShareMallocM1c(str1 - str0 + 1);
      strncpy(s, str0, str1 - str0);
      s[str1 - str0] = 0;
      strarr[num] = s;
      num++;
    }
    str0 = str1 + strlen(sepa);
  }
  *len = num;
  return strarr;
}


x86 assembly (clang, -O0):
<ShareSplitStr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1070,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x103c(%rbp)
movq   $0x0,-0x1048(%rbp)
movq   $0x0,-0x1050(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cmp    $0x1000,%eax
jle    11c1 <ShareSplitStr+0x61>
mov    $0xfff,%eax
mov    %eax,-0x1058(%rbp)
jmp    11d0 <ShareSplitStr+0x70>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1058(%rbp)
mov    -0x1058(%rbp),%eax
mov    %eax,-0x1054(%rbp)
lea    -0x1020(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x1054(%rbp),%edx
call   1030 <strncpy@plt>
movslq -0x1054(%rbp),%rax
movb   $0x0,-0x1020(%rbp,%rax,1)
lea    -0x1020(%rbp),%rax
mov    %rax,-0x1030(%rbp)
mov    %rax,-0x1028(%rbp)
lea    -0x1020(%rbp),%rax
mov    %rax,-0x1060(%rbp)
lea    -0x1020(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x1060(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x1038(%rbp)
mov    $0x1,%edi
xor    %esi,%esi
call   1070 <ShareMallocM2c@plt>
mov    %rax,-0x1050(%rbp)
mov    -0x1028(%rbp),%rax
cmp    -0x1038(%rbp),%rax
ja     13b9 <ShareSplitStr+0x259>
mov    -0x1028(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x1030(%rbp)
cmpq   $0x0,-0x1030(%rbp)
jne    12a0 <ShareSplitStr+0x140>
mov    -0x1038(%rbp),%rax
mov    %rax,-0x1030(%rbp)
mov    -0x1030(%rbp),%rax
cmp    -0x1028(%rbp),%rax
jb     12be <ShareSplitStr+0x15e>
cmpl   $0x0,-0x1c(%rbp)
je     12dc <ShareSplitStr+0x17c>
mov    -0x1030(%rbp),%rax
cmp    -0x1028(%rbp),%rax
jbe    1387 <ShareSplitStr+0x227>
cmpl   $0x1,-0x1c(%rbp)
jne    1387 <ShareSplitStr+0x227>
mov    -0x1050(%rbp),%rdi
mov    -0x103c(%rbp),%esi
add    $0x1,%esi
xor    %edx,%edx
call   1050 <ShareReallocM2c@plt>
mov    %rax,-0x1050(%rbp)
mov    -0x1030(%rbp),%rax
mov    -0x1028(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1060 <ShareMallocM1c@plt>
mov    %rax,-0x1048(%rbp)
mov    -0x1048(%rbp),%rdi
mov    -0x1028(%rbp),%rsi
mov    -0x1030(%rbp),%rax
mov    -0x1028(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x1048(%rbp),%rax
mov    -0x1030(%rbp),%rcx
mov    -0x1028(%rbp),%rdx
sub    %rdx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x1048(%rbp),%rdx
mov    -0x1050(%rbp),%rax
movslq -0x103c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x103c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x103c(%rbp)
mov    -0x1030(%rbp),%rax
mov    %rax,-0x1068(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x1068(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x1028(%rbp)
jmp    1259 <ShareSplitStr+0xf9>
mov    -0x103c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1050(%rbp),%rax
add    $0x1070,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ShareSplitStr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1038,%rsp
mov    %ecx,0x14(%rsp)
mov    %rdx,0x20(%rsp)
mov    %rsi,0x18(%rsp)
mov    %rdi,%r14
call   1040 <strlen@plt>
mov    $0xfff,%ebx
cmp    $0xfff,%eax
jg     119d <ShareSplitStr+0x3d>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
lea    0x30(%rsp),%r13
mov    %r13,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
movslq %ebx,%rax
movb   $0x0,0x30(%rsp,%rax,1)
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    $0x1,%edi
xor    %esi,%esi
call   1070 <ShareMallocM2c@plt>
mov    %rax,0x8(%rsp)
test   %ebx,%ebx
js     12b1 <ShareSplitStr+0x151>
movslq %ebx,%rax
lea    (%rsp,%rax,1),%rbp
add    $0x30,%rbp
xor    %eax,%eax
mov    %rax,(%rsp)
mov    0x18(%rsp),%r14
mov    %rbp,0x28(%rsp)
jmp    126c <ShareSplitStr+0x10c>
nopw   0x0(%rax,%rax,1)
mov    (%rsp),%rbp
lea    0x1(%rbp),%esi
mov    %esi,0x10(%rsp)
mov    0x8(%rsp),%rdi
xor    %edx,%edx
call   1050 <ShareReallocM2c@plt>
mov    %rax,%r15
lea    0x1(%r14),%edi
call   1060 <ShareMallocM1c@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r13,%rsi
mov    %r14d,%edx
call   1030 <strncpy@plt>
movb   $0x0,(%r12,%r14,1)
movslq %ebp,%rax
mov    %r15,0x8(%rsp)
mov    %r12,(%r15,%rax,8)
mov    0x10(%rsp),%eax
mov    %rax,(%rsp)
mov    0x18(%rsp),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
cltq
add    %rax,%rbx
mov    %rbx,%r13
mov    0x28(%rsp),%rbp
cmp    %rbp,%rbx
ja     12b7 <ShareSplitStr+0x157>
mov    0x14(%rsp),%eax
cmp    $0x1,%eax
sete   %r15b
test   %eax,%eax
sete   %r12b
mov    %r13,%rdi
mov    %r14,%rsi
call   1080 <strstr@plt>
mov    %rax,%rbx
test   %rax,%rax
cmove  %rbp,%rbx
mov    %rbx,%r14
sub    %r13,%r14
setae  %cl
seta   %al
test   %cl,%r12b
jne    1200 <ShareSplitStr+0xa0>
and    %al,%r15b
je     124d <ShareSplitStr+0xed>
jmp    1200 <ShareSplitStr+0xa0>
xor    %eax,%eax
mov    %rax,(%rsp)
mov    0x20(%rsp),%rax
mov    (%rsp),%rcx
mov    %ecx,(%rax)
mov    0x8(%rsp),%rax
add    $0x1038,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8** @ShareSplitStr(i8* %0, i8* %1, i32* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca [4096 x i8]
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i8**
%16 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
store i32 %3, i32* %8
store i32 0, i32* %13
store i8* null, i8** %14
store i8** null, i8*** %15
%17 = load i8*, i8** %5
%18 = call i32 @strlen(i8* %17)
%19 = add nsw i32 %18, 1
%20 = icmp sgt i32 %19, 4096
br i1 %20, label %21, label %22
21: ; preds = %4
br label %25
22: ; preds = %4
%23 = load i8*, i8** %5
%24 = call i32 @strlen(i8* %23)
br label %25
25: ; preds = %22, %21
%26 = phi i32 [ 4095, %21 ], [ %24, %22 ]
store i32 %26, i32* %16
%27 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%28 = load i8*, i8** %5
%29 = load i32, i32* %16
%30 = call i32 @strncpy(i8* %27, i8* %28, i32 %29)
%31 = load i32, i32* %16
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 %32
store i8 0, i8* %33
%34 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
store i8* %34, i8** %11
store i8* %34, i8** %10
%35 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%36 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%37 = call i32 @strlen(i8* %36)
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %35, i64 %38
store i8* %39, i8** %12
%40 = call i8** @ShareMallocM2c(i32 1, i32 0)
store i8** %40, i8*** %15
br label %41
41: ; preds = %103, %25
%42 = load i8*, i8** %10
%43 = load i8*, i8** %12
%44 = icmp ule i8* %42, %43
br i1 %44, label %45, label %109
45: ; preds = %41
%46 = load i8*, i8** %10
%47 = load i8*, i8** %6
%48 = call i8* @strstr(i8* %46, i8* %47)
store i8* %48, i8** %11
%49 = load i8*, i8** %11
%50 = icmp eq i8* %49, null
br i1 %50, label %51, label %53
51: ; preds = %45
%52 = load i8*, i8** %12
store i8* %52, i8** %11
br label %53
53: ; preds = %51, %45
%54 = load i8*, i8** %11
%55 = load i8*, i8** %10
%56 = icmp uge i8* %54, %55
br i1 %56, label %57, label %60
57: ; preds = %53
%58 = load i32, i32* %8
%59 = icmp eq i32 %58, 0
br i1 %59, label %67, label %60
60: ; preds = %57, %53
%61 = load i8*, i8** %11
%62 = load i8*, i8** %10
%63 = icmp ugt i8* %61, %62
br i1 %63, label %64, label %103
64: ; preds = %60
%65 = load i32, i32* %8
%66 = icmp eq i32 %65, 1
br i1 %66, label %67, label %103
67: ; preds = %64, %57
%68 = load i8**, i8*** %15
%69 = load i32, i32* %13
%70 = add nsw i32 %69, 1
%71 = call i8** @ShareReallocM2c(i8** %68, i32 %70, i32 0)
store i8** %71, i8*** %15
%72 = load i8*, i8** %11
%73 = load i8*, i8** %10
%74 = ptrtoint i8* %72 to i64
%75 = ptrtoint i8* %73 to i64
%76 = sub i64 %74, %75
%77 = add nsw i64 %76, 1
%78 = trunc i64 %77 to i32
%79 = call i8* @ShareMallocM1c(i32 %78)
store i8* %79, i8** %14
%80 = load i8*, i8** %14
%81 = load i8*, i8** %10
%82 = load i8*, i8** %11
%83 = load i8*, i8** %10
%84 = ptrtoint i8* %82 to i64
%85 = ptrtoint i8* %83 to i64
%86 = sub i64 %84, %85
%87 = trunc i64 %86 to i32
%88 = call i32 @strncpy(i8* %80, i8* %81, i32 %87)
%89 = load i8*, i8** %14
%90 = load i8*, i8** %11
%91 = load i8*, i8** %10
%92 = ptrtoint i8* %90 to i64
%93 = ptrtoint i8* %91 to i64
%94 = sub i64 %92, %93
%95 = getelementptr inbounds i8, i8* %89, i64 %94
store i8 0, i8* %95
%96 = load i8*, i8** %14
%97 = load i8**, i8*** %15
%98 = load i32, i32* %13
%99 = sext i32 %98 to i64
%100 = getelementptr inbounds i8*, i8** %97, i64 %99
store i8* %96, i8** %100
%101 = load i32, i32* %13
%102 = add nsw i32 %101, 1
store i32 %102, i32* %13
br label %103
103: ; preds = %67, %64, %60
%104 = load i8*, i8** %11
%105 = load i8*, i8** %6
%106 = call i32 @strlen(i8* %105)
%107 = sext i32 %106 to i64
%108 = getelementptr inbounds i8, i8* %104, i64 %107
store i8* %108, i8** %10
br label %41
109: ; preds = %41
%110 = load i32, i32* %13
%111 = load i32*, i32** %7
store i32 %110, i32* %111
%112 = load i8**, i8*** %15
ret i8** %112
}

LLVM IR (-O2):
define i8** @ShareSplitStr(i8* %0, i8* %1, i32* nocapture writeonly %2, i32 %3) {
%5 = alloca [4096 x i8]
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%7 = tail call i32 @strlen(i8* %0)
%8 = icmp sgt i32 %7, 4095
br i1 %8, label %11, label %9
9: ; preds = %4
%10 = tail call i32 @strlen(i8* %0)
br label %11
11: ; preds = %4, %9
%12 = phi i32 [ %10, %9 ], [ 4095, %4 ]
%13 = call i32 @strncpy(i8* %6, i8* %0, i32 %12)
%14 = sext i32 %12 to i64
%15 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 %14
store i8 0, i8* %15
%16 = call i32 @strlen(i8* %6)
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 %17
%19 = call i8** @ShareMallocM2c(i32 1, i32 0)
%20 = icmp slt i32 %16, 0
br i1 %20, label %56, label %21
21: ; preds = %11
%22 = icmp eq i32 %3, 0
%23 = icmp eq i32 %3, 1
br label %24
24: ; preds = %21, %49
%25 = phi i8* [ %6, %21 ], [ %54, %49 ]
%26 = phi i8** [ %19, %21 ], [ %51, %49 ]
%27 = phi i32 [ 0, %21 ], [ %50, %49 ]
%28 = call i8* @strstr(i8* dereferenceable(1) %25, i8* dereferenceable(1) %1)
%29 = icmp eq i8* %28, null
%30 = select i1 %29, i8* %18, i8* %28
%31 = icmp uge i8* %30, %25
%32 = and i1 %22, %31
%33 = icmp ugt i8* %30, %25
%34 = and i1 %23, %33
%35 = or i1 %32, %34
br i1 %35, label %36, label %49
36: ; preds = %24
%37 = add nsw i32 %27, 1
%38 = call i8** @ShareReallocM2c(i8** %26, i32 %37, i32 0)
%39 = ptrtoint i8* %30 to i64
%40 = ptrtoint i8* %25 to i64
%41 = sub i64 %39, %40
%42 = trunc i64 %41 to i32
%43 = add i32 %42, 1
%44 = call i8* @ShareMallocM1c(i32 %43)
%45 = call i32 @strncpy(i8* %44, i8* %25, i32 %42)
%46 = getelementptr inbounds i8, i8* %44, i64 %41
store i8 0, i8* %46
%47 = sext i32 %27 to i64
%48 = getelementptr inbounds i8*, i8** %38, i64 %47
store i8* %44, i8** %48
br label %49
49: ; preds = %24, %36
%50 = phi i32 [ %37, %36 ], [ %27, %24 ]
%51 = phi i8** [ %38, %36 ], [ %26, %24 ]
%52 = call i32 @strlen(i8* %1)
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %30, i64 %53
%55 = icmp ugt i8* %54, %18
br i1 %55, label %56, label %24
56: ; preds = %49, %11
%57 = phi i32 [ 0, %11 ], [ %50, %49 ]
%58 = phi i8** [ %19, %11 ], [ %51, %49 ]
store i32 %57, i32* %2
ret i8** %58
}