#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 get_page_size();
char getchar();
int /*<<< orphan*/ i_print_history(int, int);
int /*<<< orphan*/ printf(char *, ...);
int read_chat_history() {
  printf("****char*history***");
  printf("(n-nextpage; p-prepage; q-quit)\n");
  int page_num;
  int remains;
  int berry = get_page_size();
  page_num = berry / 8;
  remains = berry % 8;
  if (remains != 0)
    page_num++;
  else
    page_num = page_num;
  printf("there are %d page total %d items", page_num, berry);
  int i = -1;
  while (1) {
    char flag;
    if ((berry + i * 8) >= 0) {
      printf("(%d~%d)\n", (berry + i * 8), (berry + (i + 1) * 8));
      i_print_history(8, i);
      printf("@@@\n");
      while ('\n' == (flag = getchar())) {
      }
      switch (flag) {
      case 'p':
        i--;
        break;
      case 'n':
        i++;
        break;
      case 'q':
        return (0);
      default:
        break;
      }
      if (i >= 0) {
        printf("have at the end!\n");
        printf("return to menu!\n");
      }
    } else {
      printf("(1~%d)\n", remains);
      i_print_history(remains, 0);
      printf("#########over##############\n");
      return (0);
    }
  }
  return (0);
}


x86 assembly (clang, -O0):
<read_chat_history>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xeb1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeb7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1060 <get_page_size@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %edx,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11a2 <read_chat_history+0x62>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11a8 <read_chat_history+0x68>
mov    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%edx
lea    0xe80(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x14(%rbp)
mov    -0x10(%rbp),%eax
mov    -0x14(%rbp),%ecx
shl    $0x3,%ecx
add    %ecx,%eax
cmp    $0x0,%eax
jl     12c7 <read_chat_history+0x187>
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%eax
shl    $0x3,%eax
add    %eax,%esi
mov    -0x10(%rbp),%edx
mov    -0x14(%rbp),%eax
add    $0x1,%eax
shl    $0x3,%eax
add    %eax,%edx
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%esi
mov    $0x8,%edi
call   1050 <i_print_history@plt>
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %al,-0x15(%rbp)
movsbl %al,%ecx
mov    $0xa,%eax
cmp    %ecx,%eax
jne    1238 <read_chat_history+0xf8>
jmp    1219 <read_chat_history+0xd9>
movsbl -0x15(%rbp),%eax
mov    %eax,-0x1c(%rbp)
sub    $0x6e,%eax
je     127d <read_chat_history+0x13d>
jmp    124d <read_chat_history+0x10d>
mov    -0x1c(%rbp),%eax
sub    $0x70,%eax
je     126f <read_chat_history+0x12f>
jmp    125e <read_chat_history+0x11e>
mov    -0x1c(%rbp),%eax
sub    $0x71,%eax
je     128b <read_chat_history+0x14b>
jmp    1297 <read_chat_history+0x157>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    129c <read_chat_history+0x15c>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    129c <read_chat_history+0x15c>
movl   $0x0,-0x4(%rbp)
jmp    1301 <read_chat_history+0x1c1>
jmp    129c <read_chat_history+0x15c>
cmpl   $0x0,-0x14(%rbp)
jl     12c2 <read_chat_history+0x182>
lea    0xdb7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xdbb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12fc <read_chat_history+0x1bc>
mov    -0xc(%rbp),%esi
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%edi
xor    %esi,%esi
call   1050 <i_print_history@plt>
lea    0xda6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1301 <read_chat_history+0x1c1>
jmp    11c3 <read_chat_history+0x83>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_chat_history>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe9e(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xed6(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1070 <get_page_size@plt>
mov    %eax,%ebx
lea    0x7(%rbx),%eax
test   %ebx,%ebx
cmovns %ebx,%eax
mov    %eax,%ecx
sar    $0x3,%ecx
and    $0xfffffff8,%eax
xor    %esi,%esi
mov    %ebx,%edx
sub    %eax,%edx
mov    %edx,0x4(%rsp)
setne  %sil
add    %ecx,%esi
lea    0xe6f(%rip),%rdi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
cmp    $0x8,%ebx
jl     1270 <read_chat_history+0x120>
lea    -0x8(%rbx),%esi
mov    $0xffffffff,%ebp
lea    0xe6f(%rip),%r15
lea    0xeb5(%rip),%r12
lea    0xec3(%rip),%r14
jmp    11ef <read_chat_history+0x9f>
cs nopw 0x0(%rax,%rax,1)
lea    0x0(,%rbp,8),%esi
add    %ebx,%esi
js     1270 <read_chat_history+0x120>
lea    0x1(%rbp),%r13d
lea    (%rbx,%rbp,8),%edx
add    $0x8,%edx
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x8,%edi
mov    %ebp,%esi
call   1060 <i_print_history@plt>
mov    %r12,%rdi
call   1030 <puts@plt>
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1050 <getchar@plt>
cmp    $0xa,%al
je     1220 <read_chat_history+0xd0>
movsbl %al,%eax
cmp    $0x6e,%eax
je     1240 <read_chat_history+0xf0>
cmp    $0x70,%eax
je     1250 <read_chat_history+0x100>
cmp    $0x71,%eax
jne    1243 <read_chat_history+0xf3>
jmp    1299 <read_chat_history+0x149>
nop
mov    %r13d,%ebp
test   %ebp,%ebp
jns    1257 <read_chat_history+0x107>
jmp    11e0 <read_chat_history+0x90>
nopl   0x0(%rax)
add    $0xffffffff,%ebp
test   %ebp,%ebp
js     11e0 <read_chat_history+0x90>
lea    0xe28(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
jmp    11e0 <read_chat_history+0x90>
lea    0xdc7(%rip),%rdi
mov    0x4(%rsp),%ebx
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1060 <i_print_history@plt>
lea    0xdd2(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"****char*history***\00"
@.str.1 = [33 x i8] c"(n-nextpage; p-prepage; q-quit)\0A\00"
@.str.2 = [33 x i8] c"there are %d page total %d items\00"
@.str.3 = [9 x i8] c"(%d~%d)\0A\00"
@.str.4 = [5 x i8] c"@@@\0A\00"
@.str.5 = [18 x i8] c"have at the end!\0A\00"
@.str.6 = [17 x i8] c"return to menu!\0A\00"
@.str.7 = [8 x i8] c"(1~%d)\0A\00"
@.str.8 = [29 x i8] c"#########over##############\0A\00"
define i32 @read_chat_history() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 (...) @get_page_size()
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = sdiv i32 %10, 8
store i32 %11, i32* %2
%12 = load i32, i32* %4
%13 = srem i32 %12, 8
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %0
%17 = load i32, i32* %2
%18 = add nsw i32 %17, 1
store i32 %18, i32* %2
br label %21
19: ; preds = %0
%20 = load i32, i32* %2
store i32 %20, i32* %2
br label %21
21: ; preds = %19, %16
%22 = load i32, i32* %2
%23 = load i32, i32* %4
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %22, i32 %23)
store i32 -1, i32* %5
br label %25
25: ; preds = %21, %74
%26 = load i32, i32* %4
%27 = load i32, i32* %5
%28 = mul nsw i32 %27, 8
%29 = add nsw i32 %26, %28
%30 = icmp sge i32 %29, 0
br i1 %30, label %31, label %68
31: ; preds = %25
%32 = load i32, i32* %4
%33 = load i32, i32* %5
%34 = mul nsw i32 %33, 8
%35 = add nsw i32 %32, %34
%36 = load i32, i32* %4
%37 = load i32, i32* %5
%38 = add nsw i32 %37, 1
%39 = mul nsw i32 %38, 8
%40 = add nsw i32 %36, %39
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %35, i32 %40)
%42 = load i32, i32* %5
%43 = call i32 @i_print_history(i32 8, i32 %42)
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %45
45: ; preds = %49, %31
%46 = call signext i8 (...) @getchar()
store i8 %46, i8* %6
%47 = sext i8 %46 to i32
%48 = icmp eq i32 10, %47
br i1 %48, label %49, label %50
49: ; preds = %45
br label %45
50: ; preds = %45
%51 = load i8, i8* %6
%52 = sext i8 %51 to i32
switch i32 %52, label %60 [
i32 112, label %53
i32 110, label %56
i32 113, label %59
]
53: ; preds = %50
%54 = load i32, i32* %5
%55 = add nsw i32 %54, -1
store i32 %55, i32* %5
br label %61
56: ; preds = %50
%57 = load i32, i32* %5
%58 = add nsw i32 %57, 1
store i32 %58, i32* %5
br label %61
59: ; preds = %50
store i32 0, i32* %1
br label %75
60: ; preds = %50
br label %61
61: ; preds = %60, %56, %53
%62 = load i32, i32* %5
%63 = icmp sge i32 %62, 0
br i1 %63, label %64, label %67
64: ; preds = %61
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0))
br label %67
67: ; preds = %64, %61
br label %74
68: ; preds = %25
%69 = load i32, i32* %3
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i32 %69)
%71 = load i32, i32* %3
%72 = call i32 @i_print_history(i32 %71, i32 0)
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.8, i64 0, i64 0))
store i32 0, i32* %1
br label %75
74: ; preds = %67
br label %25
75: ; preds = %68, %59
%76 = load i32, i32* %1
ret i32 %76
}

LLVM IR (-O2):
@.str = [20 x i8] c"****char*history***\00"
@.str.2 = [33 x i8] c"there are %d page total %d items\00"
@.str.3 = [9 x i8] c"(%d~%d)\0A\00"
@.str.7 = [8 x i8] c"(1~%d)\0A\00"
@str = [32 x i8] c"(n-nextpage; p-prepage; q-quit)\00"
@str.9 = [28 x i8] c"#########over##############\00"
@str.10 = [4 x i8] c"@@@\00"
@str.11 = [17 x i8] c"have at the end!\00"
@str.12 = [16 x i8] c"return to menu!\00"
define i32 @read_chat_history() {
%1 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
%3 = tail call i32 (...) @get_page_size()
%4 = sdiv i32 %3, 8
%5 = srem i32 %3, 8
%6 = icmp ne i32 %5, 0
%7 = zext i1 %6 to i32
%8 = add nsw i32 %4, %7
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %8, i32 %3)
%10 = icmp sgt i32 %3, 7
br i1 %10, label %11, label %36
11: ; preds = %0
%12 = add nsw i32 %3, -8
br label %13
13: ; preds = %11, %40
%14 = phi i32 [ %42, %40 ], [ %12, %11 ]
%15 = phi i32 [ %31, %40 ], [ -1, %11 ]
%16 = add nsw i32 %15, 1
%17 = shl nsw i32 %16, 3
%18 = add nsw i32 %17, %3
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %14, i32 %18)
%20 = tail call i32 @i_print_history(i32 8, i32 %15)
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str.10, i64 0, i64 0))
br label %22
22: ; preds = %22, %13
%23 = tail call signext i8 (...) @getchar()
%24 = icmp eq i8 %23, 10
br i1 %24, label %22, label %25
25: ; preds = %22
%26 = sext i8 %23 to i32
switch i32 %26, label %30 [
i32 112, label %27
i32 110, label %29
i32 113, label %44
]
27: ; preds = %25
%28 = add nsw i32 %15, -1
br label %30
29: ; preds = %25
br label %30
30: ; preds = %25, %29, %27
%31 = phi i32 [ %15, %25 ], [ %16, %29 ], [ %28, %27 ]
%32 = icmp sgt i32 %31, -1
br i1 %32, label %33, label %40
33: ; preds = %30
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.11, i64 0, i64 0))
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.12, i64 0, i64 0))
br label %40
36: ; preds = %40, %0
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i32 %5)
%38 = tail call i32 @i_print_history(i32 %5, i32 0)
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.9, i64 0, i64 0))
br label %44
40: ; preds = %33, %30
%41 = shl nsw i32 %31, 3
%42 = add nsw i32 %41, %3
%43 = icmp sgt i32 %42, -1
br i1 %43, label %13, label %36
44: ; preds = %25, %36
ret i32 0
}