void kprintf(const char *str, ...) {
  __builtin_va_list list;
  __builtin_va_start(list, str);
  const char *c = str;
  while (*c != '\0') {
    switch (*c) {
    case '%': {
      char fn = *(++c);
      if (fn == 'X') {
        int k1 = __builtin_va_arg(list, int);
        char buffer[20];
        kprintf(itoa(k1, buffer, 16));
      } else if (fn == 'd') {
        int k1 = __builtin_va_arg(list, int);
        char buffer[20];
        kprintf(itoa(k1, buffer, 10));
      }
      break;
    }
    default:
      kputchar(*c);
      break;
    }
    ++c;
  }
}


x86 assembly (clang, -O0):
<kprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
test   %al,%al
je     1178 <kprintf+0x48>
movaps %xmm0,-0xf0(%rbp)
movaps %xmm1,-0xe0(%rbp)
movaps %xmm2,-0xd0(%rbp)
movaps %xmm3,-0xc0(%rbp)
movaps %xmm4,-0xb0(%rbp)
movaps %xmm5,-0xa0(%rbp)
movaps %xmm6,-0x90(%rbp)
movaps %xmm7,-0x80(%rbp)
mov    %r9,-0xf8(%rbp)
mov    %r8,-0x100(%rbp)
mov    %rcx,-0x108(%rbp)
mov    %rdx,-0x110(%rbp)
mov    %rsi,-0x118(%rbp)
mov    %rdi,-0x8(%rbp)
lea    -0x20(%rbp),%rax
lea    -0x120(%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,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     135b <kprintf+0x22b>
mov    -0x28(%rbp),%rax
mov    (%rax),%al
sub    $0x25,%al
jne    133c <kprintf+0x20c>
jmp    11ee <kprintf+0xbe>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
mov    0x1(%rax),%al
mov    %al,-0x29(%rbp)
movsbl -0x29(%rbp),%eax
cmp    $0x58,%eax
jne    129d <kprintf+0x16d>
lea    -0x20(%rbp),%rax
mov    %rax,-0x130(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x124(%rbp)
cmp    $0x28,%eax
ja     1252 <kprintf+0x122>
mov    -0x130(%rbp),%rcx
mov    -0x124(%rbp),%edx
movslq %edx,%rax
add    0x10(%rcx),%rax
add    $0x8,%edx
mov    %edx,(%rcx)
mov    %rax,-0x138(%rbp)
jmp    126f <kprintf+0x13f>
mov    -0x130(%rbp),%rcx
mov    0x8(%rcx),%rdx
mov    %rdx,%rax
add    $0x8,%rdx
mov    %rdx,0x8(%rcx)
mov    %rax,-0x138(%rbp)
mov    -0x138(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x10,%edx
mov    $0x0,%al
call   1050 <itoa@plt>
movslq %eax,%rdi
mov    $0x0,%al
call   1040 <kprintf@plt>
jmp    1337 <kprintf+0x207>
movsbl -0x29(%rbp),%eax
cmp    $0x64,%eax
jne    1332 <kprintf+0x202>
lea    -0x20(%rbp),%rax
mov    %rax,-0x148(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x13c(%rbp)
cmp    $0x28,%eax
ja     12ec <kprintf+0x1bc>
mov    -0x148(%rbp),%rcx
mov    -0x13c(%rbp),%edx
movslq %edx,%rax
add    0x10(%rcx),%rax
add    $0x8,%edx
mov    %edx,(%rcx)
mov    %rax,-0x150(%rbp)
jmp    1309 <kprintf+0x1d9>
mov    -0x148(%rbp),%rcx
mov    0x8(%rcx),%rdx
mov    %rdx,%rax
add    $0x8,%rdx
mov    %rdx,0x8(%rcx)
mov    %rax,-0x150(%rbp)
mov    -0x150(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0xa,%edx
mov    $0x0,%al
call   1050 <itoa@plt>
movslq %eax,%rdi
mov    $0x0,%al
call   1040 <kprintf@plt>
jmp    1337 <kprintf+0x207>
jmp    134a <kprintf+0x21a>
mov    -0x28(%rbp),%rax
movsbl (%rax),%edi
mov    $0x0,%al
call   1030 <kputchar@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11cb <kprintf+0x9b>
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kprintf>:
push   %r14
push   %rbx
sub    $0xf8,%rsp
mov    %rdi,%rbx
mov    %rsi,0x48(%rsp)
mov    %rdx,0x50(%rsp)
mov    %rcx,0x58(%rsp)
mov    %r8,0x60(%rsp)
mov    %r9,0x68(%rsp)
test   %al,%al
je     1197 <kprintf+0x67>
movaps %xmm0,0x70(%rsp)
movaps %xmm1,0x80(%rsp)
movaps %xmm2,0x90(%rsp)
movaps %xmm3,0xa0(%rsp)
movaps %xmm4,0xb0(%rsp)
movaps %xmm5,0xc0(%rsp)
movaps %xmm6,0xd0(%rsp)
movaps %xmm7,0xe0(%rsp)
lea    0x40(%rsp),%rax
mov    %rax,0x10(%rsp)
lea    0x110(%rsp),%rax
mov    %rax,0x8(%rsp)
movabs $0x3000000008,%rax
mov    %rax,(%rsp)
lea    0x20(%rsp),%r14
jmp    11d4 <kprintf+0xa4>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%rbx
movsbl (%rbx),%edi
cmp    $0x25,%edi
je     11f0 <kprintf+0xc0>
test   %dil,%dil
je     127e <kprintf+0x14e>
xor    %eax,%eax
call   1030 <kputchar@plt>
jmp    11d0 <kprintf+0xa0>
xchg   %ax,%ax
movzbl 0x1(%rbx),%eax
add    $0x1,%rbx
cmp    $0x64,%al
je     1219 <kprintf+0xe9>
cmp    $0x58,%al
jne    11d0 <kprintf+0xa0>
mov    (%rsp),%ecx
cmp    $0x28,%rcx
ja     1232 <kprintf+0x102>
mov    %rcx,%rax
add    0x10(%rsp),%rax
add    $0x8,%ecx
mov    %ecx,(%rsp)
jmp    1240 <kprintf+0x110>
mov    (%rsp),%ecx
cmp    $0x28,%rcx
ja     124c <kprintf+0x11c>
mov    %rcx,%rax
add    0x10(%rsp),%rax
add    $0x8,%ecx
mov    %ecx,(%rsp)
jmp    125a <kprintf+0x12a>
mov    0x8(%rsp),%rax
lea    0x8(%rax),%rcx
mov    %rcx,0x8(%rsp)
mov    (%rax),%edi
mov    %r14,%rsi
mov    $0x10,%edx
jmp    1264 <kprintf+0x134>
mov    0x8(%rsp),%rax
lea    0x8(%rax),%rcx
mov    %rcx,0x8(%rsp)
mov    (%rax),%edi
mov    %r14,%rsi
mov    $0xa,%edx
xor    %eax,%eax
call   1050 <itoa@plt>
movslq %eax,%rdi
xor    %eax,%eax
call   1040 <kprintf@plt>
add    $0x1,%rbx
jmp    11d4 <kprintf+0xa4>
add    $0xf8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
%struct.__va_list_tag = type { i32, i32, i8*, i8* }
define void @kprintf(i8* nocapture %0, ...) {
%2 = alloca [1 x %struct.__va_list_tag]
%3 = alloca [20 x i8]
%4 = alloca [20 x i8]
%5 = bitcast [1 x %struct.__va_list_tag]* %2 to i8*
%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
%9 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%10 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
br label %11
11: ; preds = %56, %1
%12 = phi i8* [ %0, %1 ], [ %58, %56 ]
%13 = load i8, i8* %12
switch i8 %13, label %53 [
i8 0, label %59
i8 37, label %14
]
14: ; preds = %11
%15 = getelementptr inbounds i8, i8* %12, i64 1
%16 = load i8, i8* %15
switch i8 %16, label %56 [
i8 88, label %17
i8 100, label %35
]
17: ; preds = %14
%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 i32*
%31 = load i32, i32* %30
%32 = call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @itoa to i32 (i32, i8*, i32, ...)*)(i32 %31, i8* %10, i32 16)
%33 = sext i32 %32 to i64
%34 = inttoptr i64 %33 to i8*
br label %56
35: ; preds = %14
%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*, i32, ...) bitcast (i32 (...)* @itoa to i32 (i32, i8*, i32, ...)*)(i32 %49, i8* %9, i32 10)
%51 = sext i32 %50 to i64
%52 = inttoptr i64 %51 to i8*
br label %56
53: ; preds = %11
%54 = sext i8 %13 to i32
%55 = call i32 (i32, ...) bitcast (i32 (...)* @kputchar to i32 (i32, ...)*)(i32 %54)
br label %56
56: ; preds = %28, %46, %14, %53
%57 = phi i8* [ %12, %53 ], [ %15, %14 ], [ %15, %46 ], [ %15, %28 ]
%58 = getelementptr inbounds i8, i8* %57, i64 1
br label %11
59: ; preds = %11
ret void
}