void center(char *str, int len, int separate) {
  len -= strlen(str);
  printf("%*s%s%*s", len / 2, "", str, len / 2 + len % 2, "");
  if (separate)
    printf("%*s", separate, "");
}


x86 assembly (clang, -O0):
<center>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
movslq -0xc(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%esi
mov    -0x8(%rbp),%rcx
mov    -0xc(%rbp),%eax
mov    $0x2,%edi
cltd
idiv   %edi
mov    %eax,%r8d
mov    -0xc(%rbp),%eax
mov    $0x2,%edi
cltd
idiv   %edi
add    %edx,%r8d
lea    0xe84(%rip),%rdi
lea    0xe85(%rip),%r9
mov    %r9,%rdx
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x10(%rbp)
je     11af <center+0x8f>
mov    -0x10(%rbp),%esi
lea    0xe64(%rip),%rdi
lea    0xe60(%rip),%rdx
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<center>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%rbx
call   1030 <strlen@plt>
sub    %eax,%ebp
mov    %ebp,%esi
shr    $0x1f,%esi
add    %ebp,%esi
mov    %esi,%eax
and    $0xfffffffe,%eax
sub    %eax,%ebp
sar    %esi
add    %esi,%ebp
lea    0xeb4(%rip),%rdi
lea    0xeb5(%rip),%rdx
mov    %rbx,%rcx
mov    %ebp,%r8d
mov    %rdx,%r9
xor    %eax,%eax
call   1040 <printf@plt>
test   %r14d,%r14d
je     1184 <center+0x64>
lea    0xe96(%rip),%rdi
lea    0xe92(%rip),%rdx
mov    %r14d,%esi
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <printf@plt>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"%*s%s%*s\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [4 x i8] c"%*s\00"
define void @center(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%7 = load i8*, i8** %4
%8 = call i64 @strlen(i8* %7)
%9 = load i32, i32* %5
%10 = sext i32 %9 to i64
%11 = sub i64 %10, %8
%12 = trunc i64 %11 to i32
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = sdiv i32 %13, 2
%15 = load i8*, i8** %4
%16 = load i32, i32* %5
%17 = sdiv i32 %16, 2
%18 = load i32, i32* %5
%19 = srem i32 %18, 2
%20 = add nsw i32 %17, %19
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %14, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8* %15, i32 %20, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* %6
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %3
%25 = load i32, i32* %6
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %25, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %24, %3
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"%*s%s%*s\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [4 x i8] c"%*s\00"
define void @center(i8* %0, i32 %1, i32 %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = trunc i64 %4 to i32
%6 = sub i32 %1, %5
%7 = sdiv i32 %6, 2
%8 = srem i32 %6, 2
%9 = add nsw i32 %7, %8
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %9, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32 %2, 0
br i1 %11, label %14, label %12
12: ; preds = %3
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %2, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %3
ret void
}