const char *fmt_word(int word) {
  enum { NUM_BUFS = 16, BUF_SIZE = 64 };
  static char buf[NUM_BUFS][BUF_SIZE];
  static int last_buf = 0;
  char bit_str[(2 * 8)];
  int i;
  last_buf = (last_buf + 1) % NUM_BUFS;
  for (i = 0; i < (2 * 8); i++)
    bit_str[i] = (word & (1ULL << ((2 * 8) - i - 1))) ? '1' : '0';
  snprintf(buf[last_buf], sizeof(*buf), "0b%.*s 0x%.*X", (2 * 8), bit_str,
           (2 * 8) / 4, word);
  for (i = 0; i < (2 * 8); i++) {
    if (word & (1 << i))
      sprintf(&buf[last_buf][strlen(buf[last_buf])], " %i", i);
    else
      sprintf(&buf[last_buf][strlen(buf[last_buf])], i < 10 ? "  " : "   ");
  }
  strcat(buf[last_buf], "\n");
  return buf[last_buf];
}


x86 assembly (clang, -O0):
<fmt_word>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    0x32ff(%rip),%eax
add    $0x1,%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    %edx,0x32ee(%rip)
movl   $0x0,-0x24(%rbp)
cmpl   $0x10,-0x24(%rbp)
jge    11ba <fmt_word+0x7a>
movslq -0x4(%rbp),%rdx
mov    $0x10,%eax
sub    -0x24(%rbp),%eax
sub    $0x1,%eax
mov    %eax,%eax
mov    %eax,%ecx
mov    $0x1,%eax
shl    %cl,%rax
and    %rax,%rdx
mov    $0x30,%eax
mov    $0x31,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
mov    %al,%cl
movslq -0x24(%rbp),%rax
mov    %cl,-0x20(%rbp,%rax,1)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1169 <fmt_word+0x29>
movslq 0x328f(%rip),%rax
lea    0x2e88(%rip),%rdi
shl    $0x6,%rax
add    %rax,%rdi
lea    -0x20(%rbp),%r8
mov    -0x4(%rbp),%eax
mov    $0x40,%esi
lea    0xe1e(%rip),%rdx
mov    $0x10,%ecx
mov    $0x4,%r9d
mov    %eax,(%rsp)
mov    $0x0,%al
call   1040 <snprintf@plt>
movl   $0x0,-0x24(%rbp)
cmpl   $0x10,-0x24(%rbp)
jge    12de <fmt_word+0x19e>
mov    -0x4(%rbp),%eax
mov    -0x24(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     1272 <fmt_word+0x132>
movslq 0x3227(%rip),%rcx
lea    0x2e20(%rip),%rax
shl    $0x6,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
movslq 0x320e(%rip),%rax
lea    0x2e07(%rip),%rdi
shl    $0x6,%rax
add    %rax,%rdi
call   1030 <strlen@plt>
mov    -0x30(%rbp),%rdi
add    %rax,%rdi
mov    -0x24(%rbp),%edx
lea    0xda8(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
jmp    12cb <fmt_word+0x18b>
movslq 0x31d7(%rip),%rcx
lea    0x2dd0(%rip),%rax
shl    $0x6,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
movslq 0x31be(%rip),%rax
lea    0x2db7(%rip),%rdi
shl    $0x6,%rax
add    %rax,%rdi
call   1030 <strlen@plt>
mov    -0x38(%rbp),%rdi
add    %rax,%rdi
mov    -0x24(%rbp),%ecx
lea    0xd5c(%rip),%rsi
lea    0xd56(%rip),%rax
cmp    $0xa,%ecx
cmovl  %rax,%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
jmp    12d0 <fmt_word+0x190>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11fe <fmt_word+0xbe>
movslq 0x316b(%rip),%rax
lea    0x2d64(%rip),%rdi
shl    $0x6,%rax
add    %rax,%rdi
lea    0xd1c(%rip),%rsi
call   1050 <strcat@plt>
movslq 0x314a(%rip),%rcx
lea    0x2d43(%rip),%rax
shl    $0x6,%rcx
add    %rcx,%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fmt_word>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edi,%r15d
mov    0x3309(%rip),%eax
lea    0x1(%rax),%ecx
lea    0x10(%rax),%edx
test   %ecx,%ecx
cmovns %ecx,%edx
and    $0xfffffff0,%edx
neg    %edx
add    %edx,%eax
add    $0x1,%eax
mov    %eax,0x32ee(%rip)
mov    %edi,%ecx
mov    %ecx,%edx
and    $0x8000,%edx
cmp    $0x1,%rdx
mov    $0x31,%dl
sbb    $0x0,%dl
mov    %dl,0x10(%rsp)
mov    %ecx,%edx
and    $0x4000,%edx
cmp    $0x1,%rdx
mov    $0x31,%dl
sbb    $0x0,%dl
mov    %dl,0x11(%rsp)
mov    %ecx,%edx
and    $0x2000,%edx
cmp    $0x1,%rdx
mov    $0x31,%dl
sbb    $0x0,%dl
mov    %dl,0x12(%rsp)
mov    %ecx,%edx
and    $0x1000,%edx
cmp    $0x1,%rdx
mov    $0x31,%dl
sbb    $0x0,%dl
mov    %dl,0x13(%rsp)
mov    %ecx,%edx
and    $0x800,%edx
cmp    $0x1,%rdx
mov    $0x31,%dl
sbb    $0x0,%dl
mov    %dl,0x14(%rsp)
mov    %ecx,%edx
and    $0x400,%edx
cmp    $0x1,%rdx
mov    $0x31,%dl
sbb    $0x0,%dl
mov    %dl,0x15(%rsp)
mov    %ecx,%edx
and    $0x200,%edx
cmp    $0x1,%rdx
mov    $0x31,%dl
sbb    $0x0,%dl
mov    %dl,0x16(%rsp)
mov    %ecx,%edx
and    $0x100,%edx
cmp    $0x1,%rdx
mov    $0x31,%dl
sbb    $0x0,%dl
mov    %dl,0x17(%rsp)
mov    %ecx,%edx
and    $0x80,%edx
cmp    $0x1,%rdx
mov    $0x31,%dl
sbb    $0x0,%dl
mov    %dl,0x18(%rsp)
mov    %ecx,%edx
and    $0x40,%edx
cmp    $0x1,%rdx
mov    $0x31,%dl
sbb    $0x0,%dl
mov    %dl,0x19(%rsp)
mov    %ecx,%edx
and    $0x20,%edx
cmp    $0x1,%rdx
mov    $0x31,%dl
sbb    $0x0,%dl
mov    %dl,0x1a(%rsp)
mov    %ecx,%edx
and    $0x10,%edx
cmp    $0x1,%rdx
mov    $0x31,%dl
sbb    $0x0,%dl
mov    %dl,0x1b(%rsp)
mov    %ecx,%edx
and    $0x8,%edx
cmp    $0x1,%rdx
mov    $0x31,%dl
sbb    $0x0,%dl
mov    $0x31,%bl
mov    %dl,0x1c(%rsp)
mov    %ecx,%edx
and    $0x4,%edx
cmp    $0x1,%rdx
mov    $0x31,%dl
sbb    $0x0,%dl
mov    %dl,0x1d(%rsp)
and    $0x2,%ecx
cmp    $0x1,%rcx
sbb    $0x0,%bl
mov    %bl,0x1e(%rsp)
mov    %r15d,%ecx
and    $0x1,%cl
or     $0x30,%cl
mov    %cl,0x1f(%rsp)
movslq %eax,%rdi
shl    $0x6,%rdi
lea    0x2daa(%rip),%r12
add    %r12,%rdi
mov    %r15d,(%rsp)
lea    0xd4c(%rip),%rdx
lea    0x10(%rsp),%r8
mov    $0x40,%esi
mov    $0x10,%ecx
mov    $0x4,%r9d
xor    %eax,%eax
call   1040 <snprintf@plt>
lea    0xd3b(%rip),%r13
lea    0xd30(%rip),%r14
xor    %ebp,%ebp
jmp    1313 <fmt_word+0x1e3>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmp    $0xa,%ebp
mov    %r13,%rsi
lea    0xd16(%rip),%rcx
cmovb  %rcx,%rsi
mov    %rax,%rdi
xor    %eax,%eax
call   1050 <sprintf@plt>
add    $0x1,%ebp
cmp    $0x10,%ebp
je     1343 <fmt_word+0x213>
movslq 0x3136(%rip),%rbx
shl    $0x6,%rbx
add    %r12,%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
add    %rbx,%rax
bt     %ebp,%r15d
jae    12f0 <fmt_word+0x1c0>
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <sprintf@plt>
jmp    130b <fmt_word+0x1db>
movslq 0x3106(%rip),%rax
shl    $0x6,%rax
add    %rax,%r12
mov    %r12,%rdi
call   1030 <strlen@plt>
movw   $0xa,(%rax,%r12,1)
mov    %r12,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@fmt_word.buf = internal global [16 x [64 x i8]] zeroinitializer
@fmt_word.last_buf = internal global i32 0
@.str = [14 x i8] c"0b%.*s 0x%.*X\00"
@.str.1 = [4 x i8] c" %i\00"
@.str.2 = [3 x i8] c" \00"
@.str.3 = [4 x i8] c" \00"
@.str.4 = [2 x i8] c"\0A\00"
define i8* @fmt_word(i32 %0) {
%2 = alloca i32
%3 = alloca [16 x i8]
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* @fmt_word.last_buf
%6 = add nsw i32 %5, 1
%7 = srem i32 %6, 16
store i32 %7, i32* @fmt_word.last_buf
store i32 0, i32* %4
br label %8
8: ; preds = %27, %1
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 16
br i1 %10, label %11, label %30
11: ; preds = %8
%12 = load i32, i32* %2
%13 = sext i32 %12 to i64
%14 = load i32, i32* %4
%15 = sub nsw i32 16, %14
%16 = sub nsw i32 %15, 1
%17 = zext i32 %16 to i64
%18 = shl i64 1, %17
%19 = and i64 %13, %18
%20 = icmp ne i64 %19, 0
%21 = zext i1 %20 to i64
%22 = select i1 %20, i32 49, i32 48
%23 = trunc i32 %22 to i8
%24 = load i32, i32* %4
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 %25
store i8 %23, i8* %26
br label %27
27: ; preds = %11
%28 = load i32, i32* %4
%29 = add nsw i32 %28, 1
store i32 %29, i32* %4
br label %8
30: ; preds = %8
%31 = load i32, i32* @fmt_word.last_buf
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [16 x [64 x i8]], [16 x [64 x i8]]* @fmt_word.buf, i64 0, i64 %32
%34 = getelementptr inbounds [64 x i8], [64 x i8]* %33, i64 0, i64 0
%35 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%36 = load i32, i32* %2
%37 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %34, i64 64, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 16, i8* %35, i32 4, i32 %36)
store i32 0, i32* %4
br label %38
38: ; preds = %75, %30
%39 = load i32, i32* %4
%40 = icmp slt i32 %39, 16
br i1 %40, label %41, label %78
41: ; preds = %38
%42 = load i32, i32* %2
%43 = load i32, i32* %4
%44 = shl i32 1, %43
%45 = and i32 %42, %44
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %59
47: ; preds = %41
%48 = load i32, i32* @fmt_word.last_buf
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [16 x [64 x i8]], [16 x [64 x i8]]* @fmt_word.buf, i64 0, i64 %49
%51 = load i32, i32* @fmt_word.last_buf
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [16 x [64 x i8]], [16 x [64 x i8]]* @fmt_word.buf, i64 0, i64 %52
%54 = getelementptr inbounds [64 x i8], [64 x i8]* %53, i64 0, i64 0
%55 = call i64 @strlen(i8* %54)
%56 = getelementptr inbounds [64 x i8], [64 x i8]* %50, i64 0, i64 %55
%57 = load i32, i32* %4
%58 = call i32 (i8*, i8*, ...) @sprintf(i8* %56, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %57)
br label %74
59: ; preds = %41
%60 = load i32, i32* @fmt_word.last_buf
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds [16 x [64 x i8]], [16 x [64 x i8]]* @fmt_word.buf, i64 0, i64 %61
%63 = load i32, i32* @fmt_word.last_buf
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds [16 x [64 x i8]], [16 x [64 x i8]]* @fmt_word.buf, i64 0, i64 %64
%66 = getelementptr inbounds [64 x i8], [64 x i8]* %65, i64 0, i64 0
%67 = call i64 @strlen(i8* %66)
%68 = getelementptr inbounds [64 x i8], [64 x i8]* %62, i64 0, i64 %67
%69 = load i32, i32* %4
%70 = icmp slt i32 %69, 10
%71 = zext i1 %70 to i64
%72 = select i1 %70, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0)
%73 = call i32 (i8*, i8*, ...) @sprintf(i8* %68, i8* %72)
br label %74
74: ; preds = %59, %47
br label %75
75: ; preds = %74
%76 = load i32, i32* %4
%77 = add nsw i32 %76, 1
store i32 %77, i32* %4
br label %38
78: ; preds = %38
%79 = load i32, i32* @fmt_word.last_buf
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds [16 x [64 x i8]], [16 x [64 x i8]]* @fmt_word.buf, i64 0, i64 %80
%82 = getelementptr inbounds [64 x i8], [64 x i8]* %81, i64 0, i64 0
%83 = call i8* @strcat(i8* %82, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%84 = load i32, i32* @fmt_word.last_buf
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds [16 x [64 x i8]], [16 x [64 x i8]]* @fmt_word.buf, i64 0, i64 %85
%87 = getelementptr inbounds [64 x i8], [64 x i8]* %86, i64 0, i64 0
ret i8* %87
}

LLVM IR (-O2):
@fmt_word.buf = internal global [16 x [64 x i8]] zeroinitializer
@fmt_word.last_buf = internal unnamed_addr global i32 0
@.str = [14 x i8] c"0b%.*s 0x%.*X\00"
@.str.1 = [4 x i8] c" %i\00"
@.str.2 = [3 x i8] c" \00"
@.str.3 = [4 x i8] c" \00"
define i8* @fmt_word(i32 %0) {
%2 = alloca [16 x i8]
%3 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @fmt_word.last_buf
%5 = add nsw i32 %4, 1
%6 = srem i32 %5, 16
store i32 %6, i32* @fmt_word.last_buf
%7 = zext i32 %0 to i64
%8 = and i64 %7, 32768
%9 = icmp eq i64 %8, 0
%10 = select i1 %9, i8 48, i8 49
store i8 %10, i8* %3
%11 = and i64 %7, 16384
%12 = icmp eq i64 %11, 0
%13 = select i1 %12, i8 48, i8 49
%14 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 1
store i8 %13, i8* %14
%15 = and i64 %7, 8192
%16 = icmp eq i64 %15, 0
%17 = select i1 %16, i8 48, i8 49
%18 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 2
store i8 %17, i8* %18
%19 = and i64 %7, 4096
%20 = icmp eq i64 %19, 0
%21 = select i1 %20, i8 48, i8 49
%22 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 3
store i8 %21, i8* %22
%23 = and i64 %7, 2048
%24 = icmp eq i64 %23, 0
%25 = select i1 %24, i8 48, i8 49
%26 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 4
store i8 %25, i8* %26
%27 = and i64 %7, 1024
%28 = icmp eq i64 %27, 0
%29 = select i1 %28, i8 48, i8 49
%30 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 5
store i8 %29, i8* %30
%31 = and i64 %7, 512
%32 = icmp eq i64 %31, 0
%33 = select i1 %32, i8 48, i8 49
%34 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 6
store i8 %33, i8* %34
%35 = and i64 %7, 256
%36 = icmp eq i64 %35, 0
%37 = select i1 %36, i8 48, i8 49
%38 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 7
store i8 %37, i8* %38
%39 = and i64 %7, 128
%40 = icmp eq i64 %39, 0
%41 = select i1 %40, i8 48, i8 49
%42 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 8
store i8 %41, i8* %42
%43 = and i64 %7, 64
%44 = icmp eq i64 %43, 0
%45 = select i1 %44, i8 48, i8 49
%46 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 9
store i8 %45, i8* %46
%47 = and i64 %7, 32
%48 = icmp eq i64 %47, 0
%49 = select i1 %48, i8 48, i8 49
%50 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 10
store i8 %49, i8* %50
%51 = and i64 %7, 16
%52 = icmp eq i64 %51, 0
%53 = select i1 %52, i8 48, i8 49
%54 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 11
store i8 %53, i8* %54
%55 = and i64 %7, 8
%56 = icmp eq i64 %55, 0
%57 = select i1 %56, i8 48, i8 49
%58 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 12
store i8 %57, i8* %58
%59 = and i64 %7, 4
%60 = icmp eq i64 %59, 0
%61 = select i1 %60, i8 48, i8 49
%62 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 13
store i8 %61, i8* %62
%63 = and i64 %7, 2
%64 = icmp eq i64 %63, 0
%65 = select i1 %64, i8 48, i8 49
%66 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 14
store i8 %65, i8* %66
%67 = and i64 %7, 1
%68 = icmp eq i64 %67, 0
%69 = select i1 %68, i8 48, i8 49
%70 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 15
store i8 %69, i8* %70
%71 = sext i32 %6 to i64
%72 = getelementptr inbounds [16 x [64 x i8]], [16 x [64 x i8]]* @fmt_word.buf, i64 0, i64 %71, i64 0
%73 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %72, i64 64, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 16, i8* %3, i32 4, i32 %0)
br label %74
74: ; preds = %1, %90
%75 = phi i32 [ 0, %1 ], [ %91, %90 ]
%76 = shl nuw nsw i32 1, %75
%77 = and i32 %76, %0
%78 = icmp eq i32 %77, 0
%79 = load i32, i32* @fmt_word.last_buf
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds [16 x [64 x i8]], [16 x [64 x i8]]* @fmt_word.buf, i64 0, i64 %80, i64 0
%82 = call i64 @strlen(i8* %81)
%83 = getelementptr inbounds [16 x [64 x i8]], [16 x [64 x i8]]* @fmt_word.buf, i64 0, i64 %80, i64 %82
br i1 %78, label %86, label %84
84: ; preds = %74
%85 = call i32 (i8*, i8*, ...) @sprintf(i8* %83, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %75)
br label %90
86: ; preds = %74
%87 = icmp ult i32 %75, 10
%88 = select i1 %87, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0)
%89 = call i32 (i8*, i8*, ...) @sprintf(i8* %83, i8* dereferenceable(1) %88)
br label %90
90: ; preds = %84, %86
%91 = add nuw nsw i32 %75, 1
%92 = icmp eq i32 %91, 16
br i1 %92, label %93, label %74
93: ; preds = %90
%94 = load i32, i32* @fmt_word.last_buf
%95 = sext i32 %94 to i64
%96 = getelementptr inbounds [16 x [64 x i8]], [16 x [64 x i8]]* @fmt_word.buf, i64 0, i64 %95, i64 0
%97 = call i64 @strlen(i8* %96)
%98 = getelementptr [16 x [64 x i8]], [16 x [64 x i8]]* @fmt_word.buf, i64 0, i64 %95, i64 %97
%99 = bitcast i8* %98 to i16*
store i16 10, i16* %99
ret i8* %96
}