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


x86 assembly (clang, -O0):
<handle_scrolling>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0xfa0,-0x8(%rbp)
jge    1143 <handle_scrolling+0x23>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f0 <handle_scrolling+0xd0>
movl   $0x1,-0xc(%rbp)
cmpl   $0x19,-0xc(%rbp)
jge    119b <handle_scrolling+0x7b>
mov    -0xc(%rbp),%edi
xor    %esi,%esi
call   1030 <get_screen_offset@plt>
add    $0xb8000,%rax
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%edi
sub    $0x1,%edi
xor    %esi,%esi
call   1030 <get_screen_offset@plt>
mov    -0x28(%rbp),%rdi
mov    %rax,%rsi
add    $0xb8000,%rsi
mov    $0xa0,%edx
call   1040 <memcpy@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    114a <handle_scrolling+0x2a>
mov    $0x18,%edi
xor    %esi,%esi
call   1030 <get_screen_offset@plt>
add    $0xb8000,%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0xa0,-0x1c(%rbp)
jge    11df <handle_scrolling+0xbf>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%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    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_scrolling>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%r14d
cmp    $0xfa0,%edi
jl     11d2 <handle_scrolling+0xb2>
mov    $0x1,%ebx
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
xor    %esi,%esi
call   1030 <get_screen_offset@plt>
lea    0xb8000(%rax),%rbp
lea    -0x1(%rbx),%edi
xor    %esi,%esi
call   1030 <get_screen_offset@plt>
lea    0xb8000(%rax),%rsi
mov    %rbp,%rdi
mov    $0xa0,%edx
call   1040 <memcpy@plt>
add    $0x1,%ebx
cmp    $0x19,%ebx
jne    1140 <handle_scrolling+0x20>
mov    $0x18,%edi
xor    %esi,%esi
call   1030 <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*
%6 = alloca i32
store i32 %0, i32* %3
%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* %4
br label %12
12: ; preds = %26, %11
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 25
br i1 %14, label %15, label %29
15: ; preds = %12
%16 = load i32, i32* %4
%17 = call i64 @get_screen_offset(i32 %16, i32 0)
%18 = inttoptr i64 %17 to i8*
%19 = getelementptr inbounds i8, i8* %18, i64 753664
%20 = load i32, i32* %4
%21 = sub nsw i32 %20, 1
%22 = call i64 @get_screen_offset(i32 %21, i32 0)
%23 = inttoptr i64 %22 to i8*
%24 = getelementptr inbounds i8, i8* %23, i64 753664
%25 = call i32 @memcpy(i8* %19, i8* %24, i32 160)
br label %26
26: ; preds = %15
%27 = load i32, i32* %4
%28 = add nsw i32 %27, 1
store i32 %28, i32* %4
br label %12
29: ; preds = %12
%30 = call i64 @get_screen_offset(i32 24, i32 0)
%31 = inttoptr i64 %30 to i8*
%32 = getelementptr inbounds i8, i8* %31, i64 753664
store i8* %32, i8** %5
store i32 0, i32* %6
br label %33
33: ; preds = %41, %29
%34 = load i32, i32* %6
%35 = icmp slt i32 %34, 160
br i1 %35, label %36, label %44
36: ; preds = %33
%37 = load i8*, i8** %5
%38 = load i32, i32* %6
%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* %6
%43 = add nsw i32 %42, 1
store i32 %43, i32* %6
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 %20, label %8
3: ; preds = %8
%4 = tail call i64 @get_screen_offset(i32 24, i32 0)
%5 = inttoptr i64 %4 to i8*
%6 = getelementptr inbounds i8, i8* %5, i64 753664
%7 = add nsw i32 %0, -160
br label %20
8: ; preds = %1, %8
%9 = phi i32 [ %18, %8 ], [ 1, %1 ]
%10 = tail call i64 @get_screen_offset(i32 %9, i32 0)
%11 = inttoptr i64 %10 to i8*
%12 = getelementptr inbounds i8, i8* %11, i64 753664
%13 = add nsw i32 %9, -1
%14 = tail call i64 @get_screen_offset(i32 %13, i32 0)
%15 = inttoptr i64 %14 to i8*
%16 = getelementptr inbounds i8, i8* %15, i64 753664
%17 = tail call i32 @memcpy(i8* %12, i8* %16, i32 160)
%18 = add nuw nsw i32 %9, 1
%19 = icmp eq i32 %18, 25
br i1 %19, label %3, label %8
20: ; preds = %1, %3
%21 = phi i32 [ %7, %3 ], [ %0, %1 ]
ret i32 %21
}