#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 */
typedef int /*<<< orphan*/ WINDOW;

/* Variables and functions */
#define KEY_DOWN 129
#define KEY_UP 128
scalar_t__ LINES;
int /*<<< orphan*/ O_RDONLY;
int getch();
int /*<<< orphan*/ memset(long *, int /*<<< orphan*/, int);
int /*<<< orphan*/ mvaddstr(scalar_t__, int /*<<< orphan*/, char *);
int /*<<< orphan*/ notify(char *, char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ refresh();
int /*<<< orphan*/ update_content(int /*<<< orphan*/ *, int, int, long *,
                                  int *);
int switch_to_bk(WINDOW *wp, char *filename) {
  int c = 0, choice = 0, ins = 0, fd = 0, now = 0;
  long index[1024];
  memset(index, 0, sizeof(long) * 1024);
  if ((fd = open(filename, O_RDONLY)) < -1) {
    mvaddstr(LINES - 1, 0, "Can not open this file.");
    refresh();
    return -1;
  }
  notify("Done", "book", filename);
  update_content(wp, fd, ins, index, &now);
  while (1) {
    ins = 0;
    choice = -1;
    c = getch();
    switch (c) {
    case KEY_UP:
    case 'k':
      ins = 1;
      break;
    case KEY_DOWN:
    case 'j':
      ins = 2;
      break;
    case 'q':
      choice = -2;
      break;
    }
    notify("Done", "book", filename);
    update_content(wp, fd, ins, index, &now);
    if (choice != -1) {
      if (choice == -2) {
        return 0;
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<switch_to_bk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
lea    -0x2030(%rbp),%rdi
xor    %esi,%esi
mov    $0x2000,%edx
call   1030 <memset@plt>
mov    -0x18(%rbp),%rdi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x28(%rbp)
cmp    $0xffffffff,%eax
jge    1206 <switch_to_bk+0x96>
mov    0x2e02(%rip),%rax
mov    (%rax),%rdi
sub    $0x1,%rdi
xor    %esi,%esi
lea    0xe72(%rip),%rdx
call   1090 <mvaddstr@plt>
mov    $0x0,%al
call   1050 <refresh@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f8 <switch_to_bk+0x188>
mov    -0x18(%rbp),%rdx
lea    0xe67(%rip),%rdi
lea    0xe65(%rip),%rsi
call   1040 <notify@plt>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%esi
mov    -0x24(%rbp),%edx
lea    -0x2030(%rbp),%rcx
lea    -0x2c(%rbp),%r8
call   1080 <update_content@plt>
movl   $0x0,-0x24(%rbp)
movl   $0xffffffff,-0x20(%rbp)
xor    %eax,%eax
call   1060 <getch@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0xffffff96,%eax
mov    %eax,%ecx
mov    %rcx,-0x2038(%rbp)
sub    $0x17,%eax
ja     129d <switch_to_bk+0x12d>
mov    -0x2038(%rbp),%rax
lea    0xd8b(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movl   $0x1,-0x24(%rbp)
jmp    129d <switch_to_bk+0x12d>
movl   $0x2,-0x24(%rbp)
jmp    129d <switch_to_bk+0x12d>
movl   $0xfffffffe,-0x20(%rbp)
mov    -0x18(%rbp),%rdx
lea    0xdd0(%rip),%rdi
lea    0xdce(%rip),%rsi
call   1040 <notify@plt>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%esi
mov    -0x24(%rbp),%edx
lea    -0x2030(%rbp),%rcx
lea    -0x2c(%rbp),%r8
call   1080 <update_content@plt>
cmpl   $0xffffffff,-0x20(%rbp)
je     12f3 <switch_to_bk+0x183>
cmpl   $0xfffffffe,-0x20(%rbp)
jne    12ee <switch_to_bk+0x17e>
movl   $0x0,-0x4(%rbp)
jmp    12f8 <switch_to_bk+0x188>
jmp    12f3 <switch_to_bk+0x183>
jmp    1237 <switch_to_bk+0xc7>
mov    -0x4(%rbp),%eax
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<switch_to_bk>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2018,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
movl   $0x0,0xc(%rsp)
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x2000,%edx
call   1030 <memset@plt>
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
jge    11e3 <switch_to_bk+0x73>
mov    0x2e23(%rip),%rax
mov    (%rax),%rdi
add    $0xffffffffffffffff,%rdi
lea    0xe95(%rip),%rdx
xor    %esi,%esi
call   1090 <mvaddstr@plt>
xor    %eax,%eax
call   1050 <refresh@plt>
mov    $0xffffffff,%ebx
jmp    128e <switch_to_bk+0x11e>
mov    %eax,%ebp
lea    0xc(%rsp),%r15
lea    0xe0f(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %r12d,%r12d
jmp    1216 <switch_to_bk+0xa6>
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0x2,%r12d
lea    0xe5b(%rip),%rdi
lea    0xe59(%rip),%rsi
mov    %rbx,%rdx
call   1040 <notify@plt>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %r12d,%edx
lea    0x10(%rsp),%rcx
mov    %r15,%r8
call   1080 <update_content@plt>
xor    %eax,%eax
call   1060 <getch@plt>
add    $0xffffff96,%eax
cmp    $0x17,%eax
ja     1200 <switch_to_bk+0x90>
mov    $0x1,%r12d
movslq 0x0(%r13,%rax,4),%rax
add    %r13,%rax
jmp    *%rax
lea    0xe11(%rip),%rdi
lea    0xe0f(%rip),%rsi
mov    %rbx,%rdx
call   1040 <notify@plt>
xor    %ebx,%ebx
lea    0x10(%rsp),%rcx
lea    0xc(%rsp),%r8
mov    %r14,%rdi
mov    %ebp,%esi
xor    %edx,%edx
call   1080 <update_content@plt>
mov    %ebx,%eax
add    $0x2018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@LINES = global i64 0
@.str = [24 x i8] c"Can not open this file.\00"
@.str.1 = [5 x i8] c"Done\00"
@.str.2 = [5 x i8] c"book\00"
define i32 @switch_to_bk(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [1024 x i64]
store i32* %0, i32** %4
store i8* %1, i8** %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
%12 = getelementptr inbounds [1024 x i64], [1024 x i64]* %11, i64 0, i64 0
%13 = call i32 @memset(i64* %12, i32 0, i32 8192)
%14 = load i8*, i8** %5
%15 = load i32, i32* @O_RDONLY
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %9
%17 = icmp slt i32 %16, -1
br i1 %17, label %18, label %23
18: ; preds = %2
%19 = load i64, i64* @LINES
%20 = sub nsw i64 %19, 1
%21 = call i32 @mvaddstr(i64 %20, i32 0, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%22 = call i32 (...) @refresh()
store i32 -1, i32* %3
br label %53
23: ; preds = %2
%24 = load i8*, i8** %5
%25 = call i32 @notify(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %24)
%26 = load i32*, i32** %4
%27 = load i32, i32* %9
%28 = load i32, i32* %8
%29 = getelementptr inbounds [1024 x i64], [1024 x i64]* %11, i64 0, i64 0
%30 = call i32 @update_content(i32* %26, i32 %27, i32 %28, i64* %29, i32* %10)
br label %31
31: ; preds = %23, %52
store i32 0, i32* %8
store i32 -1, i32* %7
%32 = call i32 (...) @getch()
store i32 %32, i32* %6
%33 = load i32, i32* %6
switch i32 %33, label %37 [
i32 128, label %34
i32 107, label %34
i32 129, label %35
i32 106, label %35
i32 113, label %36
]
34: ; preds = %31, %31
store i32 1, i32* %8
br label %37
35: ; preds = %31, %31
store i32 2, i32* %8
br label %37
36: ; preds = %31
store i32 -2, i32* %7
br label %37
37: ; preds = %31, %36, %35, %34
%38 = load i8*, i8** %5
%39 = call i32 @notify(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %38)
%40 = load i32*, i32** %4
%41 = load i32, i32* %9
%42 = load i32, i32* %8
%43 = getelementptr inbounds [1024 x i64], [1024 x i64]* %11, i64 0, i64 0
%44 = call i32 @update_content(i32* %40, i32 %41, i32 %42, i64* %43, i32* %10)
%45 = load i32, i32* %7
%46 = icmp ne i32 %45, -1
br i1 %46, label %47, label %52
47: ; preds = %37
%48 = load i32, i32* %7
%49 = icmp eq i32 %48, -2
br i1 %49, label %50, label %51
50: ; preds = %47
store i32 0, i32* %3
br label %53
51: ; preds = %47
br label %52
52: ; preds = %51, %37
br label %31
53: ; preds = %50, %18
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@LINES = global i64 0
@.str = [24 x i8] c"Can not open this file.\00"
@.str.1 = [5 x i8] c"Done\00"
@.str.2 = [5 x i8] c"book\00"
define i32 @switch_to_bk(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca [1024 x i64]
%5 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%6 = bitcast [1024 x i64]* %4 to i8*
%7 = getelementptr inbounds [1024 x i64], [1024 x i64]* %4, i64 0, i64 0
%8 = call i32 @memset(i64* %7, i32 0, i32 8192)
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %1, i32 %9)
%11 = icmp slt i32 %10, -1
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i64, i64* @LINES
%14 = add nsw i64 %13, -1
%15 = call i32 @mvaddstr(i64 %14, i32 0, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%16 = call i32 (...) @refresh()
br label %29
17: ; preds = %18
br label %24
18: ; preds = %2, %24
%19 = phi i32 [ %25, %24 ], [ 0, %2 ]
%20 = call i32 @notify(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%21 = call i32 @update_content(i32* %0, i32 %10, i32 %19, i64* %7, i32* %3)
%22 = call i32 (...) @getch()
switch i32 %22, label %17 [
i32 128, label %24
i32 107, label %24
i32 129, label %23
i32 106, label %23
i32 113, label %26
]
23: ; preds = %18, %18
br label %24
24: ; preds = %23, %18, %18, %17
%25 = phi i32 [ 0, %17 ], [ 2, %23 ], [ 1, %18 ], [ 1, %18 ]
br label %18
26: ; preds = %18
%27 = call i32 @notify(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%28 = call i32 @update_content(i32* %0, i32 %10, i32 0, i64* %7, i32* %3)
br label %29
29: ; preds = %26, %12
%30 = phi i32 [ -1, %12 ], [ 0, %26 ]
ret i32 %30
}