#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*/ memmove(char *, char *, scalar_t__);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char const *);
char *strstr(char *, char const *);
int str_tok(char *dest, char *src, const char *needle) {
  char *pdest, *psrc;
  if (!dest || !src || !*src) {
    return -1;
  }
  psrc = src;
  strcpy(dest, src);
  pdest = strstr(dest, needle);
  if (pdest != NULL) {
    *pdest = 0;
    psrc += (pdest - dest) + strlen(needle);
    memmove(src, psrc, (strlen(psrc) + 1));
  } else {
    *psrc = 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<str_tok>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1177 <str_tok+0x37>
cmpq   $0x0,-0x18(%rbp)
je     1177 <str_tok+0x37>
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
jne    1183 <str_tok+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    1225 <str_tok+0xe5>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1217 <str_tok+0xd7>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
add    %rcx,%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1050 <memmove@plt>
jmp    121e <str_tok+0xde>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_tok>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    $0xffffffff,%eax
test   %rdi,%rdi
je     11b4 <str_tok+0x74>
mov    %rsi,%r15
test   %rsi,%rsi
je     11b4 <str_tok+0x74>
cmpb   $0x0,(%r15)
je     11b4 <str_tok+0x74>
mov    %rdx,%r14
mov    %rdi,%rbx
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <strstr@plt>
test   %rax,%rax
je     11ae <str_tok+0x6e>
mov    %rax,%r12
movb   $0x0,(%rax)
sub    %rbx,%r12
mov    %r14,%rdi
call   1040 <strlen@plt>
add    %rax,%r12
add    %r15,%r12
mov    %r12,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %r15,%rdi
mov    %r12,%rsi
call   1050 <memmove@plt>
jmp    11b2 <str_tok+0x72>
movb   $0x0,(%r15)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @str_tok(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %19
12: ; preds = %3
%13 = load i8*, i8** %6
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = load i8*, i8** %6
%17 = load i8, i8* %16
%18 = icmp ne i8 %17, 0
br i1 %18, label %20, label %19
19: ; preds = %15, %12, %3
store i32 -1, i32* %4
br label %51
20: ; preds = %15
%21 = load i8*, i8** %6
store i8* %21, i8** %9
%22 = load i8*, i8** %5
%23 = load i8*, i8** %6
%24 = call i32 @strcpy(i8* %22, i8* %23)
%25 = load i8*, i8** %5
%26 = load i8*, i8** %7
%27 = call i8* @strstr(i8* %25, i8* %26)
store i8* %27, i8** %8
%28 = load i8*, i8** %8
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %48
30: ; preds = %20
%31 = load i8*, i8** %8
store i8 0, i8* %31
%32 = load i8*, i8** %8
%33 = load i8*, i8** %5
%34 = ptrtoint i8* %32 to i64
%35 = ptrtoint i8* %33 to i64
%36 = sub i64 %34, %35
%37 = load i8*, i8** %7
%38 = call i64 @strlen(i8* %37)
%39 = add nsw i64 %36, %38
%40 = load i8*, i8** %9
%41 = getelementptr inbounds i8, i8* %40, i64 %39
store i8* %41, i8** %9
%42 = load i8*, i8** %6
%43 = load i8*, i8** %9
%44 = load i8*, i8** %9
%45 = call i64 @strlen(i8* %44)
%46 = add nsw i64 %45, 1
%47 = call i32 @memmove(i8* %42, i8* %43, i64 %46)
br label %50
48: ; preds = %20
%49 = load i8*, i8** %9
store i8 0, i8* %49
br label %50
50: ; preds = %48, %30
store i32 0, i32* %4
br label %51
51: ; preds = %50, %19
%52 = load i32, i32* %4
ret i32 %52
}

LLVM IR (-O2):
define i32 @str_tok(i8* %0, i8* %1, i8* nocapture %2) {
%4 = icmp ne i8* %0, null
%5 = icmp ne i8* %1, null
%6 = and i1 %4, %5
br i1 %6, label %7, label %25
7: ; preds = %3
%8 = load i8, i8* %1
%9 = icmp eq i8 %8, 0
br i1 %9, label %25, label %10
10: ; preds = %7
%11 = tail call i32 @strcpy(i8* %0, i8* %1)
%12 = tail call i8* @strstr(i8* %0, i8* dereferenceable(1) %2)
%13 = icmp eq i8* %12, null
br i1 %13, label %24, label %14
14: ; preds = %10
store i8 0, i8* %12
%15 = ptrtoint i8* %12 to i64
%16 = ptrtoint i8* %0 to i64
%17 = sub i64 %15, %16
%18 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%19 = add nsw i64 %17, %18
%20 = getelementptr inbounds i8, i8* %1, i64 %19
%21 = tail call i64 @strlen(i8* %20)
%22 = add nsw i64 %21, 1
%23 = tail call i32 @memmove(i8* %1, i8* %20, i64 %22)
br label %25
24: ; preds = %10
store i8 0, i8* %1
br label %25
25: ; preds = %14, %24, %3, %7
%26 = phi i32 [ -1, %7 ], [ -1, %3 ], [ 0, %24 ], [ 0, %14 ]
ret i32 %26
}