#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;
      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     1219 <substr+0xd9>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1214 <substr+0xd4>
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    1221 <substr+0xe1>
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    1221 <substr+0xe1>
jmp    1219 <substr+0xd9>
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     11a7 <substr+0x67>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
call   1060 <strstr@plt>
test   %rax,%rax
je     11a7 <substr+0x67>
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     11a7 <substr+0x67>
mov    %rax,%r12
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    11aa <substr+0x6a>
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 %52
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 %51
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 %53
41: ; preds = %25
%42 = load i8*, i8** %8
%43 = load i8*, i8** %10
%44 = load i8*, i8** %6
%45 = call i32 @strlen(i8* %44)
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %43, i64 %46
%48 = load i32, i32* %13
%49 = call i32 @strncpy(i8* %42, i8* %47, i32 %48)
%50 = load i8*, i8** %8
store i8* %50, i8** %4
br label %53
51: ; preds = %19
br label %52
52: ; preds = %51, %3
store i8* null, i8** %4
br label %53
53: ; preds = %52, %41, %40
%54 = load i8*, i8** %4
ret i8* %54
}

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 %24, 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 %24, 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 %24, label %19
19: ; preds = %9
%20 = tail call i32 @strlen(i8* %1)
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %4, i64 %21
%23 = tail call i32 @strncpy(i8* %17, i8* %22, i32 %15)
br label %24
24: ; preds = %9, %19, %3, %6
%25 = phi i8* [ null, %6 ], [ null, %3 ], [ null, %9 ], [ %17, %19 ]
ret i8* %25
}