#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 _vga_get_video_memory_offset(int, int);
int /*<<< orphan*/ port_io_write_byte(int, short const);
void vga_set_cursor_position(int row, int column) {
  const short CURSOR_POSITION_HIGH_BYTE_REGISTER = 14;
  const short CURSOR_POSITION_LOW_BYTE_REGISTER = 15;
  const int VIDEO_MEMORY_CURSOR_CHAR_OFFSET =
      _vga_get_video_memory_offset(row, column) / 2;
  if (row < 0 || column < 0 || row > 25 || column > 80) {
    return;
  }
  port_io_write_byte(0x3D4, CURSOR_POSITION_HIGH_BYTE_REGISTER);
  port_io_write_byte(0x3D5,
                     (unsigned char)(VIDEO_MEMORY_CURSOR_CHAR_OFFSET >> 8));
  port_io_write_byte(0x3D4, CURSOR_POSITION_LOW_BYTE_REGISTER);
  port_io_write_byte(0x3D5, (unsigned char)VIDEO_MEMORY_CURSOR_CHAR_OFFSET);
}


x86 assembly (clang, -O0):
<vga_set_cursor_position>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movw   $0xe,-0xa(%rbp)
movw   $0xf,-0xc(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1030 <_vga_get_video_memory_offset@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x4(%rbp)
jl     1178 <vga_set_cursor_position+0x58>
cmpl   $0x0,-0x8(%rbp)
jl     1178 <vga_set_cursor_position+0x58>
cmpl   $0x19,-0x4(%rbp)
jg     1178 <vga_set_cursor_position+0x58>
cmpl   $0x50,-0x8(%rbp)
jle    117d <vga_set_cursor_position+0x5d>
jmp    11c4 <vga_set_cursor_position+0xa4>
mov    $0x3d4,%edi
mov    $0xe,%esi
call   1040 <port_io_write_byte@plt>
mov    -0x10(%rbp),%eax
sar    $0x8,%eax
movzbl %al,%eax
mov    $0x3d5,%edi
movswl %ax,%esi
call   1040 <port_io_write_byte@plt>
mov    $0x3d4,%edi
mov    $0xf,%esi
call   1040 <port_io_write_byte@plt>
mov    -0x10(%rbp),%eax
movzbl %al,%eax
mov    $0x3d5,%edi
movswl %ax,%esi
call   1040 <port_io_write_byte@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vga_set_cursor_position>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
call   1030 <_vga_get_video_memory_offset@plt>
cmp    $0x50,%ebx
jg     1181 <vga_set_cursor_position+0x61>
cmp    $0x19,%ebp
jg     1181 <vga_set_cursor_position+0x61>
or     %ebp,%ebx
js     1181 <vga_set_cursor_position+0x61>
mov    %eax,%ebx
shr    $0x1f,%ebx
add    %eax,%ebx
sar    %ebx
mov    $0x3d4,%edi
mov    $0xe,%esi
call   1040 <port_io_write_byte@plt>
movzbl %bh,%esi
mov    $0x3d5,%edi
call   1040 <port_io_write_byte@plt>
mov    $0x3d4,%edi
mov    $0xf,%esi
call   1040 <port_io_write_byte@plt>
movzbl %bl,%esi
mov    $0x3d5,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <port_io_write_byte@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define void @vga_set_cursor_position(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i16
%6 = alloca i16
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i16 14, i16* %5
store i16 15, i16* %6
%8 = load i32, i32* %3
%9 = load i32, i32* %4
%10 = call i32 @_vga_get_video_memory_offset(i32 %8, i32 %9)
%11 = sdiv i32 %10, 2
store i32 %11, i32* %7
%12 = load i32, i32* %3
%13 = icmp slt i32 %12, 0
br i1 %13, label %23, label %14
14: ; preds = %2
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 0
br i1 %16, label %23, label %17
17: ; preds = %14
%18 = load i32, i32* %3
%19 = icmp sgt i32 %18, 25
br i1 %19, label %23, label %20
20: ; preds = %17
%21 = load i32, i32* %4
%22 = icmp sgt i32 %21, 80
br i1 %22, label %23, label %24
23: ; preds = %20, %17, %14, %2
br label %36
24: ; preds = %20
%25 = call i32 @port_io_write_byte(i32 980, i16 signext 14)
%26 = load i32, i32* %7
%27 = ashr i32 %26, 8
%28 = trunc i32 %27 to i8
%29 = zext i8 %28 to i16
%30 = call i32 @port_io_write_byte(i32 981, i16 signext %29)
%31 = call i32 @port_io_write_byte(i32 980, i16 signext 15)
%32 = load i32, i32* %7
%33 = trunc i32 %32 to i8
%34 = zext i8 %33 to i16
%35 = call i32 @port_io_write_byte(i32 981, i16 signext %34)
br label %36
36: ; preds = %24, %23
ret void
}

LLVM IR (-O2):
define void @vga_set_cursor_position(i32 %0, i32 %1) {
%3 = tail call i32 @_vga_get_video_memory_offset(i32 %0, i32 %1)
%4 = or i32 %1, %0
%5 = icmp slt i32 %4, 0
%6 = icmp sgt i32 %0, 25
%7 = or i1 %6, %5
%8 = icmp sgt i32 %1, 80
%9 = or i1 %8, %7
br i1 %9, label %19, label %10
10: ; preds = %2
%11 = sdiv i32 %3, 2
%12 = tail call i32 @port_io_write_byte(i32 980, i16 signext 14)
%13 = trunc i32 %11 to i16
%14 = lshr i16 %13, 8
%15 = tail call i32 @port_io_write_byte(i32 981, i16 signext %14)
%16 = tail call i32 @port_io_write_byte(i32 980, i16 signext 15)
%17 = and i16 %13, 255
%18 = tail call i32 @port_io_write_byte(i32 981, i16 signext %17)
br label %19
19: ; preds = %2, %10
ret void
}