#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 *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, int, char);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
void slice2(char *array, int first, int last) {
  char *retarray = (char *)malloc(last - first);
  int i;
  printf("Getting slice[%d:%d] of array \n", first, last);
  printf("(Elements %d to %d of array.) \n", first, last);
  puts("Here is the slice array... ");
  strncpy(retarray, array + first, last);
  for (i = first; i < last + 1; i++) {
    printf("Element[%d] is %c \n", i - first, retarray[i - first]);
  }
  free(retarray);
  retarray = NULL;
}


x86 assembly (clang, -O0):
<slice2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x10(%rbp),%edi
sub    -0xc(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%eax
lea    0xe82(%rip),%rdi
movsbl %al,%edx
call   1060 <printf@plt>
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%eax
lea    0xe8d(%rip),%rdi
movsbl %al,%edx
call   1060 <printf@plt>
lea    0xe9e(%rip),%rdi
call   1050 <puts@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
movslq -0xc(%rbp),%rax
add    %rax,%rsi
mov    -0x10(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x10(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jge    1206 <slice2+0xb6>
mov    -0x1c(%rbp),%esi
sub    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    -0xc(%rbp),%ecx
movslq %ecx,%rcx
lea    0xe6d(%rip),%rdi
movsbl (%rax,%rcx,1),%edx
call   1060 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11c4 <slice2+0x74>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x18(%rbp)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<slice2>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r12d
mov    %esi,%ebx
mov    %rdi,%r15
mov    %edx,%edi
sub    %esi,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
movsbl %r12b,%ebp
lea    0xe89(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
call   1060 <printf@plt>
lea    0xe99(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
call   1060 <printf@plt>
lea    0xea9(%rip),%rdi
call   1050 <puts@plt>
movslq %ebx,%rbx
add    %rbx,%r15
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
call   1040 <strncpy@plt>
sub    %ebx,%r12d
jl     11e8 <slice2+0x98>
add    $0x1,%r12d
xor    %ebx,%ebx
lea    0xe9a(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsbl (%r14,%rbx,1),%edx
mov    %r15,%rdi
mov    %ebx,%esi
call   1060 <printf@plt>
add    $0x1,%rbx
cmp    %ebx,%r12d
jne    11d0 <slice2+0x80>
mov    %r14,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [32 x i8] c"Getting slice[%d:%d] of array \0A\00"
@.str.1 = [32 x i8] c"(Elements %d to %d of array.) \0A\00"
@.str.2 = [28 x i8] c"Here is the slice array... \00"
@.str.3 = [20 x i8] c"Element[%d] is %c \0A\00"
define void @slice2(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%9 = load i32, i32* %6
%10 = load i32, i32* %5
%11 = sub nsw i32 %9, %10
%12 = call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %7
%14 = load i32, i32* %5
%15 = load i32, i32* %6
%16 = trunc i32 %15 to i8
%17 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %14, i8 signext %16)
%18 = load i32, i32* %5
%19 = load i32, i32* %6
%20 = trunc i32 %19 to i8
%21 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %18, i8 signext %20)
%22 = call i32 @puts(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%23 = load i8*, i8** %7
%24 = load i8*, i8** %4
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i32, i32* %6
%29 = call i32 @strncpy(i8* %23, i8* %27, i32 %28)
%30 = load i32, i32* %5
store i32 %30, i32* %8
br label %31
31: ; preds = %48, %3
%32 = load i32, i32* %8
%33 = load i32, i32* %6
%34 = add nsw i32 %33, 1
%35 = icmp slt i32 %32, %34
br i1 %35, label %36, label %51
36: ; preds = %31
%37 = load i32, i32* %8
%38 = load i32, i32* %5
%39 = sub nsw i32 %37, %38
%40 = load i8*, i8** %7
%41 = load i32, i32* %8
%42 = load i32, i32* %5
%43 = sub nsw i32 %41, %42
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %40, i64 %44
%46 = load i8, i8* %45
%47 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %39, i8 signext %46)
br label %48
48: ; preds = %36
%49 = load i32, i32* %8
%50 = add nsw i32 %49, 1
store i32 %50, i32* %8
br label %31
51: ; preds = %31
%52 = load i8*, i8** %7
%53 = call i32 @free(i8* %52)
store i8* null, i8** %7
ret void
}

LLVM IR (-O2):
@.str = [32 x i8] c"Getting slice[%d:%d] of array \0A\00"
@.str.1 = [32 x i8] c"(Elements %d to %d of array.) \0A\00"
@.str.2 = [28 x i8] c"Here is the slice array... \00"
@.str.3 = [20 x i8] c"Element[%d] is %c \0A\00"
define void @slice2(i8* %0, i32 %1, i32 %2) {
%4 = sub nsw i32 %2, %1
%5 = tail call i64 @malloc(i32 %4)
%6 = inttoptr i64 %5 to i8*
%7 = trunc i32 %2 to i8
%8 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %1, i8 signext %7)
%9 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %1, i8 signext %7)
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%11 = sext i32 %1 to i64
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = tail call i32 @strncpy(i8* %6, i8* %12, i32 %2)
%14 = icmp slt i32 %2, %1
br i1 %14, label %27, label %15
15: ; preds = %3
%16 = add i32 %2, 1
br label %17
17: ; preds = %15, %17
%18 = phi i64 [ %11, %15 ], [ %24, %17 ]
%19 = sub nsw i64 %18, %11
%20 = getelementptr inbounds i8, i8* %6, i64 %19
%21 = load i8, i8* %20
%22 = trunc i64 %19 to i32
%23 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %22, i8 signext %21)
%24 = add nsw i64 %18, 1
%25 = trunc i64 %24 to i32
%26 = icmp eq i32 %16, %25
br i1 %26, label %27, label %17
27: ; preds = %17, %3
%28 = tail call i32 @free(i8* %6)
ret void
}