#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*/ diagnostics(int, char *, char *);
char *getStringBraceParam(char **);
int strlen(char *);
char *strstr(char *, char *);
char *ExtractAndRemoveTag(char *tag, char *text) {
  char *s, *contents, *start = NULL;
  if (text == NULL || *text == '\0')
    return NULL;
  s = text;
  diagnostics(5, "target tag = <%s>", tag);
  diagnostics(5, "original text = <%s>", text);
  while (s) {
    start = strstr(s, tag);
    if (!start)
      return NULL;
    s = start + strlen(tag);
    if (*s == ' ' || *s == '{')
      break;
  }
  contents = getStringBraceParam(&s);
  if (contents == NULL)
    return NULL;
  do
    *start++ = *s++;
  while (*s);
  *start = '\0';
  diagnostics(5, "final contents = <%s>", contents);
  diagnostics(5, "final text = <%s>", text);
  return contents;
}


x86 assembly (clang, -O0):
<ExtractAndRemoveTag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1173 <ExtractAndRemoveTag+0x33>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1180 <ExtractAndRemoveTag+0x40>
movq   $0x0,-0x8(%rbp)
jmp    12c3 <ExtractAndRemoveTag+0x183>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdx
mov    $0x5,%edi
lea    0xe68(%rip),%rsi
call   1040 <diagnostics@plt>
mov    -0x18(%rbp),%rdx
mov    $0x5,%edi
lea    0xe65(%rip),%rsi
call   1040 <diagnostics@plt>
cmpq   $0x0,-0x20(%rbp)
je     1231 <ExtractAndRemoveTag+0xf1>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11e6 <ExtractAndRemoveTag+0xa6>
movq   $0x0,-0x8(%rbp)
jmp    12c3 <ExtractAndRemoveTag+0x183>
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
je     1227 <ExtractAndRemoveTag+0xe7>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7b,%eax
jne    122c <ExtractAndRemoveTag+0xec>
jmp    1231 <ExtractAndRemoveTag+0xf1>
jmp    11b2 <ExtractAndRemoveTag+0x72>
lea    -0x20(%rbp),%rdi
call   1050 <getStringBraceParam@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1256 <ExtractAndRemoveTag+0x116>
movq   $0x0,-0x8(%rbp)
jmp    12c3 <ExtractAndRemoveTag+0x183>
jmp    125b <ExtractAndRemoveTag+0x11b>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
mov    (%rax),%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
jne    125b <ExtractAndRemoveTag+0x11b>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rdx
mov    $0x5,%edi
lea    0xd86(%rip),%rsi
call   1040 <diagnostics@plt>
mov    -0x18(%rbp),%rdx
mov    $0x5,%edi
lea    0xd87(%rip),%rsi
call   1040 <diagnostics@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ExtractAndRemoveTag>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rsi,%rsi
je     1227 <ExtractAndRemoveTag+0xe7>
mov    %rsi,%r15
cmpb   $0x0,(%rsi)
je     1227 <ExtractAndRemoveTag+0xe7>
mov    %rdi,%r12
lea    0xe99(%rip),%rsi
mov    $0x5,%edi
mov    %r12,%rdx
call   1040 <diagnostics@plt>
lea    0xe97(%rip),%rsi
mov    $0x5,%edi
mov    %r15,%rdx
call   1040 <diagnostics@plt>
xor    %r14d,%r14d
mov    %r15,%rdi
xchg   %ax,%ax
mov    %r12,%rsi
call   1060 <strstr@plt>
test   %rax,%rax
je     122a <ExtractAndRemoveTag+0xea>
mov    %rax,%rbx
mov    %r12,%rdi
call   1030 <strlen@plt>
cltq
mov    %rbx,%rdi
add    %rax,%rdi
mov    %rdi,(%rsp)
movzbl (%rbx,%rax,1),%eax
cmp    $0x7b,%al
je     11c4 <ExtractAndRemoveTag+0x84>
cmp    $0x20,%al
jne    1190 <ExtractAndRemoveTag+0x50>
mov    %rsp,%rdi
call   1050 <getStringBraceParam@plt>
test   %rax,%rax
je     1227 <ExtractAndRemoveTag+0xe7>
mov    %rax,%r14
mov    (%rsp),%rax
nopl   0x0(%rax,%rax,1)
lea    0x1(%rax),%rcx
mov    %rcx,(%rsp)
movzbl (%rax),%eax
mov    %al,(%rbx)
add    $0x1,%rbx
mov    (%rsp),%rax
cmpb   $0x0,(%rax)
jne    11e0 <ExtractAndRemoveTag+0xa0>
movb   $0x0,(%rbx)
lea    0xe23(%rip),%rsi
mov    $0x5,%edi
mov    %r14,%rdx
call   1040 <diagnostics@plt>
lea    0xe25(%rip),%rsi
mov    $0x5,%edi
mov    %r15,%rdx
call   1040 <diagnostics@plt>
jmp    122a <ExtractAndRemoveTag+0xea>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [18 x i8] c"target tag = <%s>\00"
@.str.1 = [21 x i8] c"original text = <%s>\00"
@.str.2 = [22 x i8] c"final contents = <%s>\00"
@.str.3 = [18 x i8] c"final text = <%s>\00"
define i8* @ExtractAndRemoveTag(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* null, i8** %8
%9 = load i8*, i8** %5
%10 = icmp eq i8* %9, null
br i1 %10, label %16, label %11
11: ; preds = %2
%12 = load i8*, i8** %5
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %11, %2
store i8* null, i8** %3
br label %73
17: ; preds = %11
%18 = load i8*, i8** %5
store i8* %18, i8** %6
%19 = load i8*, i8** %4
%20 = call i32 @diagnostics(i32 5, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = load i8*, i8** %5
%22 = call i32 @diagnostics(i32 5, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %21)
br label %23
23: ; preds = %49, %17
%24 = load i8*, i8** %6
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %50
26: ; preds = %23
%27 = load i8*, i8** %6
%28 = load i8*, i8** %4
%29 = call i8* @strstr(i8* %27, i8* %28)
store i8* %29, i8** %8
%30 = load i8*, i8** %8
%31 = icmp ne i8* %30, null
br i1 %31, label %33, label %32
32: ; preds = %26
store i8* null, i8** %3
br label %73
33: ; preds = %26
%34 = load i8*, i8** %8
%35 = load i8*, i8** %4
%36 = call i32 @strlen(i8* %35)
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %34, i64 %37
store i8* %38, i8** %6
%39 = load i8*, i8** %6
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 32
br i1 %42, label %48, label %43
43: ; preds = %33
%44 = load i8*, i8** %6
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp eq i32 %46, 123
br i1 %47, label %48, label %49
48: ; preds = %43, %33
br label %50
49: ; preds = %43
br label %23
50: ; preds = %48, %23
%51 = call i8* @getStringBraceParam(i8** %6)
store i8* %51, i8** %7
%52 = load i8*, i8** %7
%53 = icmp eq i8* %52, null
br i1 %53, label %54, label %55
54: ; preds = %50
store i8* null, i8** %3
br label %73
55: ; preds = %50
br label %56
56: ; preds = %62, %55
%57 = load i8*, i8** %6
%58 = getelementptr inbounds i8, i8* %57, i32 1
store i8* %58, i8** %6
%59 = load i8, i8* %57
%60 = load i8*, i8** %8
%61 = getelementptr inbounds i8, i8* %60, i32 1
store i8* %61, i8** %8
store i8 %59, i8* %60
br label %62
62: ; preds = %56
%63 = load i8*, i8** %6
%64 = load i8, i8* %63
%65 = icmp ne i8 %64, 0
br i1 %65, label %56, label %66
66: ; preds = %62
%67 = load i8*, i8** %8
store i8 0, i8* %67
%68 = load i8*, i8** %7
%69 = call i32 @diagnostics(i32 5, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %68)
%70 = load i8*, i8** %5
%71 = call i32 @diagnostics(i32 5, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %70)
%72 = load i8*, i8** %7
store i8* %72, i8** %3
br label %73
73: ; preds = %66, %54, %32, %16
%74 = load i8*, i8** %3
ret i8* %74
}

LLVM IR (-O2):
@.str = [18 x i8] c"target tag = <%s>\00"
@.str.1 = [21 x i8] c"original text = <%s>\00"
@.str.2 = [22 x i8] c"final contents = <%s>\00"
@.str.3 = [18 x i8] c"final text = <%s>\00"
define i8* @ExtractAndRemoveTag(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = icmp eq i8* %1, null
br i1 %5, label %38, label %6
6: ; preds = %2
%7 = load i8, i8* %1
%8 = icmp eq i8 %7, 0
br i1 %8, label %38, label %9
9: ; preds = %6
%10 = tail call i32 @diagnostics(i32 5, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %0)
%11 = tail call i32 @diagnostics(i32 5, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %12
12: ; preds = %9, %16
%13 = phi i8* [ %1, %9 ], [ %19, %16 ]
%14 = tail call i8* @strstr(i8* dereferenceable(1) %13, i8* dereferenceable(1) %0)
%15 = icmp eq i8* %14, null
br i1 %15, label %38, label %16
16: ; preds = %12
%17 = tail call i32 @strlen(i8* %0)
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %14, i64 %18
store i8* %19, i8** %3
%20 = load i8, i8* %19
switch i8 %20, label %12 [
i8 32, label %21
i8 123, label %21
]
21: ; preds = %16, %16
%22 = call i8* @getStringBraceParam(i8** %3)
%23 = icmp eq i8* %22, null
br i1 %23, label %38, label %24
24: ; preds = %21
%25 = load i8*, i8** %3
br label %26
26: ; preds = %24, %26
%27 = phi i8* [ %32, %26 ], [ %25, %24 ]
%28 = phi i8* [ %31, %26 ], [ %14, %24 ]
%29 = getelementptr inbounds i8, i8* %27, i64 1
store i8* %29, i8** %3
%30 = load i8, i8* %27
%31 = getelementptr inbounds i8, i8* %28, i64 1
store i8 %30, i8* %28
%32 = load i8*, i8** %3
%33 = load i8, i8* %32
%34 = icmp eq i8 %33, 0
br i1 %34, label %35, label %26
35: ; preds = %26
store i8 0, i8* %31
%36 = call i32 @diagnostics(i32 5, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %22)
%37 = call i32 @diagnostics(i32 5, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %1)
br label %38
38: ; preds = %12, %21, %2, %6, %35
%39 = phi i8* [ %22, %35 ], [ null, %6 ], [ null, %2 ], [ null, %21 ], [ null, %12 ]
ret i8* %39
}