#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 */
int BUFSIZ;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int strlen(char *);
int /*<<< orphan*/ ttyin();
void print(FILE *fp, int pagesize) {
  static int lines = 0;
  char buf[BUFSIZ];
  while (fgets(buf, sizeof buf, fp) != NULL)
    if (++lines < pagesize)
      fputs(buf, stdout);
    else {
      buf[strlen(buf) - 1] = '\0';
      fputs(buf, stdout);
      fflush(stdout);
      ttyin();
      lines = 0;
    }
}


x86 assembly (clang, -O0):
<print>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x30(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    %eax,%esi
mov    -0x8(%rbp),%rdx
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1223 <print+0xd3>
mov    0x2e9a(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2e91(%rip)
cmp    -0xc(%rbp),%eax
jge    11db <print+0x8b>
mov    -0x28(%rbp),%rdi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
call   1050 <fputs@plt>
jmp    121e <print+0xce>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x28(%rbp),%rdi
sub    $0x1,%eax
cltq
movb   $0x0,(%rdi,%rax,1)
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
call   1050 <fputs@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    $0x0,%al
call   1030 <ttyin@plt>
movl   $0x0,0x2e2e(%rip)
jmp    118f <print+0x3f>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
mov    0x2e75(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
mov    %r12,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
mov    %r12d,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     120a <print+0xba>
mov    0x2e3c(%rip),%r13
jmp    11e8 <print+0x98>
xchg   %ax,%ax
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    0x0(%r13),%esi
mov    %rbx,%rdi
call   1050 <fputs@plt>
mov    0x0(%r13),%edi
call   1070 <fflush@plt>
xor    %eax,%eax
call   1030 <ttyin@plt>
movl   $0x0,0x2e77(%rip)
mov    %rbx,%rdi
mov    %r12d,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     120a <print+0xba>
mov    0x2e5e(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2e55(%rip)
cmp    %r14d,%eax
jge    11a0 <print+0x50>
mov    0x0(%r13),%esi
mov    %rbx,%rdi
call   1050 <fputs@plt>
jmp    11d5 <print+0x85>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@print.lines = internal global i32 0
@BUFSIZ = global i32 0
@stdout = global i32 0
define void @print(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
store i32* %0, i32** %3
store i32 %1, i32* %4
%7 = load i32, i32* @BUFSIZ
%8 = zext i32 %7 to i64
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %5
%10 = alloca i8, i64 %8
store i64 %8, i64* %6
br label %11
11: ; preds = %34, %2
%12 = trunc i64 %8 to i32
%13 = load i32*, i32** %3
%14 = call i32* @fgets(i8* %10, i32 %12, i32* %13)
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %35
16: ; preds = %11
%17 = load i32, i32* @print.lines
%18 = add nsw i32 %17, 1
store i32 %18, i32* @print.lines
%19 = load i32, i32* %4
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load i32, i32* @stdout
%23 = call i32 @fputs(i8* %10, i32 %22)
br label %34
24: ; preds = %16
%25 = call i32 @strlen(i8* %10)
%26 = sub nsw i32 %25, 1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %10, i64 %27
store i8 0, i8* %28
%29 = load i32, i32* @stdout
%30 = call i32 @fputs(i8* %10, i32 %29)
%31 = load i32, i32* @stdout
%32 = call i32 @fflush(i32 %31)
%33 = call i32 (...) @ttyin()
store i32 0, i32* @print.lines
br label %34
34: ; preds = %24, %21
br label %11
35: ; preds = %11
%36 = load i8*, i8** %5
ret void
}

LLVM IR (-O2):
@print.lines = internal unnamed_addr global i32 0
@BUFSIZ = global i32 0
@stdout = global i32 0
define void @print(i32* nocapture %0, i32 %1) {
%3 = load i32, i32* @BUFSIZ
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = call i32* @fgets(i8* %5, i32 %3, i32* %0)
%7 = icmp eq i32* %6, null
br i1 %7, label %28, label %8
8: ; preds = %2, %25
%9 = load i32, i32* @print.lines
%10 = add nsw i32 %9, 1
store i32 %10, i32* @print.lines
%11 = icmp slt i32 %10, %1
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = load i32, i32* @stdout
%14 = call i32 @fputs(i8* %5, i32 %13)
br label %25
15: ; preds = %8
%16 = call i32 @strlen(i8* %5)
%17 = add nsw i32 %16, -1
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %5, i64 %18
store i8 0, i8* %19
%20 = load i32, i32* @stdout
%21 = call i32 @fputs(i8* %5, i32 %20)
%22 = load i32, i32* @stdout
%23 = call i32 @fflush(i32 %22)
%24 = call i32 (...) @ttyin()
store i32 0, i32* @print.lines
br label %25
25: ; preds = %15, %12
%26 = call i32* @fgets(i8* %5, i32 %3, i32* %0)
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %8
28: ; preds = %25, %2
ret void
}