#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 get_cursor();
int get_screen_offset(int, int);
int handle_scrolling(int);
int /*<<< orphan*/ set_cursor(int);
void writechar(char c, int col, int row, char attribute) {
  unsigned char *vidmem = (unsigned char *)0xb8000;
  if (!attribute) {
    attribute = 0x0f;
  }
  int offset;
  if (col >= 0 && row >= 0) {
    offset = get_screen_offset(col, row);
  } else {
    offset = get_cursor();
  }
  if (c == '\n') {
    int rows = offset / (2 * 80);
    offset = get_screen_offset(rows, 79);
  } else {
    *(vidmem + offset) = c;
    *(vidmem + offset + 1) = attribute;
  }
  offset = offset + 2;
  offset = handle_scrolling(offset);
  set_cursor(offset);
}


x86 assembly (clang, -O0):
<writechar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %cl,%al
mov    %dil,%cl
mov    %cl,-0x1(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %al,-0xd(%rbp)
mov    $0xb8000,%eax
mov    %rax,-0x18(%rbp)
cmpb   $0x0,-0xd(%rbp)
jne    1170 <writechar+0x30>
movb   $0xf,-0xd(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1197 <writechar+0x57>
cmpl   $0x0,-0xc(%rbp)
jl     1197 <writechar+0x57>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1050 <get_screen_offset@plt>
mov    %eax,-0x1c(%rbp)
jmp    11a1 <writechar+0x61>
mov    $0x0,%al
call   1040 <get_cursor@plt>
mov    %eax,-0x1c(%rbp)
movsbl -0x1(%rbp),%eax
cmp    $0xa,%eax
jne    11d1 <writechar+0x91>
mov    -0x1c(%rbp),%eax
mov    $0xa0,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
mov    $0x4f,%esi
call   1050 <get_screen_offset@plt>
mov    %eax,-0x1c(%rbp)
jmp    11ee <writechar+0xae>
mov    -0x1(%rbp),%dl
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0xd(%rbp),%dl
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,0x1(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <handle_scrolling@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <set_cursor@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writechar>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
test   %cl,%cl
mov    $0xf,%ebp
cmovne %ecx,%ebp
mov    %edx,%eax
or     %esi,%eax
js     1185 <writechar+0x45>
mov    %esi,%edi
mov    %edx,%esi
call   1050 <get_screen_offset@plt>
cmp    $0xa,%bl
jne    1191 <writechar+0x51>
cltq
imul   $0x66666667,%rax,%rdi
mov    %rdi,%rax
shr    $0x3f,%rax
sar    $0x26,%rdi
add    %eax,%edi
mov    $0x4f,%esi
call   1050 <get_screen_offset@plt>
jmp    11a1 <writechar+0x61>
xor    %eax,%eax
call   1040 <get_cursor@plt>
cmp    $0xa,%bl
je     1163 <writechar+0x23>
movslq %eax,%rcx
mov    %bl,0xb8000(%rcx)
mov    %bpl,0xb8001(%rcx)
add    $0x2,%eax
mov    %eax,%edi
call   1030 <handle_scrolling@plt>
mov    %eax,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1060 <set_cursor@plt>

LLVM IR (-O0):
define void @writechar(i8 signext %0, i32 %1, i32 %2, i8 signext %3) {
%5 = alloca i8
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i8 %0, i8* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8 %3, i8* %8
store i8* inttoptr (i64 753664 to i8*), i8** %9
%12 = load i8, i8* %8
%13 = icmp ne i8 %12, 0
br i1 %13, label %15, label %14
14: ; preds = %4
store i8 15, i8* %8
br label %15
15: ; preds = %14, %4
%16 = load i32, i32* %6
%17 = icmp sge i32 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %15
%19 = load i32, i32* %7
%20 = icmp sge i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = load i32, i32* %6
%23 = load i32, i32* %7
%24 = call i32 @get_screen_offset(i32 %22, i32 %23)
store i32 %24, i32* %10
br label %27
25: ; preds = %18, %15
%26 = call i32 (...) @get_cursor()
store i32 %26, i32* %10
br label %27
27: ; preds = %25, %21
%28 = load i8, i8* %5
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 10
br i1 %30, label %31, label %36
31: ; preds = %27
%32 = load i32, i32* %10
%33 = sdiv i32 %32, 160
store i32 %33, i32* %11
%34 = load i32, i32* %11
%35 = call i32 @get_screen_offset(i32 %34, i32 79)
store i32 %35, i32* %10
br label %48
36: ; preds = %27
%37 = load i8, i8* %5
%38 = load i8*, i8** %9
%39 = load i32, i32* %10
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
store i8 %37, i8* %41
%42 = load i8, i8* %8
%43 = load i8*, i8** %9
%44 = load i32, i32* %10
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
%47 = getelementptr inbounds i8, i8* %46, i64 1
store i8 %42, i8* %47
br label %48
48: ; preds = %36, %31
%49 = load i32, i32* %10
%50 = add nsw i32 %49, 2
store i32 %50, i32* %10
%51 = load i32, i32* %10
%52 = call i32 @handle_scrolling(i32 %51)
store i32 %52, i32* %10
%53 = load i32, i32* %10
%54 = call i32 @set_cursor(i32 %53)
ret void
}

LLVM IR (-O2):
define void @writechar(i8 signext %0, i32 %1, i32 %2, i8 signext %3) {
%5 = icmp eq i8 %3, 0
%6 = select i1 %5, i8 15, i8 %3
%7 = or i32 %2, %1
%8 = icmp sgt i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %4
%10 = tail call i32 @get_screen_offset(i32 %1, i32 %2)
br label %13
11: ; preds = %4
%12 = tail call i32 (...) @get_cursor()
br label %13
13: ; preds = %11, %9
%14 = phi i32 [ %10, %9 ], [ %12, %11 ]
%15 = icmp eq i8 %0, 10
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = sdiv i32 %14, 160
%18 = tail call i32 @get_screen_offset(i32 %17, i32 79)
br label %23
19: ; preds = %13
%20 = sext i32 %14 to i64
%21 = getelementptr inbounds i8, i8* inttoptr (i64 753664 to i8*), i64 %20
store i8 %0, i8* %21
%22 = getelementptr inbounds i8, i8* %21, i64 1
store i8 %6, i8* %22
br label %23
23: ; preds = %19, %16
%24 = phi i32 [ %18, %16 ], [ %14, %19 ]
%25 = add nsw i32 %24, 2
%26 = tail call i32 @handle_scrolling(i32 %25)
%27 = tail call i32 @set_cursor(i32 %26)
ret void
}