#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*/ buf;
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*/ opentty();
int /*<<< orphan*/ stdout;
int strlen(char *);
void print(FILE *fp, int pgsize) {
  static int lines = 0;
  char buf[BUFSIZ];
  while (fgets(buf, sizeof(buf), fp) != NULL)
    if (++lines < pgsize) {
      fputs(buf, stdout);
    } else {
      buf[strlen(buf) - 1] = '\0';
      fflush(stdout);
      opentty();
      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     1218 <print+0xc8>
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    1213 <print+0xc3>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    $0x0,%al
call   1030 <opentty@plt>
movl   $0x0,0x2e39(%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     11fe <print+0xae>
mov    0x2e3c(%rip),%r13
jmp    11dc <print+0x8c>
xchg   %ax,%ax
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    0x0(%r13),%edi
call   1070 <fflush@plt>
xor    %eax,%eax
call   1030 <opentty@plt>
movl   $0x0,0x2e83(%rip)
mov    %rbx,%rdi
mov    %r12d,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     11fe <print+0xae>
mov    0x2e6a(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2e61(%rip)
cmp    %r14d,%eax
jge    11a0 <print+0x50>
mov    0x0(%r13),%esi
mov    %rbx,%rdi
call   1050 <fputs@plt>
jmp    11c9 <print+0x79>
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 = %32, %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 %33
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 %32
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 @fflush(i32 %29)
%31 = call i32 (...) @opentty()
store i32 0, i32* @print.lines
br label %32
32: ; preds = %24, %21
br label %11
33: ; preds = %11
%34 = 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 %26, label %8
8: ; preds = %2, %23
%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 %23
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 @fflush(i32 %20)
%22 = call i32 (...) @opentty()
store i32 0, i32* @print.lines
br label %23
23: ; preds = %15, %12
%24 = call i32* @fgets(i8* %5, i32 %3, i32* %0)
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %8
26: ; preds = %23, %2
ret void
}