#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*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, scalar_t__);
char *strstr(char *, char *);
char *gettag(char *buffer, char *what) {
  char *aux;
  char *aux2;
  char *result = (char *)malloc(1024);
  char *tmp = (char *)malloc(1024);
  memset(result, 0, 1024);
  memset(tmp, 0, 1024);
  sprintf(tmp, "<%s>", what);
  aux = strstr(buffer, tmp);
  if (aux) {
    aux += strlen(tmp);
    sprintf(tmp, "</%s>", what);
    aux2 = strstr(aux, tmp);
    if (aux2) {
      strncpy(result, aux, strlen(aux) - strlen(aux2));
      free(tmp);
      return result;
    }
  }
  free(tmp);
  free(result);
  return NULL;
}


x86 assembly (clang, -O0):
<gettag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe35(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1282 <gettag+0x112>
mov    -0x38(%rbp),%rdi
call   1050 <strlen@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xdf9(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     127d <gettag+0x10d>
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x50(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x50(%rbp),%rdx
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
sub    %rax,%rdx
call   1040 <strncpy@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    129c <gettag+0x12c>
jmp    1282 <gettag+0x112>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gettag>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%rbx
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,%r12
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
lea    0xe40(%rip),%rsi
mov    %r12,%rdi
mov    %r15,%rdx
call   1080 <sprintf@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1090 <strstr@plt>
test   %rax,%rax
je     123a <gettag+0xca>
mov    %rax,%rbx
mov    %r12,%rdi
call   1050 <strlen@plt>
add    %rax,%rbx
lea    0xe15(%rip),%rsi
mov    %r12,%rdi
mov    %r15,%rdx
call   1080 <sprintf@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1090 <strstr@plt>
test   %rax,%rax
je     123a <gettag+0xca>
mov    %rax,%r13
mov    %r14,%r15
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbp
mov    %r13,%rdi
call   1050 <strlen@plt>
sub    %rax,%rbp
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1040 <strncpy@plt>
mov    %r12,%r14
jmp    1245 <gettag+0xd5>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"<%s>\00"
@.str.1 = [6 x i8] c"</%s>\00"
define i8* @gettag(i8* %0, i8* %1) {
%3 = tail call i64 @malloc(i32 1024)
%4 = inttoptr i64 %3 to i8*
%5 = tail call i64 @malloc(i32 1024)
%6 = inttoptr i64 %5 to i8*
%7 = tail call i32 @memset(i8* %4, i32 0, i32 1024)
%8 = tail call i32 @memset(i8* %6, i32 0, i32 1024)
%9 = tail call i32 @sprintf(i8* dereferenceable(1) %6, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %1)
%10 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %6)
%11 = icmp eq i8* %10, null
br i1 %11, label %23, label %12
12: ; preds = %2
%13 = tail call i64 @strlen(i8* dereferenceable(1) %6)
%14 = getelementptr inbounds i8, i8* %10, i64 %13
%15 = tail call i32 @sprintf(i8* dereferenceable(1) %6, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%16 = tail call i8* @strstr(i8* %14, i8* dereferenceable(1) %6)
%17 = icmp eq i8* %16, null
br i1 %17, label %23, label %18
18: ; preds = %12
%19 = tail call i64 @strlen(i8* %14)
%20 = tail call i64 @strlen(i8* %16)
%21 = sub nsw i64 %19, %20
%22 = tail call i32 @strncpy(i8* %4, i8* %14, i64 %21)
br label %25
23: ; preds = %12, %2
%24 = tail call i32 @free(i8* %6)
br label %25
25: ; preds = %23, %18
%26 = phi i8* [ %4, %23 ], [ %6, %18 ]
%27 = phi i8* [ null, %23 ], [ %4, %18 ]
%28 = tail call i32 @free(i8* %26)
ret i8* %27
}