#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_screen_offset(int, int /*<<< orphan*/);
int /*<<< orphan*/ mem_copy(unsigned char *, unsigned char *, int);
int handle_scroll(int offset) {
  int i;
  if (offset < 25 * 80 * 2)
    return offset;
  unsigned char *vm = (unsigned char *)0xb8000;
  mem_copy(vm + get_screen_offset(1, 0), vm, (25 - 1) * 80 * 2);
  offset = get_screen_offset(25 - 1, 0);
  for (i = 0; i < 80; i++)
    vm[offset + i * 2] = 0;
  return offset;
}


x86 assembly (clang, -O0):
<handle_scroll>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0xfa0,-0x8(%rbp)
jge    1143 <handle_scroll+0x23>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c1 <handle_scroll+0xa1>
mov    $0xb8000,%eax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x1,%edi
xor    %esi,%esi
call   1040 <get_screen_offset@plt>
mov    -0x20(%rbp),%rdi
cltq
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    $0xf00,%edx
call   1030 <mem_copy@plt>
mov    $0x18,%edi
xor    %esi,%esi
call   1040 <get_screen_offset@plt>
mov    %eax,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x50,-0xc(%rbp)
jge    11bb <handle_scroll+0x9b>
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%ecx
mov    -0xc(%rbp),%edx
shl    $0x1,%edx
add    %edx,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    118d <handle_scroll+0x6d>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_scroll>:
mov    %edi,%eax
cmp    $0xfa0,%edi
jl     1196 <handle_scroll+0x76>
push   %rax
mov    $0x1,%edi
xor    %esi,%esi
call   1040 <get_screen_offset@plt>
movslq %eax,%rdi
add    $0xb8000,%rdi
mov    $0xb8000,%esi
mov    $0xf00,%edx
call   1030 <mem_copy@plt>
mov    $0x18,%edi
xor    %esi,%esi
call   1040 <get_screen_offset@plt>
movslq %eax,%rcx
add    $0xb8000,%rcx
mov    $0x4,%edx
nopl   0x0(%rax,%rax,1)
movb   $0x0,-0x8(%rcx,%rdx,2)
movb   $0x0,-0x6(%rcx,%rdx,2)
movb   $0x0,-0x4(%rcx,%rdx,2)
movb   $0x0,-0x2(%rcx,%rdx,2)
movb   $0x0,(%rcx,%rdx,2)
add    $0x5,%rdx
cmp    $0x54,%rdx
jne    1170 <handle_scroll+0x50>
add    $0x8,%rsp
ret

LLVM IR (-O0):
define i32 @handle_scroll(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = icmp slt i32 %6, 4000
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = load i32, i32* %3
store i32 %9, i32* %2
br label %34
10: ; preds = %1
store i8* inttoptr (i64 753664 to i8*), i8** %5
%11 = load i8*, i8** %5
%12 = call i32 @get_screen_offset(i32 1, i32 0)
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %11, i64 %13
%15 = load i8*, i8** %5
%16 = call i32 @mem_copy(i8* %14, i8* %15, i32 3840)
%17 = call i32 @get_screen_offset(i32 24, i32 0)
store i32 %17, i32* %3
store i32 0, i32* %4
br label %18
18: ; preds = %29, %10
%19 = load i32, i32* %4
%20 = icmp slt i32 %19, 80
br i1 %20, label %21, label %32
21: ; preds = %18
%22 = load i8*, i8** %5
%23 = load i32, i32* %3
%24 = load i32, i32* %4
%25 = mul nsw i32 %24, 2
%26 = add nsw i32 %23, %25
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %22, i64 %27
store i8 0, i8* %28
br label %29
29: ; preds = %21
%30 = load i32, i32* %4
%31 = add nsw i32 %30, 1
store i32 %31, i32* %4
br label %18
32: ; preds = %18
%33 = load i32, i32* %3
store i32 %33, i32* %2
br label %34
34: ; preds = %32, %8
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
define i32 @handle_scroll(i32 %0) {
%2 = icmp slt i32 %0, 4000
br i1 %2, label %33, label %3
3: ; preds = %1
%4 = tail call i32 @get_screen_offset(i32 1, i32 0)
%5 = sext i32 %4 to i64
%6 = getelementptr inbounds i8, i8* inttoptr (i64 753664 to i8*), i64 %5
%7 = tail call i32 @mem_copy(i8* %6, i8* inttoptr (i64 753664 to i8*), i32 3840)
%8 = tail call i32 @get_screen_offset(i32 24, i32 0)
%9 = sext i32 %8 to i64
br label %10
10: ; preds = %10, %3
%11 = phi i64 [ 0, %3 ], [ %31, %10 ]
%12 = shl nuw nsw i64 %11, 1
%13 = add nsw i64 %12, %9
%14 = getelementptr inbounds i8, i8* inttoptr (i64 753664 to i8*), i64 %13
store i8 0, i8* %14
%15 = shl nuw i64 %11, 1
%16 = add i64 %15, 2
%17 = add nsw i64 %16, %9
%18 = getelementptr inbounds i8, i8* inttoptr (i64 753664 to i8*), i64 %17
store i8 0, i8* %18
%19 = shl nuw i64 %11, 1
%20 = add i64 %19, 4
%21 = add nsw i64 %20, %9
%22 = getelementptr inbounds i8, i8* inttoptr (i64 753664 to i8*), i64 %21
store i8 0, i8* %22
%23 = shl nuw i64 %11, 1
%24 = add i64 %23, 6
%25 = add nsw i64 %24, %9
%26 = getelementptr inbounds i8, i8* inttoptr (i64 753664 to i8*), i64 %25
store i8 0, i8* %26
%27 = shl nuw i64 %11, 1
%28 = add i64 %27, 8
%29 = add nsw i64 %28, %9
%30 = getelementptr inbounds i8, i8* inttoptr (i64 753664 to i8*), i64 %29
store i8 0, i8* %30
%31 = add nuw nsw i64 %11, 5
%32 = icmp eq i64 %31, 80
br i1 %32, label %33, label %10
33: ; preds = %10, %1
%34 = phi i32 [ %0, %1 ], [ %8, %10 ]
ret i32 %34
}