#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 */
char *malloc(size_t);
int strlen(char const *);
char *strstr(char const *, char const *);
char *str_between(const char *src, size_t *olen, const char *start,
                  const char *end) {
  char *opening, *closing;
  char *dst;
  int start_found, end_found;
  size_t start_offset, end_offset;
  size_t output_len;
  size_t iter;
  start_found = 0;
  end_found = 0;
  start_offset = 0;
  end_offset = 0;
  opening = NULL;
  closing = NULL;
  dst = NULL;
  opening = strstr(src, start);
  if (opening != NULL) {
    start_found = 1;
    opening = opening + strlen(start);
    start_offset = opening - src;
  }
  closing = strstr(src, end);
  if (closing != NULL) {
    end_found = 1;
    end_offset = closing - src;
  }
  if (!start_found || !end_found)
    return NULL;
  output_len = end_offset - start_offset;
  dst = malloc(output_len + 1);
  for (iter = 0; iter < output_len; iter++)
    dst[iter] = src[start_offset + iter];
  dst[output_len] = '\0';
  *olen = output_len;
  return dst;
}


x86 assembly (clang, -O0):
<str_between>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x0,-0x44(%rbp)
movl   $0x0,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
movq   $0x0,-0x58(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11d1 <str_between+0xa1>
movl   $0x1,-0x44(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x70(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <strstr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1203 <str_between+0xd3>
movl   $0x1,-0x48(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x58(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     1217 <str_between+0xe7>
cmpl   $0x0,-0x48(%rbp)
jne    1224 <str_between+0xf4>
movq   $0x0,-0x8(%rbp)
jmp    12a1 <str_between+0x171>
mov    -0x58(%rbp),%rax
sub    -0x50(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rdi
add    $0x1,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x40(%rbp)
movq   $0x0,-0x68(%rbp)
mov    -0x68(%rbp),%rax
cmp    -0x60(%rbp),%rax
jae    1282 <str_between+0x152>
mov    -0x10(%rbp),%rax
mov    -0x50(%rbp),%rcx
add    -0x68(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x40(%rbp),%rax
mov    -0x68(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x68(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x68(%rbp)
jmp    1249 <str_between+0x119>
mov    -0x40(%rbp),%rax
mov    -0x60(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x60(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_between>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%r13
mov    %rsi,%r14
mov    %rdi,%r12
mov    %rdx,%rsi
call   1060 <strstr@plt>
test   %rax,%rax
je     11bc <str_between+0x7c>
mov    %rax,%rbx
mov    %r13,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    %r12,%rdi
mov    %r15,%rsi
call   1060 <strstr@plt>
test   %rax,%rax
je     11bc <str_between+0x7c>
mov    %rax,%r15
movslq %ebp,%rax
add    %rax,%rbx
sub    %r12,%rbx
add    %r12,%rbx
sub    %rbx,%r15
lea    0x1(%r15),%rdi
call   1050 <malloc@plt>
mov    %rax,%r12
test   %r15,%r15
je     11b2 <str_between+0x72>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%r12,%r15,1)
mov    %r15,(%r14)
jmp    11bf <str_between+0x7f>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @str_between(i8* %0, i64* %1, i8* %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i64
%16 = alloca i64
%17 = alloca i64
%18 = alloca i64
store i8* %0, i8** %6
store i64* %1, i64** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i32 0, i32* %13
store i32 0, i32* %14
store i64 0, i64* %15
store i64 0, i64* %16
store i8* null, i8** %10
store i8* null, i8** %11
store i8* null, i8** %12
%19 = load i8*, i8** %6
%20 = load i8*, i8** %8
%21 = call i8* @strstr(i8* %19, i8* %20)
store i8* %21, i8** %10
%22 = load i8*, i8** %10
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %35
24: ; preds = %4
store i32 1, i32* %13
%25 = load i8*, i8** %10
%26 = load i8*, i8** %8
%27 = call i32 @strlen(i8* %26)
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %25, i64 %28
store i8* %29, i8** %10
%30 = load i8*, i8** %10
%31 = load i8*, i8** %6
%32 = ptrtoint i8* %30 to i64
%33 = ptrtoint i8* %31 to i64
%34 = sub i64 %32, %33
store i64 %34, i64* %15
br label %35
35: ; preds = %24, %4
%36 = load i8*, i8** %6
%37 = load i8*, i8** %9
%38 = call i8* @strstr(i8* %36, i8* %37)
store i8* %38, i8** %11
%39 = load i8*, i8** %11
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %47
41: ; preds = %35
store i32 1, i32* %14
%42 = load i8*, i8** %11
%43 = load i8*, i8** %6
%44 = ptrtoint i8* %42 to i64
%45 = ptrtoint i8* %43 to i64
%46 = sub i64 %44, %45
store i64 %46, i64* %16
br label %47
47: ; preds = %41, %35
%48 = load i32, i32* %13
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %47
%51 = load i32, i32* %14
%52 = icmp ne i32 %51, 0
br i1 %52, label %54, label %53
53: ; preds = %50, %47
store i8* null, i8** %5
br label %85
54: ; preds = %50
%55 = load i64, i64* %16
%56 = load i64, i64* %15
%57 = sub i64 %55, %56
store i64 %57, i64* %17
%58 = load i64, i64* %17
%59 = add i64 %58, 1
%60 = call i8* @malloc(i64 %59)
store i8* %60, i8** %12
store i64 0, i64* %18
br label %61
61: ; preds = %75, %54
%62 = load i64, i64* %18
%63 = load i64, i64* %17
%64 = icmp ult i64 %62, %63
br i1 %64, label %65, label %78
65: ; preds = %61
%66 = load i8*, i8** %6
%67 = load i64, i64* %15
%68 = load i64, i64* %18
%69 = add i64 %67, %68
%70 = getelementptr inbounds i8, i8* %66, i64 %69
%71 = load i8, i8* %70
%72 = load i8*, i8** %12
%73 = load i64, i64* %18
%74 = getelementptr inbounds i8, i8* %72, i64 %73
store i8 %71, i8* %74
br label %75
75: ; preds = %65
%76 = load i64, i64* %18
%77 = add i64 %76, 1
store i64 %77, i64* %18
br label %61
78: ; preds = %61
%79 = load i8*, i8** %12
%80 = load i64, i64* %17
%81 = getelementptr inbounds i8, i8* %79, i64 %80
store i8 0, i8* %81
%82 = load i64, i64* %17
%83 = load i64*, i64** %7
store i64 %82, i64* %83
%84 = load i8*, i8** %12
store i8* %84, i8** %5
br label %85
85: ; preds = %78, %53
%86 = load i8*, i8** %5
ret i8* %86
}

LLVM IR (-O2):
define noalias i8* @str_between(i8* %0, i64* nocapture writeonly %1, i8* %2, i8* nocapture %3) {
%5 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %2)
%6 = icmp eq i8* %5, null
br i1 %6, label %28, label %7
7: ; preds = %4
%8 = tail call i32 @strlen(i8* %2)
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %5, i64 %9
%11 = ptrtoint i8* %10 to i64
%12 = ptrtoint i8* %0 to i64
%13 = sub i64 %11, %12
%14 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %3)
%15 = icmp eq i8* %14, null
br i1 %15, label %28, label %16
16: ; preds = %7
%17 = ptrtoint i8* %14 to i64
%18 = ptrtoint i8* %0 to i64
%19 = add i64 %13, %18
%20 = sub i64 %17, %19
%21 = add i64 %20, 1
%22 = tail call i8* @malloc(i64 %21)
%23 = icmp eq i64 %20, 0
br i1 %23, label %26, label %24
24: ; preds = %16
%25 = getelementptr i8, i8* %0, i64 %13
br label %26
26: ; preds = %24, %16
%27 = getelementptr inbounds i8, i8* %22, i64 %20
store i8 0, i8* %27
store i64 %20, i64* %1
br label %28
28: ; preds = %4, %7, %26
%29 = phi i8* [ %22, %26 ], [ null, %7 ], [ null, %4 ]
ret i8* %29
}