#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*/ ReportError(int, char *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *);
scalar_t__ malloc(int);
scalar_t__ realloc(char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *strtok(char *, char *);
char *SafeStrtok(char *string, char *delims) {
  static char *szScratch = NULL;
  static int nScratchLen = 0;
  if (string != NULL) {
    if (szScratch == NULL) {
      szScratch = (char *)malloc(strlen(string) + 1);
      if (szScratch == NULL) {
        fprintf(stderr, "Malloc Failed at %d in %s\n", 362,
                "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                "outputs/2022-01-22/02-19-57/repos/pingcap/tidb-bench/refs/"
                "heads/master/00_compile_tpcds/tools/grammar.c");
        exit(1);
      };
      if (szScratch == NULL)
        ReportError(-3, "SafeStrtok", 1);
      else
        nScratchLen = strlen(string);
    } else {
      if (nScratchLen < (int)strlen(string)) {
        szScratch = (char *)realloc(szScratch, strlen(string) + 1);
        if (szScratch == NULL)
          ReportError(-3, "SafeStrtok", 1);
        else
          nScratchLen = strlen(string);
      }
    }
    strcpy(szScratch, string);
    return (strtok(szScratch, delims));
  }
  return (strtok(NULL, delims));
}


x86 assembly (clang, -O0):
<SafeStrtok>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     12ec <SafeStrtok+0x16c>
cmpq   $0x0,0x2ec5(%rip)
jne    1239 <SafeStrtok+0xb9>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,0x2ea5(%rip)
cmpq   $0x0,0x2e9d(%rip)
jne    11fc <SafeStrtok+0x7c>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
mov    $0x16a,%edx
lea    0xe2e(%rip),%rcx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpq   $0x0,0x2e64(%rip)
jne    1225 <SafeStrtok+0xa5>
mov    $0xfffffffd,%edi
lea    0xea5(%rip),%rsi
mov    $0x1,%edx
call   1050 <ReportError@plt>
jmp    1234 <SafeStrtok+0xb4>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,0x2e3c(%rip)
jmp    12c3 <SafeStrtok+0x143>
mov    0x2e31(%rip),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
cmp    %ecx,%eax
jge    12be <SafeStrtok+0x13e>
mov    0x2e09(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%esi
add    $0x1,%esi
call   1080 <realloc@plt>
mov    %rax,0x2de7(%rip)
cmpq   $0x0,0x2ddf(%rip)
jne    12aa <SafeStrtok+0x12a>
mov    $0xfffffffd,%edi
lea    0xe20(%rip),%rsi
mov    $0x1,%edx
call   1050 <ReportError@plt>
jmp    12b9 <SafeStrtok+0x139>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,0x2db7(%rip)
jmp    12be <SafeStrtok+0x13e>
jmp    12c3 <SafeStrtok+0x143>
mov    0x2d9e(%rip),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    0x2d8e(%rip),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x8(%rbp)
jmp    12fd <SafeStrtok+0x17d>
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1090 <strtok@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SafeStrtok>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
test   %rdi,%rdi
je     11ec <SafeStrtok+0x6c>
mov    %rdi,%rbx
cmpq   $0x0,0x2ece(%rip)
je     11f0 <SafeStrtok+0x70>
mov    0x2ece(%rip),%ebp
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmp    %eax,%ebp
jge    121a <SafeStrtok+0x9a>
mov    0x2eb3(%rip),%r15
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%esi
mov    %r15,%rdi
call   1080 <realloc@plt>
mov    %rax,0x2e99(%rip)
test   %rax,%rax
jne    120c <SafeStrtok+0x8c>
lea    0xee0(%rip),%rsi
mov    $0xfffffffd,%edi
mov    $0x1,%edx
call   1050 <ReportError@plt>
jmp    121a <SafeStrtok+0x9a>
xor    %edi,%edi
jmp    1230 <SafeStrtok+0xb0>
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%edi
call   1070 <malloc@plt>
mov    %rax,0x2e61(%rip)
test   %rax,%rax
je     1242 <SafeStrtok+0xc2>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,0x2e56(%rip)
mov    0x2e47(%rip),%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    0x2e38(%rip),%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <strtok@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
lea    0xdae(%rip),%rsi
lea    0xdc2(%rip),%rcx
mov    $0x16a,%edx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@SafeStrtok.szScratch = internal global i8* null
@SafeStrtok.nScratchLen = internal global i32 0
@stderr = global i32 0
@.str = [27 x i8] c"Malloc Failed at %d in %s\0A\00"
@.str.1 = [160 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/pingcap/tidb-bench/refs/heads/master/00_compile_tpcds/tools/grammar.c\00"
@.str.2 = [11 x i8] c"SafeStrtok\00"
define i8* @SafeStrtok(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%6 = load i8*, i8** %4
%7 = icmp ne i8* %6, null
br i1 %7, label %8, label %60
8: ; preds = %2
%9 = load i8*, i8** @SafeStrtok.szScratch
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %32
11: ; preds = %8
%12 = load i8*, i8** %4
%13 = call i32 @strlen(i8* %12)
%14 = add nsw i32 %13, 1
%15 = call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** @SafeStrtok.szScratch
%17 = load i8*, i8** @SafeStrtok.szScratch
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %23
19: ; preds = %11
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 362, i8* getelementptr inbounds ([160 x i8], [160 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %11
%24 = load i8*, i8** @SafeStrtok.szScratch
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 @ReportError(i32 -3, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 1)
br label %31
28: ; preds = %23
%29 = load i8*, i8** %4
%30 = call i32 @strlen(i8* %29)
store i32 %30, i32* @SafeStrtok.nScratchLen
br label %31
31: ; preds = %28, %26
br label %53
32: ; preds = %8
%33 = load i32, i32* @SafeStrtok.nScratchLen
%34 = load i8*, i8** %4
%35 = call i32 @strlen(i8* %34)
%36 = icmp slt i32 %33, %35
br i1 %36, label %37, label %52
37: ; preds = %32
%38 = load i8*, i8** @SafeStrtok.szScratch
%39 = load i8*, i8** %4
%40 = call i32 @strlen(i8* %39)
%41 = add nsw i32 %40, 1
%42 = call i64 @realloc(i8* %38, i32 %41)
%43 = inttoptr i64 %42 to i8*
store i8* %43, i8** @SafeStrtok.szScratch
%44 = load i8*, i8** @SafeStrtok.szScratch
%45 = icmp eq i8* %44, null
br i1 %45, label %46, label %48
46: ; preds = %37
%47 = call i32 @ReportError(i32 -3, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 1)
br label %51
48: ; preds = %37
%49 = load i8*, i8** %4
%50 = call i32 @strlen(i8* %49)
store i32 %50, i32* @SafeStrtok.nScratchLen
br label %51
51: ; preds = %48, %46
br label %52
52: ; preds = %51, %32
br label %53
53: ; preds = %52, %31
%54 = load i8*, i8** @SafeStrtok.szScratch
%55 = load i8*, i8** %4
%56 = call i32 @strcpy(i8* %54, i8* %55)
%57 = load i8*, i8** @SafeStrtok.szScratch
%58 = load i8*, i8** %5
%59 = call i8* @strtok(i8* %57, i8* %58)
store i8* %59, i8** %3
br label %63
60: ; preds = %2
%61 = load i8*, i8** %5
%62 = call i8* @strtok(i8* null, i8* %61)
store i8* %62, i8** %3
br label %63
63: ; preds = %60, %53
%64 = load i8*, i8** %3
ret i8* %64
}

LLVM IR (-O2):
@SafeStrtok.szScratch = internal unnamed_addr global i8* null
@SafeStrtok.nScratchLen = internal unnamed_addr global i32 0
@stderr = global i32 0
@.str = [27 x i8] c"Malloc Failed at %d in %s\0A\00"
@.str.1 = [160 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/pingcap/tidb-bench/refs/heads/master/00_compile_tpcds/tools/grammar.c\00"
@.str.2 = [11 x i8] c"SafeStrtok\00"
define i8* @SafeStrtok(i8* %0, i8* nocapture %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %38, label %4
4: ; preds = %2
%5 = load i8*, i8** @SafeStrtok.szScratch
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %19
7: ; preds = %4
%8 = tail call i32 @strlen(i8* %0)
%9 = add nsw i32 %8, 1
%10 = tail call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** @SafeStrtok.szScratch
%12 = icmp eq i64 %10, 0
br i1 %12, label %13, label %17
13: ; preds = %7
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 362, i8* getelementptr inbounds ([160 x i8], [160 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %7
%18 = tail call i32 @strlen(i8* %0)
store i32 %18, i32* @SafeStrtok.nScratchLen
br label %34
19: ; preds = %4
%20 = load i32, i32* @SafeStrtok.nScratchLen
%21 = tail call i32 @strlen(i8* %0)
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %34
23: ; preds = %19
%24 = load i8*, i8** @SafeStrtok.szScratch
%25 = tail call i32 @strlen(i8* %0)
%26 = add nsw i32 %25, 1
%27 = tail call i64 @realloc(i8* %24, i32 %26)
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** @SafeStrtok.szScratch
%29 = icmp eq i64 %27, 0
br i1 %29, label %30, label %32
30: ; preds = %23
%31 = tail call i32 @ReportError(i32 -3, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 1)
br label %34
32: ; preds = %23
%33 = tail call i32 @strlen(i8* %0)
store i32 %33, i32* @SafeStrtok.nScratchLen
br label %34
34: ; preds = %19, %32, %30, %17
%35 = load i8*, i8** @SafeStrtok.szScratch
%36 = tail call i32 @strcpy(i8* %35, i8* %0)
%37 = load i8*, i8** @SafeStrtok.szScratch
br label %38
38: ; preds = %2, %34
%39 = phi i8* [ %37, %34 ], [ null, %2 ]
%40 = tail call i8* @strtok(i8* %39, i8* %1)
ret i8* %40
}