void printf(char *fmt, ...) {
  __builtin_va_list vl;
  __builtin_va_start(vl, fmt);
  char c;
  char buf[32];
  while ((c = *(fmt++)) != 0) {
    if (c == '%') {
      c = *(fmt++);
      switch (c) {
      case 's':
        vga_write(__builtin_va_arg(vl, char *));
        break;
      case 'd':
        itoa(__builtin_va_arg(vl, int), buf);
        vga_write(buf);
        break;
      }
    } else {
      vga_putchar(c);
    }
  }
  vga_updatepos();
  __builtin_va_end(vl);
}


x86 assembly (clang, -O0):
<printf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
test   %al,%al
je     1182 <printf+0x42>
movaps %xmm0,-0xd0(%rbp)
movaps %xmm1,-0xc0(%rbp)
movaps %xmm2,-0xb0(%rbp)
movaps %xmm3,-0xa0(%rbp)
movaps %xmm4,-0x90(%rbp)
movaps %xmm5,-0x80(%rbp)
movaps %xmm6,-0x70(%rbp)
movaps %xmm7,-0x60(%rbp)
mov    %r9,-0xd8(%rbp)
mov    %r8,-0xe0(%rbp)
mov    %rcx,-0xe8(%rbp)
mov    %rdx,-0xf0(%rbp)
mov    %rsi,-0xf8(%rbp)
mov    %rdi,-0x8(%rbp)
lea    -0x20(%rbp),%rax
lea    -0x100(%rbp),%rcx
mov    %rcx,0x10(%rax)
lea    0x10(%rbp),%rcx
mov    %rcx,0x8(%rax)
movl   $0x30,0x4(%rax)
movl   $0x8,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
mov    (%rax),%al
mov    %al,-0x21(%rbp)
movsbl %al,%eax
cmp    $0x0,%eax
je     1342 <printf+0x202>
movsbl -0x21(%rbp),%eax
cmp    $0x25,%eax
jne    1332 <printf+0x1f2>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
mov    (%rax),%al
mov    %al,-0x21(%rbp)
movsbl -0x21(%rbp),%eax
mov    %eax,-0x104(%rbp)
sub    $0x64,%eax
je     12af <printf+0x16f>
jmp    1226 <printf+0xe6>
mov    -0x104(%rbp),%eax
sub    $0x73,%eax
jne    132d <printf+0x1ed>
jmp    123a <printf+0xfa>
lea    -0x20(%rbp),%rax
mov    %rax,-0x110(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x108(%rbp)
cmp    $0x28,%eax
ja     127c <printf+0x13c>
mov    -0x110(%rbp),%rcx
mov    -0x108(%rbp),%edx
movslq %edx,%rax
add    0x10(%rcx),%rax
add    $0x8,%edx
mov    %edx,(%rcx)
mov    %rax,-0x118(%rbp)
jmp    1299 <printf+0x159>
mov    -0x110(%rbp),%rcx
mov    0x8(%rcx),%rdx
mov    %rdx,%rax
add    $0x8,%rdx
mov    %rdx,0x8(%rcx)
mov    %rax,-0x118(%rbp)
mov    -0x118(%rbp),%rax
mov    (%rax),%rdi
mov    $0x0,%al
call   1040 <vga_write@plt>
jmp    132d <printf+0x1ed>
lea    -0x20(%rbp),%rax
mov    %rax,-0x128(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x11c(%rbp)
cmp    $0x28,%eax
ja     12f1 <printf+0x1b1>
mov    -0x128(%rbp),%rcx
mov    -0x11c(%rbp),%edx
movslq %edx,%rax
add    0x10(%rcx),%rax
add    $0x8,%edx
mov    %edx,(%rcx)
mov    %rax,-0x130(%rbp)
jmp    130e <printf+0x1ce>
mov    -0x128(%rbp),%rcx
mov    0x8(%rcx),%rdx
mov    %rdx,%rax
add    $0x8,%rdx
mov    %rdx,0x8(%rcx)
mov    %rax,-0x130(%rbp)
mov    -0x130(%rbp),%rax
mov    (%rax),%edi
lea    -0x50(%rbp),%rsi
mov    $0x0,%al
call   1050 <itoa@plt>
lea    -0x50(%rbp),%rdi
mov    $0x0,%al
call   1040 <vga_write@plt>
jmp    133d <printf+0x1fd>
movsbl -0x21(%rbp),%edi
mov    $0x0,%al
call   1030 <vga_putchar@plt>
jmp    11cd <printf+0x8d>
xor    %eax,%eax
call   1060 <vga_updatepos@plt>
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<printf>:
push   %r14
push   %rbx
sub    $0xf8,%rsp
mov    %rdi,%rbx
mov    %rsi,0x28(%rsp)
mov    %rdx,0x30(%rsp)
mov    %rcx,0x38(%rsp)
mov    %r8,0x40(%rsp)
mov    %r9,0x48(%rsp)
test   %al,%al
je     11a1 <printf+0x61>
movaps %xmm0,0x50(%rsp)
movaps %xmm1,0x60(%rsp)
movaps %xmm2,0x70(%rsp)
movaps %xmm3,0x80(%rsp)
movaps %xmm4,0x90(%rsp)
movaps %xmm5,0xa0(%rsp)
movaps %xmm6,0xb0(%rsp)
movaps %xmm7,0xc0(%rsp)
lea    0x20(%rsp),%rax
mov    %rax,0x10(%rsp)
lea    0x110(%rsp),%rax
mov    %rax,0x8(%rsp)
movabs $0x3000000008,%rax
mov    %rax,(%rsp)
lea    0xd0(%rsp),%r14
jmp    11e8 <printf+0xa8>
mov    0x8(%rsp),%rax
lea    0x8(%rax),%rcx
mov    %rcx,0x8(%rsp)
mov    (%rax),%rdi
xor    %eax,%eax
call   1040 <vga_write@plt>
movsbl (%rbx),%edi
cmp    $0x25,%edi
je     1210 <printf+0xd0>
test   %dil,%dil
je     1276 <printf+0x136>
add    $0x1,%rbx
xor    %eax,%eax
call   1030 <vga_putchar@plt>
jmp    11e8 <printf+0xa8>
cs nopw 0x0(%rax,%rax,1)
movsbl 0x1(%rbx),%eax
add    $0x2,%rbx
cmp    $0x64,%eax
je     123b <printf+0xfb>
cmp    $0x73,%eax
jne    11e8 <printf+0xa8>
mov    (%rsp),%ecx
cmp    $0x28,%rcx
ja     11d0 <printf+0x90>
mov    %rcx,%rax
add    0x10(%rsp),%rax
add    $0x8,%ecx
mov    %ecx,(%rsp)
jmp    11de <printf+0x9e>
mov    (%rsp),%ecx
cmp    $0x28,%rcx
ja     1254 <printf+0x114>
mov    %rcx,%rax
add    0x10(%rsp),%rax
add    $0x8,%ecx
mov    %ecx,(%rsp)
jmp    1262 <printf+0x122>
mov    0x8(%rsp),%rax
lea    0x8(%rax),%rcx
mov    %rcx,0x8(%rsp)
mov    (%rax),%edi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <itoa@plt>
mov    %r14,%rdi
jmp    11e1 <printf+0xa1>
xor    %eax,%eax
call   1060 <vga_updatepos@plt>
add    $0xf8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.__va_list_tag = type { i32, i32, i8*, i8* }
define void @printf(i8* %0, ...) {
%2 = alloca i8*
%3 = alloca [1 x %struct.__va_list_tag]
%4 = alloca i8
%5 = alloca [32 x i8]
store i8* %0, i8** %2
%6 = getelementptr inbounds [1 x %struct.__va_list_tag], [1 x %struct.__va_list_tag]* %3, i64 0, i64 0
%7 = bitcast %struct.__va_list_tag* %6 to i8*
br label %8
8: ; preds = %72, %1
%9 = load i8*, i8** %2
%10 = getelementptr inbounds i8, i8* %9, i32 1
store i8* %10, i8** %2
%11 = load i8, i8* %9
store i8 %11, i8* %4
%12 = sext i8 %11 to i32
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %73
14: ; preds = %8
%15 = load i8, i8* %4
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 37
br i1 %17, label %18, label %68
18: ; preds = %14
%19 = load i8*, i8** %2
%20 = getelementptr inbounds i8, i8* %19, i32 1
store i8* %20, i8** %2
%21 = load i8, i8* %19
store i8 %21, i8* %4
%22 = load i8, i8* %4
%23 = sext i8 %22 to i32
switch i32 %23, label %67 [
i32 115, label %24
i32 100, label %44
]
24: ; preds = %18
%25 = getelementptr inbounds [1 x %struct.__va_list_tag], [1 x %struct.__va_list_tag]* %3, i64 0, i64 0
%26 = getelementptr inbounds %struct.__va_list_tag, %struct.__va_list_tag* %25, i32 0, i32 0
%27 = load i32, i32* %26
%28 = icmp ule i32 %27, 40
br i1 %28, label %29, label %35
29: ; preds = %24
%30 = getelementptr inbounds %struct.__va_list_tag, %struct.__va_list_tag* %25, i32 0, i32 3
%31 = load i8*, i8** %30
%32 = getelementptr i8, i8* %31, i32 %27
%33 = bitcast i8* %32 to i8**
%34 = add i32 %27, 8
store i32 %34, i32* %26
br label %40
35: ; preds = %24
%36 = getelementptr inbounds %struct.__va_list_tag, %struct.__va_list_tag* %25, i32 0, i32 2
%37 = load i8*, i8** %36
%38 = bitcast i8* %37 to i8**
%39 = getelementptr i8, i8* %37, i32 8
store i8* %39, i8** %36
br label %40
40: ; preds = %35, %29
%41 = phi i8** [ %33, %29 ], [ %38, %35 ]
%42 = load i8*, i8** %41
%43 = call i32 (i8*, ...) bitcast (i32 (...)* @vga_write to i32 (i8*, ...)*)(i8* %42)
br label %67
44: ; preds = %18
%45 = getelementptr inbounds [1 x %struct.__va_list_tag], [1 x %struct.__va_list_tag]* %3, i64 0, i64 0
%46 = getelementptr inbounds %struct.__va_list_tag, %struct.__va_list_tag* %45, i32 0, i32 0
%47 = load i32, i32* %46
%48 = icmp ule i32 %47, 40
br i1 %48, label %49, label %55
49: ; preds = %44
%50 = getelementptr inbounds %struct.__va_list_tag, %struct.__va_list_tag* %45, i32 0, i32 3
%51 = load i8*, i8** %50
%52 = getelementptr i8, i8* %51, i32 %47
%53 = bitcast i8* %52 to i32*
%54 = add i32 %47, 8
store i32 %54, i32* %46
br label %60
55: ; preds = %44
%56 = getelementptr inbounds %struct.__va_list_tag, %struct.__va_list_tag* %45, i32 0, i32 2
%57 = load i8*, i8** %56
%58 = bitcast i8* %57 to i32*
%59 = getelementptr i8, i8* %57, i32 8
store i8* %59, i8** %56
br label %60
60: ; preds = %55, %49
%61 = phi i32* [ %53, %49 ], [ %58, %55 ]
%62 = load i32, i32* %61
%63 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%64 = call i32 (i32, i8*, ...) bitcast (i32 (...)* @itoa to i32 (i32, i8*, ...)*)(i32 %62, i8* %63)
%65 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%66 = call i32 (i8*, ...) bitcast (i32 (...)* @vga_write to i32 (i8*, ...)*)(i8* %65)
br label %67
67: ; preds = %18, %60, %40
br label %72
68: ; preds = %14
%69 = load i8, i8* %4
%70 = sext i8 %69 to i32
%71 = call i32 (i32, ...) bitcast (i32 (...)* @vga_putchar to i32 (i32, ...)*)(i32 %70)
br label %72
72: ; preds = %68, %67
br label %8
73: ; preds = %8
%74 = call i32 (...) @vga_updatepos()
%75 = getelementptr inbounds [1 x %struct.__va_list_tag], [1 x %struct.__va_list_tag]* %3, i64 0, i64 0
%76 = bitcast %struct.__va_list_tag* %75 to i8*
ret void
}

LLVM IR (-O2):
%struct.__va_list_tag = type { i32, i32, i8*, i8* }
define void @printf(i8* nocapture %0, ...) {
%2 = alloca [1 x %struct.__va_list_tag]
%3 = alloca [32 x i8]
%4 = bitcast [1 x %struct.__va_list_tag]* %2 to i8*
%5 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [1 x %struct.__va_list_tag], [1 x %struct.__va_list_tag]* %2, i64 0, i64 0, i32 0
%7 = getelementptr inbounds [1 x %struct.__va_list_tag], [1 x %struct.__va_list_tag]* %2, i64 0, i64 0, i32 2
%8 = getelementptr inbounds [1 x %struct.__va_list_tag], [1 x %struct.__va_list_tag]* %2, i64 0, i64 0, i32 3
br label %9
9: ; preds = %33, %1
%10 = phi i8* [ %0, %1 ], [ %34, %33 ]
%11 = getelementptr inbounds i8, i8* %10, i64 1
%12 = load i8, i8* %10
switch i8 %12, label %52 [
i8 0, label %55
i8 37, label %13
]
13: ; preds = %9
%14 = getelementptr inbounds i8, i8* %10, i64 2
%15 = load i8, i8* %11
%16 = sext i8 %15 to i32
switch i32 %16, label %33 [
i32 115, label %17
i32 100, label %35
]
17: ; preds = %13
%18 = load i32, i32* %6
%19 = icmp ult i32 %18, 41
br i1 %19, label %20, label %25
20: ; preds = %17
%21 = load i8*, i8** %8
%22 = zext i32 %18 to i64
%23 = getelementptr i8, i8* %21, i64 %22
%24 = add nuw nsw i32 %18, 8
store i32 %24, i32* %6
br label %28
25: ; preds = %17
%26 = load i8*, i8** %7
%27 = getelementptr i8, i8* %26, i64 8
store i8* %27, i8** %7
br label %28
28: ; preds = %25, %20
%29 = phi i8* [ %23, %20 ], [ %26, %25 ]
%30 = bitcast i8* %29 to i8**
%31 = load i8*, i8** %30
%32 = call i32 (i8*, ...) bitcast (i32 (...)* @vga_write to i32 (i8*, ...)*)(i8* %31)
br label %33
33: ; preds = %28, %46, %13, %52
%34 = phi i8* [ %14, %13 ], [ %14, %46 ], [ %14, %28 ], [ %11, %52 ]
br label %9
35: ; preds = %13
%36 = load i32, i32* %6
%37 = icmp ult i32 %36, 41
br i1 %37, label %38, label %43
38: ; preds = %35
%39 = load i8*, i8** %8
%40 = zext i32 %36 to i64
%41 = getelementptr i8, i8* %39, i64 %40
%42 = add nuw nsw i32 %36, 8
store i32 %42, i32* %6
br label %46
43: ; preds = %35
%44 = load i8*, i8** %7
%45 = getelementptr i8, i8* %44, i64 8
store i8* %45, i8** %7
br label %46
46: ; preds = %43, %38
%47 = phi i8* [ %41, %38 ], [ %44, %43 ]
%48 = bitcast i8* %47 to i32*
%49 = load i32, i32* %48
%50 = call i32 (i32, i8*, ...) bitcast (i32 (...)* @itoa to i32 (i32, i8*, ...)*)(i32 %49, i8* %5)
%51 = call i32 (i8*, ...) bitcast (i32 (...)* @vga_write to i32 (i8*, ...)*)(i8* %5)
br label %33
52: ; preds = %9
%53 = sext i8 %12 to i32
%54 = call i32 (i32, ...) bitcast (i32 (...)* @vga_putchar to i32 (i32, ...)*)(i32 %53)
br label %33
55: ; preds = %9
%56 = call i32 (...) @vga_updatepos()
ret void
}