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) {
    *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    118b <display_putc+0x8b>
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<display_putc>:
cmp    $0xa9f,%esi
ja     1144 <display_putc+0x44>
movzwl %si,%eax
imul   $0xcccd,%eax,%r9d
shr    $0x16,%r9d
mov    %r9d,%eax
shl    $0x4,%eax
lea    (%rax,%rax,4),%eax
sub    %eax,%esi
movzwl %si,%eax
shl    $0x8,%r9
or     %rax,%r9
shl    $0x14,%r8d
shl    $0x8,%ecx
and    $0xfff00,%ecx
or     %r8d,%ecx
movsbl %dl,%eax
or     %ecx,%eax
mov    %eax,(%rdi,%r9,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 %39
36: ; preds = %5
%37 = load i32, i32* %12
%38 = load i32*, i32** %11
store i32 %37, i32* %38
br label %39
39: ; preds = %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
br i1 %6, label %7, label %23
7: ; preds = %5
%8 = bitcast i8* %0 to i32*
%9 = trunc i32 %1 to i16
%10 = urem i16 %9, 80
%11 = zext i16 %10 to i64
%12 = udiv i16 %9, 80
%13 = zext i16 %12 to i64
%14 = shl nuw nsw i64 %13, 8
%15 = or i64 %14, %11
%16 = getelementptr inbounds i32, i32* %8, i64 %15
%17 = shl i32 %4, 20
%18 = shl i32 %3, 8
%19 = and i32 %18, 1048320
%20 = or i32 %17, %19
%21 = sext i8 %2 to i32
%22 = or i32 %20, %21
store i32 %22, i32* %16
br label %23
23: ; preds = %7, %5
ret void
}