#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 /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
scalar_t__ read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
void saveRestoreScreen(int save) {
  char tputCode;
  FILE *tput;
  if (save)
    tput = popen("tput smcup", "r");
  if (!save)
    tput = popen("tput rmcup", "r");
  while (read(fileno(tput), &tputCode, 1) > 0)
    write(STDOUT_FILENO, &tputCode, 1);
  pclose(tput);
}


x86 assembly (clang, -O0):
<saveRestoreScreen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     117c <saveRestoreScreen+0x2c>
lea    0xe94(%rip),%rdi
lea    0xe98(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x10(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    119d <saveRestoreScreen+0x4d>
lea    0xe80(%rip),%rdi
lea    0xe77(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x10(%rbp)
jmp    11a2 <saveRestoreScreen+0x52>
mov    -0x10(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
lea    -0x5(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x0,%rax
jle    11e1 <saveRestoreScreen+0x91>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
lea    -0x5(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    11a2 <saveRestoreScreen+0x52>
mov    -0x10(%rbp),%rdi
call   1040 <pclose@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<saveRestoreScreen>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
test   %edi,%edi
lea    0xeab(%rip),%rax
lea    0xe97(%rip),%rdi
cmove  %rax,%rdi
lea    0xe97(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1060 <fileno@plt>
lea    0xf(%rsp),%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1050 <read@plt>
test   %rax,%rax
jle    11dc <saveRestoreScreen+0x8c>
mov    0x2e4f(%rip),%r15
lea    0xf(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
mov    (%r15),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %r14,%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
test   %rax,%rax
jg     11b0 <saveRestoreScreen+0x60>
mov    %r14,%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <pclose@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"tput smcup\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [11 x i8] c"tput rmcup\00"
@STDOUT_FILENO = global i32 0
define void @saveRestoreScreen(i32 %0) {
%2 = alloca i32
%3 = alloca i8
%4 = alloca i32*
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = icmp ne i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32* @popen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %4
br label %9
9: ; preds = %7, %1
%10 = load i32, i32* %2
%11 = icmp ne i32 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = call i32* @popen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %4
br label %14
14: ; preds = %12, %9
br label %15
15: ; preds = %20, %14
%16 = load i32*, i32** %4
%17 = call i32 @fileno(i32* %16)
%18 = call i64 @read(i32 %17, i8* %3, i32 1)
%19 = icmp sgt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i32, i32* @STDOUT_FILENO
%22 = call i32 @write(i32 %21, i8* %3, i32 1)
br label %15
23: ; preds = %15
%24 = load i32*, i32** %4
%25 = call i32 @pclose(i32* %24)
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"tput smcup\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [11 x i8] c"tput rmcup\00"
@STDOUT_FILENO = global i32 0
define void @saveRestoreScreen(i32 %0) {
%2 = alloca i8
%3 = icmp eq i32 %0, 0
%4 = select i1 %3, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0)
%5 = tail call i32* @popen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32 @fileno(i32* %5)
%7 = call i64 @read(i32 %6, i8* %2, i32 1)
%8 = icmp sgt i64 %7, 0
br i1 %8, label %9, label %15
9: ; preds = %1, %9
%10 = load i32, i32* @STDOUT_FILENO
%11 = call i32 @write(i32 %10, i8* %2, i32 1)
%12 = tail call i32 @fileno(i32* %5)
%13 = call i64 @read(i32 %12, i8* %2, i32 1)
%14 = icmp sgt i64 %13, 0
br i1 %14, label %9, label %15
15: ; preds = %9, %1
%16 = tail call i32 @pclose(i32* %5)
ret void
}