#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 */
int fb_ConsoleGetBotRow();
int fb_ConsoleGetTopRow();
int /*<<< orphan*/ fb_ConsoleSetTopBotRows(int, int);
int /*<<< orphan*/ fb_GetSize(int /*<<< orphan*/, int *);
int /*<<< orphan*/ fb_Locate(int, int, int, int /*<<< orphan*/,
                             int /*<<< orphan*/);
int /*<<< orphan*/ fb_ViewUpdate();
int fb_ConsoleViewEx(int toprow, int botrow, int set_cursor) {
  int do_update = 0;
  int maxrow, minrow;
  minrow = 1;
  fb_GetSize(0, &maxrow);
  if (maxrow == 0)
    maxrow = 25;
  if (toprow > 0) {
    do_update = 1;
  } else if (toprow == 0) {
    do_update = 1;
    toprow = minrow;
  } else {
    toprow = fb_ConsoleGetTopRow() + 1;
  }
  if (botrow > 0) {
    do_update = 1;
  } else if (botrow == 0) {
    do_update = 1;
    botrow = maxrow;
  } else {
    botrow = fb_ConsoleGetBotRow() + 1;
  }
  if (toprow > botrow || toprow < 1 || botrow < 1 || toprow > maxrow ||
      botrow > maxrow) {
    do_update = 0;
    botrow = toprow = 0;
  }
  if (do_update) {
    fb_ConsoleSetTopBotRows(toprow - 1, botrow - 1);
    fb_ViewUpdate();
    if (set_cursor) {
      fb_Locate(toprow, 1, -1, 0, 0);
    }
  }
  return toprow + (botrow << 16);
}


x86 assembly (clang, -O0):
<fb_ConsoleViewEx>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x1,-0x18(%rbp)
xor    %edi,%edi
lea    -0x14(%rbp),%rsi
call   1080 <fb_GetSize@plt>
cmpl   $0x0,-0x14(%rbp)
jne    119b <fb_ConsoleViewEx+0x3b>
movl   $0x19,-0x14(%rbp)
cmpl   $0x0,-0x4(%rbp)
jle    11b1 <fb_ConsoleViewEx+0x51>
movl   $0x1,-0x10(%rbp)
jmp    11df <fb_ConsoleViewEx+0x7f>
cmpl   $0x0,-0x4(%rbp)
jne    11cd <fb_ConsoleViewEx+0x6d>
movl   $0x1,-0x10(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11da <fb_ConsoleViewEx+0x7a>
mov    $0x0,%al
call   1050 <fb_ConsoleGetTopRow@plt>
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    11df <fb_ConsoleViewEx+0x7f>
cmpl   $0x0,-0x8(%rbp)
jle    11f5 <fb_ConsoleViewEx+0x95>
movl   $0x1,-0x10(%rbp)
jmp    1223 <fb_ConsoleViewEx+0xc3>
cmpl   $0x0,-0x8(%rbp)
jne    1211 <fb_ConsoleViewEx+0xb1>
movl   $0x1,-0x10(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x8(%rbp)
jmp    121e <fb_ConsoleViewEx+0xbe>
mov    $0x0,%al
call   1070 <fb_ConsoleGetBotRow@plt>
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1223 <fb_ConsoleViewEx+0xc3>
mov    -0x4(%rbp),%eax
cmp    -0x8(%rbp),%eax
jg     125b <fb_ConsoleViewEx+0xfb>
cmpl   $0x1,-0x4(%rbp)
jl     125b <fb_ConsoleViewEx+0xfb>
cmpl   $0x1,-0x8(%rbp)
jl     125b <fb_ConsoleViewEx+0xfb>
mov    -0x4(%rbp),%eax
cmp    -0x14(%rbp),%eax
jg     125b <fb_ConsoleViewEx+0xfb>
mov    -0x8(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    1270 <fb_ConsoleViewEx+0x110>
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     12b9 <fb_ConsoleViewEx+0x159>
mov    -0x4(%rbp),%edi
sub    $0x1,%edi
mov    -0x8(%rbp),%esi
sub    $0x1,%esi
call   1030 <fb_ConsoleSetTopBotRows@plt>
mov    $0x0,%al
call   1060 <fb_ViewUpdate@plt>
cmpl   $0x0,-0xc(%rbp)
je     12b4 <fb_ConsoleViewEx+0x154>
mov    -0x4(%rbp),%edi
mov    $0x1,%esi
mov    $0xffffffff,%edx
xor    %r8d,%r8d
mov    %r8d,%ecx
call   1040 <fb_Locate@plt>
jmp    12b9 <fb_ConsoleViewEx+0x159>
mov    -0x4(%rbp),%eax
mov    -0x8(%rbp),%ecx
shl    $0x10,%ecx
add    %ecx,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fb_ConsoleViewEx>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r15d
mov    %esi,%r14d
mov    %edi,%ebx
lea    0xc(%rsp),%rsi
xor    %edi,%edi
call   1080 <fb_GetSize@plt>
cmpl   $0x0,0xc(%rsp)
jne    118f <fb_ConsoleViewEx+0x2f>
movl   $0x19,0xc(%rsp)
mov    $0x1,%r12d
mov    $0x1,%ebp
test   %ebx,%ebx
jle    11b8 <fb_ConsoleViewEx+0x58>
test   %r14d,%r14d
jg     11e5 <fb_ConsoleViewEx+0x85>
je     11cf <fb_ConsoleViewEx+0x6f>
xor    %eax,%eax
call   1070 <fb_ConsoleGetBotRow@plt>
mov    %eax,%r14d
add    $0x1,%r14d
mov    %ebp,%r12d
jmp    11e5 <fb_ConsoleViewEx+0x85>
je     11d6 <fb_ConsoleViewEx+0x76>
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <fb_ConsoleGetTopRow@plt>
mov    %eax,%ebx
add    $0x1,%ebx
test   %r14d,%r14d
jg     11e5 <fb_ConsoleViewEx+0x85>
jmp    11a3 <fb_ConsoleViewEx+0x43>
mov    0xc(%rsp),%r14d
jmp    11e5 <fb_ConsoleViewEx+0x85>
mov    $0x1,%ebp
mov    $0x1,%ebx
test   %r14d,%r14d
jle    11a3 <fb_ConsoleViewEx+0x43>
xor    %ecx,%ecx
test   %r14d,%r14d
jle    1241 <fb_ConsoleViewEx+0xe1>
test   %ebx,%ebx
jle    1241 <fb_ConsoleViewEx+0xe1>
cmp    %r14d,%ebx
jg     1241 <fb_ConsoleViewEx+0xe1>
mov    0xc(%rsp),%edx
cmp    %edx,%ebx
jg     1241 <fb_ConsoleViewEx+0xe1>
mov    $0x0,%eax
cmp    %edx,%r14d
jg     1243 <fb_ConsoleViewEx+0xe3>
test   %r12d,%r12d
je     123a <fb_ConsoleViewEx+0xda>
lea    -0x1(%rbx),%edi
lea    -0x1(%r14),%esi
call   1030 <fb_ConsoleSetTopBotRows@plt>
xor    %eax,%eax
call   1060 <fb_ViewUpdate@plt>
test   %r15d,%r15d
je     123a <fb_ConsoleViewEx+0xda>
mov    %ebx,%edi
mov    $0x1,%esi
mov    $0xffffffff,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1040 <fb_Locate@plt>
mov    %ebx,%ecx
mov    %r14d,%eax
jmp    1243 <fb_ConsoleViewEx+0xe3>
xor    %eax,%eax
shl    $0x10,%eax
add    %ecx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @fb_ConsoleViewEx(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 0, i32* %7
store i32 1, i32* %9
%10 = call i32 @fb_GetSize(i32 0, i32* %8)
%11 = load i32, i32* %8
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %3
store i32 25, i32* %8
br label %14
14: ; preds = %13, %3
%15 = load i32, i32* %4
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %14
store i32 1, i32* %7
br label %27
18: ; preds = %14
%19 = load i32, i32* %4
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
store i32 1, i32* %7
%22 = load i32, i32* %9
store i32 %22, i32* %4
br label %26
23: ; preds = %18
%24 = call i32 (...) @fb_ConsoleGetTopRow()
%25 = add nsw i32 %24, 1
store i32 %25, i32* %4
br label %26
26: ; preds = %23, %21
br label %27
27: ; preds = %26, %17
%28 = load i32, i32* %5
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %27
store i32 1, i32* %7
br label %40
31: ; preds = %27
%32 = load i32, i32* %5
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31
store i32 1, i32* %7
%35 = load i32, i32* %8
store i32 %35, i32* %5
br label %39
36: ; preds = %31
%37 = call i32 (...) @fb_ConsoleGetBotRow()
%38 = add nsw i32 %37, 1
store i32 %38, i32* %5
br label %39
39: ; preds = %36, %34
br label %40
40: ; preds = %39, %30
%41 = load i32, i32* %4
%42 = load i32, i32* %5
%43 = icmp sgt i32 %41, %42
br i1 %43, label %58, label %44
44: ; preds = %40
%45 = load i32, i32* %4
%46 = icmp slt i32 %45, 1
br i1 %46, label %58, label %47
47: ; preds = %44
%48 = load i32, i32* %5
%49 = icmp slt i32 %48, 1
br i1 %49, label %58, label %50
50: ; preds = %47
%51 = load i32, i32* %4
%52 = load i32, i32* %8
%53 = icmp sgt i32 %51, %52
br i1 %53, label %58, label %54
54: ; preds = %50
%55 = load i32, i32* %5
%56 = load i32, i32* %8
%57 = icmp sgt i32 %55, %56
br i1 %57, label %58, label %59
58: ; preds = %54, %50, %47, %44, %40
store i32 0, i32* %7
store i32 0, i32* %4
store i32 0, i32* %5
br label %59
59: ; preds = %58, %54
%60 = load i32, i32* %7
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %75
62: ; preds = %59
%63 = load i32, i32* %4
%64 = sub nsw i32 %63, 1
%65 = load i32, i32* %5
%66 = sub nsw i32 %65, 1
%67 = call i32 @fb_ConsoleSetTopBotRows(i32 %64, i32 %66)
%68 = call i32 (...) @fb_ViewUpdate()
%69 = load i32, i32* %6
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %74
71: ; preds = %62
%72 = load i32, i32* %4
%73 = call i32 @fb_Locate(i32 %72, i32 1, i32 -1, i32 0, i32 0)
br label %74
74: ; preds = %71, %62
br label %75
75: ; preds = %74, %59
%76 = load i32, i32* %4
%77 = load i32, i32* %5
%78 = shl i32 %77, 16
%79 = add nsw i32 %76, %78
ret i32 %79
}

LLVM IR (-O2):
define i32 @fb_ConsoleViewEx(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = call i32 @fb_GetSize(i32 0, i32* %4)
%7 = load i32, i32* %4
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %3
store i32 25, i32* %4
br label %10
10: ; preds = %9, %3
%11 = icmp sgt i32 %0, 0
br i1 %11, label %17, label %12
12: ; preds = %10
%13 = icmp eq i32 %0, 0
br i1 %13, label %17, label %14
14: ; preds = %12
%15 = call i32 (...) @fb_ConsoleGetTopRow()
%16 = add nsw i32 %15, 1
br label %17
17: ; preds = %12, %10, %14
%18 = phi i32 [ 0, %14 ], [ 1, %10 ], [ 1, %12 ]
%19 = phi i32 [ %16, %14 ], [ %0, %10 ], [ 1, %12 ]
%20 = icmp sgt i32 %1, 0
br i1 %20, label %28, label %21
21: ; preds = %17
%22 = icmp eq i32 %1, 0
br i1 %22, label %23, label %25
23: ; preds = %21
%24 = load i32, i32* %4
br label %28
25: ; preds = %21
%26 = call i32 (...) @fb_ConsoleGetBotRow()
%27 = add nsw i32 %26, 1
br label %28
28: ; preds = %17, %23, %25
%29 = phi i32 [ %24, %23 ], [ %27, %25 ], [ %1, %17 ]
%30 = phi i32 [ 1, %23 ], [ %18, %25 ], [ 1, %17 ]
%31 = icmp sgt i32 %19, %29
%32 = icmp slt i32 %19, 1
%33 = or i1 %32, %31
%34 = icmp slt i32 %29, 1
%35 = or i1 %34, %33
br i1 %35, label %51, label %36
36: ; preds = %28
%37 = load i32, i32* %4
%38 = icmp sgt i32 %19, %37
%39 = icmp sgt i32 %29, %37
%40 = select i1 %38, i1 true, i1 %39
br i1 %40, label %51, label %41
41: ; preds = %36
%42 = icmp eq i32 %30, 0
br i1 %42, label %51, label %43
43: ; preds = %41
%44 = add nsw i32 %19, -1
%45 = add nsw i32 %29, -1
%46 = call i32 @fb_ConsoleSetTopBotRows(i32 %44, i32 %45)
%47 = call i32 (...) @fb_ViewUpdate()
%48 = icmp eq i32 %2, 0
br i1 %48, label %51, label %49
49: ; preds = %43
%50 = call i32 @fb_Locate(i32 %19, i32 1, i32 -1, i32 0, i32 0)
br label %51
51: ; preds = %36, %28, %43, %49, %41
%52 = phi i32 [ %19, %43 ], [ %19, %49 ], [ %19, %41 ], [ 0, %28 ], [ 0, %36 ]
%53 = phi i32 [ %29, %43 ], [ %29, %49 ], [ %29, %41 ], [ 0, %28 ], [ 0, %36 ]
%54 = shl i32 %53, 16
%55 = add nsw i32 %54, %52
ret i32 %55
}