#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*/ clear();
int scroll(int);
void putchar(char c, char color) {
  static int pos = 0;
  char *fb = (char *)0xC00b8000;
  if (c == '\n') {
    pos = (pos / 80 + 1) * 80;
    return;
  }
  if (c == '\f') {
    clear();
    pos = 0;
    return;
  }
  if (pos >= 2000)
    pos = scroll(pos);
  *(fb + 2 * pos) = c;
  *(fb + 2 * pos + 1) = color;
  pos++;
}


x86 assembly (clang, -O0):
<putchar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %sil,%al
mov    %dil,%cl
mov    %cl,-0x1(%rbp)
mov    %al,-0x2(%rbp)
mov    $0xc00b8000,%eax
mov    %rax,-0x10(%rbp)
movsbl -0x1(%rbp),%eax
cmp    $0xa,%eax
jne    1169 <putchar+0x49>
mov    0x2ee4(%rip),%eax
mov    $0x50,%ecx
cltd
idiv   %ecx
add    $0x1,%eax
imul   $0x50,%eax,%eax
mov    %eax,0x2ed0(%rip)
jmp    11e9 <putchar+0xc9>
movsbl -0x1(%rbp),%eax
cmp    $0xc,%eax
jne    118c <putchar+0x6c>
mov    $0x0,%al
call   1030 <clear@plt>
movl   $0x0,0x2ead(%rip)
jmp    11e9 <putchar+0xc9>
cmpl   $0x7d0,0x2e9e(%rip)
jl     11ad <putchar+0x8d>
mov    0x2e92(%rip),%edi
call   1040 <scroll@plt>
mov    %eax,0x2e87(%rip)
mov    -0x1(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    0x2e7a(%rip),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x2(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    0x2e64(%rip),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,0x1(%rax,%rcx,1)
mov    0x2e54(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2e4b(%rip)
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<putchar>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
cmp    $0xc,%bpl
je     1157 <putchar+0x37>
cmp    $0xa,%bpl
jne    1162 <putchar+0x42>
movslq 0x2efc(%rip),%rax
imul   $0x66666667,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x25,%rax
add    %ecx,%eax
lea    (%rax,%rax,4),%ebx
shl    $0x4,%ebx
add    $0x50,%ebx
jmp    119d <putchar+0x7d>
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <clear@plt>
jmp    119d <putchar+0x7d>
mov    %esi,%ebx
mov    0x2eca(%rip),%eax
cmp    $0x7d0,%eax
jl     117e <putchar+0x5e>
mov    %eax,%edi
call   1040 <scroll@plt>
mov    %eax,0x2eb6(%rip)
cltq
mov    $0xc00b8000,%ecx
mov    %bpl,(%rcx,%rax,2)
movslq 0x2ea4(%rip),%rax
mov    %bl,0x1(%rcx,%rax,2)
mov    0x2e9a(%rip),%ebx
add    $0x1,%ebx
mov    %ebx,0x2e91(%rip)
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
@putchar.pos = internal unnamed_addr global i32 0
define void @putchar(i8 signext %0, i8 signext %1) {
switch i8 %0, label %10 [
i8 10, label %3
i8 12, label %8
]
3: ; preds = %2
%4 = load i32, i32* @putchar.pos
%5 = srem i32 %4, 80
%6 = add i32 %4, 80
%7 = sub i32 %6, %5
br label %27
8: ; preds = %2
%9 = tail call i32 (...) @clear()
br label %27
10: ; preds = %2
%11 = load i32, i32* @putchar.pos
%12 = icmp sgt i32 %11, 1999
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @scroll(i32 %11)
store i32 %14, i32* @putchar.pos
br label %15
15: ; preds = %13, %10
%16 = phi i32 [ %14, %13 ], [ %11, %10 ]
%17 = shl nsw i32 %16, 1
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* inttoptr (i64 3221979136 to i8*), i64 %18
store i8 %0, i8* %19
%20 = load i32, i32* @putchar.pos
%21 = shl nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* inttoptr (i64 3221979136 to i8*), i64 %22
%24 = getelementptr inbounds i8, i8* %23, i64 1
store i8 %1, i8* %24
%25 = load i32, i32* @putchar.pos
%26 = add nsw i32 %25, 1
br label %27
27: ; preds = %15, %8, %3
%28 = phi i32 [ %26, %15 ], [ 0, %8 ], [ %7, %3 ]
store i32 %28, i32* @putchar.pos
ret void
}