#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*/ O_RDWR;
int STDERR_FILENO;
int STDIN_FILENO;
int STDOUT_FILENO;
scalar_t__ close(int);
int dup(int);
int /*<<< orphan*/ errno_exit(char *);
int /*<<< orphan*/ error_exit(char *);
scalar_t__ getpgrp();
int open(char const *, int /*<<< orphan*/);
scalar_t__ tcgetpgrp(int);
void prepare_slave_pseudo_terminal_fds(const char *slave_name) {
  if (close(STDIN_FILENO) < 0)
    errno_exit("cannot close old stdin");
  int slave_fd = open(slave_name, O_RDWR);
  if (slave_fd != STDIN_FILENO)
    errno_exit("cannot open slave pseudo-terminal at stdin");
  if (close(STDOUT_FILENO) < 0)
    errno_exit("cannot close old stdout");
  if (dup(slave_fd) != STDOUT_FILENO)
    errno_exit("cannot open slave pseudo-terminal at stdout");
  if (close(STDERR_FILENO) < 0)
    errno_exit("cannot close old stderr");
  if (dup(slave_fd) != STDERR_FILENO)
    errno_exit("cannot open slave pseudo-terminal at stderr");
  if (tcgetpgrp(slave_fd) != getpgrp())
    error_exit("cannot become controlling process of slave pseudo-terminal");
}


x86 assembly (clang, -O0):
<prepare_slave_pseudo_terminal_fds>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
jge    11a0 <prepare_slave_pseudo_terminal_fds+0x30>
lea    0xe65(%rip),%rdi
call   1040 <errno_exit@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2e09(%rip),%rcx
cmp    (%rcx),%eax
je     11d3 <prepare_slave_pseudo_terminal_fds+0x63>
lea    0xe49(%rip),%rdi
call   1040 <errno_exit@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
jge    11f7 <prepare_slave_pseudo_terminal_fds+0x87>
lea    0xe50(%rip),%rdi
call   1040 <errno_exit@plt>
mov    -0xc(%rbp),%edi
call   1030 <dup@plt>
mov    0x2dea(%rip),%rcx
cmp    (%rcx),%eax
je     121a <prepare_slave_pseudo_terminal_fds+0xaa>
lea    0xe45(%rip),%rdi
call   1040 <errno_exit@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
jge    123e <prepare_slave_pseudo_terminal_fds+0xce>
lea    0xe4d(%rip),%rdi
call   1040 <errno_exit@plt>
mov    -0xc(%rbp),%edi
call   1030 <dup@plt>
mov    0x2d83(%rip),%rcx
cmp    (%rcx),%eax
je     1261 <prepare_slave_pseudo_terminal_fds+0xf1>
lea    0xe42(%rip),%rdi
call   1040 <errno_exit@plt>
mov    -0xc(%rbp),%edi
call   1070 <tcgetpgrp@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1080 <getpgrp@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
cmp    %rcx,%rax
je     1290 <prepare_slave_pseudo_terminal_fds+0x120>
lea    0xe3f(%rip),%rdi
call   1060 <error_exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prepare_slave_pseudo_terminal_fds>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e4a(%rip),%r14
mov    (%r14),%edi
call   1050 <close@plt>
test   %rax,%rax
jns    1197 <prepare_slave_pseudo_terminal_fds+0x27>
lea    0xe6e(%rip),%rdi
call   1040 <errno_exit@plt>
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
mov    %eax,%ebx
cmp    (%r14),%eax
je     11bb <prepare_slave_pseudo_terminal_fds+0x4b>
lea    0xe61(%rip),%rdi
call   1040 <errno_exit@plt>
mov    0x2e2e(%rip),%r14
mov    (%r14),%edi
call   1050 <close@plt>
test   %rax,%rax
jns    11db <prepare_slave_pseudo_terminal_fds+0x6b>
lea    0xe6c(%rip),%rdi
call   1040 <errno_exit@plt>
mov    %ebx,%edi
call   1030 <dup@plt>
cmp    (%r14),%eax
je     11f3 <prepare_slave_pseudo_terminal_fds+0x83>
lea    0xe6c(%rip),%rdi
call   1040 <errno_exit@plt>
mov    0x2dd6(%rip),%r14
mov    (%r14),%edi
call   1050 <close@plt>
test   %rax,%rax
jns    1213 <prepare_slave_pseudo_terminal_fds+0xa3>
lea    0xe78(%rip),%rdi
call   1040 <errno_exit@plt>
mov    %ebx,%edi
call   1030 <dup@plt>
cmp    (%r14),%eax
je     122b <prepare_slave_pseudo_terminal_fds+0xbb>
lea    0xe78(%rip),%rdi
call   1040 <errno_exit@plt>
mov    %ebx,%edi
call   1070 <tcgetpgrp@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1080 <getpgrp@plt>
cmp    %rax,%rbx
jne    1249 <prepare_slave_pseudo_terminal_fds+0xd9>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe7a(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <error_exit@plt>

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@.str = [23 x i8] c"cannot close old stdin\00"
@O_RDWR = global i32 0
@.str.1 = [43 x i8] c"cannot open slave pseudo-terminal at stdin\00"
@STDOUT_FILENO = global i32 0
@.str.2 = [24 x i8] c"cannot close old stdout\00"
@.str.3 = [44 x i8] c"cannot open slave pseudo-terminal at stdout\00"
@STDERR_FILENO = global i32 0
@.str.4 = [24 x i8] c"cannot close old stderr\00"
@.str.5 = [44 x i8] c"cannot open slave pseudo-terminal at stderr\00"
@.str.6 = [59 x i8] c"cannot become controlling process of slave pseudo-terminal\00"
define void @prepare_slave_pseudo_terminal_fds(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
%4 = load i32, i32* @STDIN_FILENO
%5 = call i64 @close(i32 %4)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32 @errno_exit(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %1
%10 = load i8*, i8** %2
%11 = load i32, i32* @O_RDWR
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %3
%13 = load i32, i32* %3
%14 = load i32, i32* @STDIN_FILENO
%15 = icmp ne i32 %13, %14
br i1 %15, label %16, label %18
16: ; preds = %9
%17 = call i32 @errno_exit(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %9
%19 = load i32, i32* @STDOUT_FILENO
%20 = call i64 @close(i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 @errno_exit(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %22, %18
%25 = load i32, i32* %3
%26 = call i32 @dup(i32 %25)
%27 = load i32, i32* @STDOUT_FILENO
%28 = icmp ne i32 %26, %27
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 @errno_exit(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
br label %31
31: ; preds = %29, %24
%32 = load i32, i32* @STDERR_FILENO
%33 = call i64 @close(i32 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = call i32 @errno_exit(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %37
37: ; preds = %35, %31
%38 = load i32, i32* %3
%39 = call i32 @dup(i32 %38)
%40 = load i32, i32* @STDERR_FILENO
%41 = icmp ne i32 %39, %40
br i1 %41, label %42, label %44
42: ; preds = %37
%43 = call i32 @errno_exit(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0))
br label %44
44: ; preds = %42, %37
%45 = load i32, i32* %3
%46 = call i64 @tcgetpgrp(i32 %45)
%47 = call i64 (...) @getpgrp()
%48 = icmp ne i64 %46, %47
br i1 %48, label %49, label %51
49: ; preds = %44
%50 = call i32 @error_exit(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.6, i64 0, i64 0))
br label %51
51: ; preds = %49, %44
ret void
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@.str = [23 x i8] c"cannot close old stdin\00"
@O_RDWR = global i32 0
@.str.1 = [43 x i8] c"cannot open slave pseudo-terminal at stdin\00"
@STDOUT_FILENO = global i32 0
@.str.2 = [24 x i8] c"cannot close old stdout\00"
@.str.3 = [44 x i8] c"cannot open slave pseudo-terminal at stdout\00"
@STDERR_FILENO = global i32 0
@.str.4 = [24 x i8] c"cannot close old stderr\00"
@.str.5 = [44 x i8] c"cannot open slave pseudo-terminal at stderr\00"
@.str.6 = [59 x i8] c"cannot become controlling process of slave pseudo-terminal\00"
define void @prepare_slave_pseudo_terminal_fds(i8* nocapture %0) {
%2 = load i32, i32* @STDIN_FILENO
%3 = tail call i64 @close(i32 %2)
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @errno_exit(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %1
%8 = load i32, i32* @O_RDWR
%9 = tail call i32 @open(i8* %0, i32 %8)
%10 = load i32, i32* @STDIN_FILENO
%11 = icmp eq i32 %9, %10
br i1 %11, label %14, label %12
12: ; preds = %7
%13 = tail call i32 @errno_exit(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %7
%15 = load i32, i32* @STDOUT_FILENO
%16 = tail call i64 @close(i32 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = tail call i32 @errno_exit(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %18, %14
%21 = tail call i32 @dup(i32 %9)
%22 = load i32, i32* @STDOUT_FILENO
%23 = icmp eq i32 %21, %22
br i1 %23, label %26, label %24
24: ; preds = %20
%25 = tail call i32 @errno_exit(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
br label %26
26: ; preds = %24, %20
%27 = load i32, i32* @STDERR_FILENO
%28 = tail call i64 @close(i32 %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = tail call i32 @errno_exit(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %32
32: ; preds = %30, %26
%33 = tail call i32 @dup(i32 %9)
%34 = load i32, i32* @STDERR_FILENO
%35 = icmp eq i32 %33, %34
br i1 %35, label %38, label %36
36: ; preds = %32
%37 = tail call i32 @errno_exit(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0))
br label %38
38: ; preds = %36, %32
%39 = tail call i64 @tcgetpgrp(i32 %9)
%40 = tail call i64 (...) @getpgrp()
%41 = icmp eq i64 %39, %40
br i1 %41, label %44, label %42
42: ; preds = %38
%43 = tail call i32 @error_exit(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.6, i64 0, i64 0))
br label %44
44: ; preds = %42, %38
ret void
}