#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*/ FILE;

/* Variables and functions */
scalar_t__ EOF;
int /*<<< orphan*/ exit(int);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fputs(char *, int /*<<< orphan*/);
int see_more(int /*<<< orphan*/ *);
int /*<<< orphan*/ stdout;
void do_more(FILE *fp) {
  char line[512];
  int num_of_lines = 0;
  int reply;
  FILE *tty_file;
  if ((tty_file = fopen("/dev/tty", "r")) == NULL)
    exit(1);
  while (fgets(line, 512, fp)) {
    if (num_of_lines == 24) {
      reply = see_more(tty_file);
      if (reply == 0)
        break;
      num_of_lines -= reply;
    }
    if (fputs(line, stdout) == EOF)
      exit(1);
    num_of_lines++;
  }
}


x86 assembly (clang, -O0):
<do_more>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x214(%rbp)
lea    0xe90(%rip),%rdi
lea    0xe92(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x220(%rbp)
cmp    $0x0,%rax
jne    1197 <do_more+0x47>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    119c <do_more+0x4c>
lea    -0x210(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x200,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1243 <do_more+0xf3>
cmpl   $0x18,-0x214(%rbp)
jne    1200 <do_more+0xb0>
mov    -0x220(%rbp),%rdi
call   1030 <see_more@plt>
mov    %eax,-0x218(%rbp)
cmpl   $0x0,-0x218(%rbp)
jne    11ec <do_more+0x9c>
jmp    1243 <do_more+0xf3>
mov    -0x218(%rbp),%ecx
mov    -0x214(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x214(%rbp)
lea    -0x210(%rbp),%rdi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
call   1040 <fputs@plt>
mov    0x2dcc(%rip),%rcx
cmp    (%rcx),%rax
jne    122f <do_more+0xdf>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x214(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x214(%rbp)
jmp    119c <do_more+0x4c>
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_more>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rdi,%r15
lea    0xe95(%rip),%rdi
lea    0xe97(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1204 <do_more+0xb4>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x200,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11f2 <do_more+0xa2>
xor    %ebp,%ebp
mov    0x2e37(%rip),%r12
mov    %rsp,%rbx
mov    0x2e3d(%rip),%r13
nopl   0x0(%rax,%rax,1)
cmp    $0x18,%ebp
jne    11c8 <do_more+0x78>
mov    %r14,%rdi
call   1030 <see_more@plt>
test   %eax,%eax
je     11f2 <do_more+0xa2>
mov    $0x18,%ebp
sub    %eax,%ebp
mov    (%r12),%esi
mov    %rbx,%rdi
call   1040 <fputs@plt>
cmp    0x0(%r13),%rax
je     1204 <do_more+0xb4>
add    $0x1,%ebp
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    11b0 <do_more+0x60>
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/tty\00"
@.str.1 = [2 x i8] c"r\00"
@stdout = global i32 0
@EOF = global i64 0
define void @do_more(i32* %0) {
%2 = alloca i32*
%3 = alloca [512 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
store i32* %0, i32** %2
store i32 0, i32* %4
%7 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %6
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %1
br label %12
12: ; preds = %38, %11
%13 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%14 = load i32*, i32** %2
%15 = call i64 @fgets(i8* %13, i32 512, i32* %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %41
17: ; preds = %12
%18 = load i32, i32* %4
%19 = icmp eq i32 %18, 24
br i1 %19, label %20, label %30
20: ; preds = %17
%21 = load i32*, i32** %6
%22 = call i32 @see_more(i32* %21)
store i32 %22, i32* %5
%23 = load i32, i32* %5
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %20
br label %41
26: ; preds = %20
%27 = load i32, i32* %5
%28 = load i32, i32* %4
%29 = sub nsw i32 %28, %27
store i32 %29, i32* %4
br label %30
30: ; preds = %26, %17
%31 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%32 = load i32, i32* @stdout
%33 = call i64 @fputs(i8* %31, i32 %32)
%34 = load i64, i64* @EOF
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %38
36: ; preds = %30
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %30
%39 = load i32, i32* %4
%40 = add nsw i32 %39, 1
store i32 %40, i32* %4
br label %12
41: ; preds = %25, %12
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/tty\00"
@.str.1 = [2 x i8] c"r\00"
@stdout = global i32 0
@EOF = global i64 0
define void @do_more(i32* nocapture %0) {
%2 = alloca [512 x i8]
%3 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = call i64 @fgets(i8* %3, i32 512, i32* %0)
%8 = icmp eq i64 %7, 0
br i1 %8, label %31, label %15
9: ; preds = %1
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %23
%12 = add nsw i32 %24, 1
%13 = call i64 @fgets(i8* %3, i32 512, i32* %0)
%14 = icmp eq i64 %13, 0
br i1 %14, label %31, label %15
15: ; preds = %6, %11
%16 = phi i32 [ %12, %11 ], [ 0, %6 ]
%17 = icmp eq i32 %16, 24
br i1 %17, label %18, label %23
18: ; preds = %15
%19 = call i32 @see_more(i32* %4)
%20 = icmp eq i32 %19, 0
br i1 %20, label %31, label %21
21: ; preds = %18
%22 = sub nsw i32 24, %19
br label %23
23: ; preds = %21, %15
%24 = phi i32 [ %22, %21 ], [ %16, %15 ]
%25 = load i32, i32* @stdout
%26 = call i64 @fputs(i8* %3, i32 %25)
%27 = load i64, i64* @EOF
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %11
29: ; preds = %23
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %11, %18, %6
ret void
}