#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__ malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int strlen(char *);
char *strsearch(char *, char *);
char *xml_findend(char *buffer, char *tag) {
  char endtag[(512)], *p = NULL;
  int length = strlen(tag);
  if (length < (512) - 4) {
    sprintf(endtag, "</%s>", tag);
    p = strsearch(buffer, endtag);
  } else {
    char *endptr = (char *)malloc(sizeof(char) * (length + 5));
    if (endptr) {
      sprintf(endptr, "</%s>", tag);
      p = strsearch(buffer, endptr);
      free(endptr);
    }
  }
  if (p && *p) {
    if (*p)
      p++;
    while (*p && *(p - 1) != '>')
      p++;
  }
  return p;
}


x86 assembly (clang, -O0):
<xml_findend>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x218(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x21c(%rbp)
cmpl   $0x1fc,-0x21c(%rbp)
jge    11c0 <xml_findend+0x70>
lea    -0x210(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe61(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x210(%rbp),%rsi
call   1050 <strsearch@plt>
mov    %rax,-0x218(%rbp)
jmp    122a <xml_findend+0xda>
mov    -0x21c(%rbp),%eax
add    $0x5,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x228(%rbp)
cmpq   $0x0,-0x228(%rbp)
je     1225 <xml_findend+0xd5>
mov    -0x228(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe03(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x228(%rbp),%rsi
call   1050 <strsearch@plt>
mov    %rax,-0x218(%rbp)
mov    -0x228(%rbp),%rdi
call   1030 <free@plt>
jmp    122a <xml_findend+0xda>
cmpq   $0x0,-0x218(%rbp)
je     12d3 <xml_findend+0x183>
mov    -0x218(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12d3 <xml_findend+0x183>
mov    -0x218(%rbp),%rax
cmpb   $0x0,(%rax)
je     126d <xml_findend+0x11d>
mov    -0x218(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x218(%rbp)
jmp    1272 <xml_findend+0x122>
mov    -0x218(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x229(%rbp)
je     12a4 <xml_findend+0x154>
mov    -0x218(%rbp),%rax
movsbl -0x1(%rax),%eax
cmp    $0x3e,%eax
setne  %al
mov    %al,-0x229(%rbp)
mov    -0x229(%rbp),%al
test   $0x1,%al
jne    12b7 <xml_findend+0x167>
jmp    12ce <xml_findend+0x17e>
mov    -0x218(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x218(%rbp)
jmp    1272 <xml_findend+0x122>
jmp    12d3 <xml_findend+0x183>
mov    -0x218(%rbp),%rax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xml_findend>:
push   %r15
push   %r14
push   %rbx
sub    $0x200,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1040 <strlen@plt>
cmp    $0x1fb,%eax
jg     119b <xml_findend+0x4b>
lea    0xe88(%rip),%rsi
mov    %rsp,%r15
mov    %r15,%rdi
mov    %rbx,%rdx
call   1070 <sprintf@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1050 <strsearch@plt>
mov    %rax,%rbx
test   %rbx,%rbx
jne    11da <xml_findend+0x8a>
jmp    1204 <xml_findend+0xb4>
add    $0x5,%eax
mov    %eax,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1204 <xml_findend+0xb4>
mov    %rax,%r15
lea    0xe4c(%rip),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
call   1070 <sprintf@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1050 <strsearch@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1030 <free@plt>
test   %rbx,%rbx
je     1204 <xml_findend+0xb4>
cmpb   $0x0,(%rbx)
je     1206 <xml_findend+0xb6>
add    $0x1,%rbx
mov    %rbx,%rax
cs nopw 0x0(%rax,%rax,1)
mov    %rax,%rbx
cmpb   $0x0,(%rax)
je     1206 <xml_findend+0xb6>
lea    0x1(%rbx),%rax
cmpb   $0x3e,-0x1(%rbx)
jne    11f0 <xml_findend+0xa0>
jmp    1206 <xml_findend+0xb6>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [6 x i8] c"</%s>\00"
define i8* @xml_findend(i8* %0, i8* %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @strlen(i8* %1)
%6 = icmp slt i32 %5, 508
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %1)
%9 = call i8* @strsearch(i8* %0, i8* %4)
br label %19
10: ; preds = %2
%11 = add nuw nsw i32 %5, 5
%12 = tail call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8*
%14 = icmp eq i64 %12, 0
br i1 %14, label %33, label %15
15: ; preds = %10
%16 = tail call i32 @sprintf(i8* %13, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %1)
%17 = tail call i8* @strsearch(i8* %0, i8* %13)
%18 = tail call i32 @free(i8* %13)
br label %19
19: ; preds = %15, %7
%20 = phi i8* [ %9, %7 ], [ %17, %15 ]
%21 = icmp eq i8* %20, null
br i1 %21, label %33, label %22
22: ; preds = %19
%23 = load i8, i8* %20
%24 = icmp eq i8 %23, 0
br i1 %24, label %33, label %25
25: ; preds = %22, %30
%26 = phi i8* [ %27, %30 ], [ %20, %22 ]
%27 = getelementptr inbounds i8, i8* %26, i64 1
%28 = load i8, i8* %27
%29 = icmp eq i8 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %25
%31 = load i8, i8* %26
%32 = icmp eq i8 %31, 62
br i1 %32, label %33, label %25
33: ; preds = %30, %25, %10, %22, %19
%34 = phi i8* [ %20, %22 ], [ null, %19 ], [ null, %10 ], [ %27, %25 ], [ %27, %30 ]
ret i8* %34
}