#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*/ exit(int);
scalar_t__ malloc(int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
char *strstr(char *, char *);
char *get_substring(char *string, char *start, char *end, int error) {
  char *startOfSubstring = strstr(string, start);
  if (startOfSubstring == NULL) {
    if (error == -1) {
      return NULL;
    }
    exit(error);
  }
  startOfSubstring += strlen(start);
  char *endOfSubstring = strstr(startOfSubstring, end);
  if (endOfSubstring == NULL) {
    if (error == -1) {
      return NULL;
    }
    exit(error);
  }
  size_t charectersInSubstring = endOfSubstring - startOfSubstring;
  char *substring = (char *)malloc(sizeof(char) * (charectersInSubstring + 1));
  if (substring == NULL) {
    exit(21);
  }
  strncpy(substring, startOfSubstring, charectersInSubstring);
  substring[charectersInSubstring] = '\0';
  return substring;
}


x86 assembly (clang, -O0):
<get_substring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11a2 <get_substring+0x52>
cmpl   $0xffffffff,-0x24(%rbp)
jne    119a <get_substring+0x4a>
movq   $0x0,-0x8(%rbp)
jmp    1256 <get_substring+0x106>
mov    -0x24(%rbp),%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11f6 <get_substring+0xa6>
cmpl   $0xffffffff,-0x24(%rbp)
jne    11ee <get_substring+0x9e>
movq   $0x0,-0x8(%rbp)
jmp    1256 <get_substring+0x106>
mov    -0x24(%rbp),%edi
call   1060 <exit@plt>
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
add    $0x1,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1231 <get_substring+0xe1>
mov    $0x15,%edi
call   1060 <exit@plt>
mov    -0x48(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x40(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x48(%rbp),%rax
mov    -0x40(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_substring>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %rsi,%rbp
call   1070 <strstr@plt>
test   %rax,%rax
je     11b7 <get_substring+0x67>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1040 <strlen@plt>
cltq
add    %rax,%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     11b7 <get_substring+0x67>
mov    %rax,%r15
sub    %rbx,%r15
lea    0x1(%r15),%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11d5 <get_substring+0x85>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1030 <strncpy@plt>
movb   $0x0,0x0(%rbp,%r15,1)
jmp    11bf <get_substring+0x6f>
cmp    $0xffffffff,%r14d
jne    11cd <get_substring+0x7d>
xor    %ebp,%ebp
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14d,%edi
call   1060 <exit@plt>
mov    $0x15,%edi
call   1060 <exit@plt>

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

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