void display_putc(void *display_io, unsigned int pos, char c,
                  unsigned int forecolor, unsigned int backcolor) {
  unsigned int *addr;
  unsigned int cdata =
      ((backcolor & 0xfff) << 20) | (forecolor & 0xfff) << 8 | c;
  addr = (unsigned int *)display_io;
  addr += (pos % 80) + (pos / 80 * 0x100);
  if (pos < 80 * 34 && c >= 0x20) {
    *addr = cdata;
  }
}


x86 assembly (clang, -O0):
<display_putc>:
push   %rbp
mov    %rsp,%rbp
mov    %dl,%al
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %al,-0xd(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8d,-0x18(%rbp)
mov    -0x18(%rbp),%eax
and    $0xfff,%eax
shl    $0x14,%eax
mov    -0x14(%rbp),%ecx
and    $0xfff,%ecx
shl    $0x8,%ecx
or     %ecx,%eax
movsbl -0xd(%rbp),%ecx
or     %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x50,%ecx
xor    %edx,%edx
div    %ecx
mov    %edx,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x50,%ecx
xor    %edx,%edx
div    %ecx
mov    -0x28(%rbp),%edx
shl    $0x8,%eax
add    %eax,%edx
mov    -0x20(%rbp),%rax
mov    %edx,%ecx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
cmpl   $0xaa0,-0xc(%rbp)
jae    1198 <display_putc+0x98>
movsbl -0xd(%rbp),%eax
cmp    $0x20,%eax
jl     1198 <display_putc+0x98>
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<display_putc>:
cmp    $0xa9f,%esi
ja     1147 <display_putc+0x47>
cmp    $0x20,%dl
jl     1147 <display_putc+0x47>
movzbl %dl,%r9d
movzwl %si,%edx
imul   $0xcccd,%edx,%eax
shr    $0x16,%eax
mov    %eax,%edx
shl    $0x4,%edx
lea    (%rdx,%rdx,4),%edx
sub    %edx,%esi
movzwl %si,%edx
shl    $0x8,%rax
or     %rdx,%rax
shl    $0x14,%r8d
shl    $0x8,%ecx
and    $0xfff00,%ecx
or     %r8d,%ecx
or     %r9d,%ecx
mov    %ecx,(%rdi,%rax,4)
ret

LLVM IR (-O0):
define void @display_putc(i8* %0, i32 %1, i8 signext %2, i32 %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8 %2, i8* %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%13 = load i32, i32* %10
%14 = and i32 %13, 4095
%15 = shl i32 %14, 20
%16 = load i32, i32* %9
%17 = and i32 %16, 4095
%18 = shl i32 %17, 8
%19 = or i32 %15, %18
%20 = load i8, i8* %8
%21 = sext i8 %20 to i32
%22 = or i32 %19, %21
store i32 %22, i32* %12
%23 = load i8*, i8** %6
%24 = bitcast i8* %23 to i32*
store i32* %24, i32** %11
%25 = load i32, i32* %7
%26 = urem i32 %25, 80
%27 = load i32, i32* %7
%28 = udiv i32 %27, 80
%29 = mul i32 %28, 256
%30 = add i32 %26, %29
%31 = load i32*, i32** %11
%32 = zext i32 %30 to i64
%33 = getelementptr inbounds i32, i32* %31, i64 %32
store i32* %33, i32** %11
%34 = load i32, i32* %7
%35 = icmp ult i32 %34, 2720
br i1 %35, label %36, label %43
36: ; preds = %5
%37 = load i8, i8* %8
%38 = sext i8 %37 to i32
%39 = icmp sge i32 %38, 32
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = load i32, i32* %12
%42 = load i32*, i32** %11
store i32 %41, i32* %42
br label %43
43: ; preds = %40, %36, %5
ret void
}

LLVM IR (-O2):
define void @display_putc(i8* nocapture writeonly %0, i32 %1, i8 signext %2, i32 %3, i32 %4) {
%6 = icmp ult i32 %1, 2720
%7 = icmp sgt i8 %2, 31
%8 = and i1 %6, %7
br i1 %8, label %9, label %25
9: ; preds = %5
%10 = zext i8 %2 to i32
%11 = bitcast i8* %0 to i32*
%12 = trunc i32 %1 to i16
%13 = urem i16 %12, 80
%14 = zext i16 %13 to i64
%15 = udiv i16 %12, 80
%16 = zext i16 %15 to i64
%17 = shl nuw nsw i64 %16, 8
%18 = or i64 %17, %14
%19 = getelementptr inbounds i32, i32* %11, i64 %18
%20 = shl i32 %4, 20
%21 = shl i32 %3, 8
%22 = and i32 %21, 1048320
%23 = or i32 %20, %22
%24 = or i32 %23, %10
store i32 %24, i32* %19
br label %25
25: ; preds = %9, %5
ret void
}