#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_offset();
int /*<<< orphan*/ scroll_up();
void print_char(char character, int row, int col, char attribute_byte) {
  unsigned char *vidmem = (unsigned char *)0xb8000;
  int offset;
  if (!attribute_byte)
    attribute_byte = 0x02;
  offset = get_cursor_offset();
  if (offset > 80 * 25) {
    scroll_up();
    offset = offset - 80;
  }
  offset = offset * 2;
  {
    vidmem[offset] = character;
    vidmem[offset + 1] = attribute_byte;
  }
}


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    1150 <print_char+0x30>
movb   $0x2,-0xd(%rbp)
mov    $0x0,%al
call   1030 <get_cursor_offset@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x7d0,-0x1c(%rbp)
jle    1177 <print_char+0x57>
mov    $0x0,%al
call   1040 <scroll_up@plt>
mov    -0x1c(%rbp),%eax
sub    $0x50,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x1c(%rbp)
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)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_char>:
push   %rbp
push   %r14
push   %rbx
mov    %ecx,%ebp
mov    %edi,%r14d
xor    %eax,%eax
call   1030 <get_cursor_offset@plt>
mov    %eax,%ebx
cmp    $0x7d1,%eax
jl     1143 <print_char+0x23>
xor    %eax,%eax
call   1040 <scroll_up@plt>
add    $0xffffffb0,%ebx
test   %bpl,%bpl
movzbl %bpl,%eax
mov    $0x2,%ecx
cmovne %eax,%ecx
movslq %ebx,%rax
lea    (%rax,%rax,1),%rdx
mov    %r14b,0xb8000(%rax,%rax,1)
movslq %edx,%rax
mov    %cl,0xb8001(%rax)
pop    %rbx
pop    %r14
pop    %rbp
ret

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
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
%11 = load i8, i8* %8
%12 = icmp ne i8 %11, 0
br i1 %12, label %14, label %13
13: ; preds = %4
store i8 2, i8* %8
br label %14
14: ; preds = %13, %4
%15 = call i32 (...) @get_cursor_offset()
store i32 %15, i32* %10
%16 = load i32, i32* %10
%17 = icmp sgt i32 %16, 2000
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = call i32 (...) @scroll_up()
%20 = load i32, i32* %10
%21 = sub nsw i32 %20, 80
store i32 %21, i32* %10
br label %22
22: ; preds = %18, %14
%23 = load i32, i32* %10
%24 = mul nsw i32 %23, 2
store i32 %24, i32* %10
%25 = load i8, i8* %5
%26 = load i8*, i8** %9
%27 = load i32, i32* %10
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 %25, i8* %29
%30 = load i8, i8* %8
%31 = load i8*, i8** %9
%32 = load i32, i32* %10
%33 = add nsw i32 %32, 1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %31, i64 %34
store i8 %30, i8* %35
ret void
}

LLVM IR (-O2):
define void @print_char(i8 signext %0, i32 %1, i32 %2, i8 signext %3) {
%5 = tail call i32 (...) @get_cursor_offset()
%6 = icmp sgt i32 %5, 2000
br i1 %6, label %7, label %10
7: ; preds = %4
%8 = tail call i32 (...) @scroll_up()
%9 = add nsw i32 %5, -80
br label %10
10: ; preds = %7, %4
%11 = phi i32 [ %9, %7 ], [ %5, %4 ]
%12 = icmp eq i8 %3, 0
%13 = select i1 %12, i8 2, i8 %3
%14 = shl nsw i32 %11, 1
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* inttoptr (i64 753664 to i8*), i64 %15
store i8 %0, i8* %16
%17 = or i32 %14, 1
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* inttoptr (i64 753664 to i8*), i64 %18
store i8 %13, i8* %19
ret void
}