#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 /*<<< 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 = 1; i < 25; ++i) {
    memory_copy((char *)(get_screen_offset(0, i) + 0xb8000),
                (char *)(get_screen_offset(0, i - 1) + 0xb8000), 80 * 2);
  }
  char *last_line = (char *)(get_screen_offset(0, 25 - 1) + 0xb8000);
  for (i = 0; i < 80; ++i) {
    last_line[i * 2] = 0;
    last_line[i * 2 + 1] = 0x0f;
  }
  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    1206 <handle_scrolling+0xe6>
movl   $0x1,-0xc(%rbp)
cmpl   $0x19,-0xc(%rbp)
jge    119b <handle_scrolling+0x7b>
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   $0x50,-0xc(%rbp)
jge    11f5 <handle_scrolling+0xd5>
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
shl    $0x1,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0xf,(%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   %r14
push   %rbx
mov    %edi,%r14d
cmp    $0xfa0,%edi
jl     11d6 <handle_scrolling+0xb6>
mov    $0x1,%ebx
nopl   0x0(%rax,%rax,1)
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    $0x19,%ebx
jne    1140 <handle_scrolling+0x20>
xor    %edi,%edi
mov    $0x18,%esi
call   1040 <get_screen_offset@plt>
movaps 0xe77(%rip),%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 %54
10: ; preds = %1
store i32 1, i32* %4
br label %11
11: ; preds = %25, %10
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 25
br i1 %13, label %14, label %28
14: ; preds = %11
%15 = load i32, i32* %4
%16 = call i64 @get_screen_offset(i32 0, i32 %15)
%17 = add nsw i64 %16, 753664
%18 = inttoptr i64 %17 to i8*
%19 = load i32, i32* %4
%20 = sub nsw i32 %19, 1
%21 = call i64 @get_screen_offset(i32 0, i32 %20)
%22 = add nsw i64 %21, 753664
%23 = inttoptr i64 %22 to i8*
%24 = call i32 @memory_copy(i8* %18, i8* %23, i32 160)
br label %25
25: ; preds = %14
%26 = load i32, i32* %4
%27 = add nsw i32 %26, 1
store i32 %27, i32* %4
br label %11
28: ; preds = %11
%29 = call i64 @get_screen_offset(i32 0, i32 24)
%30 = add nsw i64 %29, 753664
%31 = inttoptr i64 %30 to i8*
store i8* %31, i8** %5
store i32 0, i32* %4
br label %32
32: ; preds = %47, %28
%33 = load i32, i32* %4
%34 = icmp slt i32 %33, 80
br i1 %34, label %35, label %50
35: ; preds = %32
%36 = load i8*, i8** %5
%37 = load i32, i32* %4
%38 = mul nsw i32 %37, 2
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %36, i64 %39
store i8 0, i8* %40
%41 = load i8*, i8** %5
%42 = load i32, i32* %4
%43 = mul nsw i32 %42, 2
%44 = add nsw i32 %43, 1
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %41, i64 %45
store i8 15, i8* %46
br label %47
47: ; preds = %35
%48 = load i32, i32* %4
%49 = add nsw i32 %48, 1
store i32 %49, i32* %4
br label %32
50: ; preds = %32
%51 = load i32, i32* %3
%52 = sub nsw i32 %51, 160
store i32 %52, i32* %3
%53 = load i32, i32* %3
store i32 %53, i32* %2
br label %54
54: ; preds = %50, %8
%55 = load i32, i32* %2
ret i32 %55
}

LLVM IR (-O2):
define i32 @handle_scrolling(i32 %0) {
%2 = icmp slt i32 %0, 4000
br i1 %2, label %39, label %3
3: ; preds = %1, %3
%4 = phi i32 [ %13, %3 ], [ 1, %1 ]
%5 = tail call i64 @get_screen_offset(i32 0, i32 %4)
%6 = add nsw i64 %5, 753664
%7 = inttoptr i64 %6 to i8*
%8 = add nsw i32 %4, -1
%9 = tail call i64 @get_screen_offset(i32 0, i32 %8)
%10 = add nsw i64 %9, 753664
%11 = inttoptr i64 %10 to i8*
%12 = tail call i32 @memory_copy(i8* %7, i8* %11, i32 160)
%13 = add nuw nsw i32 %4, 1
%14 = icmp eq i32 %13, 25
br i1 %14, label %15, label %3
15: ; preds = %3
%16 = tail call i64 @get_screen_offset(i32 0, i32 24)
%17 = add nsw i64 %16, 753664
%18 = inttoptr i64 %17 to i8*
%19 = inttoptr i64 %17 to <16 x i8>*
store <16 x i8> <i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15>, <16 x i8>* %19
%20 = getelementptr inbounds i8, i8* %18, i64 16
%21 = bitcast i8* %20 to <16 x i8>*
store <16 x i8> <i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15>, <16 x i8>* %21
%22 = getelementptr inbounds i8, i8* %18, i64 32
%23 = bitcast i8* %22 to <16 x i8>*
store <16 x i8> <i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15>, <16 x i8>* %23
%24 = getelementptr inbounds i8, i8* %18, i64 48
%25 = bitcast i8* %24 to <16 x i8>*
store <16 x i8> <i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15>, <16 x i8>* %25
%26 = getelementptr inbounds i8, i8* %18, i64 64
%27 = bitcast i8* %26 to <16 x i8>*
store <16 x i8> <i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15>, <16 x i8>* %27
%28 = getelementptr inbounds i8, i8* %18, i64 80
%29 = bitcast i8* %28 to <16 x i8>*
store <16 x i8> <i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15>, <16 x i8>* %29
%30 = getelementptr inbounds i8, i8* %18, i64 96
%31 = bitcast i8* %30 to <16 x i8>*
store <16 x i8> <i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15>, <16 x i8>* %31
%32 = getelementptr inbounds i8, i8* %18, i64 112
%33 = bitcast i8* %32 to <16 x i8>*
store <16 x i8> <i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15>, <16 x i8>* %33
%34 = getelementptr inbounds i8, i8* %18, i64 128
%35 = bitcast i8* %34 to <16 x i8>*
store <16 x i8> <i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15>, <16 x i8>* %35
%36 = getelementptr inbounds i8, i8* %18, i64 144
%37 = bitcast i8* %36 to <16 x i8>*
store <16 x i8> <i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15, i8 0, i8 15>, <16 x i8>* %37
%38 = add nsw i32 %0, -160
br label %39
39: ; preds = %1, %15
%40 = phi i32 [ %38, %15 ], [ %0, %1 ]
ret i32 %40
}