#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 *);
char *malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char *);
int strlen(char *);
char *strstr(char *, char *);
int xml_search(char *data, size_t len, char *node, char **result) {
  char startTag[256];
  char endTag[256];
  char *startOffset;
  char *endOffset;
  data = strdup(data);
  sprintf(startTag, "<%s>", node);
  sprintf(endTag, "</%s>", node);
  startOffset = strstr(data, startTag);
  if (startOffset == NULL) {
    free(data);
    return -1;
  }
  endOffset = strstr(data, endTag);
  if (endOffset == NULL) {
    free(data);
    return -1;
  }
  *endOffset = 0;
  *result = malloc(strlen(startOffset + strlen(startTag)) + 1);
  strcpy(*result, startOffset + strlen(startTag));
  free(data);
  return 0;
}


x86 assembly (clang, -O0):
<xml_search>:
push   %rbp
mov    %rsp,%rbp
sub    $0x260,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x10(%rbp)
lea    -0x130(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe56(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x230(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe44(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x130(%rbp),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x238(%rbp)
cmpq   $0x0,-0x238(%rbp)
jne    1200 <xml_search+0x90>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d3 <xml_search+0x163>
mov    -0x10(%rbp),%rdi
lea    -0x230(%rbp),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x240(%rbp)
cmpq   $0x0,-0x240(%rbp)
jne    123a <xml_search+0xca>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d3 <xml_search+0x163>
mov    -0x240(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x238(%rbp),%rax
mov    %rax,-0x258(%rbp)
lea    -0x130(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x258(%rbp),%rdi
cltq
add    %rax,%rdi
call   1050 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x248(%rbp)
mov    -0x238(%rbp),%rax
mov    %rax,-0x250(%rbp)
lea    -0x130(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x250(%rbp),%rsi
mov    -0x248(%rbp),%rdi
cltq
add    %rax,%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x260,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xml_search>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x200,%rsp
mov    %rcx,%r14
mov    %rdx,%rbx
call   1080 <strdup@plt>
mov    %rax,%r15
lea    0xe6b(%rip),%rsi
lea    0x100(%rsp),%r12
mov    %r12,%rdi
mov    %rbx,%rdx
call   1070 <sprintf@plt>
lea    0xe56(%rip),%rsi
mov    %rsp,%rdi
mov    %rbx,%rdx
call   1070 <sprintf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1090 <strstr@plt>
test   %rax,%rax
je     122b <xml_search+0xbb>
mov    %rax,%rbx
mov    %rsp,%rsi
mov    %r15,%rdi
call   1090 <strstr@plt>
test   %rax,%rax
je     122b <xml_search+0xbb>
movb   $0x0,(%rax)
lea    0x100(%rsp),%r12
mov    %r12,%rdi
call   1050 <strlen@plt>
movslq %eax,%rdi
add    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%edi
call   1060 <malloc@plt>
mov    %rax,%r13
mov    %rax,(%r14)
mov    %r12,%rdi
call   1050 <strlen@plt>
movslq %eax,%rsi
add    %rbx,%rsi
mov    %r13,%rdi
call   1040 <strcpy@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    1238 <xml_search+0xc8>
mov    %r15,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%eax
add    $0x200,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [5 x i8] c"<%s>\00"
@.str.1 = [6 x i8] c"</%s>\00"
define i32 @xml_search(i8* %0, i64 %1, i8* %2, i8** %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8**
%10 = alloca [256 x i8]
%11 = alloca [256 x i8]
%12 = alloca i8*
%13 = alloca i8*
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i8** %3, i8*** %9
%14 = load i8*, i8** %6
%15 = call i8* @strdup(i8* %14)
store i8* %15, i8** %6
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%17 = load i8*, i8** %8
%18 = call i32 @sprintf(i8* %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%20 = load i8*, i8** %8
%21 = call i32 @sprintf(i8* %19, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = load i8*, i8** %6
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%24 = call i8* @strstr(i8* %22, i8* %23)
store i8* %24, i8** %12
%25 = load i8*, i8** %12
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %30
27: ; preds = %4
%28 = load i8*, i8** %6
%29 = call i32 @free(i8* %28)
store i32 -1, i32* %5
br label %60
30: ; preds = %4
%31 = load i8*, i8** %6
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%33 = call i8* @strstr(i8* %31, i8* %32)
store i8* %33, i8** %13
%34 = load i8*, i8** %13
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %39
36: ; preds = %30
%37 = load i8*, i8** %6
%38 = call i32 @free(i8* %37)
store i32 -1, i32* %5
br label %60
39: ; preds = %30
%40 = load i8*, i8** %13
store i8 0, i8* %40
%41 = load i8*, i8** %12
%42 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%43 = call i32 @strlen(i8* %42)
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %41, i64 %44
%46 = call i32 @strlen(i8* %45)
%47 = add nsw i32 %46, 1
%48 = call i8* @malloc(i32 %47)
%49 = load i8**, i8*** %9
store i8* %48, i8** %49
%50 = load i8**, i8*** %9
%51 = load i8*, i8** %50
%52 = load i8*, i8** %12
%53 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%54 = call i32 @strlen(i8* %53)
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %52, i64 %55
%57 = call i32 @strcpy(i8* %51, i8* %56)
%58 = load i8*, i8** %6
%59 = call i32 @free(i8* %58)
store i32 0, i32* %5
br label %60
60: ; preds = %39, %36, %27
%61 = load i32, i32* %5
ret i32 %61
}

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