#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*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ puts(char *);
int strlen(char *);
char *substr(char *str, int first, int last) {
  char *ret = malloc(80);
  int len = strlen(str);
  if (first < 1 || first > len || last < 1 || last > len) {
    puts("Freeing ret:");
    free(ret);
    return "ERROR!";
  }
  int i;
  int j = 0;
  for (i = first - 1; i < last; i++) {
    ret[j] = str[i];
    j++;
  }
  ret[j] = '\0';
  return ret;
}


x86 assembly (clang, -O0):
<substr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    $0x50,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x14(%rbp)
jl     1198 <substr+0x58>
mov    -0x14(%rbp),%eax
cmp    -0x24(%rbp),%eax
jg     1198 <substr+0x58>
cmpl   $0x1,-0x18(%rbp)
jl     1198 <substr+0x58>
mov    -0x18(%rbp),%eax
cmp    -0x24(%rbp),%eax
jle    11bd <substr+0x7d>
lea    0xe61(%rip),%rdi
call   1040 <puts@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
lea    0xe59(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    121a <substr+0xda>
movl   $0x0,-0x2c(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1206 <substr+0xc6>
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11cd <substr+0x8d>
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<substr>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edx,%r12d
mov    %esi,%ebx
mov    %rdi,%r15
mov    $0x50,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
mov    %r15,%rdi
call   1050 <strlen@plt>
test   %ebx,%ebx
jle    1190 <substr+0x40>
cmp    %r12d,%eax
jl     1190 <substr+0x40>
test   %r12d,%r12d
jle    1190 <substr+0x40>
cmp    %ebx,%eax
jl     1190 <substr+0x40>
sub    %ebx,%r12d
jae    11ad <substr+0x5d>
xor    %eax,%eax
jmp    11c8 <substr+0x78>
lea    0xe69(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   1030 <free@plt>
lea    0xe62(%rip),%r14
jmp    11cd <substr+0x7d>
add    $0xffffffff,%ebx
add    %rbx,%r15
add    $0x1,%r12
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1060 <memcpy@plt>
mov    %r12d,%eax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [13 x i8] c"Freeing ret:\00"
@.str.1 = [7 x i8] c"ERROR!\00"
define i8* @substr(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%12 = call i8* @malloc(i32 80)
store i8* %12, i8** %8
%13 = load i8*, i8** %5
%14 = call i32 @strlen(i8* %13)
store i32 %14, i32* %9
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 1
br i1 %16, label %28, label %17
17: ; preds = %3
%18 = load i32, i32* %6
%19 = load i32, i32* %9
%20 = icmp sgt i32 %18, %19
br i1 %20, label %28, label %21
21: ; preds = %17
%22 = load i32, i32* %7
%23 = icmp slt i32 %22, 1
br i1 %23, label %28, label %24
24: ; preds = %21
%25 = load i32, i32* %7
%26 = load i32, i32* %9
%27 = icmp sgt i32 %25, %26
br i1 %27, label %28, label %32
28: ; preds = %24, %21, %17, %3
%29 = call i32 @puts(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%30 = load i8*, i8** %8
%31 = call i32 @free(i8* %30)
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8** %4
br label %60
32: ; preds = %24
store i32 0, i32* %11
%33 = load i32, i32* %6
%34 = sub nsw i32 %33, 1
store i32 %34, i32* %10
br label %35
35: ; preds = %51, %32
%36 = load i32, i32* %10
%37 = load i32, i32* %7
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %54
39: ; preds = %35
%40 = load i8*, i8** %5
%41 = load i32, i32* %10
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
%44 = load i8, i8* %43
%45 = load i8*, i8** %8
%46 = load i32, i32* %11
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
store i8 %44, i8* %48
%49 = load i32, i32* %11
%50 = add nsw i32 %49, 1
store i32 %50, i32* %11
br label %51
51: ; preds = %39
%52 = load i32, i32* %10
%53 = add nsw i32 %52, 1
store i32 %53, i32* %10
br label %35
54: ; preds = %35
%55 = load i8*, i8** %8
%56 = load i32, i32* %11
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %55, i64 %57
store i8 0, i8* %58
%59 = load i8*, i8** %8
store i8* %59, i8** %4
br label %60
60: ; preds = %54, %28
%61 = load i8*, i8** %4
ret i8* %61
}

LLVM IR (-O2):
@.str = [13 x i8] c"Freeing ret:\00"
@.str.1 = [7 x i8] c"ERROR!\00"
define i8* @substr(i8* %0, i32 %1, i32 %2) {
%4 = tail call dereferenceable_or_null(80) i8* @malloc(i32 80)
%5 = tail call i32 @strlen(i8* %0)
%6 = icmp slt i32 %1, 1
br i1 %6, label %13, label %7
7: ; preds = %3
%8 = icmp slt i32 %5, %1
%9 = icmp slt i32 %2, 1
%10 = or i1 %9, %8
%11 = icmp slt i32 %5, %2
%12 = or i1 %11, %10
br i1 %12, label %13, label %16
13: ; preds = %7, %3
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 @free(i8* %4)
br label %29
16: ; preds = %7
%17 = icmp ult i32 %2, %1
br i1 %17, label %26, label %18
18: ; preds = %16
%19 = add nsw i32 %1, -1
%20 = zext i32 %19 to i64
%21 = getelementptr i8, i8* %0, i64 %20
%22 = sub nsw i32 %2, %1
%23 = zext i32 %22 to i64
%24 = add nuw nsw i64 %23, 1
%25 = and i64 %24, 4294967295
br label %26
26: ; preds = %18, %16
%27 = phi i64 [ 0, %16 ], [ %25, %18 ]
%28 = getelementptr inbounds i8, i8* %4, i64 %27
store i8 0, i8* %28
br label %29
29: ; preds = %26, %13
%30 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %13 ], [ %4, %26 ]
ret i8* %30
}