#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(int);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char *, char *);
char *substr(char *haystack, char *begin, char *end) {
  char *ret, *r;
  char *b = strstr(haystack, begin);
  if (b) {
    char *e = strstr(b, end);
    if (e) {
      int offset = e - b;
      int retlen = offset - strlen(begin);
      if ((ret = malloc(retlen + 1)) == NULL)
        return NULL;
      ret[retlen] = '\0';
      strncpy(ret, b + strlen(begin), retlen);
      return ret;
    }
  }
  return NULL;
}


x86 assembly (clang, -O0):
<substr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%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,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1225 <substr+0xe5>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1220 <substr+0xe0>
mov    -0x40(%rbp),%rax
mov    -0x38(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x4c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x48(%rbp)
mov    -0x48(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11d9 <substr+0x99>
movq   $0x0,-0x8(%rbp)
jmp    122d <substr+0xed>
mov    -0x28(%rbp),%rax
movslq -0x48(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x60(%rbp),%rsi
mov    -0x58(%rbp),%rdi
cltq
add    %rax,%rsi
mov    -0x48(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    122d <substr+0xed>
jmp    1225 <substr+0xe5>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<substr>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
call   1060 <strstr@plt>
test   %rax,%rax
je     11af <substr+0x6f>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
call   1060 <strstr@plt>
test   %rax,%rax
je     11af <substr+0x6f>
mov    %rax,%rbx
sub    %r15d,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
sub    %eax,%ebx
lea    0x1(%rbx),%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11af <substr+0x6f>
mov    %rax,%r12
movslq %ebx,%rax
movb   $0x0,(%r12,%rax,1)
mov    %r14,%rdi
call   1040 <strlen@plt>
cltq
add    %rax,%r15
mov    %r12,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
jmp    11b2 <substr+0x72>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @substr(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 %26, label %6
6: ; preds = %3
%7 = tail call i8* @strstr(i8* %4, i8* dereferenceable(1) %2)
%8 = icmp eq i8* %7, null
br i1 %8, label %26, label %9
9: ; preds = %6
%10 = ptrtoint i8* %7 to i64
%11 = ptrtoint i8* %4 to i64
%12 = sub i64 %10, %11
%13 = trunc i64 %12 to i32
%14 = tail call i32 @strlen(i8* %1)
%15 = sub nsw i32 %13, %14
%16 = add nsw i32 %15, 1
%17 = tail call i8* @malloc(i32 %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %26, label %19
19: ; preds = %9
%20 = sext i32 %15 to i64
%21 = getelementptr inbounds i8, i8* %17, i64 %20
store i8 0, i8* %21
%22 = tail call i32 @strlen(i8* %1)
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %4, i64 %23
%25 = tail call i32 @strncpy(i8* %17, i8* %24, i32 %15)
br label %26
26: ; preds = %9, %19, %3, %6
%27 = phi i8* [ null, %6 ], [ null, %3 ], [ null, %9 ], [ %17, %19 ]
ret i8* %27
}