#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
char *itoa(int, char *, int);
int /*<<< orphan*/ vga_putch(char);
int /*<<< orphan*/ vga_puts(char *);
void vga_printf(const char *format, ...) {
  char **args = (char **)&format;
  char buf[20];
  char *p = 0;
  char c;
  args++;
  while ((c = *format++) != '\0') {
    if (c != '%') {
      vga_putch(c);
    } else {
      c = *format++;
      switch (c) {
      case 'd':
      case 'u':
        p = itoa(*((int *)args++), buf, 10);
        break;
      case 'x':
        p = itoa(*((int *)args++), buf, 16);
        break;
      case 's':
        p = *args++;
        if (p == 0) {
          p = "(null)";
        }
        break;
      default:
        vga_putch(**args++);
        break;
      }
      if (p != 0) {
        vga_puts(p);
        p = 0;
      }
    }
  }
}


x86 assembly (clang, -O0):
<vga_printf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
mov    (%rax),%al
mov    %al,-0x39(%rbp)
movsbl %al,%eax
cmp    $0x0,%eax
je     1292 <vga_printf+0x162>
movsbl -0x39(%rbp),%eax
cmp    $0x25,%eax
je     1193 <vga_printf+0x63>
movsbl -0x39(%rbp),%edi
call   1030 <vga_putch@plt>
jmp    128d <vga_printf+0x15d>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
mov    (%rax),%al
mov    %al,-0x39(%rbp)
movsbl -0x39(%rbp),%eax
add    $0xffffff9c,%eax
mov    %eax,%ecx
mov    %rcx,-0x48(%rbp)
sub    $0x14,%eax
ja     1252 <vga_printf+0x122>
mov    -0x48(%rbp),%rax
lea    0xe38(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
mov    $0xa,%edx
call   1050 <itoa@plt>
mov    %rax,-0x38(%rbp)
jmp    126c <vga_printf+0x13c>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1050 <itoa@plt>
mov    %rax,-0x38(%rbp)
jmp    126c <vga_printf+0x13c>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    124d <vga_printf+0x11d>
lea    0xe0b(%rip),%rax
mov    %rax,-0x38(%rbp)
jmp    126c <vga_printf+0x13c>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%rax
movsbl (%rax),%edi
call   1030 <vga_putch@plt>
cmpq   $0x0,-0x38(%rbp)
je     1288 <vga_printf+0x158>
mov    -0x38(%rbp),%rdi
call   1040 <vga_puts@plt>
movq   $0x0,-0x38(%rbp)
jmp    128d <vga_printf+0x15d>
jmp    1158 <vga_printf+0x28>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vga_printf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
lea    0x10(%rsp),%r15
lea    0xeb3(%rip),%r12
lea    0x10(%rsp),%r14
lea    0xefb(%rip),%r13
add    $0x1,%rbx
mov    %rbx,%rbp
mov    %rbp,0x8(%rsp)
movzbl -0x1(%rbp),%eax
test   %al,%al
je     11fe <vga_printf+0xce>
cmp    $0x25,%al
je     1190 <vga_printf+0x60>
movsbl %al,%edi
call   1030 <vga_putch@plt>
add    $0x1,%rbp
jmp    1160 <vga_printf+0x30>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0x1(%rbp),%rbx
mov    %rbx,0x8(%rsp)
movsbl 0x0(%rbp),%eax
add    $0xffffff9c,%eax
cmp    $0x14,%eax
ja     11c3 <vga_printf+0x93>
mov    $0xa,%edx
movslq (%r12,%rax,4),%rax
add    %r12,%rax
jmp    *%rax
mov    (%r15),%rax
test   %rax,%rax
cmove  %r13,%rax
add    $0x8,%r15
jmp    11f1 <vga_printf+0xc1>
mov    (%r15),%rax
movsbl (%rax),%edi
call   1030 <vga_putch@plt>
add    $0x8,%r15
jmp    1159 <vga_printf+0x29>
mov    $0x10,%edx
mov    (%r15),%edi
mov    %r14,%rsi
call   1050 <itoa@plt>
add    $0x8,%r15
test   %rax,%rax
je     1159 <vga_printf+0x29>
mov    %rax,%rdi
call   1040 <vga_puts@plt>
jmp    1159 <vga_printf+0x29>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"(null)\00"
define void @vga_printf(i8* %0, ...) {
%2 = alloca i8*
%3 = alloca i8**
%4 = alloca [20 x i8]
%5 = alloca i8*
%6 = alloca i8
store i8* %0, i8** %2
store i8** %2, i8*** %3
store i8* null, i8** %5
%7 = load i8**, i8*** %3
%8 = getelementptr inbounds i8*, i8** %7, i32 1
store i8** %8, i8*** %3
br label %9
9: ; preds = %63, %1
%10 = load i8*, i8** %2
%11 = getelementptr inbounds i8, i8* %10, i32 1
store i8* %11, i8** %2
%12 = load i8, i8* %10
store i8 %12, i8* %6
%13 = sext i8 %12 to i32
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %64
15: ; preds = %9
%16 = load i8, i8* %6
%17 = sext i8 %16 to i32
%18 = icmp ne i32 %17, 37
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i8, i8* %6
%21 = call i32 @vga_putch(i8 signext %20)
br label %63
22: ; preds = %15
%23 = load i8*, i8** %2
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %2
%25 = load i8, i8* %23
store i8 %25, i8* %6
%26 = load i8, i8* %6
%27 = sext i8 %26 to i32
switch i32 %27, label %50 [
i32 100, label %28
i32 117, label %28
i32 120, label %35
i32 115, label %42
]
28: ; preds = %22, %22
%29 = load i8**, i8*** %3
%30 = getelementptr inbounds i8*, i8** %29, i32 1
store i8** %30, i8*** %3
%31 = bitcast i8** %29 to i32*
%32 = load i32, i32* %31
%33 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%34 = call i8* @itoa(i32 %32, i8* %33, i32 10)
store i8* %34, i8** %5
br label %56
35: ; preds = %22
%36 = load i8**, i8*** %3
%37 = getelementptr inbounds i8*, i8** %36, i32 1
store i8** %37, i8*** %3
%38 = bitcast i8** %36 to i32*
%39 = load i32, i32* %38
%40 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%41 = call i8* @itoa(i32 %39, i8* %40, i32 16)
store i8* %41, i8** %5
br label %56
42: ; preds = %22
%43 = load i8**, i8*** %3
%44 = getelementptr inbounds i8*, i8** %43, i32 1
store i8** %44, i8*** %3
%45 = load i8*, i8** %43
store i8* %45, i8** %5
%46 = load i8*, i8** %5
%47 = icmp eq i8* %46, null
br i1 %47, label %48, label %49
48: ; preds = %42
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8** %5
br label %49
49: ; preds = %48, %42
br label %56
50: ; preds = %22
%51 = load i8**, i8*** %3
%52 = getelementptr inbounds i8*, i8** %51, i32 1
store i8** %52, i8*** %3
%53 = load i8*, i8** %51
%54 = load i8, i8* %53
%55 = call i32 @vga_putch(i8 signext %54)
br label %56
56: ; preds = %50, %49, %35, %28
%57 = load i8*, i8** %5
%58 = icmp ne i8* %57, null
br i1 %58, label %59, label %62
59: ; preds = %56
%60 = load i8*, i8** %5
%61 = call i32 @vga_puts(i8* %60)
store i8* null, i8** %5
br label %62
62: ; preds = %59, %56
br label %63
63: ; preds = %62, %19
br label %9
64: ; preds = %9
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"(null)\00"
define void @vga_printf(i8* %0, ...) {
%2 = alloca i8*
%3 = alloca [20 x i8]
%4 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds i8*, i8** %2, i64 1
br label %6
6: ; preds = %41, %1
%7 = phi i8* [ %0, %1 ], [ %16, %41 ]
%8 = phi i8** [ %5, %1 ], [ %42, %41 ]
br label %9
9: ; preds = %6, %13
%10 = phi i8* [ %11, %13 ], [ %7, %6 ]
%11 = getelementptr inbounds i8, i8* %10, i64 1
store i8* %11, i8** %2
%12 = load i8, i8* %10
switch i8 %12, label %13 [
i8 0, label %43
i8 37, label %15
]
13: ; preds = %9
%14 = call i32 @vga_putch(i8 signext %12)
br label %9
15: ; preds = %9
%16 = getelementptr inbounds i8, i8* %10, i64 2
store i8* %16, i8** %2
%17 = load i8, i8* %11
%18 = sext i8 %17 to i32
switch i32 %18, label %25 [
i32 100, label %30
i32 117, label %30
i32 120, label %19
i32 115, label %20
]
19: ; preds = %15
br label %30
20: ; preds = %15
%21 = load i8*, i8** %8
%22 = icmp eq i8* %21, null
%23 = select i1 %22, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %21
%24 = getelementptr inbounds i8*, i8** %8, i64 1
br label %37
25: ; preds = %15
%26 = load i8*, i8** %8
%27 = load i8, i8* %26
%28 = call i32 @vga_putch(i8 signext %27)
%29 = getelementptr inbounds i8*, i8** %8, i64 1
br label %41
30: ; preds = %15, %15, %19
%31 = phi i32 [ 16, %19 ], [ 10, %15 ], [ 10, %15 ]
%32 = bitcast i8** %8 to i32*
%33 = load i32, i32* %32
%34 = call i8* @itoa(i32 %33, i8* %4, i32 %31)
%35 = getelementptr inbounds i8*, i8** %8, i64 1
%36 = icmp eq i8* %34, null
br i1 %36, label %41, label %37
37: ; preds = %20, %30
%38 = phi i8** [ %24, %20 ], [ %35, %30 ]
%39 = phi i8* [ %23, %20 ], [ %34, %30 ]
%40 = call i32 @vga_puts(i8* %39)
br label %41
41: ; preds = %37, %30, %25
%42 = phi i8** [ %29, %25 ], [ %35, %30 ], [ %38, %37 ]
br label %6
43: ; preds = %9
ret void
}