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


x86 assembly (clang, -O0):
<handle_scrolling>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    $0x18,%edi
xor    %esi,%esi
call   1040 <get_screen_offset@plt>
add    $0xb8000,%rax
mov    %rax,-0x18(%rbp)
cmpl   $0xfa0,-0x8(%rbp)
jge    1159 <handle_scrolling+0x39>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f0 <handle_scrolling+0xd0>
movl   $0x0,-0xc(%rbp)
cmpl   $0x19,-0xc(%rbp)
jge    11b1 <handle_scrolling+0x91>
mov    -0xc(%rbp),%edi
xor    %esi,%esi
call   1040 <get_screen_offset@plt>
add    $0xb8000,%rax
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
sub    $0x1,%edi
xor    %esi,%esi
call   1040 <get_screen_offset@plt>
mov    -0x20(%rbp),%rdi
mov    %rax,%rsi
add    $0xb8000,%rsi
mov    $0xa0,%edx
call   1030 <memory_copy@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1160 <handle_scrolling+0x40>
movl   $0x0,-0xc(%rbp)
cmpl   $0xa0,-0xc(%rbp)
jge    11df <handle_scrolling+0xbf>
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11b8 <handle_scrolling+0x98>
mov    -0x8(%rbp),%eax
sub    $0xa0,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_scrolling>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
xor    %ebp,%ebp
mov    $0x18,%edi
xor    %esi,%esi
call   1040 <get_screen_offset@plt>
cmp    $0xfa0,%r14d
jl     11c7 <handle_scrolling+0xa7>
mov    %rax,%r15
add    $0xb8000,%r15
nop
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <get_screen_offset@plt>
lea    0xb8000(%rax),%rbx
lea    -0x1(%rbp),%edi
xor    %esi,%esi
call   1040 <get_screen_offset@plt>
lea    0xb8000(%rax),%rsi
mov    %rbx,%rdi
mov    $0xa0,%edx
call   1030 <memory_copy@plt>
add    $0x1,%ebp
cmp    $0x19,%ebp
jne    1150 <handle_scrolling+0x30>
xorps  %xmm0,%xmm0
movups %xmm0,0x90(%r15)
movups %xmm0,0x80(%r15)
movups %xmm0,0x70(%r15)
movups %xmm0,0x60(%r15)
movups %xmm0,0x50(%r15)
movups %xmm0,0x40(%r15)
movups %xmm0,0x30(%r15)
movups %xmm0,0x20(%r15)
movups %xmm0,0x10(%r15)
movups %xmm0,(%r15)
add    $0xffffff60,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @handle_scrolling(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i32 %0, i32* %3
%6 = call i64 @get_screen_offset(i32 24, i32 0)
%7 = inttoptr i64 %6 to i8*
%8 = getelementptr inbounds i8, i8* %7, i64 753664
store i8* %8, i8** %5
%9 = load i32, i32* %3
%10 = icmp slt i32 %9, 4000
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = load i32, i32* %3
store i32 %12, i32* %2
br label %47
13: ; preds = %1
store i32 0, i32* %4
br label %14
14: ; preds = %28, %13
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 25
br i1 %16, label %17, label %31
17: ; preds = %14
%18 = load i32, i32* %4
%19 = call i64 @get_screen_offset(i32 %18, i32 0)
%20 = add nsw i64 %19, 753664
%21 = inttoptr i64 %20 to i8*
%22 = load i32, i32* %4
%23 = sub nsw i32 %22, 1
%24 = call i64 @get_screen_offset(i32 %23, i32 0)
%25 = add nsw i64 %24, 753664
%26 = inttoptr i64 %25 to i8*
%27 = call i32 @memory_copy(i8* %21, i8* %26, i32 160)
br label %28
28: ; preds = %17
%29 = load i32, i32* %4
%30 = add nsw i32 %29, 1
store i32 %30, i32* %4
br label %14
31: ; preds = %14
store i32 0, i32* %4
br label %32
32: ; preds = %40, %31
%33 = load i32, i32* %4
%34 = icmp slt i32 %33, 160
br i1 %34, label %35, label %43
35: ; preds = %32
%36 = load i8*, i8** %5
%37 = load i32, i32* %4
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
store i8 0, i8* %39
br label %40
40: ; preds = %35
%41 = load i32, i32* %4
%42 = add nsw i32 %41, 1
store i32 %42, i32* %4
br label %32
43: ; preds = %32
%44 = load i32, i32* %3
%45 = sub nsw i32 %44, 160
store i32 %45, i32* %3
%46 = load i32, i32* %3
store i32 %46, i32* %2
br label %47
47: ; preds = %43, %11
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
define i32 @handle_scrolling(i32 %0) {
%2 = tail call i64 @get_screen_offset(i32 24, i32 0)
%3 = inttoptr i64 %2 to i8*
%4 = getelementptr i8, i8* %3, i64 753664
%5 = icmp slt i32 %0, 4000
br i1 %5, label %20, label %6
6: ; preds = %1, %6
%7 = phi i32 [ %16, %6 ], [ 0, %1 ]
%8 = tail call i64 @get_screen_offset(i32 %7, i32 0)
%9 = add nsw i64 %8, 753664
%10 = inttoptr i64 %9 to i8*
%11 = add nsw i32 %7, -1
%12 = tail call i64 @get_screen_offset(i32 %11, i32 0)
%13 = add nsw i64 %12, 753664
%14 = inttoptr i64 %13 to i8*
%15 = tail call i32 @memory_copy(i8* %10, i8* %14, i32 160)
%16 = add nuw nsw i32 %7, 1
%17 = icmp eq i32 %16, 25
br i1 %17, label %18, label %6
18: ; preds = %6
%19 = add nsw i32 %0, -160
br label %20
20: ; preds = %1, %18
%21 = phi i32 [ %19, %18 ], [ %0, %1 ]
ret i32 %21
}