#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 IndexOf(char *, char);
char *malloc(int);
char *strcpy(char *, char *);
int strlen(char *);
char *strncpy(char *, char *, int);
char *SubstringBetween(char *string, char startChar, char endChar) {
  char *retBuffer;
  int firstOccur = IndexOf(string, startChar);
  if (firstOccur == -1 || firstOccur == (strlen(string) - 1))
    return NULL;
  char *startOf = string + firstOccur + 1;
  int secondOccur = IndexOf(startOf, endChar);
  if (secondOccur == -1) {
    retBuffer = malloc(strlen(startOf) + 1);
    return strcpy(retBuffer, startOf);
  }
  retBuffer = malloc(secondOccur + 1);
  retBuffer[secondOccur] = '\0';
  return strncpy(retBuffer, startOf, secondOccur);
}


x86 assembly (clang, -O0):
<SubstringBetween>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %dl,%al
mov    %sil,%cl
mov    %rdi,-0x10(%rbp)
mov    %cl,-0x11(%rbp)
mov    %al,-0x12(%rbp)
mov    -0x10(%rbp),%rdi
movsbl -0x11(%rbp),%esi
call   1070 <IndexOf@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
je     11a0 <SubstringBetween+0x50>
mov    -0x24(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    11ad <SubstringBetween+0x5d>
movq   $0x0,-0x8(%rbp)
jmp    1236 <SubstringBetween+0xe6>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
add    %rcx,%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
movsbl -0x12(%rbp),%esi
call   1070 <IndexOf@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1207 <SubstringBetween+0xb7>
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1040 <strcpy@plt>
mov    %rax,-0x8(%rbp)
jmp    1236 <SubstringBetween+0xe6>
mov    -0x34(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x34(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1030 <strncpy@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SubstringBetween>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rdi,%rbx
call   1070 <IndexOf@plt>
cmp    $0xffffffff,%eax
je     1175 <SubstringBetween+0x25>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1050 <strlen@plt>
add    $0xffffffff,%eax
cmp    %eax,%ebp
jne    117f <SubstringBetween+0x2f>
xor    %ebp,%ebp
mov    %rbp,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret
movslq %ebp,%rax
add    %rax,%rbx
add    $0x1,%rbx
movsbl %r14b,%esi
mov    %rbx,%rdi
call   1070 <IndexOf@plt>
cmp    $0xffffffff,%eax
je     11bc <SubstringBetween+0x6c>
mov    %eax,%ebp
lea    0x1(%rbp),%edi
call   1060 <malloc@plt>
movslq %ebp,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <strncpy@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%edi
call   1060 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
jmp    1177 <SubstringBetween+0x27>

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

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