int putchar(int ch) {
  static unsigned char *vram = (unsigned char *)0xB8000;
  if (ch == '\n') {
    vram = ((unsigned char *)((unsigned int)vram + 160 -
                              ((unsigned int)vram % 160) + 64));
    return ch;
  }
  *vram++ = ch;
  *vram++ = 0x07;
  return ch;
}


x86 assembly (clang, -O0):
<putchar>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
cmpl   $0xa,-0x8(%rbp)
jne    114c <putchar+0x4c>
mov    0x2f08(%rip),%rax
add    $0xa0,%eax
mov    %eax,-0xc(%rbp)
mov    0x2ef9(%rip),%rax
mov    $0xa0,%ecx
xor    %edx,%edx
div    %ecx
mov    -0xc(%rbp),%eax
sub    %edx,%eax
add    $0x40,%eax
mov    %eax,%eax
mov    %rax,0x2edf(%rip)
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1186 <putchar+0x86>
mov    -0x8(%rbp),%eax
mov    %al,%cl
mov    0x2ec8(%rip),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,0x2eba(%rip)
mov    %cl,(%rax)
mov    0x2eb1(%rip),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,0x2ea3(%rip)
movb   $0x7,(%rax)
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<putchar>:
mov    %edi,%eax
cmp    $0xa,%edi
jne    112e <putchar+0x2e>
mov    0x2f13(%rip),%ecx
mov    $0xcccccccd,%edx
imul   %rcx,%rdx
shr    $0x27,%rdx
lea    (%rdx,%rdx,4),%ecx
shl    $0x5,%ecx
add    $0xe0,%ecx
mov    %rcx,0x2ef3(%rip)
ret
mov    0x2eeb(%rip),%rcx
mov    %al,(%rcx)
lea    0x2(%rcx),%rdx
mov    %rdx,0x2ede(%rip)
movb   $0x7,0x1(%rcx)
ret

LLVM IR (-O0):
@putchar.vram = internal global i8* inttoptr (i64 753664 to i8*)
define i32 @putchar(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i32, i32* %3
%5 = icmp eq i32 %4, 10
br i1 %5, label %6, label %18
6: ; preds = %1
%7 = load i8*, i8** @putchar.vram
%8 = ptrtoint i8* %7 to i32
%9 = add i32 %8, 160
%10 = load i8*, i8** @putchar.vram
%11 = ptrtoint i8* %10 to i32
%12 = urem i32 %11, 160
%13 = sub i32 %9, %12
%14 = add i32 %13, 64
%15 = zext i32 %14 to i64
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** @putchar.vram
%17 = load i32, i32* %3
store i32 %17, i32* %2
br label %26
18: ; preds = %1
%19 = load i32, i32* %3
%20 = trunc i32 %19 to i8
%21 = load i8*, i8** @putchar.vram
%22 = getelementptr inbounds i8, i8* %21, i32 1
store i8* %22, i8** @putchar.vram
store i8 %20, i8* %21
%23 = load i8*, i8** @putchar.vram
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** @putchar.vram
store i8 7, i8* %23
%25 = load i32, i32* %3
store i32 %25, i32* %2
br label %26
26: ; preds = %18, %6
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
@putchar.vram = internal unnamed_addr global i8* inttoptr (i64 753664 to i8*)
define i32 @putchar(i32 returned %0) {
%2 = icmp eq i32 %0, 10
br i1 %2, label %3, label %12
3: ; preds = %1
%4 = load i8*, i8** @putchar.vram
%5 = ptrtoint i8* %4 to i64
%6 = trunc i64 %5 to i32
%7 = urem i32 %6, 160
%8 = add i32 %6, 224
%9 = sub i32 %8, %7
%10 = zext i32 %9 to i64
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** @putchar.vram
br label %17
12: ; preds = %1
%13 = trunc i32 %0 to i8
%14 = load i8*, i8** @putchar.vram
%15 = getelementptr inbounds i8, i8* %14, i64 1
store i8 %13, i8* %14
%16 = getelementptr inbounds i8, i8* %14, i64 2
store i8* %16, i8** @putchar.vram
store i8 7, i8* %15
br label %17
17: ; preds = %12, %3
ret i32 %0
}