#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);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *malloc(int);
int slength(char *);
int /*<<< orphan*/ stderr;
char *substring(char *source, int begin, int end) {
  int st_ln, e;
  char *t;
  if (begin > end) {
    fprintf(stderr, "sstring, substring: begin point is after end point.\n");
    exit(1);
  }
  st_ln = slength(source);
  if (begin > st_ln) {
    fprintf(stderr, "sstring, substring: begin point is not in string.\n");
    exit(1);
  }
  if (end > st_ln) {
    fprintf(stderr, "sstring, substring: end point is not in string.\n");
    exit(1);
  }
  t = malloc(sizeof(char) * (1 + (end - begin)));
  for (e = begin; e <= end; ++e) {
    t[e - begin] = source[e];
  }
  t[e - begin] = '\0';
  return t;
}


x86 assembly (clang, -O0):
<substring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jle    117d <substring+0x3d>
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
lea    0xe92(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x8(%rbp),%rdi
call   1060 <slength@plt>
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    11b4 <substring+0x74>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
lea    0xe90(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x10(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    11df <substring+0x9f>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    0xe98(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x10(%rbp),%eax
sub    -0xc(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x10(%rbp),%eax
jg     1234 <substring+0xf4>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    -0xc(%rbp),%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11ff <substring+0xbf>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    -0xc(%rbp),%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<substring>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%ebx
sub    %esi,%ebx
jl     11b1 <substring+0x61>
mov    %edx,%r15d
mov    %esi,%r12d
mov    %rdi,%r14
call   1070 <slength@plt>
cmp    %r12d,%eax
jl     11c3 <substring+0x73>
cmp    %r15d,%eax
jl     11d5 <substring+0x85>
lea    0x1(%rbx),%r15d
mov    %r15d,%edi
call   1050 <malloc@plt>
mov    %rax,%rbp
movslq %r12d,%rax
add    %rax,%r14
add    $0x1,%rbx
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
movslq %r15d,%rax
movb   $0x0,0x0(%rbp,%rax,1)
mov    %rbp,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
lea    0xe3f(%rip),%rsi
jmp    11e5 <substring+0x95>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
lea    0xe62(%rip),%rsi
jmp    11e5 <substring+0x95>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
lea    0xe83(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [53 x i8] c"sstring, substring: begin point is after end point.\0A\00"
@.str.1 = [51 x i8] c"sstring, substring: begin point is not in string.\0A\00"
@.str.2 = [49 x i8] c"sstring, substring: end point is not in string.\0A\00"
define i8* @substring(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%10 = load i32, i32* %5
%11 = load i32, i32* %6
%12 = icmp sgt i32 %10, %11
br i1 %12, label %13, label %17
13: ; preds = %3
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %3
%18 = load i8*, i8** %4
%19 = call i32 @slength(i8* %18)
store i32 %19, i32* %7
%20 = load i32, i32* %5
%21 = load i32, i32* %7
%22 = icmp sgt i32 %20, %21
br i1 %22, label %23, label %27
23: ; preds = %17
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %17
%28 = load i32, i32* %6
%29 = load i32, i32* %7
%30 = icmp sgt i32 %28, %29
br i1 %30, label %31, label %35
31: ; preds = %27
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %27
%36 = load i32, i32* %6
%37 = load i32, i32* %5
%38 = sub nsw i32 %36, %37
%39 = add nsw i32 1, %38
%40 = sext i32 %39 to i64
%41 = mul i64 1, %40
%42 = trunc i64 %41 to i32
%43 = call i8* @malloc(i32 %42)
store i8* %43, i8** %9
%44 = load i32, i32* %5
store i32 %44, i32* %8
br label %45
45: ; preds = %61, %35
%46 = load i32, i32* %8
%47 = load i32, i32* %6
%48 = icmp sle i32 %46, %47
br i1 %48, label %49, label %64
49: ; preds = %45
%50 = load i8*, i8** %4
%51 = load i32, i32* %8
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
%54 = load i8, i8* %53
%55 = load i8*, i8** %9
%56 = load i32, i32* %8
%57 = load i32, i32* %5
%58 = sub nsw i32 %56, %57
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %55, i64 %59
store i8 %54, i8* %60
br label %61
61: ; preds = %49
%62 = load i32, i32* %8
%63 = add nsw i32 %62, 1
store i32 %63, i32* %8
br label %45
64: ; preds = %45
%65 = load i8*, i8** %9
%66 = load i32, i32* %8
%67 = load i32, i32* %5
%68 = sub nsw i32 %66, %67
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds i8, i8* %65, i64 %69
store i8 0, i8* %70
%71 = load i8*, i8** %9
ret i8* %71
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [53 x i8] c"sstring, substring: begin point is after end point.\0A\00"
@.str.1 = [51 x i8] c"sstring, substring: begin point is not in string.\0A\00"
@.str.2 = [49 x i8] c"sstring, substring: end point is not in string.\0A\00"
define noalias i8* @substring(i8* %0, i32 %1, i32 %2) {
%4 = icmp slt i32 %2, %1
br i1 %4, label %5, label %9
5: ; preds = %3
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %3
%10 = tail call i32 @slength(i8* %0)
%11 = icmp slt i32 %10, %1
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %9
%17 = icmp slt i32 %10, %2
br i1 %17, label %18, label %22
18: ; preds = %16
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %16
%23 = sub i32 1, %1
%24 = add i32 %23, %2
%25 = tail call i8* @malloc(i32 %24)
%26 = sext i32 %1 to i64
%27 = getelementptr i8, i8* %0, i64 %26
%28 = sub i32 %2, %1
%29 = zext i32 %28 to i64
%30 = add nuw nsw i64 %29, 1
%31 = add i32 %2, 1
%32 = sub nsw i32 %31, %1
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %25, i64 %33
store i8 0, i8* %34
ret i8* %25
}