void pad_with_zeros(char *str, int n) {
  char *tmp = strdup(str);
  char *buf = malloc(n + 1);
  buf[n] = '\0';
  memset(buf, '0', n);
  sprintf(str, "%.*s%s", n - strlen(tmp), buf, tmp);
  free(tmp);
}


x86 assembly (clang, -O0):
<pad_with_zeros>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
movslq %eax,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
movslq -0xc(%rbp),%rdx
mov    $0x30,%esi
call   1050 <memset@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
movslq -0xc(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdx
mov    -0x28(%rbp),%rdi
sub    %rax,%rdx
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0xe21(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pad_with_zeros>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %esi,%r12d
mov    %rdi,%r14
call   1080 <strdup@plt>
mov    %rax,%r15
lea    0x1(%r12),%eax
movslq %eax,%rdi
call   1060 <malloc@plt>
mov    %rax,%r13
movslq %r12d,%rbx
movb   $0x0,(%rax,%rbx,1)
mov    %rax,%rdi
mov    $0x30,%esi
mov    %rbx,%rdx
call   1050 <memset@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
sub    %rax,%rbx
lea    0xe50(%rip),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
mov    %r13,%rcx
mov    %r15,%r8
xor    %eax,%eax
call   1070 <sprintf@plt>
mov    %r15,%rdi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"%.*s%s\00"
define void @pad_with_zeros(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i8*, i8** %3
%8 = call i8* @strdup(i8* %7)
store i8* %8, i8** %5
%9 = load i32, i32* %4
%10 = add nsw i32 %9, 1
%11 = sext i32 %10 to i64
%12 = call i8* @malloc(i64 %11)
store i8* %12, i8** %6
%13 = load i8*, i8** %6
%14 = load i32, i32* %4
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
store i8 0, i8* %16
%17 = load i8*, i8** %6
%18 = load i32, i32* %4
%19 = sext i32 %18 to i64
%20 = load i8*, i8** %3
%21 = load i32, i32* %4
%22 = sext i32 %21 to i64
%23 = load i8*, i8** %5
%24 = call i64 @strlen(i8* %23)
%25 = sub i64 %22, %24
%26 = load i8*, i8** %6
%27 = load i8*, i8** %5
%28 = call i32 (i8*, i8*, ...) @sprintf(i8* %20, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i64 %25, i8* %26, i8* %27)
%29 = load i8*, i8** %5
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"%.*s%s\00"
define void @pad_with_zeros(i8* nocapture %0, i32 %1) {
%3 = tail call i8* @strdup(i8* %0)
%4 = add nsw i32 %1, 1
%5 = sext i32 %4 to i64
%6 = tail call i8* @malloc(i64 %5)
%7 = sext i32 %1 to i64
%8 = getelementptr inbounds i8, i8* %6, i64 %7
store i8 0, i8* %8
tail call void @llvm.memset.p0i8.i64(i8* align 1 %6, i8 48, i64 %7, i1 false)
%9 = tail call i64 @strlen(i8* dereferenceable(1) %3)
%10 = sub i64 %7, %9
%11 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i64 %10, i8* %6, i8* %3)
tail call void @free(i8* %3)
ret void
}