#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_position();
int get_screen_offset(int, int);
int handle_scrolling(int);
int /*<<< orphan*/ set_cursor(int);
void print_char(char character, int row, int col, char attribute_byte) {
  unsigned char *video_memory = (unsigned char *)0xb8000;
  char is_in_range = (row >= 0 && col >= 0);
  int offset =
      (is_in_range) ? get_screen_offset(row, col) : get_cursor_position();
  int rows;
  if (!attribute_byte)
    attribute_byte = 0x0f;
  if (character == '\n') {
    rows = offset / (2 * 80);
    offset = get_screen_offset(rows, 79);
  } else {
    video_memory[offset] = character;
    video_memory[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    $0x30,%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)
xor    %eax,%eax
cmpl   $0x0,-0x8(%rbp)
mov    %al,-0x25(%rbp)
jl     117b <print_char+0x3b>
cmpl   $0x0,-0xc(%rbp)
setge  %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %al,-0x19(%rbp)
movsbl -0x19(%rbp),%eax
cmp    $0x0,%eax
je     11a6 <print_char+0x66>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1040 <get_screen_offset@plt>
mov    %eax,-0x2c(%rbp)
jmp    11b0 <print_char+0x70>
mov    $0x0,%al
call   1060 <get_cursor_position@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x20(%rbp)
cmpb   $0x0,-0xd(%rbp)
jne    11c4 <print_char+0x84>
movb   $0xf,-0xd(%rbp)
movsbl -0x1(%rbp),%eax
cmp    $0xa,%eax
jne    11f4 <print_char+0xb4>
mov    -0x20(%rbp),%eax
mov    $0xa0,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    $0x4f,%esi
call   1040 <get_screen_offset@plt>
mov    %eax,-0x20(%rbp)
jmp    1215 <print_char+0xd5>
mov    -0x1(%rbp),%dl
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0xd(%rbp),%dl
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
call   1030 <handle_scrolling@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
call   1050 <set_cursor@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_char>:
push   %rbp
push   %rbx
push   %rax
mov    %ecx,%ebp
mov    %edi,%ebx
mov    %edx,%eax
or     %esi,%eax
js     117d <print_char+0x3d>
mov    %esi,%edi
mov    %edx,%esi
call   1040 <get_screen_offset@plt>
cmp    $0xa,%bl
jne    1189 <print_char+0x49>
cltq
imul   $0x66666667,%rax,%rdi
mov    %rdi,%rax
shr    $0x3f,%rax
sar    $0x26,%rdi
add    %eax,%edi
mov    $0x4f,%esi
call   1040 <get_screen_offset@plt>
jmp    11a7 <print_char+0x67>
xor    %eax,%eax
call   1060 <get_cursor_position@plt>
cmp    $0xa,%bl
je     115b <print_char+0x1b>
test   %bpl,%bpl
movzbl %bpl,%ecx
mov    $0xf,%edx
cmovne %ecx,%edx
movslq %eax,%rcx
mov    %bl,0xb8000(%rcx)
mov    %dl,0xb8001(%rcx)
add    $0x2,%eax
mov    %eax,%edi
call   1030 <handle_scrolling@plt>
mov    %eax,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <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 i8
%11 = alloca i32
%12 = 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
%13 = load i32, i32* %6
%14 = icmp sge i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %4
%16 = load i32, i32* %7
%17 = icmp sge i32 %16, 0
br label %18
18: ; preds = %15, %4
%19 = phi i1 [ false, %4 ], [ %17, %15 ]
%20 = zext i1 %19 to i32
%21 = trunc i32 %20 to i8
store i8 %21, i8* %10
%22 = load i8, i8* %10
%23 = sext i8 %22 to i32
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %18
%26 = load i32, i32* %6
%27 = load i32, i32* %7
%28 = call i32 @get_screen_offset(i32 %26, i32 %27)
br label %31
29: ; preds = %18
%30 = call i32 (...) @get_cursor_position()
br label %31
31: ; preds = %29, %25
%32 = phi i32 [ %28, %25 ], [ %30, %29 ]
store i32 %32, i32* %11
%33 = load i8, i8* %8
%34 = icmp ne i8 %33, 0
br i1 %34, label %36, label %35
35: ; preds = %31
store i8 15, i8* %8
br label %36
36: ; preds = %35, %31
%37 = load i8, i8* %5
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %38, 10
br i1 %39, label %40, label %45
40: ; preds = %36
%41 = load i32, i32* %11
%42 = sdiv i32 %41, 160
store i32 %42, i32* %12
%43 = load i32, i32* %12
%44 = call i32 @get_screen_offset(i32 %43, i32 79)
store i32 %44, i32* %11
br label %57
45: ; preds = %36
%46 = load i8, i8* %5
%47 = load i8*, i8** %9
%48 = load i32, i32* %11
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
store i8 %46, i8* %50
%51 = load i8, i8* %8
%52 = load i8*, i8** %9
%53 = load i32, i32* %11
%54 = add nsw i32 %53, 1
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %52, i64 %55
store i8 %51, i8* %56
br label %57
57: ; preds = %45, %40
%58 = load i32, i32* %11
%59 = add nsw i32 %58, 2
store i32 %59, i32* %11
%60 = load i32, i32* %11
%61 = call i32 @handle_scrolling(i32 %60)
store i32 %61, i32* %11
%62 = load i32, i32* %11
%63 = call i32 @set_cursor(i32 %62)
ret void
}

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