#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 get_offset(int, int);
int get_offset_col(int);
int get_offset_row(int);
int print_char(char, int, int, int);
void kprint_at(char *message, int col, int row) {
  int offset;
  if (col >= 0 && row >= 0)
    offset = get_offset(col, row);
  else {
    offset = get_cursor_offset();
    row = get_offset_row(offset);
    col = get_offset_col(offset);
  }
  int i = 0;
  while (message[i] != 0) {
    offset = print_char(message[i++], col, row, 0x0f);
    row = get_offset_row(offset);
    col = get_offset_col(offset);
  }
}


x86 assembly (clang, -O0):
<kprint_at>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     1189 <kprint_at+0x39>
cmpl   $0x0,-0x10(%rbp)
jl     1189 <kprint_at+0x39>
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1070 <get_offset@plt>
mov    %eax,-0x14(%rbp)
jmp    11a9 <kprint_at+0x59>
mov    $0x0,%al
call   1040 <get_cursor_offset@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <get_offset_row@plt>
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <get_offset_col@plt>
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     120b <kprint_at+0xbb>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x18(%rbp)
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    $0xf,%ecx
movsbl %al,%edi
call   1050 <print_char@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <get_offset_row@plt>
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <get_offset_col@plt>
mov    %eax,-0xc(%rbp)
jmp    11b0 <kprint_at+0x60>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kprint_at>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    %edx,%eax
or     %esi,%eax
js     116d <kprint_at+0x1d>
mov    %edx,%r14d
mov    %esi,%ebp
mov    %esi,%edi
mov    %edx,%esi
call   1070 <get_offset@plt>
jmp    1189 <kprint_at+0x39>
xor    %eax,%eax
call   1040 <get_cursor_offset@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1060 <get_offset_row@plt>
mov    %eax,%r14d
mov    %ebp,%edi
call   1030 <get_offset_col@plt>
mov    %eax,%ebp
mov    (%rbx),%al
test   %al,%al
je     11d2 <kprint_at+0x82>
add    $0x1,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsbl %al,%edi
mov    %ebp,%esi
mov    %r14d,%edx
mov    $0xf,%ecx
call   1050 <print_char@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1060 <get_offset_row@plt>
mov    %eax,%r14d
mov    %ebp,%edi
call   1030 <get_offset_col@plt>
mov    %eax,%ebp
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
jne    11a0 <kprint_at+0x50>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define void @kprint_at(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%9 = load i32, i32* %5
%10 = icmp sge i32 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %3
%12 = load i32, i32* %6
%13 = icmp sge i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i32, i32* %5
%16 = load i32, i32* %6
%17 = call i32 @get_offset(i32 %15, i32 %16)
store i32 %17, i32* %7
br label %24
18: ; preds = %11, %3
%19 = call i32 (...) @get_cursor_offset()
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = call i32 @get_offset_row(i32 %20)
store i32 %21, i32* %6
%22 = load i32, i32* %7
%23 = call i32 @get_offset_col(i32 %22)
store i32 %23, i32* %5
br label %24
24: ; preds = %18, %14
store i32 0, i32* %8
br label %25
25: ; preds = %33, %24
%26 = load i8*, i8** %4
%27 = load i32, i32* %8
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %47
33: ; preds = %25
%34 = load i8*, i8** %4
%35 = load i32, i32* %8
%36 = add nsw i32 %35, 1
store i32 %36, i32* %8
%37 = sext i32 %35 to i64
%38 = getelementptr inbounds i8, i8* %34, i64 %37
%39 = load i8, i8* %38
%40 = load i32, i32* %5
%41 = load i32, i32* %6
%42 = call i32 @print_char(i8 signext %39, i32 %40, i32 %41, i32 15)
store i32 %42, i32* %7
%43 = load i32, i32* %7
%44 = call i32 @get_offset_row(i32 %43)
store i32 %44, i32* %6
%45 = load i32, i32* %7
%46 = call i32 @get_offset_col(i32 %45)
store i32 %46, i32* %5
br label %25
47: ; preds = %25
ret void
}

LLVM IR (-O2):
define void @kprint_at(i8* nocapture %0, i32 %1, i32 %2) {
%4 = or i32 %2, %1
%5 = icmp sgt i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @get_offset(i32 %1, i32 %2)
br label %12
8: ; preds = %3
%9 = tail call i32 (...) @get_cursor_offset()
%10 = tail call i32 @get_offset_row(i32 %9)
%11 = tail call i32 @get_offset_col(i32 %9)
br label %12
12: ; preds = %8, %6
%13 = phi i32 [ %1, %6 ], [ %11, %8 ]
%14 = phi i32 [ %2, %6 ], [ %10, %8 ]
%15 = load i8, i8* %0
%16 = icmp eq i8 %15, 0
br i1 %16, label %29, label %17
17: ; preds = %12, %17
%18 = phi i64 [ %22, %17 ], [ 0, %12 ]
%19 = phi i8 [ %27, %17 ], [ %15, %12 ]
%20 = phi i32 [ %24, %17 ], [ %14, %12 ]
%21 = phi i32 [ %25, %17 ], [ %13, %12 ]
%22 = add nuw i64 %18, 1
%23 = tail call i32 @print_char(i8 signext %19, i32 %21, i32 %20, i32 15)
%24 = tail call i32 @get_offset_row(i32 %23)
%25 = tail call i32 @get_offset_col(i32 %23)
%26 = getelementptr inbounds i8, i8* %0, i64 %22
%27 = load i8, i8* %26
%28 = icmp eq i8 %27, 0
br i1 %28, label %29, label %17
29: ; preds = %17, %12
ret void
}