#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 pid_t;

/* Variables and functions */
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
char *check_env(char *, char *, char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ isatty(int /*<<< orphan*/);
scalar_t__ pipe(int *);
int /*<<< orphan*/ stderr;
int do_page(void) {
  int err;
  err = 0;
  pid_t pid;
  int p[2];
  char *pager_args[2];
  if (isatty(STDOUT_FILENO)) {
    if (pipe(p)) {
      fprintf(stderr, "Error opening pipe!\n");
      err = 1;
    } else if ((pid = fork()) == -1) {
      fprintf(stderr, "Error forking!\n");
      err = 1;
    } else if (pid == 0) {
      close(p[0]);
      dup2(p[1], STDOUT_FILENO);
      close(p[1]);
    } else {
      pager_args[0] = check_env("SMPAGER", "PAGER", "less");
      pager_args[1] = NULL;
      close(p[1]);
      dup2(p[0], STDIN_FILENO);
      close(p[0]);
      execvp(pager_args[0], pager_args);
      fprintf(stderr, "Failed to execute pager!\n");
      exit(1);
    }
  }
  return (err);
}


x86 assembly (clang, -O0):
<do_page>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
call   1030 <isatty@plt>
cmp    $0x0,%rax
je     12d1 <do_page+0x141>
lea    -0x10(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0x0,%rax
je     11eb <do_page+0x5b>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
lea    0xe26(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12cc <do_page+0x13c>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    121f <do_page+0x8f>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
lea    0xe07(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c7 <do_page+0x137>
cmpl   $0x0,-0x8(%rbp)
jne    124f <do_page+0xbf>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
jmp    12c2 <do_page+0x132>
lea    0xdcf(%rip),%rdi
lea    0xdca(%rip),%rsi
lea    0xdc9(%rip),%rdx
call   1040 <check_env@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%edi
mov    0x2d49(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1090 <execvp@plt>
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
lea    0xd7f(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    12c7 <do_page+0x137>
jmp    12cc <do_page+0x13c>
jmp    12d1 <do_page+0x141>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_page>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0x2e52(%rip),%r14
mov    (%r14),%edi
call   1030 <isatty@plt>
test   %rax,%rax
je     11cc <do_page+0x3c>
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
test   %rax,%rax
je     11d0 <do_page+0x40>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
lea    0xe36(%rip),%rsi
jmp    11f2 <do_page+0x62>
xor    %ebx,%ebx
jmp    11fc <do_page+0x6c>
xor    %ebx,%ebx
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     1206 <do_page+0x76>
cmp    $0xffffffff,%eax
jne    1226 <do_page+0x96>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
mov    0x8(%rsp),%edi
call   1060 <close@plt>
mov    0xc(%rsp),%edi
mov    (%r14),%esi
call   1050 <dup2@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
jmp    11fc <do_page+0x6c>
lea    0xdf8(%rip),%rdi
lea    0xdf3(%rip),%rsi
lea    0xdf2(%rip),%rdx
call   1040 <check_env@plt>
mov    %rax,%rbx
mov    %rax,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    0x8(%rsp),%edi
mov    0x2d6b(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   1090 <execvp@plt>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
lea    0xda0(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"Error opening pipe!\0A\00"
@.str.1 = [16 x i8] c"Error forking!\0A\00"
@.str.2 = [8 x i8] c"SMPAGER\00"
@.str.3 = [6 x i8] c"PAGER\00"
@.str.4 = [5 x i8] c"less\00"
@STDIN_FILENO = global i32 0
@.str.5 = [26 x i8] c"Failed to execute pager!\0A\00"
define i32 @do_page() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [2 x i32]
%4 = alloca [2 x i8*]
store i32 0, i32* %1
%5 = load i32, i32* @STDOUT_FILENO
%6 = call i64 @isatty(i32 %5)
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %59
8: ; preds = %0
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%10 = call i64 @pipe(i32* %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %58
15: ; preds = %8
%16 = call i32 (...) @fork()
store i32 %16, i32* %2
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %57
21: ; preds = %15
%22 = load i32, i32* %2
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %35
24: ; preds = %21
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%29 = load i32, i32* %28
%30 = load i32, i32* @STDOUT_FILENO
%31 = call i32 @dup2(i32 %29, i32 %30)
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%33 = load i32, i32* %32
%34 = call i32 @close(i32 %33)
br label %56
35: ; preds = %21
%36 = call i8* @check_env(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%37 = getelementptr inbounds [2 x i8*], [2 x i8*]* %4, i64 0, i64 0
store i8* %36, i8** %37
%38 = getelementptr inbounds [2 x i8*], [2 x i8*]* %4, i64 0, i64 1
store i8* null, i8** %38
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%40 = load i32, i32* %39
%41 = call i32 @close(i32 %40)
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%43 = load i32, i32* %42
%44 = load i32, i32* @STDIN_FILENO
%45 = call i32 @dup2(i32 %43, i32 %44)
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%47 = load i32, i32* %46
%48 = call i32 @close(i32 %47)
%49 = getelementptr inbounds [2 x i8*], [2 x i8*]* %4, i64 0, i64 0
%50 = load i8*, i8** %49
%51 = getelementptr inbounds [2 x i8*], [2 x i8*]* %4, i64 0, i64 0
%52 = call i32 @execvp(i8* %50, i8** %51)
%53 = load i32, i32* @stderr
%54 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %24
br label %57
57: ; preds = %56, %18
br label %58
58: ; preds = %57, %12
br label %59
59: ; preds = %58, %0
%60 = load i32, i32* %1
ret i32 %60
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"Error opening pipe!\0A\00"
@.str.1 = [16 x i8] c"Error forking!\0A\00"
@.str.2 = [8 x i8] c"SMPAGER\00"
@.str.3 = [6 x i8] c"PAGER\00"
@.str.4 = [5 x i8] c"less\00"
@STDIN_FILENO = global i32 0
@.str.5 = [26 x i8] c"Failed to execute pager!\0A\00"
define i32 @do_page() {
%1 = alloca [2 x i32]
%2 = alloca [2 x i8*]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = bitcast [2 x i8*]* %2 to i8*
%5 = load i32, i32* @STDOUT_FILENO
%6 = tail call i64 @isatty(i32 %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %45, label %8
8: ; preds = %0
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%10 = call i64 @pipe(i32* %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %8
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %45
15: ; preds = %8
%16 = call i32 (...) @fork()
switch i32 %16, label %29 [
i32 -1, label %17
i32 0, label %20
]
17: ; preds = %15
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %45
20: ; preds = %15
%21 = load i32, i32* %9
%22 = call i32 @close(i32 %21)
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%24 = load i32, i32* %23
%25 = load i32, i32* @STDOUT_FILENO
%26 = call i32 @dup2(i32 %24, i32 %25)
%27 = load i32, i32* %23
%28 = call i32 @close(i32 %27)
br label %45
29: ; preds = %15
%30 = call i8* @check_env(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%31 = getelementptr inbounds [2 x i8*], [2 x i8*]* %2, i64 0, i64 0
store i8* %30, i8** %31
%32 = getelementptr inbounds [2 x i8*], [2 x i8*]* %2, i64 0, i64 1
store i8* null, i8** %32
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%34 = load i32, i32* %33
%35 = call i32 @close(i32 %34)
%36 = load i32, i32* %9
%37 = load i32, i32* @STDIN_FILENO
%38 = call i32 @dup2(i32 %36, i32 %37)
%39 = load i32, i32* %9
%40 = call i32 @close(i32 %39)
%41 = call i32 @execvp(i8* %30, i8** %31)
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %12, %20, %17, %0
%46 = phi i32 [ 1, %12 ], [ 1, %17 ], [ 0, %20 ], [ 0, %0 ]
ret i32 %46
}