char *strings_prefix(char *string, unsigned int length) {
  assert(string);
  unsigned int string_length = strings_length(string);
  assert(length <= string_length);
  char *ret = strings_alloc(length + 1);
  memcpy(ret, string, length * sizeof(char));
  ret[length] = '\0';
  return ret;
}


x86 assembly (clang, -O0):
<strings_prefix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x0,%al
call   1050 <assert@plt>
mov    -0x8(%rbp),%rdi
mov    $0x0,%al
call   1030 <strings_length@plt>
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
setbe  %al
and    $0x1,%al
movzbl %al,%edi
mov    $0x0,%al
call   1050 <assert@plt>
mov    -0xc(%rbp),%edi
add    $0x1,%edi
mov    $0x0,%al
call   1040 <strings_alloc@plt>
cltq
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%eax
mov    %eax,%edx
shl    $0x0,%rdx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strings_prefix>:
push   %r15
push   %r14
push   %rbx
mov    %esi,%r15d
mov    %rdi,%r14
xor    %eax,%eax
call   1050 <assert@plt>
mov    %r14,%rdi
xor    %eax,%eax
call   1030 <strings_length@plt>
xor    %edi,%edi
cmp    %r15d,%eax
setae  %dil
xor    %eax,%eax
call   1050 <assert@plt>
lea    0x1(%r15),%edi
xor    %eax,%eax
call   1040 <strings_alloc@plt>
movslq %eax,%rbx
mov    %r15d,%r15d
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1060 <memcpy@plt>
movb   $0x0,(%rbx,%r15,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @strings_prefix(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i8*, i8** %3
%8 = call i32 (i8*, ...) bitcast (i32 (...)* @assert to i32 (i8*, ...)*)(i8* %7)
%9 = load i8*, i8** %3
%10 = call i32 (i8*, ...) bitcast (i32 (...)* @strings_length to i32 (i8*, ...)*)(i8* %9)
store i32 %10, i32* %5
%11 = load i32, i32* %4
%12 = load i32, i32* %5
%13 = icmp ule i32 %11, %12
%14 = zext i1 %13 to i32
%15 = call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 %14)
%16 = load i32, i32* %4
%17 = add i32 %16, 1
%18 = call i32 (i32, ...) bitcast (i32 (...)* @strings_alloc to i32 (i32, ...)*)(i32 %17)
%19 = sext i32 %18 to i64
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %6
%21 = load i8*, i8** %6
%22 = load i8*, i8** %3
%23 = load i32, i32* %4
%24 = zext i32 %23 to i64
%25 = mul i64 %24, 1
%26 = load i8*, i8** %6
%27 = load i32, i32* %4
%28 = zext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 0, i8* %29
%30 = load i8*, i8** %6
ret i8* %30
}

LLVM IR (-O2):
define i8* @strings_prefix(i8* %0, i32 %1) {
%3 = tail call i32 (i8*, ...) bitcast (i32 (...)* @assert to i32 (i8*, ...)*)(i8* %0)
%4 = tail call i32 (i8*, ...) bitcast (i32 (...)* @strings_length to i32 (i8*, ...)*)(i8* %0)
%5 = icmp uge i32 %4, %1
%6 = zext i1 %5 to i32
%7 = tail call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 %6)
%8 = add i32 %1, 1
%9 = tail call i32 (i32, ...) bitcast (i32 (...)* @strings_alloc to i32 (i32, ...)*)(i32 %8)
%10 = sext i32 %9 to i64
%11 = inttoptr i64 %10 to i8*
%12 = zext i32 %1 to i64
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %11, i8* align 1 %0, i64 %12, i1 false)
%13 = getelementptr inbounds i8, i8* %11, i64 %12
store i8 0, i8* %13
ret i8* %11
}