#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 */
scalar_t__ malloc(int);
int /*<<< orphan*/ strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char const *, char const *);
char *qstrdup_between(const char *str, const char *start, const char *end) {
  char *s;
  if ((s = strstr(str, start)) == NULL)
    return NULL;
  s += strlen(start);
  char *e;
  if ((e = strstr(s, end)) == NULL)
    return NULL;
  int len = e - s;
  char *buf = (char *)malloc(sizeof(char) * (len + 1));
  strncpy(buf, s, len);
  buf[len] = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<qstrdup_between>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    117c <qstrdup_between+0x3c>
movq   $0x0,-0x8(%rbp)
jmp    1206 <qstrdup_between+0xc6>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11bd <qstrdup_between+0x7d>
movq   $0x0,-0x8(%rbp)
jmp    1206 <qstrdup_between+0xc6>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x40(%rbp),%rax
movslq -0x34(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<qstrdup_between>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
call   1060 <strstr@plt>
test   %rax,%rax
je     119f <qstrdup_between+0x5f>
mov    %rax,%rbx
mov    %r15,%rdi
call   1040 <strlen@plt>
cltq
add    %rax,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <strstr@plt>
test   %rax,%rax
je     119f <qstrdup_between+0x5f>
mov    %rax,%r14
sub    %rbx,%r14
lea    0x1(%r14),%edi
call   1050 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <strncpy@plt>
movslq %r14d,%rax
movb   $0x0,(%r15,%rax,1)
jmp    11a2 <qstrdup_between+0x62>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @qstrdup_between(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%12 = load i8*, i8** %5
%13 = load i8*, i8** %6
%14 = call i8* @strstr(i8* %12, i8* %13)
store i8* %14, i8** %8
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %17
16: ; preds = %3
store i8* null, i8** %4
br label %51
17: ; preds = %3
%18 = load i8*, i8** %6
%19 = call i32 @strlen(i8* %18)
%20 = load i8*, i8** %8
%21 = sext i32 %19 to i64
%22 = getelementptr inbounds i8, i8* %20, i64 %21
store i8* %22, i8** %8
%23 = load i8*, i8** %8
%24 = load i8*, i8** %7
%25 = call i8* @strstr(i8* %23, i8* %24)
store i8* %25, i8** %9
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %28
27: ; preds = %17
store i8* null, i8** %4
br label %51
28: ; preds = %17
%29 = load i8*, i8** %9
%30 = load i8*, i8** %8
%31 = ptrtoint i8* %29 to i64
%32 = ptrtoint i8* %30 to i64
%33 = sub i64 %31, %32
%34 = trunc i64 %33 to i32
store i32 %34, i32* %10
%35 = load i32, i32* %10
%36 = add nsw i32 %35, 1
%37 = sext i32 %36 to i64
%38 = mul i64 1, %37
%39 = trunc i64 %38 to i32
%40 = call i64 @malloc(i32 %39)
%41 = inttoptr i64 %40 to i8*
store i8* %41, i8** %11
%42 = load i8*, i8** %11
%43 = load i8*, i8** %8
%44 = load i32, i32* %10
%45 = call i32 @strncpy(i8* %42, i8* %43, i32 %44)
%46 = load i8*, i8** %11
%47 = load i32, i32* %10
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %46, i64 %48
store i8 0, i8* %49
%50 = load i8*, i8** %11
store i8* %50, i8** %4
br label %51
51: ; preds = %28, %27, %16
%52 = load i8*, i8** %4
ret i8* %52
}

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