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


x86 assembly (clang, -O0):
<handle_scrolling>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0xfa0,-0x8(%rbp)
jge    1143 <handle_scrolling+0x23>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f3 <handle_scrolling+0xd3>
movl   $0x0,-0xc(%rbp)
cmpl   $0xa0,-0xc(%rbp)
jge    119e <handle_scrolling+0x7e>
mov    -0xc(%rbp),%esi
xor    %edi,%edi
call   1040 <get_screen_offset@plt>
add    $0xb8000,%rax
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%esi
sub    $0x1,%esi
xor    %edi,%edi
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    114a <handle_scrolling+0x2a>
xor    %edi,%edi
mov    $0x18,%esi
call   1040 <get_screen_offset@plt>
add    $0xb8000,%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0xa0,-0xc(%rbp)
jge    11e2 <handle_scrolling+0xc2>
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    11bb <handle_scrolling+0x9b>
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   %r14
push   %rbx
mov    %edi,%r14d
cmp    $0xfa0,%edi
jl     11d5 <handle_scrolling+0xb5>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
xor    %edi,%edi
mov    %ebx,%esi
call   1040 <get_screen_offset@plt>
lea    0xb8000(%rax),%rbp
lea    -0x1(%rbx),%esi
xor    %edi,%edi
call   1040 <get_screen_offset@plt>
lea    0xb8000(%rax),%rsi
mov    %rbp,%rdi
mov    $0xa0,%edx
call   1030 <memory_copy@plt>
add    $0x1,%ebx
cmp    $0xa0,%ebx
jne    1140 <handle_scrolling+0x20>
xor    %edi,%edi
mov    $0x18,%esi
call   1040 <get_screen_offset@plt>
xorps  %xmm0,%xmm0
movups %xmm0,0xb8000(%rax)
movups %xmm0,0xb8010(%rax)
movups %xmm0,0xb8020(%rax)
movups %xmm0,0xb8030(%rax)
movups %xmm0,0xb8040(%rax)
movups %xmm0,0xb8050(%rax)
movups %xmm0,0xb8060(%rax)
movups %xmm0,0xb8070(%rax)
movups %xmm0,0xb8080(%rax)
movups %xmm0,0xb8090(%rax)
add    $0xffffff60,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
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 = 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 %44
10: ; preds = %1
store i32 0, i32* %4
br label %11
11: ; preds = %23, %10
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 160
br i1 %13, label %14, label %26
14: ; preds = %11
%15 = load i32, i32* %4
%16 = call i8* @get_screen_offset(i32 0, i32 %15)
%17 = getelementptr inbounds i8, i8* %16, i64 753664
%18 = load i32, i32* %4
%19 = sub nsw i32 %18, 1
%20 = call i8* @get_screen_offset(i32 0, i32 %19)
%21 = getelementptr inbounds i8, i8* %20, i64 753664
%22 = call i32 @memory_copy(i8* %17, i8* %21, i32 160)
br label %23
23: ; preds = %14
%24 = load i32, i32* %4
%25 = add nsw i32 %24, 1
store i32 %25, i32* %4
br label %11
26: ; preds = %11
%27 = call i8* @get_screen_offset(i32 0, i32 24)
%28 = getelementptr inbounds i8, i8* %27, i64 753664
store i8* %28, i8** %5
store i32 0, i32* %4
br label %29
29: ; preds = %37, %26
%30 = load i32, i32* %4
%31 = icmp slt i32 %30, 160
br i1 %31, label %32, label %40
32: ; preds = %29
%33 = load i8*, i8** %5
%34 = load i32, i32* %4
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
store i8 0, i8* %36
br label %37
37: ; preds = %32
%38 = load i32, i32* %4
%39 = add nsw i32 %38, 1
store i32 %39, i32* %4
br label %29
40: ; preds = %29
%41 = load i32, i32* %3
%42 = sub nsw i32 %41, 160
store i32 %42, i32* %3
%43 = load i32, i32* %3
store i32 %43, i32* %2
br label %44
44: ; preds = %40, %8
%45 = load i32, i32* %2
ret i32 %45
}

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