#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*/ free(char *);
scalar_t__ strcmp(char const *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char *);
int /*<<< orphan*/ strlen(char *);
char *strtok_r(char *, char const *, char **);
char *strdel(char *str, const char *token, const char *delim) {
  char *last = str;
  char *original = strdup(str);
  char *p;
  char *saveptr;
  char *t;
  for (p = original; (t = strtok_r(p, delim, &saveptr)); p = NULL) {
    if (t == NULL)
      break;
    if (strcmp(token, t) == 0)
      continue;
    if (p == NULL)
      *last++ = ' ';
    strcpy(last, t);
    last += strlen(t);
  }
  free(original);
  *last = '\0';
  return str;
}


x86 assembly (clang, -O0):
<strdel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x38(%rbp),%rdx
call   1060 <strtok_r@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
je     122c <strdel+0xcc>
cmpq   $0x0,-0x40(%rbp)
jne    11c0 <strdel+0x60>
jmp    122c <strdel+0xcc>
mov    -0x10(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    11dc <strdel+0x7c>
jmp    121f <strdel+0xbf>
cmpq   $0x0,-0x30(%rbp)
jne    11f9 <strdel+0x99>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x20,(%rax)
mov    -0x20(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x40(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
jmp    1191 <strdel+0x31>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strdel>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbx
mov    %rsi,%r12
mov    %rdi,%r15
call   1080 <strdup@plt>
lea    0x10(%rsp),%rdx
mov    %rax,0x8(%rsp)
mov    %rax,%rdi
mov    %rbx,%rsi
call   1060 <strtok_r@plt>
mov    %r15,%r14
test   %rax,%rax
je     1248 <strdel+0xe8>
mov    %rax,%rbp
mov    %r12,%rdi
mov    %rax,%rsi
call   1070 <strcmp@plt>
mov    %r15,%r14
test   %rax,%rax
je     11df <strdel+0x7f>
mov    %r15,%r13
cmpq   $0x0,0x8(%rsp)
jne    11c6 <strdel+0x66>
lea    0x1(%r15),%r13
movb   $0x20,(%r15)
mov    %r13,%rdi
mov    %rbp,%rsi
call   1040 <strcpy@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
movslq %eax,%r14
add    %r13,%r14
lea    0x10(%rsp),%rdx
xor    %edi,%edi
mov    %rbx,%rsi
call   1060 <strtok_r@plt>
test   %rax,%rax
je     1248 <strdel+0xe8>
mov    %rax,%rbp
lea    0x10(%rsp),%r13
jmp    1215 <strdel+0xb5>
nopl   (%rax)
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r13,%rdx
call   1060 <strtok_r@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1248 <strdel+0xe8>
mov    %r12,%rdi
mov    %rbp,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1200 <strdel+0xa0>
lea    0x1(%r14),%rdi
movb   $0x20,(%r14)
mov    %rbp,%rsi
call   1040 <strcpy@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
cltq
add    %rax,%r14
add    $0x1,%r14
jmp    1200 <strdel+0xa0>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
movb   $0x0,(%r14)
mov    %r15,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @strdel(i8* returned %0, i8* %1, i8* nocapture %2) {
%4 = alloca i8*
%5 = tail call i8* @strdup(i8* %0)
%6 = bitcast i8** %4 to i8*
%7 = call i8* @strtok_r(i8* %5, i8* %2, i8** %4)
%8 = icmp eq i8* %7, null
br i1 %8, label %41, label %9
9: ; preds = %3
%10 = call i64 @strcmp(i8* %1, i8* %7)
%11 = icmp eq i64 %10, 0
br i1 %11, label %22, label %12
12: ; preds = %9
%13 = icmp eq i8* %5, null
br i1 %13, label %14, label %16
14: ; preds = %12
%15 = getelementptr inbounds i8, i8* %0, i64 1
store i8 32, i8* %0
br label %16
16: ; preds = %14, %12
%17 = phi i8* [ %15, %14 ], [ %0, %12 ]
%18 = call i32 @strcpy(i8* %17, i8* %7)
%19 = call i32 @strlen(i8* %7)
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %17, i64 %20
br label %22
22: ; preds = %16, %9
%23 = phi i8* [ %0, %9 ], [ %21, %16 ]
%24 = call i8* @strtok_r(i8* null, i8* %2, i8** %4)
%25 = icmp eq i8* %24, null
br i1 %25, label %41, label %26
26: ; preds = %22, %37
%27 = phi i8* [ %39, %37 ], [ %24, %22 ]
%28 = phi i8* [ %38, %37 ], [ %23, %22 ]
%29 = call i64 @strcmp(i8* %1, i8* %27)
%30 = icmp eq i64 %29, 0
br i1 %30, label %37, label %31
31: ; preds = %26
%32 = getelementptr inbounds i8, i8* %28, i64 1
store i8 32, i8* %28
%33 = call i32 @strcpy(i8* %32, i8* %27)
%34 = call i32 @strlen(i8* %27)
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %32, i64 %35
br label %37
37: ; preds = %26, %31
%38 = phi i8* [ %28, %26 ], [ %36, %31 ]
%39 = call i8* @strtok_r(i8* null, i8* %2, i8** %4)
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %26
41: ; preds = %37, %22, %3
%42 = phi i8* [ %0, %3 ], [ %23, %22 ], [ %38, %37 ]
%43 = call i32 @free(i8* %5)
store i8 0, i8* %42
ret i8* %0
}