char *str_subclone(const char *str, const int first, const int last) {
  register char *new;
  register int len = last - first + 1;
  assert(str);
  assert(first >= 0);
  assert(last >= 0);
  assert(first <= last);
  assert(last <= strlen(str));
  new = malloc((len + 1) * sizeof(char));
  if (new) {
    memcpy(new, str + first, len);
    new[len] = '\0';
  }
  return new;
}


x86 assembly (clang, -O0):
<str_subclone>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x10(%rbp),%eax
sub    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x0,%al
call   1040 <assert@plt>
cmpl   $0x0,-0xc(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
mov    $0x0,%al
call   1040 <assert@plt>
cmpl   $0x0,-0x10(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
mov    $0x0,%al
call   1040 <assert@plt>
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
setle  %al
and    $0x1,%al
movzbl %al,%edi
mov    $0x0,%al
call   1040 <assert@plt>
movslq -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
setbe  %al
and    $0x1,%al
movzbl %al,%edi
mov    $0x0,%al
call   1040 <assert@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
movslq %eax,%rdi
shl    $0x0,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1213 <str_subclone+0xd3>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
movslq -0xc(%rbp),%rax
add    %rax,%rsi
movslq -0x1c(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_subclone>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %esi,%r14d
mov    %rdi,%r15
xor    %eax,%eax
call   1040 <assert@plt>
mov    %r14d,%edi
not    %edi
shr    $0x1f,%edi
xor    %eax,%eax
call   1040 <assert@plt>
mov    %ebp,%edi
not    %edi
shr    $0x1f,%edi
xor    %eax,%eax
call   1040 <assert@plt>
xor    %edi,%edi
mov    %ebp,%ebx
sub    %r14d,%ebx
setge  %dil
xor    %eax,%eax
call   1040 <assert@plt>
movslq %ebp,%rbp
mov    %r15,%rdi
call   1030 <strlen@plt>
xor    %edi,%edi
cmp    %rbp,%rax
setae  %dil
xor    %eax,%eax
call   1040 <assert@plt>
lea    0x2(%rbx),%eax
movslq %eax,%rdi
call   1060 <malloc@plt>
mov    %rax,%rbp
test   %rax,%rax
je     11d0 <str_subclone+0x90>
movslq %ebx,%rbx
lea    0x1(%rbx),%rdx
movslq %r14d,%rax
add    %rax,%r15
mov    %rbp,%rdi
mov    %r15,%rsi
call   1050 <memcpy@plt>
movb   $0x0,0x1(%rbp,%rbx,1)
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @str_subclone(i8* %0, i32 %1, i32 %2) {
%4 = sub nsw i32 %2, %1
%5 = tail call i32 (i8*, ...) bitcast (i32 (...)* @assert to i32 (i8*, ...)*)(i8* %0)
%6 = xor i32 %1, -1
%7 = lshr i32 %6, 31
%8 = tail call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 %7)
%9 = xor i32 %2, -1
%10 = lshr i32 %9, 31
%11 = tail call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 %10)
%12 = icmp sge i32 %2, %1
%13 = zext i1 %12 to i32
%14 = tail call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 %13)
%15 = sext i32 %2 to i64
%16 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%17 = icmp uge i64 %16, %15
%18 = zext i1 %17 to i32
%19 = tail call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 %18)
%20 = add nsw i32 %4, 2
%21 = sext i32 %20 to i64
%22 = tail call i8* @malloc(i64 %21)
%23 = icmp eq i8* %22, null
br i1 %23, label %30, label %24
24: ; preds = %3
%25 = add nsw i32 %4, 1
%26 = sext i32 %1 to i64
%27 = getelementptr inbounds i8, i8* %0, i64 %26
%28 = sext i32 %25 to i64
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %22, i8* align 1 %27, i64 %28, i1 false)
%29 = getelementptr inbounds i8, i8* %22, i64 %28
store i8 0, i8* %29
br label %30
30: ; preds = %24, %3
ret i8* %22
}