char *Format(int n, int w) {
  static char buffer[256];
  static int bufptr;
  char *buf;
  if (bufptr >= sizeof(buffer) - w)
    bufptr = 0;
  buf = buffer + bufptr;
  if (n == 0)
    sprintf(buf, "%*s", w, ".");
  else
    sprintf(buf, "%*d", w, n);
  bufptr += w + 1;
  return buf;
}


x86 assembly (clang, -O0):
<Format>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movslq 0x301b(%rip),%rax
movslq -0x8(%rbp),%rdx
mov    $0x100,%ecx
sub    %rdx,%rcx
cmp    %rcx,%rax
jb     1144 <Format+0x34>
movl   $0x0,0x2ffc(%rip)
movslq 0x2ff5(%rip),%rcx
lea    0x2eee(%rip),%rax
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    1184 <Format+0x74>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe8f(%rip),%rsi
lea    0xe8c(%rip),%rcx
mov    $0x0,%al
call   1030 <sprintf@plt>
jmp    119c <Format+0x8c>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%edx
mov    -0x4(%rbp),%ecx
lea    0xe71(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
add    0x2f98(%rip),%eax
mov    %eax,0x2f92(%rip)
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Format>:
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%ecx
movslq 0x3020(%rip),%rax
movslq %esi,%rdx
mov    $0x100,%esi
sub    %rdx,%rsi
cmp    %rax,%rsi
ja     113c <Format+0x2c>
movl   $0x0,0x3006(%rip)
xor    %eax,%eax
cltq
lea    0x2efb(%rip),%rbx
add    %rax,%rbx
test   %ecx,%ecx
je     1162 <Format+0x52>
lea    0xeb3(%rip),%rsi
mov    %rbx,%rdi
mov    %r14d,%edx
xor    %eax,%eax
call   1030 <sprintf@plt>
jmp    117d <Format+0x6d>
lea    0xe97(%rip),%rsi
lea    0xe94(%rip),%rcx
mov    %rbx,%rdi
mov    %r14d,%edx
xor    %eax,%eax
call   1030 <sprintf@plt>
mov    0x2fbd(%rip),%eax
add    %r14d,%eax
add    $0x1,%eax
mov    %eax,0x2fb1(%rip)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@Format.buffer = internal global [256 x i8] zeroinitializer
@Format.bufptr = internal global i32 0
@.str = [4 x i8] c"%*s\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [4 x i8] c"%*d\00"
define i8* @Format(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i32 %0, i32* %3
store i32 %1, i32* %4
%6 = load i32, i32* @Format.bufptr
%7 = sext i32 %6 to i64
%8 = load i32, i32* %4
%9 = sext i32 %8 to i64
%10 = sub i64 256, %9
%11 = icmp uge i64 %7, %10
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 0, i32* @Format.bufptr
br label %13
13: ; preds = %12, %2
%14 = load i32, i32* @Format.bufptr
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @Format.buffer, i64 0, i64 0), i64 %15
store i8* %16, i8** %5
%17 = load i32, i32* %3
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %13
%20 = load i8*, i8** %5
%21 = load i32, i32* %4
%22 = call i32 (i8*, i8*, ...) @sprintf(i8* %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %28
23: ; preds = %13
%24 = load i8*, i8** %5
%25 = load i32, i32* %4
%26 = load i32, i32* %3
%27 = call i32 (i8*, i8*, ...) @sprintf(i8* %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %25, i32 %26)
br label %28
28: ; preds = %23, %19
%29 = load i32, i32* %4
%30 = add nsw i32 %29, 1
%31 = load i32, i32* @Format.bufptr
%32 = add nsw i32 %31, %30
store i32 %32, i32* @Format.bufptr
%33 = load i8*, i8** %5
ret i8* %33
}

LLVM IR (-O2):
@Format.buffer = internal global [256 x i8] zeroinitializer
@Format.bufptr = internal unnamed_addr global i32 0
@.str = [4 x i8] c"%*s\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [4 x i8] c"%*d\00"
define i8* @Format(i32 %0, i32 %1) {
%3 = load i32, i32* @Format.bufptr
%4 = sext i32 %3 to i64
%5 = sext i32 %1 to i64
%6 = sub nsw i64 256, %5
%7 = icmp ugt i64 %6, %4
br i1 %7, label %9, label %8
8: ; preds = %2
store i32 0, i32* @Format.bufptr
br label %9
9: ; preds = %8, %2
%10 = phi i32 [ 0, %8 ], [ %3, %2 ]
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds [256 x i8], [256 x i8]* @Format.buffer, i64 0, i64 %11
%13 = icmp eq i32 %0, 0
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = tail call i32 (i8*, i8*, ...) @sprintf(i8* %12, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %18
16: ; preds = %9
%17 = tail call i32 (i8*, i8*, ...) @sprintf(i8* %12, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %1, i32 %0)
br label %18
18: ; preds = %16, %14
%19 = add nsw i32 %1, 1
%20 = load i32, i32* @Format.bufptr
%21 = add nsw i32 %19, %20
store i32 %21, i32* @Format.bufptr
ret i8* %12
}