char *color_align(const char *argument, int size, int align) {
  int space = 0;
  int len = 0;
  static char buf[4096];
  len = color_strlen(argument);
  space = (size - len);
  if (align == 3 || len >= size)
    snprintf(buf, 4096, "%*.*s", len, len, argument);
  else if (align == 2)
    snprintf(buf, 4096, "%*s%s%*s", (space / 2), "", argument,
             ((space / 2) * 2) == space ? (space / 2) : ((space / 2) + 1), "");
  else if (align == 1)
    snprintf(buf, 4096, "%s%*s", argument, space, "");
  return buf;
}


x86 assembly (clang, -O0):
<color_align>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x0,%al
call   1030 <color_strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
sub    -0x18(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x3,-0x10(%rbp)
je     116d <color_align+0x4d>
mov    -0x18(%rbp),%eax
cmp    -0xc(%rbp),%eax
jl     1197 <color_align+0x77>
mov    -0x18(%rbp),%ecx
mov    -0x18(%rbp),%r8d
mov    -0x8(%rbp),%r9
lea    0x2ec1(%rip),%rdi
mov    $0x1000,%esi
lea    0xe75(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
jmp    126c <color_align+0x14c>
cmpl   $0x2,-0x10(%rbp)
jne    122f <color_align+0x10f>
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
shl    $0x1,%eax
cmp    -0x14(%rbp),%eax
jne    11e1 <color_align+0xc1>
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x28(%rbp)
jmp    11f2 <color_align+0xd2>
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%r9
mov    -0x24(%rbp),%ecx
mov    -0x28(%rbp),%r10d
lea    0x2e3c(%rip),%rdi
mov    $0x1000,%esi
lea    0xdf6(%rip),%rdx
lea    0xdf7(%rip),%rax
mov    %rax,%r8
mov    %r10d,(%rsp)
mov    %rax,0x8(%rsp)
mov    $0x0,%al
call   1040 <snprintf@plt>
jmp    1267 <color_align+0x147>
cmpl   $0x1,-0x10(%rbp)
jne    1262 <color_align+0x142>
mov    -0x8(%rbp),%rcx
mov    -0x14(%rbp),%r8d
lea    0x2df8(%rip),%rdi
mov    $0x1000,%esi
lea    0xdb5(%rip),%rdx
lea    0xdb3(%rip),%r9
mov    $0x0,%al
call   1040 <snprintf@plt>
jmp    1267 <color_align+0x147>
jmp    126c <color_align+0x14c>
lea    0x2dcd(%rip),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<color_align>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebx
mov    %esi,%ebp
mov    %rdi,%r14
xor    %eax,%eax
call   1030 <color_strlen@plt>
cmp    $0x3,%ebx
je     118f <color_align+0x6f>
cmp    %ebp,%eax
jge    118f <color_align+0x6f>
sub    %eax,%ebp
cmp    $0x1,%ebx
je     11b3 <color_align+0x93>
cmp    $0x2,%ebx
jne    11da <color_align+0xba>
mov    %ebp,%eax
shr    $0x1f,%eax
add    %ebp,%eax
mov    %eax,%ecx
sar    %ecx
and    $0xfffffffe,%eax
xor    %ebx,%ebx
cmp    %ebp,%eax
setne  %bl
add    %ecx,%ebx
lea    0xea5(%rip),%r8
lea    0x2ed0(%rip),%rdi
lea    0xe8f(%rip),%rdx
mov    $0x1000,%esi
mov    %r14,%r9
xor    %eax,%eax
push   %r8
push   %rbx
call   1040 <snprintf@plt>
add    $0x10,%rsp
jmp    11da <color_align+0xba>
lea    0x2eaa(%rip),%rdi
lea    0xe63(%rip),%rdx
mov    $0x1000,%esi
mov    %eax,%ecx
mov    %eax,%r8d
mov    %r14,%r9
xor    %eax,%eax
call   1040 <snprintf@plt>
jmp    11da <color_align+0xba>
lea    0x2e86(%rip),%rdi
lea    0xe48(%rip),%rdx
lea    0xe46(%rip),%r9
mov    $0x1000,%esi
mov    %r14,%rcx
mov    %ebp,%r8d
xor    %eax,%eax
call   1040 <snprintf@plt>
lea    0x2e5f(%rip),%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@color_align.buf = internal global [4096 x i8] zeroinitializer
@.str = [6 x i8] c"%*.*s\00"
@.str.1 = [9 x i8] c"%*s%s%*s\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [6 x i8] c"%s%*s\00"
define i8* @color_align(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
%9 = load i8*, i8** %4
%10 = call i32 (i8*, ...) bitcast (i32 (...)* @color_strlen to i32 (i8*, ...)*)(i8* %9)
store i32 %10, i32* %8
%11 = load i32, i32* %5
%12 = load i32, i32* %8
%13 = sub nsw i32 %11, %12
store i32 %13, i32* %7
%14 = load i32, i32* %6
%15 = icmp eq i32 %14, 3
br i1 %15, label %20, label %16
16: ; preds = %3
%17 = load i32, i32* %8
%18 = load i32, i32* %5
%19 = icmp sge i32 %17, %18
br i1 %19, label %20, label %25
20: ; preds = %16, %3
%21 = load i32, i32* %8
%22 = load i32, i32* %8
%23 = load i8*, i8** %4
%24 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @color_align.buf, i64 0, i64 0), i64 4096, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %21, i32 %22, i8* %23)
br label %56
25: ; preds = %16
%26 = load i32, i32* %6
%27 = icmp eq i32 %26, 2
br i1 %27, label %28, label %47
28: ; preds = %25
%29 = load i32, i32* %7
%30 = sdiv i32 %29, 2
%31 = load i8*, i8** %4
%32 = load i32, i32* %7
%33 = sdiv i32 %32, 2
%34 = mul nsw i32 %33, 2
%35 = load i32, i32* %7
%36 = icmp eq i32 %34, %35
br i1 %36, label %37, label %40
37: ; preds = %28
%38 = load i32, i32* %7
%39 = sdiv i32 %38, 2
br label %44
40: ; preds = %28
%41 = load i32, i32* %7
%42 = sdiv i32 %41, 2
%43 = add nsw i32 %42, 1
br label %44
44: ; preds = %40, %37
%45 = phi i32 [ %39, %37 ], [ %43, %40 ]
%46 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @color_align.buf, i64 0, i64 0), i64 4096, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %30, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), i8* %31, i32 %45, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
br label %55
47: ; preds = %25
%48 = load i32, i32* %6
%49 = icmp eq i32 %48, 1
br i1 %49, label %50, label %54
50: ; preds = %47
%51 = load i8*, i8** %4
%52 = load i32, i32* %7
%53 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @color_align.buf, i64 0, i64 0), i64 4096, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %51, i32 %52, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
br label %54
54: ; preds = %50, %47
br label %55
55: ; preds = %54, %44
br label %56
56: ; preds = %55, %20
ret i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @color_align.buf, i64 0, i64 0)
}

LLVM IR (-O2):
@color_align.buf = internal global [4096 x i8] zeroinitializer
@.str = [6 x i8] c"%*.*s\00"
@.str.1 = [9 x i8] c"%*s%s%*s\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [6 x i8] c"%s%*s\00"
define i8* @color_align(i8* %0, i32 %1, i32 %2) {
%4 = tail call i32 (i8*, ...) bitcast (i32 (...)* @color_strlen to i32 (i8*, ...)*)(i8* %0)
%5 = sub nsw i32 %1, %4
%6 = icmp ne i32 %2, 3
%7 = icmp slt i32 %4, %1
%8 = select i1 %6, i1 %7, i1 false
br i1 %8, label %11, label %9
9: ; preds = %3
%10 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* dereferenceable(1) getelementptr inbounds ([4096 x i8], [4096 x i8]* @color_align.buf, i64 0, i64 0), i64 4096, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %4, i32 %4, i8* %0)
br label %21
11: ; preds = %3
switch i32 %2, label %21 [
i32 2, label %12
i32 1, label %19
]
12: ; preds = %11
%13 = sdiv i32 %5, 2
%14 = shl nsw i32 %13, 1
%15 = icmp ne i32 %14, %5
%16 = zext i1 %15 to i32
%17 = add nsw i32 %13, %16
%18 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* dereferenceable(1) getelementptr inbounds ([4096 x i8], [4096 x i8]* @color_align.buf, i64 0, i64 0), i64 4096, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %13, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %17, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
br label %21
19: ; preds = %11
%20 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* dereferenceable(1) getelementptr inbounds ([4096 x i8], [4096 x i8]* @color_align.buf, i64 0, i64 0), i64 4096, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %0, i32 %5, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %11, %12, %19, %9
ret i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @color_align.buf, i64 0, i64 0)
}