void test_str_lenstr(void) {
  printf("------------------------\n");
  printf("TESTING:: test_str_lenstr \n");
  char *null_string = "hello ";
  unsigned char len = strlen(null_string);
  unsigned char *len_str_result = generate_lenstr(null_string, len);
  printf("hello last_i-1:: '%u'\n", len_str_result[0]);
  printf("hello str:: '%s'\n", len_str_result + 1);
  unsigned int res_len;
  char *raw = generate_raw(len_str_result, &res_len);
  printf("hello '%s'\n", raw);
  free(len_str_result);
  free(raw);
}


x86 assembly (clang, -O0):
<test_str_lenstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xead(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xebb(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %al,-0x9(%rbp)
mov    -0x8(%rbp),%rdi
movzbl -0x9(%rbp),%esi
mov    $0x0,%al
call   1040 <generate_lenstr@plt>
cltq
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movzbl (%rax),%esi
lea    0xe8f(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
mov    $0x0,%al
call   1060 <generate_raw@plt>
cltq
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xe7b(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_str_lenstr>:
push   %r14
push   %rbx
push   %rax
lea    0xee1(%rip),%rdi
call   1040 <puts@plt>
lea    0xeee(%rip),%rdi
call   1040 <puts@plt>
lea    0xe8d(%rip),%rdi
mov    $0x6,%esi
xor    %eax,%eax
call   1050 <generate_lenstr@plt>
movslq %eax,%rbx
movzbl (%rbx),%esi
lea    0xe7b(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0x1(%rbx),%rsi
lea    0xe80(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0x4(%rsp),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1060 <generate_raw@plt>
movslq %eax,%r14
lea    0xe72(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [26 x i8] c"------------------------\0A\00"
@.str.1 = [28 x i8] c"TESTING:: test_str_lenstr \0A\00"
@.str.2 = [7 x i8] c"hello \00"
@.str.3 = [23 x i8] c"hello last_i-1:: '%u'\0A\00"
@.str.4 = [18 x i8] c"hello str:: '%s'\0A\00"
@.str.5 = [12 x i8] c"hello '%s'\0A\00"
define void @test_str_lenstr() {
%1 = alloca i8*
%2 = alloca i8
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8** %1
%8 = load i8*, i8** %1
%9 = call i64 @strlen(i8* %8)
%10 = trunc i64 %9 to i8
store i8 %10, i8* %2
%11 = load i8*, i8** %1
%12 = load i8, i8* %2
%13 = zext i8 %12 to i32
%14 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @generate_lenstr to i32 (i8*, i32, ...)*)(i8* %11, i32 %13)
%15 = sext i32 %14 to i64
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %3
%17 = load i8*, i8** %3
%18 = getelementptr inbounds i8, i8* %17, i64 0
%19 = load i8, i8* %18
%20 = zext i8 %19 to i32
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %20)
%22 = load i8*, i8** %3
%23 = getelementptr inbounds i8, i8* %22, i64 1
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %23)
%25 = load i8*, i8** %3
%26 = call i32 (i8*, i32*, ...) bitcast (i32 (...)* @generate_raw to i32 (i8*, i32*, ...)*)(i8* %25, i32* %4)
%27 = sext i32 %26 to i64
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** %5
%29 = load i8*, i8** %5
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* %29)
%31 = load i8*, i8** %3
%32 = load i8*, i8** %5
ret void
}

LLVM IR (-O2):
@.str.2 = [7 x i8] c"hello \00"
@.str.3 = [23 x i8] c"hello last_i-1:: '%u'\0A\00"
@.str.4 = [18 x i8] c"hello str:: '%s'\0A\00"
@.str.5 = [12 x i8] c"hello '%s'\0A\00"
@str = [25 x i8] c"------------------------\00"
@str.6 = [27 x i8] c"TESTING:: test_str_lenstr \00"
define void @test_str_lenstr() {
%1 = alloca i32
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.6, i64 0, i64 0))
%4 = tail call i32 (i8*, i32, ...) bitcast (i32 (...)* @generate_lenstr to i32 (i8*, i32, ...)*)(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
%5 = sext i32 %4 to i64
%6 = inttoptr i64 %5 to i8*
%7 = load i8, i8* %6
%8 = zext i8 %7 to i32
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds i8, i8* %6, i64 1
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %10)
%12 = bitcast i32* %1 to i8*
%13 = call i32 (i8*, i32*, ...) bitcast (i32 (...)* @generate_raw to i32 (i8*, i32*, ...)*)(i8* %6, i32* %1)
%14 = sext i32 %13 to i64
%15 = inttoptr i64 %14 to i8*
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* %15)
ret void
}