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


x86 assembly (clang, -O0):
<print_char>:
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 <print_char+0x30>
movb   $0xf,-0xd(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1197 <print_char+0x57>
cmpl   $0x0,-0xc(%rbp)
jl     1197 <print_char+0x57>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1050 <get_screen_offset@plt>
mov    %eax,-0x1c(%rbp)
jmp    11a1 <print_char+0x61>
mov    $0x0,%al
call   1040 <get_cursor@plt>
mov    %eax,-0x1c(%rbp)
movsbl -0x1(%rbp),%eax
cmp    $0xa,%eax
jne    11d1 <print_char+0x91>
mov    -0x1c(%rbp),%eax
mov    $0xa0,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
mov    $0x4f,%edi
call   1050 <get_screen_offset@plt>
mov    %eax,-0x1c(%rbp)
jmp    11f2 <print_char+0xb2>
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
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%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):
<print_char>:
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 <print_char+0x45>
mov    %esi,%edi
mov    %edx,%esi
call   1050 <get_screen_offset@plt>
cmp    $0xa,%bl
jne    1191 <print_char+0x51>
cltq
imul   $0x66666667,%rax,%rsi
mov    %rsi,%rax
shr    $0x3f,%rax
sar    $0x26,%rsi
add    %eax,%esi
mov    $0x4f,%edi
call   1050 <get_screen_offset@plt>
jmp    11a1 <print_char+0x61>
xor    %eax,%eax
call   1040 <get_cursor@plt>
cmp    $0xa,%bl
je     1163 <print_char+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 @print_char(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 79, i32 %34)
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 = add nsw i32 %44, 1
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %43, i64 %46
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 @print_char(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 79, i32 %17)
br label %25
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 = add nsw i32 %14, 1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* inttoptr (i64 753664 to i8*), i64 %23
store i8 %6, i8* %24
br label %25
25: ; preds = %19, %16
%26 = phi i32 [ %18, %16 ], [ %14, %19 ]
%27 = add nsw i32 %26, 2
%28 = tail call i32 @handle_scrolling(i32 %27)
%29 = tail call i32 @set_cursor(i32 %28)
ret void
}