void test_lpad(char *src, int width, unsigned char pad) {
  unsigned long t1, t2;
  char *r1 = calloc(1, 1024);
  char *r2 = calloc(1, 1024);
  char *r3 = calloc(1, 1024);
  memset(r1, 'a', 1023);
  memset(r2, 'a', 1023);
  memset(r3, 'a', 1023);
  old_str_lpad(r1, src, width, pad);
  asm volatile("rdtsc" : "=A"(t1) : :);
  ;
  old_str_lpad(r1, src, width, pad);
  asm volatile("rdtsc" : "=A"(t2) : :);
  ;
  printf("old - %5lu cycles - \n%s\n", t2 - t1, r1);
  new_str_lpad(r2, src, width, pad);
  asm volatile("rdtsc" : "=A"(t1) : :);
  ;
  new_str_lpad(r2, src, width, pad);
  asm volatile("rdtsc" : "=A"(t2) : :);
  ;
  printf("new - %5lu cycles - \n%s\n", t2 - t1, r2);
  asm_str_lpad(r3, src, width, pad);
  asm volatile("rdtsc" : "=A"(t1) : :);
  ;
  asm_str_lpad(r3, src, width, pad);
  asm volatile("rdtsc" : "=A"(t2) : :);
  ;
  printf("asm - %5lu cycles - \n%s\n\n", t2 - t1, r3);
  free(r1);
  free(r2);
  free(r3);
}


x86 assembly (clang, -O0):
<test_lpad>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %dl,%al
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %al,-0xd(%rbp)
mov    $0x1,%edi
mov    $0x400,%esi
call   1090 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    $0x1,%edi
mov    $0x400,%esi
call   1090 <calloc@plt>
mov    %rax,-0x30(%rbp)
mov    $0x1,%edi
mov    $0x400,%esi
call   1090 <calloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x61,%esi
mov    $0x3ff,%edx
call   1080 <memset@plt>
mov    -0x30(%rbp),%rdi
mov    $0x61,%esi
mov    $0x3ff,%edx
call   1080 <memset@plt>
mov    -0x38(%rbp),%rdi
mov    $0x61,%esi
mov    $0x3ff,%edx
call   1080 <memset@plt>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
movzbl -0xd(%rbp),%ecx
mov    $0x0,%al
call   1060 <old_str_lpad@plt>
rdtsc
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
movzbl -0xd(%rbp),%ecx
mov    $0x0,%al
call   1060 <old_str_lpad@plt>
rdtsc
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
sub    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xdbf(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
movzbl -0xd(%rbp),%ecx
mov    $0x0,%al
call   1050 <new_str_lpad@plt>
rdtsc
mov    %rax,-0x18(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
movzbl -0xd(%rbp),%ecx
mov    $0x0,%al
call   1050 <new_str_lpad@plt>
rdtsc
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
sub    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
lea    0xd86(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
movzbl -0xd(%rbp),%ecx
mov    $0x0,%al
call   1040 <asm_str_lpad@plt>
rdtsc
mov    %rax,-0x18(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
movzbl -0xd(%rbp),%ecx
mov    $0x0,%al
call   1040 <asm_str_lpad@plt>
rdtsc
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
sub    -0x18(%rbp),%rsi
mov    -0x38(%rbp),%rdx
lea    0xd4d(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_lpad>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r13d
mov    %esi,%r15d
mov    %rdi,%r12
mov    $0x1,%edi
mov    $0x400,%esi
call   1090 <calloc@plt>
mov    %rax,%rbx
mov    $0x1,%edi
mov    $0x400,%esi
call   1090 <calloc@plt>
mov    %rax,%rbp
mov    $0x1,%edi
mov    $0x400,%esi
call   1090 <calloc@plt>
mov    %rax,%r14
mov    %rax,0x10(%rsp)
mov    $0x3ff,%edx
mov    %rbx,%rdi
mov    $0x61,%esi
call   1080 <memset@plt>
mov    $0x3ff,%edx
mov    %rbp,%rdi
mov    $0x61,%esi
call   1080 <memset@plt>
mov    $0x3ff,%edx
mov    %r14,%rdi
mov    $0x61,%esi
call   1080 <memset@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
mov    %r13d,%ecx
xor    %eax,%eax
call   1060 <old_str_lpad@plt>
rdtsc
mov    %rax,%r14
mov    %rbx,%rdi
mov    %rbx,0x8(%rsp)
mov    %r12,%rsi
mov    %r15d,%edx
mov    %r13d,%ecx
xor    %eax,%eax
call   1060 <old_str_lpad@plt>
rdtsc
sub    %r14,%rax
lea    0xdcc(%rip),%rdi
mov    %rax,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbp,%r14
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
mov    %r13d,%ecx
xor    %eax,%eax
call   1050 <new_str_lpad@plt>
rdtsc
mov    %rax,%rbx
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
mov    %r13d,%ecx
xor    %eax,%eax
call   1050 <new_str_lpad@plt>
rdtsc
sub    %rbx,%rax
lea    0xd9e(%rip),%rdi
mov    %rax,%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
mov    %r13d,%ecx
xor    %eax,%eax
call   1040 <asm_str_lpad@plt>
rdtsc
mov    %rax,%rbx
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
mov    %r13d,%ecx
xor    %eax,%eax
call   1040 <asm_str_lpad@plt>
rdtsc
sub    %rbx,%rax
lea    0xd6e(%rip),%rdi
mov    %rax,%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbp,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"old - %5lu cycles - \0A%s\0A\00"
@.str.1 = [25 x i8] c"new - %5lu cycles - \0A%s\0A\00"
@.str.2 = [26 x i8] c"asm - %5lu cycles - \0A%s\0A\0A\00"
define void @test_lpad(i8* %0, i32 %1, i8 zeroext %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8 %2, i8* %6
%12 = call i8* @calloc(i64 1, i64 1024)
store i8* %12, i8** %9
%13 = call i8* @calloc(i64 1, i64 1024)
store i8* %13, i8** %10
%14 = call i8* @calloc(i64 1, i64 1024)
store i8* %14, i8** %11
%15 = load i8*, i8** %9
%16 = load i8*, i8** %10
%17 = load i8*, i8** %11
%18 = load i8*, i8** %9
%19 = load i8*, i8** %4
%20 = load i32, i32* %5
%21 = load i8, i8* %6
%22 = zext i8 %21 to i32
%23 = call i32 (i8*, i8*, i32, i32, ...) bitcast (i32 (...)* @old_str_lpad to i32 (i8*, i8*, i32, i32, ...)*)(i8* %18, i8* %19, i32 %20, i32 %22)
%24 = call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
store i64 %24, i64* %7
%25 = load i8*, i8** %9
%26 = load i8*, i8** %4
%27 = load i32, i32* %5
%28 = load i8, i8* %6
%29 = zext i8 %28 to i32
%30 = call i32 (i8*, i8*, i32, i32, ...) bitcast (i32 (...)* @old_str_lpad to i32 (i8*, i8*, i32, i32, ...)*)(i8* %25, i8* %26, i32 %27, i32 %29)
%31 = call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
store i64 %31, i64* %8
%32 = load i64, i64* %8
%33 = load i64, i64* %7
%34 = sub i64 %32, %33
%35 = load i8*, i8** %9
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i64 %34, i8* %35)
%37 = load i8*, i8** %10
%38 = load i8*, i8** %4
%39 = load i32, i32* %5
%40 = load i8, i8* %6
%41 = zext i8 %40 to i32
%42 = call i32 (i8*, i8*, i32, i32, ...) bitcast (i32 (...)* @new_str_lpad to i32 (i8*, i8*, i32, i32, ...)*)(i8* %37, i8* %38, i32 %39, i32 %41)
%43 = call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
store i64 %43, i64* %7
%44 = load i8*, i8** %10
%45 = load i8*, i8** %4
%46 = load i32, i32* %5
%47 = load i8, i8* %6
%48 = zext i8 %47 to i32
%49 = call i32 (i8*, i8*, i32, i32, ...) bitcast (i32 (...)* @new_str_lpad to i32 (i8*, i8*, i32, i32, ...)*)(i8* %44, i8* %45, i32 %46, i32 %48)
%50 = call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
store i64 %50, i64* %8
%51 = load i64, i64* %8
%52 = load i64, i64* %7
%53 = sub i64 %51, %52
%54 = load i8*, i8** %10
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i64 %53, i8* %54)
%56 = load i8*, i8** %11
%57 = load i8*, i8** %4
%58 = load i32, i32* %5
%59 = load i8, i8* %6
%60 = zext i8 %59 to i32
%61 = call i32 (i8*, i8*, i32, i32, ...) bitcast (i32 (...)* @asm_str_lpad to i32 (i8*, i8*, i32, i32, ...)*)(i8* %56, i8* %57, i32 %58, i32 %60)
%62 = call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
store i64 %62, i64* %7
%63 = load i8*, i8** %11
%64 = load i8*, i8** %4
%65 = load i32, i32* %5
%66 = load i8, i8* %6
%67 = zext i8 %66 to i32
%68 = call i32 (i8*, i8*, i32, i32, ...) bitcast (i32 (...)* @asm_str_lpad to i32 (i8*, i8*, i32, i32, ...)*)(i8* %63, i8* %64, i32 %65, i32 %67)
%69 = call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
store i64 %69, i64* %8
%70 = load i64, i64* %8
%71 = load i64, i64* %7
%72 = sub i64 %70, %71
%73 = load i8*, i8** %11
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i64 %72, i8* %73)
%75 = load i8*, i8** %9
%76 = load i8*, i8** %10
%77 = load i8*, i8** %11
ret void
}

LLVM IR (-O2):
@.str = [25 x i8] c"old - %5lu cycles - \0A%s\0A\00"
@.str.1 = [25 x i8] c"new - %5lu cycles - \0A%s\0A\00"
@.str.2 = [26 x i8] c"asm - %5lu cycles - \0A%s\0A\0A\00"
define void @test_lpad(i8* %0, i32 %1, i8 zeroext %2) {
%4 = tail call dereferenceable_or_null(1024) i8* @calloc(i64 1, i64 1024)
%5 = tail call dereferenceable_or_null(1024) i8* @calloc(i64 1, i64 1024)
%6 = tail call dereferenceable_or_null(1024) i8* @calloc(i64 1, i64 1024)
tail call void @llvm.memset.p0i8.i64(i8* align 1 dereferenceable(1023) %4, i8 97, i64 1023, i1 false)
tail call void @llvm.memset.p0i8.i64(i8* align 1 dereferenceable(1023) %5, i8 97, i64 1023, i1 false)
tail call void @llvm.memset.p0i8.i64(i8* align 1 dereferenceable(1023) %6, i8 97, i64 1023, i1 false)
%7 = zext i8 %2 to i32
%8 = tail call i32 (i8*, i8*, i32, i32, ...) bitcast (i32 (...)* @old_str_lpad to i32 (i8*, i8*, i32, i32, ...)*)(i8* %4, i8* %0, i32 %1, i32 %7)
%9 = tail call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
%10 = tail call i32 (i8*, i8*, i32, i32, ...) bitcast (i32 (...)* @old_str_lpad to i32 (i8*, i8*, i32, i32, ...)*)(i8* %4, i8* %0, i32 %1, i32 %7)
%11 = tail call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
%12 = sub i64 %11, %9
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i64 %12, i8* %4)
%14 = tail call i32 (i8*, i8*, i32, i32, ...) bitcast (i32 (...)* @new_str_lpad to i32 (i8*, i8*, i32, i32, ...)*)(i8* %5, i8* %0, i32 %1, i32 %7)
%15 = tail call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
%16 = tail call i32 (i8*, i8*, i32, i32, ...) bitcast (i32 (...)* @new_str_lpad to i32 (i8*, i8*, i32, i32, ...)*)(i8* %5, i8* %0, i32 %1, i32 %7)
%17 = tail call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
%18 = sub i64 %17, %15
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i64 %18, i8* %5)
%20 = tail call i32 (i8*, i8*, i32, i32, ...) bitcast (i32 (...)* @asm_str_lpad to i32 (i8*, i8*, i32, i32, ...)*)(i8* %6, i8* %0, i32 %1, i32 %7)
%21 = tail call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
%22 = tail call i32 (i8*, i8*, i32, i32, ...) bitcast (i32 (...)* @asm_str_lpad to i32 (i8*, i8*, i32, i32, ...)*)(i8* %6, i8* %0, i32 %1, i32 %7)
%23 = tail call i64 asm sideeffect "rdtsc", "=A,~{dirflag},~{fpsr},~{flags}"()
%24 = sub i64 %23, %21
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i64 %24, i8* %6)
tail call void @free(i8* %4)
tail call void @free(i8* %5)
tail call void @free(i8* %6)
ret void
}