#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_findstart(char *buffer, char *tag) {
  char starttag[(512)], *startptr, *p = NULL;
  int length = strlen(tag);
  if (length < (512) - 4) {
    sprintf(starttag, "<%s>", tag);
    p = strsearch(buffer, starttag);
    if (!p) {
      sprintf(starttag, "<%s ", tag);
      p = strsearch(buffer, starttag);
    }
  } else {
    startptr = (char *)malloc(sizeof(char) * (length + 4));
    if (startptr) {
      sprintf(startptr, "<%s", tag);
      p = strsearch(buffer, startptr);
      free(startptr);
    }
  }
  return p;
}


x86 assembly (clang, -O0):
<xml_findstart>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x220(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x224(%rbp)
cmpl   $0x1fc,-0x224(%rbp)
jge    11fc <xml_findstart+0xac>
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,-0x220(%rbp)
cmpq   $0x0,-0x220(%rbp)
jne    11f7 <xml_findstart+0xa7>
lea    -0x210(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe2a(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x210(%rbp),%rsi
call   1050 <strsearch@plt>
mov    %rax,-0x220(%rbp)
jmp    1266 <xml_findstart+0x116>
mov    -0x224(%rbp),%eax
add    $0x4,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x218(%rbp)
cmpq   $0x0,-0x218(%rbp)
je     1261 <xml_findstart+0x111>
mov    -0x218(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xdd1(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x218(%rbp),%rsi
call   1050 <strsearch@plt>
mov    %rax,-0x220(%rbp)
mov    -0x218(%rbp),%rdi
call   1030 <free@plt>
jmp    1266 <xml_findstart+0x116>
mov    -0x220(%rbp),%rax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xml_findstart>:
push   %r15
push   %r14
push   %rbx
sub    $0x200,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
call   1040 <strlen@plt>
cmp    $0x1fb,%eax
jg     11b8 <xml_findstart+0x68>
lea    0xe88(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r15,%rdx
call   1070 <sprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <strsearch@plt>
test   %rax,%rax
jne    11f9 <xml_findstart+0xa9>
lea    0xe68(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r15,%rdx
call   1070 <sprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <strsearch@plt>
jmp    11f9 <xml_findstart+0xa9>
add    $0x4,%eax
mov    %eax,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11f7 <xml_findstart+0xa7>
mov    %rax,%rbx
lea    0xe39(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
call   1070 <sprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <strsearch@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rax
jmp    11f9 <xml_findstart+0xa9>
xor    %eax,%eax
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [5 x i8] c"<%s>\00"
@.str.1 = [5 x i8] c"<%s \00"
@.str.2 = [4 x i8] c"<%s\00"
define i8* @xml_findstart(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [512 x i8]
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i8* null, i8** %7
%9 = load i8*, i8** %4
%10 = call i32 @strlen(i8* %9)
store i32 %10, i32* %8
%11 = load i32, i32* %8
%12 = icmp slt i32 %11, 508
br i1 %12, label %13, label %30
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 ([5 x i8], [5 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** %7
%20 = load i8*, i8** %7
%21 = icmp ne i8* %20, null
br i1 %21, label %29, label %22
22: ; preds = %13
%23 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%24 = load i8*, i8** %4
%25 = call i32 @sprintf(i8* %23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = load i8*, i8** %3
%27 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%28 = call i8* @strsearch(i8* %26, i8* %27)
store i8* %28, i8** %7
br label %29
29: ; preds = %22, %13
br label %50
30: ; preds = %2
%31 = load i32, i32* %8
%32 = add nsw i32 %31, 4
%33 = sext i32 %32 to i64
%34 = mul i64 1, %33
%35 = trunc i64 %34 to i32
%36 = call i64 @malloc(i32 %35)
%37 = inttoptr i64 %36 to i8*
store i8* %37, i8** %6
%38 = load i8*, i8** %6
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %49
40: ; preds = %30
%41 = load i8*, i8** %6
%42 = load i8*, i8** %4
%43 = call i32 @sprintf(i8* %41, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %42)
%44 = load i8*, i8** %3
%45 = load i8*, i8** %6
%46 = call i8* @strsearch(i8* %44, i8* %45)
store i8* %46, i8** %7
%47 = load i8*, i8** %6
%48 = call i32 @free(i8* %47)
br label %49
49: ; preds = %40, %30
br label %50
50: ; preds = %49, %29
%51 = load i8*, i8** %7
ret i8* %51
}

LLVM IR (-O2):
@.str = [5 x i8] c"<%s>\00"
@.str.1 = [5 x i8] c"<%s \00"
@.str.2 = [4 x i8] c"<%s\00"
define i8* @xml_findstart(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 %14
7: ; preds = %2
%8 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %1)
%9 = call i8* @strsearch(i8* %0, i8* %4)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %23
11: ; preds = %7
%12 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%13 = call i8* @strsearch(i8* %0, i8* %4)
br label %23
14: ; preds = %2
%15 = add nuw nsw i32 %5, 4
%16 = tail call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i8*
%18 = icmp eq i64 %16, 0
br i1 %18, label %23, label %19
19: ; preds = %14
%20 = tail call i32 @sprintf(i8* %17, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%21 = tail call i8* @strsearch(i8* %0, i8* %17)
%22 = tail call i32 @free(i8* %17)
br label %23
23: ; preds = %14, %19, %7, %11
%24 = phi i8* [ %9, %7 ], [ %13, %11 ], [ %21, %19 ], [ null, %14 ]
ret i8* %24
}