#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 */
int /*<<< orphan*/ memcpy(void *, void *, int);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
void kputc(int c) {
  int xpos, ypos;
  char ch = 0;
  static short *p = (short *)(0xB8000);
  xpos = (p - (short *)(0xB8000)) % 80;
  ypos = (p - (short *)(0xB8000)) / 80;
  switch (c) {
  case '\n':
    p = (short *)(0xB8000) + (ypos + 1) * 80;
    break;
  case '\t':
    p = (short *)(0xB8000) + ((xpos + 7) / 8) * 8 + ypos * 80;
    break;
  case '\b':
  case '\r':
    break;
  default:
    ch = c;
  }
  if (ypos == 25) {
    memcpy((void *)(0xB8000), (void *)((0xB8000) + 80 * 2), 80 * 2 * (25 - 1));
    memset((void *)((0xB8000) + 80 * (25 - 1) * 2), 0, 80 * 2);
    p -= 80;
  }
  if (ch) {
    *p++ = (short)(0x0200 | ch);
  }
}


x86 assembly (clang, -O0):
<kputc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movb   $0x0,-0xd(%rbp)
mov    0x2efa(%rip),%rax
add    $0xfffffffffff48000,%rax
sar    %rax
mov    %rax,-0x20(%rbp)
movabs $0x6666666666666667,%rcx
66 66 66
imul   %rcx
mov    -0x20(%rbp),%rax
mov    %rdx,%rsi
mov    %rsi,%rdx
shr    $0x3f,%rdx
shr    $0x5,%rsi
add    %edx,%esi
shl    $0x4,%esi
mov    %esi,%edx
lea    (%rdx,%rdx,4),%edx
sub    %edx,%eax
mov    %eax,-0x8(%rbp)
mov    0x2eb8(%rip),%rax
add    $0xfffffffffff48000,%rax
sar    %rax
imul   %rcx
mov    %rdx,%rax
shr    $0x3f,%rax
mov    %eax,%ecx
shr    $0x5,%rdx
mov    %edx,%eax
add    %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%eax
add    $0xfffffff8,%eax
mov    %eax,%ecx
mov    %rcx,-0x18(%rbp)
sub    $0x5,%eax
ja     1224 <kputc+0x104>
mov    -0x18(%rbp),%rax
lea    0xe48(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0xc(%rbp),%eax
add    $0x1,%eax
imul   $0x50,%eax,%eax
movslq %eax,%rcx
mov    $0xb8000,%eax
shl    $0x1,%rcx
add    %rcx,%rax
mov    %rax,0x2e50(%rip)
jmp    122a <kputc+0x10a>
mov    -0x8(%rbp),%eax
add    $0x7,%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
shl    $0x3,%eax
movslq %eax,%rcx
mov    $0xb8000,%eax
shl    $0x1,%rcx
add    %rcx,%rax
imul   $0x50,-0xc(%rbp),%ecx
movslq %ecx,%rcx
shl    $0x1,%rcx
add    %rcx,%rax
mov    %rax,0x2e16(%rip)
jmp    122a <kputc+0x10a>
jmp    122a <kputc+0x10a>
mov    -0x4(%rbp),%eax
mov    %al,-0xd(%rbp)
cmpl   $0x19,-0xc(%rbp)
jne    126d <kputc+0x14d>
mov    $0xb8000,%edi
mov    $0xb80a0,%esi
mov    $0xf00,%edx
call   1040 <memcpy@plt>
mov    $0xb8f00,%edi
xor    %esi,%esi
mov    $0xa0,%edx
call   1030 <memset@plt>
mov    0x2dd0(%rip),%rax
add    $0xffffffffffffff60,%rax
mov    %rax,0x2dc3(%rip)
cmpb   $0x0,-0xd(%rbp)
je     129b <kputc+0x17b>
movsbl -0xd(%rbp),%eax
or     $0x200,%eax
mov    %ax,%cx
mov    0x2da6(%rip),%rax
mov    %rax,%rdx
add    $0x2,%rdx
mov    %rdx,0x2d98(%rip)
mov    %cx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kputc>:
push   %rbp
push   %rbx
push   %rax
mov    0x2f06(%rip),%rsi
lea    -0xb8000(%rsi),%rcx
sar    %rcx
movabs $0x6666666666666667,%rdx
66 66 66
mov    %rcx,%rax
imul   %rdx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x5,%rdx
add    %rax,%rdx
lea    -0x8(%rdi),%eax
cmp    $0x5,%eax
ja     123d <kputc+0x11d>
xor    %ebp,%ebp
lea    0xe99(%rip),%rbx
movslq (%rbx,%rax,4),%rax
add    %rbx,%rax
jmp    *%rax
mov    %rdx,%rax
shl    $0x4,%rax
lea    (%rax,%rax,4),%rax
sub    %rax,%rcx
add    $0x7,%cl
mov    %ecx,%eax
sar    $0x7,%al
shr    $0x5,%al
add    %cl,%al
sar    $0x3,%al
movsbl %al,%eax
shl    $0x3,%eax
cltq
mov    %rdx,%rcx
shl    $0x24,%rcx
lea    (%rcx,%rcx,4),%rcx
sar    $0x1f,%rcx
lea    (%rcx,%rax,2),%rsi
add    $0xb8000,%rsi
jmp    11d2 <kputc+0xb2>
lea    (%rdx,%rdx,4),%rax
shl    $0x24,%rax
movabs $0x5000000000,%rsi
add    %rax,%rsi
sar    $0x1f,%rsi
add    $0xb8000,%rsi
mov    %rsi,0x2e57(%rip)
xor    %ebp,%ebp
cmp    $0x19,%edx
jne    121a <kputc+0xfa>
mov    $0xb8000,%edi
mov    $0xb80a0,%esi
mov    $0xf00,%edx
call   1040 <memcpy@plt>
mov    $0xb8f00,%edi
xor    %esi,%esi
mov    $0xa0,%edx
call   1030 <memset@plt>
mov    $0xffffffffffffff60,%rsi
add    0x2e1d(%rip),%rsi
mov    %rsi,0x2e16(%rip)
test   %bpl,%bpl
je     1236 <kputc+0x116>
movsbl %bpl,%eax
or     $0x200,%eax
lea    0x2(%rsi),%rcx
mov    %rcx,0x2dfd(%rip)
mov    %ax,(%rsi)
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    %edi,%ebp
cmp    $0x19,%edx
je     11e0 <kputc+0xc0>
jmp    121a <kputc+0xfa>

LLVM IR (-O0):
@kputc.p = internal global i16* inttoptr (i64 753664 to i16*)
define void @kputc(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
store i32 %0, i32* %2
store i8 0, i8* %5
%6 = load i16*, i16** @kputc.p
%7 = ptrtoint i16* %6 to i64
%8 = sub i64 %7, 753664
%9 = sdiv exact i64 %8, 2
%10 = srem i64 %9, 80
%11 = trunc i64 %10 to i32
store i32 %11, i32* %3
%12 = load i16*, i16** @kputc.p
%13 = ptrtoint i16* %12 to i64
%14 = sub i64 %13, 753664
%15 = sdiv exact i64 %14, 2
%16 = sdiv i64 %15, 80
%17 = trunc i64 %16 to i32
store i32 %17, i32* %4
%18 = load i32, i32* %2
switch i32 %18, label %37 [
i32 10, label %19
i32 9, label %25
i32 8, label %36
i32 13, label %36
]
19: ; preds = %1
%20 = load i32, i32* %4
%21 = add nsw i32 %20, 1
%22 = mul nsw i32 %21, 80
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i16, i16* inttoptr (i64 753664 to i16*), i64 %23
store i16* %24, i16** @kputc.p
br label %40
25: ; preds = %1
%26 = load i32, i32* %3
%27 = add nsw i32 %26, 7
%28 = sdiv i32 %27, 8
%29 = mul nsw i32 %28, 8
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i16, i16* inttoptr (i64 753664 to i16*), i64 %30
%32 = load i32, i32* %4
%33 = mul nsw i32 %32, 80
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i16, i16* %31, i64 %34
store i16* %35, i16** @kputc.p
br label %40
36: ; preds = %1, %1
br label %40
37: ; preds = %1
%38 = load i32, i32* %2
%39 = trunc i32 %38 to i8
store i8 %39, i8* %5
br label %40
40: ; preds = %37, %36, %25, %19
%41 = load i32, i32* %4
%42 = icmp eq i32 %41, 25
br i1 %42, label %43, label %48
43: ; preds = %40
%44 = call i32 @memcpy(i8* inttoptr (i64 753664 to i8*), i8* inttoptr (i64 753824 to i8*), i32 3840)
%45 = call i32 @memset(i8* inttoptr (i64 757504 to i8*), i32 0, i32 160)
%46 = load i16*, i16** @kputc.p
%47 = getelementptr inbounds i16, i16* %46, i64 -80
store i16* %47, i16** @kputc.p
br label %48
48: ; preds = %43, %40
%49 = load i8, i8* %5
%50 = icmp ne i8 %49, 0
br i1 %50, label %51, label %58
51: ; preds = %48
%52 = load i8, i8* %5
%53 = sext i8 %52 to i32
%54 = or i32 512, %53
%55 = trunc i32 %54 to i16
%56 = load i16*, i16** @kputc.p
%57 = getelementptr inbounds i16, i16* %56, i32 1
store i16* %57, i16** @kputc.p
store i16 %55, i16* %56
br label %58
58: ; preds = %51, %48
ret void
}

LLVM IR (-O2):
@kputc.p = internal unnamed_addr global i16* inttoptr (i64 753664 to i16*)
define void @kputc(i32 %0) {
%2 = load i16*, i16** @kputc.p
%3 = ptrtoint i16* %2 to i64
%4 = add i64 %3, -753664
%5 = ashr exact i64 %4, 1
%6 = sdiv i64 %5, 80
%7 = srem i64 %5, 80
%8 = trunc i64 %6 to i32
switch i32 %0, label %25 [
i32 10, label %9
i32 9, label %14
i32 8, label %27
i32 13, label %27
]
9: ; preds = %1
%10 = mul i64 %6, 343597383680
%11 = add i64 %10, 343597383680
%12 = ashr exact i64 %11, 32
%13 = getelementptr inbounds i16, i16* inttoptr (i64 753664 to i16*), i64 %12
store i16* %13, i16** @kputc.p
br label %27
14: ; preds = %1
%15 = trunc i64 %7 to i8
%16 = add nsw i8 %15, 7
%17 = sdiv i8 %16, 8
%18 = sext i8 %17 to i32
%19 = shl nsw i32 %18, 3
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i16, i16* inttoptr (i64 753664 to i16*), i64 %20
%22 = mul i64 %6, 343597383680
%23 = ashr exact i64 %22, 32
%24 = getelementptr inbounds i16, i16* %21, i64 %23
store i16* %24, i16** @kputc.p
br label %27
25: ; preds = %1
%26 = trunc i32 %0 to i8
br label %27
27: ; preds = %1, %1, %25, %14, %9
%28 = phi i16* [ %2, %25 ], [ %2, %1 ], [ %2, %1 ], [ %24, %14 ], [ %13, %9 ]
%29 = phi i8 [ %26, %25 ], [ 0, %1 ], [ 0, %1 ], [ 0, %14 ], [ 0, %9 ]
%30 = icmp eq i32 %8, 25
br i1 %30, label %31, label %36
31: ; preds = %27
%32 = tail call i32 @memcpy(i8* inttoptr (i64 753664 to i8*), i8* inttoptr (i64 753824 to i8*), i32 3840)
%33 = tail call i32 @memset(i8* inttoptr (i64 757504 to i8*), i32 0, i32 160)
%34 = load i16*, i16** @kputc.p
%35 = getelementptr inbounds i16, i16* %34, i64 -80
store i16* %35, i16** @kputc.p
br label %36
36: ; preds = %31, %27
%37 = phi i16* [ %35, %31 ], [ %28, %27 ]
%38 = icmp eq i8 %29, 0
br i1 %38, label %43, label %39
39: ; preds = %36
%40 = sext i8 %29 to i16
%41 = or i16 %40, 512
%42 = getelementptr inbounds i16, i16* %37, i64 1
store i16* %42, i16** @kputc.p
store i16 %41, i16* %37
br label %43
43: ; preds = %39, %36
ret void
}