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


x86 assembly (clang, -O0):
<handle_scrolling>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    $0xb8000,%eax
mov    %rax,-0x10(%rbp)
cmpl   $0xfa0,-0x8(%rbp)
jge    114c <handle_scrolling+0x2c>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1213 <handle_scrolling+0xf3>
movl   $0x1,-0x14(%rbp)
cmpl   $0x19,-0x14(%rbp)
jge    11b5 <handle_scrolling+0x95>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%esi
xor    %edi,%edi
call   1040 <get_screen_offset@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%esi
sub    $0x1,%esi
xor    %edi,%edi
call   1040 <get_screen_offset@plt>
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdi
add    %rax,%rsi
mov    $0xa0,%edx
call   1030 <memory_copy@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1153 <handle_scrolling+0x33>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
xor    %edi,%edi
mov    $0x18,%esi
call   1040 <get_screen_offset@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x50,-0x14(%rbp)
jge    1202 <handle_scrolling+0xe2>
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11de <handle_scrolling+0xbe>
mov    -0x8(%rbp),%eax
sub    $0xa0,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_scrolling>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
cmp    $0xfa0,%edi
jl     11a3 <handle_scrolling+0x83>
mov    $0x1,%ebx
mov    $0xb8000,%r15d
nopl   (%rax)
xor    %edi,%edi
mov    %ebx,%esi
call   1040 <get_screen_offset@plt>
lea    (%rax,%r15,1),%rbp
lea    -0x1(%rbx),%esi
xor    %edi,%edi
call   1040 <get_screen_offset@plt>
lea    (%rax,%r15,1),%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>
xorps  %xmm0,%xmm0
movups %xmm0,(%r15,%rax,1)
movups %xmm0,0x10(%r15,%rax,1)
movups %xmm0,0x20(%r15,%rax,1)
movups %xmm0,0x30(%r15,%rax,1)
movups %xmm0,0x40(%r15,%rax,1)
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 i8*
%5 = alloca i32
%6 = alloca i8*
store i32 %0, i32* %3
store i8* inttoptr (i64 753664 to i8*), i8** %4
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 4000
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = load i32, i32* %3
store i32 %10, i32* %2
br label %48
11: ; preds = %1
store i32 1, i32* %5
br label %12
12: ; preds = %26, %11
%13 = load i32, i32* %5
%14 = icmp slt i32 %13, 25
br i1 %14, label %15, label %29
15: ; preds = %12
%16 = load i8*, i8** %4
%17 = load i32, i32* %5
%18 = call i64 @get_screen_offset(i32 0, i32 %17)
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i8*, i8** %4
%21 = load i32, i32* %5
%22 = sub nsw i32 %21, 1
%23 = call i64 @get_screen_offset(i32 0, i32 %22)
%24 = getelementptr inbounds i8, i8* %20, i64 %23
%25 = call i32 @memory_copy(i8* %19, i8* %24, i32 160)
br label %26
26: ; preds = %15
%27 = load i32, i32* %5
%28 = add nsw i32 %27, 1
store i32 %28, i32* %5
br label %12
29: ; preds = %12
%30 = load i8*, i8** %4
%31 = call i64 @get_screen_offset(i32 0, i32 24)
%32 = getelementptr inbounds i8, i8* %30, i64 %31
store i8* %32, i8** %6
store i32 0, i32* %5
br label %33
33: ; preds = %41, %29
%34 = load i32, i32* %5
%35 = icmp slt i32 %34, 80
br i1 %35, label %36, label %44
36: ; preds = %33
%37 = load i8*, i8** %6
%38 = load i32, i32* %5
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 0, i8* %40
br label %41
41: ; preds = %36
%42 = load i32, i32* %5
%43 = add nsw i32 %42, 1
store i32 %43, i32* %5
br label %33
44: ; preds = %33
%45 = load i32, i32* %3
%46 = sub nsw i32 %45, 160
store i32 %46, i32* %3
%47 = load i32, i32* %3
store i32 %47, i32* %2
br label %48
48: ; preds = %44, %9
%49 = load i32, i32* %2
ret i32 %49
}

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 ], [ 1, %1 ]
%5 = tail call i64 @get_screen_offset(i32 0, i32 %4)
%6 = getelementptr inbounds i8, i8* inttoptr (i64 753664 to i8*), i64 %5
%7 = add nsw i32 %4, -1
%8 = tail call i64 @get_screen_offset(i32 0, i32 %7)
%9 = getelementptr inbounds i8, i8* inttoptr (i64 753664 to i8*), i64 %8
%10 = tail call i32 @memory_copy(i8* %6, i8* %9, i32 160)
%11 = add nuw nsw i32 %4, 1
%12 = icmp eq i32 %11, 25
br i1 %12, label %13, label %3
13: ; preds = %3
%14 = tail call i64 @get_screen_offset(i32 0, i32 24)
%15 = getelementptr inbounds i8, i8* inttoptr (i64 753664 to i8*), i64 %14
%16 = add nsw i32 %0, -160
br label %17
17: ; preds = %1, %13
%18 = phi i32 [ %16, %13 ], [ %0, %1 ]
ret i32 %18
}