#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_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int see_more(int /*<<< orphan*/ *);
int /*<<< orphan*/ stdout;
void do_more(FILE *fp) {
  char line[512];
  int num_of_lines = 0;
  int reply;
  FILE *fp_tty;
  fp_tty = fopen("/dev/tty", "r");
  if (fp_tty == NULL) {
    perror("fopen() /dev/tty failed:");
    exit(EXIT_FAILURE);
  }
  while (fgets(line, 512, fp)) {
    if (num_of_lines == 24) {
      reply = see_more(fp_tty);
      if (reply == 0)
        break;
      num_of_lines -= reply;
    }
    if (fputs(line, stdout) == EOF)
      exit(EXIT_FAILURE);
    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    0xe80(%rip),%rdi
lea    0xe82(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x220(%rbp)
cmpq   $0x0,-0x220(%rbp)
jne    11bb <do_more+0x5b>
lea    0xe63(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    11c0 <do_more+0x60>
lea    -0x210(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x200,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     126b <do_more+0x10b>
cmpl   $0x18,-0x214(%rbp)
jne    1224 <do_more+0xc4>
mov    -0x220(%rbp),%rdi
call   1030 <see_more@plt>
mov    %eax,-0x218(%rbp)
cmpl   $0x0,-0x218(%rbp)
jne    1210 <do_more+0xb0>
jmp    126b <do_more+0x10b>
mov    -0x218(%rbp),%ecx
mov    -0x214(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x214(%rbp)
lea    -0x210(%rbp),%rdi
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
call   1040 <fputs@plt>
mov    0x2da8(%rip),%rcx
cmp    (%rcx),%rax
jne    1257 <do_more+0xf7>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x214(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x214(%rbp)
jmp    11c0 <do_more+0x60>
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    0xe85(%rip),%rdi
lea    0xe87(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1214 <do_more+0xb4>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x200,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1202 <do_more+0xa2>
xor    %ebp,%ebp
mov    0x2e1f(%rip),%r12
mov    %rsp,%rbx
mov    0x2e2d(%rip),%r13
nopl   0x0(%rax,%rax,1)
cmp    $0x18,%ebp
jne    11d8 <do_more+0x78>
mov    %r14,%rdi
call   1030 <see_more@plt>
test   %eax,%eax
je     1202 <do_more+0xa2>
mov    $0x18,%ebp
sub    %eax,%ebp
mov    (%r12),%esi
mov    %rbx,%rdi
call   1040 <fputs@plt>
cmp    0x0(%r13),%rax
je     1220 <do_more+0xc0>
add    $0x1,%ebp
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    11c0 <do_more+0x60>
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf0(%rip),%rdi
call   1070 <perror@plt>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

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

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