#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 */
struct winsize {
  int ws_row;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ TIOCGWINSZ;
int /*<<< orphan*/ do_more(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ ioctl(int /*<<< orphan*/, int /*<<< orphan*/, struct winsize *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ *stdin;
int main(int argc, char *argv[]) {
  struct winsize wbuf;
  if (ioctl(0, TIOCGWINSZ, &wbuf)) {
    perror("ioctl");
    exit(EXIT_FAILURE);
  }
  int pagelen = wbuf.ws_row;
  FILE *fp;
  FILE *fp_tty = fopen("/dev/tty", "w+");
  if (fp_tty == NULL) {
    printf("Can not open or write /dev/tty!\n");
    exit(EXIT_FAILURE);
  }
  if (argc == 1)
    do_more(pagelen, stdin, fp_tty);
  else
    while (--argc) {
      if ((fp = fopen(*++argv, "r")) != NULL) {
        do_more(pagelen, fp, fp_tty);
        fclose(fp);
      } else {
        printf("%s is an invalid file!\n", *argv);
        exit(EXIT_FAILURE);
      }
    }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x18(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
je     11be <main+0x4e>
lea    0xe55(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
lea    0xe3b(%rip),%rdi
lea    0xe3d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1202 <main+0x92>
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
cmpl   $0x1,-0x8(%rbp)
jne    1227 <main+0xb7>
mov    -0x1c(%rbp),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%rsi
mov    -0x30(%rbp),%rdx
call   1060 <do_more@plt>
jmp    12b6 <main+0x146>
jmp    122c <main+0xbc>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
je     12b1 <main+0x141>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    0x8(%rax),%rdi
lea    0xddb(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1289 <main+0x119>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1060 <do_more@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
jmp    12ac <main+0x13c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xd9e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    122c <main+0xbc>
jmp    12b6 <main+0x146>
mov    0x2d13(%rip),%rax
mov    (%rax),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%r15d
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
xor    %edi,%edi
call   1060 <ioctl@plt>
test   %rax,%rax
jne    126f <main+0xef>
mov    (%rsp),%r12d
lea    0xe4e(%rip),%rdi
lea    0xe50(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     127d <main+0xfd>
mov    %rax,%r13
cmp    $0x1,%r15d
jne    11ed <main+0x6d>
mov    0x2e0b(%rip),%rax
mov    (%rax),%rsi
mov    %r12d,%edi
mov    %r13,%rdx
call   1070 <do_more@plt>
jmp    1237 <main+0xb7>
mov    %r15d,%ebx
mov    $0x1,%r15d
cs nopw 0x0(%rax,%rax,1)
mov    (%r14,%r15,8),%rdi
lea    0xe07(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     124f <main+0xcf>
mov    %rax,%rbp
mov    %r12d,%edi
mov    %rax,%rsi
mov    %r13,%rdx
call   1070 <do_more@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
add    $0x1,%r15
cmp    %r15d,%ebx
jne    1200 <main+0x80>
mov    0x2d92(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r14,%r15,8),%rsi
lea    0xdba(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xd8a(%rip),%rdi
call   1090 <perror@plt>
jmp    1261 <main+0xe1>
lea    0xda8(%rip),%rdi
call   1030 <puts@plt>
jmp    1261 <main+0xe1>

LLVM IR (-O0):
%struct.winsize = type { i32 }
@TIOCGWINSZ = global i32 0
@.str = [6 x i8] c"ioctl\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [9 x i8] c"/dev/tty\00"
@.str.2 = [3 x i8] c"w+\00"
@.str.3 = [33 x i8] c"Can not open or write /dev/tty!\0A\00"
@stdin = global i32* null
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [24 x i8] c"%s is an invalid file!\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.winsize
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @TIOCGWINSZ
%11 = call i64 @ioctl(i32 0, i32 %10, %struct.winsize* %6)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
%18 = getelementptr inbounds %struct.winsize, %struct.winsize* %6, i32 0, i32 0
%19 = load i32, i32* %18
store i32 %19, i32* %7
%20 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %20, i32** %9
%21 = load i32*, i32** %9
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %27
23: ; preds = %17
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %17
%28 = load i32, i32* %4
%29 = icmp eq i32 %28, 1
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = load i32, i32* %7
%32 = load i32*, i32** @stdin
%33 = load i32*, i32** %9
%34 = call i32 @do_more(i32 %31, i32* %32, i32* %33)
br label %61
35: ; preds = %27
br label %36
36: ; preds = %59, %35
%37 = load i32, i32* %4
%38 = add nsw i32 %37, -1
store i32 %38, i32* %4
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %60
40: ; preds = %36
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i32 1
store i8** %42, i8*** %5
%43 = load i8*, i8** %42
%44 = call i32* @fopen(i8* %43, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %44, i32** %8
%45 = icmp ne i32* %44, null
br i1 %45, label %46, label %53
46: ; preds = %40
%47 = load i32, i32* %7
%48 = load i32*, i32** %8
%49 = load i32*, i32** %9
%50 = call i32 @do_more(i32 %47, i32* %48, i32* %49)
%51 = load i32*, i32** %8
%52 = call i32 @fclose(i32* %51)
br label %59
53: ; preds = %40
%54 = load i8**, i8*** %5
%55 = load i8*, i8** %54
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %55)
%57 = load i32, i32* @EXIT_FAILURE
%58 = call i32 @exit(i32 %57)
unreachable
59: ; preds = %46
br label %36
60: ; preds = %36
br label %61
61: ; preds = %60, %30
%62 = load i32, i32* @EXIT_SUCCESS
ret i32 %62
}

LLVM IR (-O2):
%struct.winsize = type { i32 }
@TIOCGWINSZ = global i32 0
@.str = [6 x i8] c"ioctl\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [9 x i8] c"/dev/tty\00"
@.str.2 = [3 x i8] c"w+\00"
@stdin = global i32* null
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [24 x i8] c"%s is an invalid file!\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [32 x i8] c"Can not open or write /dev/tty!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.winsize
%4 = bitcast %struct.winsize* %3 to i8*
%5 = load i32, i32* @TIOCGWINSZ
%6 = call i64 @ioctl(i32 0, i32 %5, %struct.winsize* %3)
%7 = icmp eq i64 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds %struct.winsize, %struct.winsize* %3, i64 0, i32 0
%14 = load i32, i32* %13
%15 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %12
%22 = icmp eq i32 %0, 1
br i1 %22, label %25, label %23
23: ; preds = %21
%24 = add nsw i32 %0, -1
br label %28
25: ; preds = %21
%26 = load i32*, i32** @stdin
%27 = call i32 @do_more(i32 %14, i32* %26, i32* %15)
br label %45
28: ; preds = %23, %35
%29 = phi i32 [ %38, %35 ], [ %24, %23 ]
%30 = phi i8** [ %31, %35 ], [ %1, %23 ]
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call i32* @fopen(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%34 = icmp eq i32* %33, null
br i1 %34, label %40, label %35
35: ; preds = %28
%36 = call i32 @do_more(i32 %14, i32* %33, i32* %15)
%37 = call i32 @fclose(i32* %33)
%38 = add nsw i32 %29, -1
%39 = icmp eq i32 %38, 0
br i1 %39, label %45, label %28
40: ; preds = %28
%41 = load i8*, i8** %31
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %41)
%43 = load i32, i32* @EXIT_FAILURE
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %35, %25
%46 = load i32, i32* @EXIT_SUCCESS
ret i32 %46
}