void center_label(char *buffer, int longest, const char *label) {
  int len = dlg_count_columns(label);
  int left = 0, right = 0;
  *buffer = 0;
  if (len < longest) {
    left = (longest - len) / 2;
    right = (longest - len - left);
    if (left > 0)
      sprintf(buffer, "%*s", left, " ");
  }
  strcat(buffer, label);
  if (right > 0)
    sprintf(buffer + strlen(buffer), "%*s", right, " ");
}


x86 assembly (clang, -O0):
<center_label>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x0,%al
call   1040 <dlg_count_columns@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11ca <center_label+0x8a>
mov    -0xc(%rbp),%eax
sub    -0x1c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
sub    -0x1c(%rbp),%eax
sub    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x20(%rbp)
jle    11c5 <center_label+0x85>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%edx
lea    0xe49(%rip),%rsi
lea    0xe46(%rip),%rcx
mov    $0x0,%al
call   1060 <sprintf@plt>
jmp    11ca <center_label+0x8a>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <strcat@plt>
cmpl   $0x0,-0x24(%rbp)
jle    1211 <center_label+0xd1>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x30(%rbp),%rdi
add    %rax,%rdi
mov    -0x24(%rbp),%edx
lea    0xdfd(%rip),%rsi
lea    0xdfa(%rip),%rcx
mov    $0x0,%al
call   1060 <sprintf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<center_label>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rdx,%rdi
xor    %eax,%eax
call   1040 <dlg_count_columns@plt>
movb   $0x0,(%rbx)
cmp    %ebp,%eax
jge    11ce <center_label+0x8e>
sub    %eax,%ebp
mov    %ebp,%edx
shr    $0x1f,%edx
add    %ebp,%edx
sar    %edx
mov    %ebp,%r15d
sub    %edx,%r15d
cmp    $0x2,%ebp
jl     118e <center_label+0x4e>
lea    0xe83(%rip),%rsi
lea    0xe80(%rip),%rcx
mov    %rbx,%rdi
xor    %eax,%eax
call   1060 <sprintf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <strcat@plt>
test   %r15d,%r15d
jle    11e3 <center_label+0xa3>
mov    %rbx,%rdi
call   1030 <strlen@plt>
add    %rax,%rbx
lea    0xe50(%rip),%rsi
lea    0xe4d(%rip),%rcx
mov    %rbx,%rdi
mov    %r15d,%edx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <sprintf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <strcat@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%*s\00"
@.str.1 = [2 x i8] c" \00"
define void @center_label(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%10 = load i8*, i8** %6
%11 = call i32 (i8*, ...) bitcast (i32 (...)* @dlg_count_columns to i32 (i8*, ...)*)(i8* %10)
store i32 %11, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
%12 = load i8*, i8** %4
store i8 0, i8* %12
%13 = load i32, i32* %7
%14 = load i32, i32* %5
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %33
16: ; preds = %3
%17 = load i32, i32* %5
%18 = load i32, i32* %7
%19 = sub nsw i32 %17, %18
%20 = sdiv i32 %19, 2
store i32 %20, i32* %8
%21 = load i32, i32* %5
%22 = load i32, i32* %7
%23 = sub nsw i32 %21, %22
%24 = load i32, i32* %8
%25 = sub nsw i32 %23, %24
store i32 %25, i32* %9
%26 = load i32, i32* %8
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %16
%29 = load i8*, i8** %4
%30 = load i32, i32* %8
%31 = call i32 (i8*, i8*, ...) @sprintf(i8* %29, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %32
32: ; preds = %28, %16
br label %33
33: ; preds = %32, %3
%34 = load i8*, i8** %4
%35 = load i8*, i8** %6
%36 = call i8* @strcat(i8* %34, i8* %35)
%37 = load i32, i32* %9
%38 = icmp sgt i32 %37, 0
br i1 %38, label %39, label %46
39: ; preds = %33
%40 = load i8*, i8** %4
%41 = load i8*, i8** %4
%42 = call i64 @strlen(i8* %41)
%43 = getelementptr inbounds i8, i8* %40, i64 %42
%44 = load i32, i32* %9
%45 = call i32 (i8*, i8*, ...) @sprintf(i8* %43, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %44, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %46
46: ; preds = %39, %33
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"%*s\00"
@.str.1 = [2 x i8] c" \00"
define void @center_label(i8* %0, i32 %1, i8* %2) {
%4 = tail call i32 (i8*, ...) bitcast (i32 (...)* @dlg_count_columns to i32 (i8*, ...)*)(i8* %2)
store i8 0, i8* %0
%5 = icmp slt i32 %4, %1
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = tail call i8* @strcat(i8* %0, i8* dereferenceable(1) %2)
br label %22
8: ; preds = %3
%9 = sub nsw i32 %1, %4
%10 = sdiv i32 %9, 2
%11 = sub nsw i32 %9, %10
%12 = icmp sgt i32 %9, 1
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = tail call i32 (i8*, i8*, ...) @sprintf(i8* %0, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %8, %13
%16 = tail call i8* @strcat(i8* %0, i8* dereferenceable(1) %2)
%17 = icmp sgt i32 %11, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = tail call i64 @strlen(i8* %0)
%20 = getelementptr inbounds i8, i8* %0, i64 %19
%21 = tail call i32 (i8*, i8*, ...) @sprintf(i8* %20, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %6, %18, %15
ret void
}