#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 */

/* Variables and functions */
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ X_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
char *getenv(char *);
int /*<<< orphan*/ isatty(int /*<<< orphan*/);
int /*<<< orphan*/ strcmp(char *, char *);
char *setup_pager(void) {
  char *pager = getenv("PAGER");
  if (!isatty(STDOUT_FILENO))
    return NULL;
  if (!(pager || access("/usr/bin/pager", X_OK)))
    pager = "/usr/bin/pager";
  if (!(pager || access("/usr/bin/less", X_OK)))
    pager = "/usr/bin/less";
  if (!pager)
    pager = "cat";
  if (!*pager || !strcmp(pager, "cat"))
    return NULL;
  return pager;
}


x86 assembly (clang, -O0):
<setup_pager>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xeb1(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
mov    0x2e91(%rip),%rax
mov    (%rax),%edi
call   1040 <isatty@plt>
cmp    $0x0,%eax
jne    117c <setup_pager+0x3c>
movq   $0x0,-0x8(%rbp)
jmp    1237 <setup_pager+0xf7>
cmpq   $0x0,-0x10(%rbp)
jne    11b1 <setup_pager+0x71>
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
lea    0xe6f(%rip),%rdi
call   1060 <access@plt>
cmp    $0x0,%rax
jne    11b1 <setup_pager+0x71>
lea    0xe59(%rip),%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11e6 <setup_pager+0xa6>
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
lea    0xe49(%rip),%rdi
call   1060 <access@plt>
cmp    $0x0,%rax
jne    11e6 <setup_pager+0xa6>
lea    0xe33(%rip),%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11fc <setup_pager+0xbc>
lea    0xe2b(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     1222 <setup_pager+0xe2>
mov    -0x10(%rbp),%rdi
lea    0xe0f(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    122f <setup_pager+0xef>
movq   $0x0,-0x8(%rbp)
jmp    1237 <setup_pager+0xf7>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_pager>:
push   %r14
push   %rbx
push   %rax
lea    0xeb5(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r14
mov    0x2e96(%rip),%rax
mov    (%rax),%edi
call   1040 <isatty@plt>
test   %eax,%eax
je     11d0 <setup_pager+0x90>
test   %r14,%r14
jne    11a2 <setup_pager+0x62>
mov    0x2e6f(%rip),%rbx
mov    (%rbx),%esi
lea    0xe8c(%rip),%r14
mov    %r14,%rdi
call   1060 <access@plt>
test   %rax,%rax
je     11a2 <setup_pager+0x62>
mov    (%rbx),%esi
lea    0xe85(%rip),%rbx
mov    %rbx,%rdi
call   1060 <access@plt>
xor    %r14d,%r14d
test   %rax,%rax
cmove  %rbx,%r14
test   %r14,%r14
lea    0xe77(%rip),%rbx
cmovne %r14,%rbx
cmpb   $0x0,(%rbx)
je     11d0 <setup_pager+0x90>
lea    0xe67(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcmp@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
cmovne %rbx,%rax
jmp    11d2 <setup_pager+0x92>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [6 x i8] c"PAGER\00"
@STDOUT_FILENO = global i32 0
@.str.1 = [15 x i8] c"/usr/bin/pager\00"
@X_OK = global i32 0
@.str.2 = [14 x i8] c"/usr/bin/less\00"
@.str.3 = [4 x i8] c"cat\00"
define i8* @setup_pager() {
%1 = alloca i8*
%2 = alloca i8*
%3 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i8* %3, i8** %2
%4 = load i32, i32* @STDOUT_FILENO
%5 = call i32 @isatty(i32 %4)
%6 = icmp ne i32 %5, 0
br i1 %6, label %8, label %7
7: ; preds = %0
store i8* null, i8** %1
br label %39
8: ; preds = %0
%9 = load i8*, i8** %2
%10 = icmp ne i8* %9, null
br i1 %10, label %16, label %11
11: ; preds = %8
%12 = load i32, i32* @X_OK
%13 = call i64 @access(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %16, label %15
15: ; preds = %11
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8** %2
br label %16
16: ; preds = %15, %11, %8
%17 = load i8*, i8** %2
%18 = icmp ne i8* %17, null
br i1 %18, label %24, label %19
19: ; preds = %16
%20 = load i32, i32* @X_OK
%21 = call i64 @access(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %24, label %23
23: ; preds = %19
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8** %2
br label %24
24: ; preds = %23, %19, %16
%25 = load i8*, i8** %2
%26 = icmp ne i8* %25, null
br i1 %26, label %28, label %27
27: ; preds = %24
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8** %2
br label %28
28: ; preds = %27, %24
%29 = load i8*, i8** %2
%30 = load i8, i8* %29
%31 = icmp ne i8 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %28
%33 = load i8*, i8** %2
%34 = call i32 @strcmp(i8* %33, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%35 = icmp ne i32 %34, 0
br i1 %35, label %37, label %36
36: ; preds = %32, %28
store i8* null, i8** %1
br label %39
37: ; preds = %32
%38 = load i8*, i8** %2
store i8* %38, i8** %1
br label %39
39: ; preds = %37, %36, %7
%40 = load i8*, i8** %1
ret i8* %40
}

LLVM IR (-O2):
@.str = [6 x i8] c"PAGER\00"
@STDOUT_FILENO = global i32 0
@.str.1 = [15 x i8] c"/usr/bin/pager\00"
@X_OK = global i32 0
@.str.2 = [14 x i8] c"/usr/bin/less\00"
@.str.3 = [4 x i8] c"cat\00"
define i8* @setup_pager() {
%1 = tail call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%2 = load i32, i32* @STDOUT_FILENO
%3 = tail call i32 @isatty(i32 %2)
%4 = icmp eq i32 %3, 0
br i1 %4, label %26, label %5
5: ; preds = %0
%6 = icmp eq i8* %1, null
br i1 %6, label %7, label %16
7: ; preds = %5
%8 = load i32, i32* @X_OK
%9 = tail call i64 @access(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %16, label %11
11: ; preds = %7
%12 = load i32, i32* @X_OK
%13 = tail call i64 @access(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = icmp eq i64 %13, 0
%15 = select i1 %14, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* null
br label %16
16: ; preds = %7, %5, %11
%17 = phi i8* [ %15, %11 ], [ %1, %5 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), %7 ]
%18 = icmp eq i8* %17, null
%19 = select i1 %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %17
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %16
%23 = tail call i32 @strcmp(i8* %19, i8* dereferenceable(4) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%24 = icmp eq i32 %23, 0
%25 = select i1 %24, i8* null, i8* %19
br label %26
26: ; preds = %22, %16, %0
%27 = phi i8* [ null, %0 ], [ null, %16 ], [ %25, %22 ]
ret i8* %27
}