void videoram_putc(char c) {
  static unsigned pos = 0;
  if (c == '\n') {
    pos += 80;
  } else if (c == '\r') {
    pos = (pos / 80) * 80;
  } else {
    ((char *)0xB8000)[2 * pos + 0] = c;
    ((char *)0xB8000)[2 * pos + 1] = (char)0x9E;
    ++pos;
  }
  if (pos >= 25 * 80) {
    pos = 0;
  }
}


x86 assembly (clang, -O0):
<videoram_putc>:
push   %rbp
mov    %rsp,%rbp
mov    %dil,%al
mov    %al,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
cmp    $0xa,%eax
jne    112b <videoram_putc+0x2b>
mov    0x2f07(%rip),%eax
add    $0x50,%eax
mov    %eax,0x2efe(%rip)
jmp    119d <videoram_putc+0x9d>
movsbl -0x1(%rbp),%eax
cmp    $0xd,%eax
jne    1155 <videoram_putc+0x55>
mov    0x2ee6(%rip),%eax
mov    $0x50,%ecx
xor    %edx,%edx
div    %ecx
imul   $0x50,%eax,%eax
mov    %eax,0x2ed4(%rip)
jmp    1198 <videoram_putc+0x98>
mov    -0x1(%rbp),%dl
mov    0x2ec6(%rip),%eax
shl    $0x1,%eax
add    $0x0,%eax
mov    %eax,%eax
mov    %eax,%ecx
mov    $0xb8000,%eax
mov    %dl,(%rax,%rcx,1)
mov    0x2eae(%rip),%eax
shl    $0x1,%eax
add    $0x1,%eax
mov    %eax,%eax
mov    %eax,%ecx
mov    $0xb8000,%eax
movb   $0x9e,(%rax,%rcx,1)
mov    0x2e95(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2e8c(%rip)
jmp    119d <videoram_putc+0x9d>
cmpl   $0x7d0,0x2e7d(%rip)
jb     11b7 <videoram_putc+0xb7>
movl   $0x0,0x2e6d(%rip)
pop    %rbp
ret

x86 assembly (clang, -O2):
<videoram_putc>:
cmp    $0xd,%dil
je     1117 <videoram_putc+0x17>
cmp    $0xa,%dil
jne    1132 <videoram_putc+0x32>
mov    0x2f12(%rip),%eax
add    $0x50,%eax
jmp    1159 <videoram_putc+0x59>
mov    0x2f07(%rip),%eax
mov    $0xcccccccd,%ecx
imul   %rax,%rcx
shr    $0x26,%rcx
shl    $0x4,%ecx
lea    (%rcx,%rcx,4),%eax
jmp    1159 <videoram_putc+0x59>
mov    0x2eec(%rip),%eax
add    %eax,%eax
mov    %dil,0xb8000(%rax)
mov    0x2edd(%rip),%eax
add    %eax,%eax
movb   $0x9e,0xb8001(%rax)
mov    0x2ece(%rip),%eax
add    $0x1,%eax
xor    %ecx,%ecx
cmp    $0x7d0,%eax
cmovb  %eax,%ecx
mov    %ecx,0x2ebb(%rip)
ret

LLVM IR (-O0):
@videoram_putc.pos = internal global i32 0
define void @videoram_putc(i8 signext %0) {
%2 = alloca i8
store i8 %0, i8* %2
%3 = load i8, i8* %2
%4 = sext i8 %3 to i32
%5 = icmp eq i32 %4, 10
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = load i32, i32* @videoram_putc.pos
%8 = add i32 %7, 80
store i32 %8, i32* @videoram_putc.pos
br label %32
9: ; preds = %1
%10 = load i8, i8* %2
%11 = sext i8 %10 to i32
%12 = icmp eq i32 %11, 13
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = load i32, i32* @videoram_putc.pos
%15 = udiv i32 %14, 80
%16 = mul i32 %15, 80
store i32 %16, i32* @videoram_putc.pos
br label %31
17: ; preds = %9
%18 = load i8, i8* %2
%19 = load i32, i32* @videoram_putc.pos
%20 = mul i32 2, %19
%21 = add i32 %20, 0
%22 = zext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* inttoptr (i64 753664 to i8*), i64 %22
store i8 %18, i8* %23
%24 = load i32, i32* @videoram_putc.pos
%25 = mul i32 2, %24
%26 = add i32 %25, 1
%27 = zext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* inttoptr (i64 753664 to i8*), i64 %27
store i8 -98, i8* %28
%29 = load i32, i32* @videoram_putc.pos
%30 = add i32 %29, 1
store i32 %30, i32* @videoram_putc.pos
br label %31
31: ; preds = %17, %13
br label %32
32: ; preds = %31, %6
%33 = load i32, i32* @videoram_putc.pos
%34 = icmp uge i32 %33, 2000
br i1 %34, label %35, label %36
35: ; preds = %32
store i32 0, i32* @videoram_putc.pos
br label %36
36: ; preds = %35, %32
ret void
}

LLVM IR (-O2):
@videoram_putc.pos = internal unnamed_addr global i32 0
define void @videoram_putc(i8 signext %0) {
switch i8 %0, label %9 [
i8 10, label %2
i8 13, label %5
]
2: ; preds = %1
%3 = load i32, i32* @videoram_putc.pos
%4 = add i32 %3, 80
br label %21
5: ; preds = %1
%6 = load i32, i32* @videoram_putc.pos
%7 = urem i32 %6, 80
%8 = sub i32 %6, %7
br label %21
9: ; preds = %1
%10 = load i32, i32* @videoram_putc.pos
%11 = shl i32 %10, 1
%12 = zext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* inttoptr (i64 753664 to i8*), i64 %12
store i8 %0, i8* %13
%14 = load i32, i32* @videoram_putc.pos
%15 = shl i32 %14, 1
%16 = or i32 %15, 1
%17 = zext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* inttoptr (i64 753664 to i8*), i64 %17
store i8 -98, i8* %18
%19 = load i32, i32* @videoram_putc.pos
%20 = add i32 %19, 1
br label %21
21: ; preds = %5, %9, %2
%22 = phi i32 [ %8, %5 ], [ %20, %9 ], [ %4, %2 ]
%23 = icmp ugt i32 %22, 1999
%24 = select i1 %23, i32 0, i32 %22
store i32 %24, i32* @videoram_putc.pos
ret void
}