#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 */
char *malloc(int);
int /*<<< orphan*/ printf(char *);
int strlen(char *);
char *slice(char *line, int so, int eo) {
  if (eo > strlen(line)) {
    printf("out of bounds error\n");
    return (char *)NULL;
  }
  int start = so;
  int size = (eo - so) + 1;
  char *slice = malloc(size * sizeof(char));
  int i;
  for (i = 0; i < size; i++) {
    slice[i] = line[start++];
  }
  slice[size] = '\0';
  return slice;
}


x86 assembly (clang, -O0):
<slice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
cmp    %ecx,%eax
jle    1177 <slice+0x47>
lea    0xe9b(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f1 <slice+0xc1>
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
sub    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
movslq -0x20(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    11dd <slice+0xad>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x1c(%rbp)
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11a3 <slice+0x73>
mov    -0x28(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<slice>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %esi,%r13d
mov    %rdi,%r14
call   1040 <strlen@plt>
cmp    %ebx,%eax
jge    116d <slice+0x2d>
lea    0xe9d(%rip),%rdi
call   1030 <puts@plt>
xor    %r15d,%r15d
jmp    11a2 <slice+0x62>
sub    %r13d,%ebx
movslq %ebx,%rbp
lea    0x1(%rbp),%r12
mov    %r12d,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
test   %ebp,%ebp
js     119d <slice+0x5d>
movslq %r13d,%rax
add    %rax,%r14
add    $0x1,%ebx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
movb   $0x0,(%r15,%r12,1)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@str = [20 x i8] c"out of bounds error\00"
define noalias i8* @slice(i8* %0, i32 %1, i32 %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = icmp slt i32 %4, %2
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %22
8: ; preds = %3
%9 = sub nsw i32 %2, %1
%10 = add nsw i32 %9, 1
%11 = sext i32 %10 to i64
%12 = tail call i8* @malloc(i32 %10)
%13 = icmp slt i32 %9, 0
br i1 %13, label %20, label %14
14: ; preds = %8
%15 = sext i32 %1 to i64
%16 = getelementptr i8, i8* %0, i64 %15
%17 = add i32 %2, 1
%18 = sub i32 %17, %1
%19 = zext i32 %18 to i64
br label %20
20: ; preds = %14, %8
%21 = getelementptr inbounds i8, i8* %12, i64 %11
store i8 0, i8* %21
br label %22
22: ; preds = %20, %6
%23 = phi i8* [ null, %6 ], [ %12, %20 ]
ret i8* %23
}