#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(scalar_t__);
int /*<<< orphan*/ sprintf(char *, char *, char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char *, char *);
char *element_body(char *doc, char *tagname, int n, char *buf, int buflen) {
  char *start_tag_pattern =
      malloc(strlen(tagname) + strlen("<") + strlen(">") + 1);
  char *end_tag_pattern =
      malloc(strlen(tagname) + strlen("<") + strlen("/") + strlen(">") + 1);
  char *body_start, *body_end;
  char *ret;
  sprintf(start_tag_pattern, "<%s>", tagname);
  sprintf(end_tag_pattern, "</%s>", tagname);
  body_start = doc;
  while (n-- > 0) {
    body_start = strstr(body_start, start_tag_pattern);
    if (!body_start) {
      ret = (NULL);
      goto end;
    };
    body_start += strlen(start_tag_pattern);
  }
  body_end = strstr(body_start, end_tag_pattern);
  if (!body_end) {
    ret = (NULL);
    goto end;
  };
  if (body_end - body_start < buflen - 1) {
    strncpy(buf, body_start, body_end - body_start);
    buf[body_end - body_start] = 0;
  } else {
    strncpy(buf, body_start, buflen - 1);
    buf[buflen - 1] = 0;
  }
  {
    ret = (buf);
    goto end;
  };
end:
  if (start_tag_pattern)
    free(start_tag_pattern);
  if (end_tag_pattern)
    free(end_tag_pattern);
  return ret;
}


x86 assembly (clang, -O0):
<element_body>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x78(%rbp)
lea    0xe6e(%rip),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x78(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x70(%rbp)
lea    0xe61(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x70(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x68(%rbp)
lea    0xe27(%rip),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x68(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x60(%rbp)
lea    0xe0f(%rip),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x60(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x58(%rbp)
lea    0xe00(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x58(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xdcf(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xdc0(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
jle    12a9 <element_body+0x149>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1293 <element_body+0x133>
movq   $0x0,-0x50(%rbp)
jmp    134d <element_body+0x1ed>
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
add    -0x40(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    1256 <element_body+0xf6>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    12d2 <element_body+0x172>
movq   $0x0,-0x50(%rbp)
jmp    134d <element_body+0x1ed>
mov    -0x48(%rbp),%rax
mov    -0x40(%rbp),%rcx
sub    %rcx,%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
jge    1321 <element_body+0x1c1>
mov    -0x20(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x48(%rbp),%rax
mov    -0x40(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1040 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    -0x40(%rbp),%rdx
sub    %rdx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1345 <element_body+0x1e5>
mov    -0x20(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x24(%rbp),%edx
sub    $0x1,%edx
call   1040 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1361 <element_body+0x201>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x38(%rbp)
je     1375 <element_body+0x215>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x50(%rbp),%rax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<element_body>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,0x4(%rsp)
mov    %rcx,%r14
mov    %edx,%ebp
mov    %rsi,%r13
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1050 <strlen@plt>
mov    %rax,%r15
lea    0x3(%rax),%rdi
call   1060 <malloc@plt>
mov    %rax,%r12
add    $0x4,%r15
mov    %r15,%rdi
call   1060 <malloc@plt>
mov    %rax,%r15
lea    0xe58(%rip),%rsi
mov    %r12,%rdi
mov    %r13,%rdx
call   1070 <sprintf@plt>
lea    0xe4b(%rip),%rsi
mov    %r15,%rdi
mov    %r13,%rdx
call   1070 <sprintf@plt>
test   %ebp,%ebp
jle    11f6 <element_body+0x96>
add    $0x1,%ebp
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r12,%rsi
call   1080 <strstr@plt>
test   %rax,%rax
je     1238 <element_body+0xd8>
mov    %rax,%rbx
mov    %r12,%rdi
call   1050 <strlen@plt>
add    %rax,%rbx
add    $0xffffffff,%ebp
cmp    $0x1,%ebp
jg     11d0 <element_body+0x70>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <strstr@plt>
test   %rax,%rax
je     1238 <element_body+0xd8>
mov    %rax,%rbp
sub    %rbx,%rbp
mov    0x4(%rsp),%eax
add    $0xffffffff,%eax
cltq
cmp    %rax,%rbp
mov    %eax,%edx
cmovl  %ebp,%edx
cmovge %rax,%rbp
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <strncpy@plt>
movb   $0x0,(%r14,%rbp,1)
test   %r12,%r12
jne    1240 <element_body+0xe0>
jmp    1248 <element_body+0xe8>
xor    %r14d,%r14d
test   %r12,%r12
je     1248 <element_body+0xe8>
mov    %r12,%rdi
call   1030 <free@plt>
test   %r15,%r15
je     1255 <element_body+0xf5>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"<\00"
@.str.1 = [2 x i8] c">\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [5 x i8] c"<%s>\00"
@.str.4 = [6 x i8] c"</%s>\00"
define i8* @element_body(i8* %0, i8* %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i8*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i32 %4, i32* %10
%16 = load i8*, i8** %7
%17 = call i64 @strlen(i8* %16)
%18 = call i64 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%19 = add nsw i64 %17, %18
%20 = call i64 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%21 = add nsw i64 %19, %20
%22 = add nsw i64 %21, 1
%23 = call i8* @malloc(i64 %22)
store i8* %23, i8** %11
%24 = load i8*, i8** %7
%25 = call i64 @strlen(i8* %24)
%26 = call i64 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%27 = add nsw i64 %25, %26
%28 = call i64 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%29 = add nsw i64 %27, %28
%30 = call i64 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%31 = add nsw i64 %29, %30
%32 = add nsw i64 %31, 1
%33 = call i8* @malloc(i64 %32)
store i8* %33, i8** %12
%34 = load i8*, i8** %11
%35 = load i8*, i8** %7
%36 = call i32 @sprintf(i8* %34, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %35)
%37 = load i8*, i8** %12
%38 = load i8*, i8** %7
%39 = call i32 @sprintf(i8* %37, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %38)
%40 = load i8*, i8** %6
store i8* %40, i8** %13
br label %41
41: ; preds = %52, %5
%42 = load i32, i32* %8
%43 = add nsw i32 %42, -1
store i32 %43, i32* %8
%44 = icmp sgt i32 %42, 0
br i1 %44, label %45, label %57
45: ; preds = %41
%46 = load i8*, i8** %13
%47 = load i8*, i8** %11
%48 = call i8* @strstr(i8* %46, i8* %47)
store i8* %48, i8** %13
%49 = load i8*, i8** %13
%50 = icmp ne i8* %49, null
br i1 %50, label %52, label %51
51: ; preds = %45
store i8* null, i8** %15
br label %104
52: ; preds = %45
%53 = load i8*, i8** %11
%54 = call i64 @strlen(i8* %53)
%55 = load i8*, i8** %13
%56 = getelementptr inbounds i8, i8* %55, i64 %54
store i8* %56, i8** %13
br label %41
57: ; preds = %41
%58 = load i8*, i8** %13
%59 = load i8*, i8** %12
%60 = call i8* @strstr(i8* %58, i8* %59)
store i8* %60, i8** %14
%61 = load i8*, i8** %14
%62 = icmp ne i8* %61, null
br i1 %62, label %64, label %63
63: ; preds = %57
store i8* null, i8** %15
br label %104
64: ; preds = %57
%65 = load i8*, i8** %14
%66 = load i8*, i8** %13
%67 = ptrtoint i8* %65 to i64
%68 = ptrtoint i8* %66 to i64
%69 = sub i64 %67, %68
%70 = load i32, i32* %10
%71 = sub nsw i32 %70, 1
%72 = sext i32 %71 to i64
%73 = icmp slt i64 %69, %72
br i1 %73, label %74, label %91
74: ; preds = %64
%75 = load i8*, i8** %9
%76 = load i8*, i8** %13
%77 = load i8*, i8** %14
%78 = load i8*, i8** %13
%79 = ptrtoint i8* %77 to i64
%80 = ptrtoint i8* %78 to i64
%81 = sub i64 %79, %80
%82 = trunc i64 %81 to i32
%83 = call i32 @strncpy(i8* %75, i8* %76, i32 %82)
%84 = load i8*, i8** %9
%85 = load i8*, i8** %14
%86 = load i8*, i8** %13
%87 = ptrtoint i8* %85 to i64
%88 = ptrtoint i8* %86 to i64
%89 = sub i64 %87, %88
%90 = getelementptr inbounds i8, i8* %84, i64 %89
store i8 0, i8* %90
br label %102
91: ; preds = %64
%92 = load i8*, i8** %9
%93 = load i8*, i8** %13
%94 = load i32, i32* %10
%95 = sub nsw i32 %94, 1
%96 = call i32 @strncpy(i8* %92, i8* %93, i32 %95)
%97 = load i8*, i8** %9
%98 = load i32, i32* %10
%99 = sub nsw i32 %98, 1
%100 = sext i32 %99 to i64
%101 = getelementptr inbounds i8, i8* %97, i64 %100
store i8 0, i8* %101
br label %102
102: ; preds = %91, %74
%103 = load i8*, i8** %9
store i8* %103, i8** %15
br label %104
104: ; preds = %102, %63, %51
%105 = load i8*, i8** %11
%106 = icmp ne i8* %105, null
br i1 %106, label %107, label %110
107: ; preds = %104
%108 = load i8*, i8** %11
%109 = call i32 @free(i8* %108)
br label %110
110: ; preds = %107, %104
%111 = load i8*, i8** %12
%112 = icmp ne i8* %111, null
br i1 %112, label %113, label %116
113: ; preds = %110
%114 = load i8*, i8** %12
%115 = call i32 @free(i8* %114)
br label %116
116: ; preds = %113, %110
%117 = load i8*, i8** %15
ret i8* %117
}

LLVM IR (-O2):
@.str.3 = [5 x i8] c"<%s>\00"
@.str.4 = [6 x i8] c"</%s>\00"
define i8* @element_body(i8* %0, i8* %1, i32 %2, i8* %3, i32 %4) {
%6 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%7 = add nsw i64 %6, 3
%8 = tail call i8* @malloc(i64 %7)
%9 = add nsw i64 %6, 4
%10 = tail call i8* @malloc(i64 %9)
%11 = tail call i32 @sprintf(i8* dereferenceable(1) %8, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %1)
%12 = tail call i32 @sprintf(i8* dereferenceable(1) %10, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %1)
%13 = icmp sgt i32 %2, 0
br i1 %13, label %14, label %24
14: ; preds = %5, %19
%15 = phi i32 [ %20, %19 ], [ %2, %5 ]
%16 = phi i8* [ %22, %19 ], [ %0, %5 ]
%17 = tail call i8* @strstr(i8* dereferenceable(1) %16, i8* dereferenceable(1) %8)
%18 = icmp eq i8* %17, null
br i1 %18, label %40, label %19
19: ; preds = %14
%20 = add nsw i32 %15, -1
%21 = tail call i64 @strlen(i8* dereferenceable(1) %8)
%22 = getelementptr inbounds i8, i8* %17, i64 %21
%23 = icmp sgt i32 %15, 1
br i1 %23, label %14, label %24
24: ; preds = %19, %5
%25 = phi i8* [ %0, %5 ], [ %22, %19 ]
%26 = tail call i8* @strstr(i8* dereferenceable(1) %25, i8* dereferenceable(1) %10)
%27 = icmp eq i8* %26, null
br i1 %27, label %40, label %28
28: ; preds = %24
%29 = ptrtoint i8* %26 to i64
%30 = ptrtoint i8* %25 to i64
%31 = sub i64 %29, %30
%32 = add nsw i32 %4, -1
%33 = sext i32 %32 to i64
%34 = icmp slt i64 %31, %33
%35 = trunc i64 %31 to i32
%36 = select i1 %34, i32 %35, i32 %32
%37 = select i1 %34, i64 %31, i64 %33
%38 = tail call i32 @strncpy(i8* %3, i8* %25, i32 %36)
%39 = getelementptr inbounds i8, i8* %3, i64 %37
store i8 0, i8* %39
br label %40
40: ; preds = %14, %28, %24
%41 = phi i8* [ null, %24 ], [ %3, %28 ], [ null, %14 ]
%42 = icmp eq i8* %8, null
br i1 %42, label %45, label %43
43: ; preds = %40
%44 = tail call i32 @free(i8* %8)
br label %45
45: ; preds = %43, %40
%46 = icmp eq i8* %10, null
br i1 %46, label %49, label %47
47: ; preds = %45
%48 = tail call i32 @free(i8* %10)
br label %49
49: ; preds = %47, %45
ret i8* %41
}