int main(void) {
  char s[32];
  char *t1 = "hello";
  char *t2 = " world";
  char *t3 = "mellow";
  char *t4 = "hello world";
  printf("str_n_cpy(s, t1, 2)"
         " -> %s\n",
         str_n_cpy(s, t1, 2));
  printf("str_n_cpy(s, t1, str_len(t1))"
         " -> %s\n",
         str_n_cpy(s, t1, str_len(t1)));
  printf("str_n_cmp(s, t1, 4)"
         " -> %i\n",
         str_n_cmp(s, t1, 4));
  printf("str_n_cmp(s, t1, str_len(t1))"
         " -> %i\n",
         str_n_cmp(s, t1, str_len(t1)));
  printf("str_n_cat(s, t2, str_len(t2))"
         " -> %s\n",
         str_n_cat(s, t2, str_len(t2)));
  printf("str_n_cmp(s, t3, 3)"
         " -> %i\n",
         str_n_cmp(s, t3, 3));
  printf("str_n_cmp(s, t1, str_len(t4))"
         " -> %i\n",
         str_n_cmp(s, t1, str_len(t4)));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe97(%rip),%rax
mov    %rax,-0x38(%rbp)
lea    0xe9e(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xe87(%rip),%rax
mov    %rax,-0x48(%rbp)
lea    0xe83(%rip),%rax
mov    %rax,-0x50(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    $0x2,%edx
mov    $0x0,%al
call   1040 <str_n_cpy@plt>
mov    %eax,%esi
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x30(%rbp),%rax
mov    %rax,-0x90(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    -0x38(%rbp),%rdi
mov    $0x0,%al
call   1070 <str_len@plt>
mov    -0x90(%rbp),%rdi
mov    -0x88(%rbp),%rsi
mov    %eax,%edx
mov    $0x0,%al
call   1040 <str_n_cpy@plt>
mov    %eax,%esi
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1060 <str_n_cmp@plt>
mov    %eax,%esi
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x30(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x38(%rbp),%rdi
mov    $0x0,%al
call   1070 <str_len@plt>
mov    -0x80(%rbp),%rdi
mov    -0x78(%rbp),%rsi
mov    %eax,%edx
mov    $0x0,%al
call   1060 <str_n_cmp@plt>
mov    %eax,%esi
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x30(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x40(%rbp),%rdi
mov    $0x0,%al
call   1070 <str_len@plt>
mov    -0x70(%rbp),%rdi
mov    -0x68(%rbp),%rsi
mov    %eax,%edx
mov    $0x0,%al
call   1030 <str_n_cat@plt>
mov    %eax,%esi
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x30(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    $0x3,%edx
mov    $0x0,%al
call   1060 <str_n_cmp@plt>
mov    %eax,%esi
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x30(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x50(%rbp),%rdi
mov    $0x0,%al
call   1070 <str_len@plt>
mov    -0x60(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    %eax,%edx
mov    $0x0,%al
call   1060 <str_n_cmp@plt>
mov    %eax,%esi
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
lea    0xea0(%rip),%r14
mov    %rsp,%r15
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x2,%edx
xor    %eax,%eax
call   1040 <str_n_cpy@plt>
lea    0xe9d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
xor    %eax,%eax
call   1070 <str_len@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <str_n_cpy@plt>
lea    0xe8f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1060 <str_n_cmp@plt>
lea    0xe92(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
xor    %eax,%eax
call   1070 <str_len@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <str_n_cmp@plt>
lea    0xe84(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe12(%rip),%rbx
mov    %rbx,%rdi
xor    %eax,%eax
call   1070 <str_len@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <str_n_cat@plt>
lea    0xe79(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdd6(%rip),%rsi
mov    %r15,%rdi
mov    $0x3,%edx
xor    %eax,%eax
call   1060 <str_n_cmp@plt>
lea    0xe78(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdb7(%rip),%rdi
xor    %eax,%eax
call   1070 <str_len@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <str_n_cmp@plt>
lea    0xe66(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [6 x i8] c"hello\00"
@.str.1 = [7 x i8] c" world\00"
@.str.2 = [7 x i8] c"mellow\00"
@.str.3 = [12 x i8] c"hello world\00"
@.str.4 = [27 x i8] c"str_n_cpy(s, t1, 2) -> %s\0A\00"
@.str.5 = [37 x i8] c"str_n_cpy(s, t1, str_len(t1)) -> %s\0A\00"
@.str.6 = [27 x i8] c"str_n_cmp(s, t1, 4) -> %i\0A\00"
@.str.7 = [37 x i8] c"str_n_cmp(s, t1, str_len(t1)) -> %i\0A\00"
@.str.8 = [37 x i8] c"str_n_cat(s, t2, str_len(t2)) -> %s\0A\00"
@.str.9 = [27 x i8] c"str_n_cmp(s, t3, 3) -> %i\0A\00"
@.str.10 = [37 x i8] c"str_n_cmp(s, t1, str_len(t4)) -> %i\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [32 x i8]
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i32 0, i32* %1
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %3
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8** %4
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8** %5
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8** %6
%7 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%8 = load i8*, i8** %3
%9 = call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @str_n_cpy to i32 (i8*, i8*, i32, ...)*)(i8* %7, i8* %8, i32 2)
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%12 = load i8*, i8** %3
%13 = load i8*, i8** %3
%14 = call i32 (i8*, ...) bitcast (i32 (...)* @str_len to i32 (i8*, ...)*)(i8* %13)
%15 = call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @str_n_cpy to i32 (i8*, i8*, i32, ...)*)(i8* %11, i8* %12, i32 %14)
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i32 %15)
%17 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%18 = load i8*, i8** %3
%19 = call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @str_n_cmp to i32 (i8*, i8*, i32, ...)*)(i8* %17, i8* %18, i32 4)
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i32 %19)
%21 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%22 = load i8*, i8** %3
%23 = load i8*, i8** %3
%24 = call i32 (i8*, ...) bitcast (i32 (...)* @str_len to i32 (i8*, ...)*)(i8* %23)
%25 = call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @str_n_cmp to i32 (i8*, i8*, i32, ...)*)(i8* %21, i8* %22, i32 %24)
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.7, i64 0, i64 0), i32 %25)
%27 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%28 = load i8*, i8** %4
%29 = load i8*, i8** %4
%30 = call i32 (i8*, ...) bitcast (i32 (...)* @str_len to i32 (i8*, ...)*)(i8* %29)
%31 = call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @str_n_cat to i32 (i8*, i8*, i32, ...)*)(i8* %27, i8* %28, i32 %30)
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.8, i64 0, i64 0), i32 %31)
%33 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%34 = load i8*, i8** %5
%35 = call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @str_n_cmp to i32 (i8*, i8*, i32, ...)*)(i8* %33, i8* %34, i32 3)
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.9, i64 0, i64 0), i32 %35)
%37 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%38 = load i8*, i8** %3
%39 = load i8*, i8** %6
%40 = call i32 (i8*, ...) bitcast (i32 (...)* @str_len to i32 (i8*, ...)*)(i8* %39)
%41 = call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @str_n_cmp to i32 (i8*, i8*, i32, ...)*)(i8* %37, i8* %38, i32 %40)
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.10, i64 0, i64 0), i32 %41)
ret i32 0
}

LLVM IR (-O2):
@.str = [6 x i8] c"hello\00"
@.str.1 = [7 x i8] c" world\00"
@.str.2 = [7 x i8] c"mellow\00"
@.str.3 = [12 x i8] c"hello world\00"
@.str.4 = [27 x i8] c"str_n_cpy(s, t1, 2) -> %s\0A\00"
@.str.5 = [37 x i8] c"str_n_cpy(s, t1, str_len(t1)) -> %s\0A\00"
@.str.6 = [27 x i8] c"str_n_cmp(s, t1, 4) -> %i\0A\00"
@.str.7 = [37 x i8] c"str_n_cmp(s, t1, str_len(t1)) -> %i\0A\00"
@.str.8 = [37 x i8] c"str_n_cat(s, t2, str_len(t2)) -> %s\0A\00"
@.str.9 = [27 x i8] c"str_n_cmp(s, t3, 3) -> %i\0A\00"
@.str.10 = [37 x i8] c"str_n_cmp(s, t1, str_len(t4)) -> %i\0A\00"
define i32 @main() {
%1 = alloca [32 x i8]
%2 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
%3 = call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @str_n_cpy to i32 (i8*, i8*, i32, ...)*)(i8* %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 2)
%4 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %3)
%5 = call i32 (i8*, ...) bitcast (i32 (...)* @str_len to i32 (i8*, ...)*)(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%6 = call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @str_n_cpy to i32 (i8*, i8*, i32, ...)*)(i8* %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i32 %6)
%8 = call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @str_n_cmp to i32 (i8*, i8*, i32, ...)*)(i8* %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 4)
%9 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i32 %8)
%10 = call i32 (i8*, ...) bitcast (i32 (...)* @str_len to i32 (i8*, ...)*)(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%11 = call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @str_n_cmp to i32 (i8*, i8*, i32, ...)*)(i8* %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.7, i64 0, i64 0), i32 %11)
%13 = call i32 (i8*, ...) bitcast (i32 (...)* @str_len to i32 (i8*, ...)*)(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @str_n_cat to i32 (i8*, i8*, i32, ...)*)(i8* %2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.8, i64 0, i64 0), i32 %14)
%16 = call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @str_n_cmp to i32 (i8*, i8*, i32, ...)*)(i8* %2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 3)
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.9, i64 0, i64 0), i32 %16)
%18 = call i32 (i8*, ...) bitcast (i32 (...)* @str_len to i32 (i8*, ...)*)(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%19 = call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @str_n_cmp to i32 (i8*, i8*, i32, ...)*)(i8* %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.10, i64 0, i64 0), i32 %19)
ret i32 0
}