#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*/ EXIT_FAILURE;
int /*<<< orphan*/ O_RDWR;
scalar_t__ STDERR_FILENO;
scalar_t__ STDIN_FILENO;
scalar_t__ STDOUT_FILENO;
scalar_t__ dup2(int, scalar_t__);
int /*<<< orphan*/ execlp(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int setsid();
void run_pty_shell(char *pty_slave_name, int connect_fd) {
  if (setsid() == -1) {
    perror("Server: Unable to set a new session.");
    exit(EXIT_FAILURE);
  }
  int slave_fd;
  if ((slave_fd = open(pty_slave_name, O_RDWR)) == -1) {
    perror("Server: Unable to set controlling terminal.");
    exit(EXIT_FAILURE);
  }
  if (dup2(slave_fd, STDIN_FILENO) != STDIN_FILENO) {
    perror("Server: Unable to redirect standard input.");
    exit(EXIT_FAILURE);
  }
  if (dup2(slave_fd, STDOUT_FILENO) != STDOUT_FILENO) {
    perror("Server: Unable to redirect standard output.");
    exit(EXIT_FAILURE);
  }
  if (dup2(slave_fd, STDERR_FILENO) != STDERR_FILENO) {
    perror("Server: Unable to redirect standard error output.");
    exit(EXIT_FAILURE);
  }
  execlp("bash", "bash", NULL);
  perror("Server: Unable to execute Bash in terminal.");
  exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<run_pty_shell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x0,%al
call   1040 <setsid@plt>
cmp    $0xffffffff,%eax
jne    1199 <run_pty_shell+0x39>
lea    0xe7a(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    11d1 <run_pty_shell+0x71>
lea    0xe67(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%edi
mov    0x2de5(%rip),%rax
mov    (%rax),%rsi
call   1030 <dup2@plt>
mov    0x2dd6(%rip),%rcx
cmp    (%rcx),%rax
je     120d <run_pty_shell+0xad>
lea    0xe57(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%edi
mov    0x2dd9(%rip),%rax
mov    (%rax),%rsi
call   1030 <dup2@plt>
mov    0x2dca(%rip),%rcx
cmp    (%rcx),%rax
je     1249 <run_pty_shell+0xe9>
lea    0xe46(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%edi
mov    0x2d7d(%rip),%rax
mov    (%rax),%rsi
call   1030 <dup2@plt>
mov    0x2d6e(%rip),%rcx
cmp    (%rcx),%rax
je     1285 <run_pty_shell+0x125>
lea    0xe36(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    0xe4e(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
call   1080 <execlp@plt>
lea    0xe40(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<run_pty_shell>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <setsid@plt>
cmp    $0xffffffff,%eax
jne    117f <run_pty_shell+0x1f>
lea    0xe86(%rip),%rdi
jmp    1215 <run_pty_shell+0xb5>
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
jne    119e <run_pty_shell+0x3e>
lea    0xe89(%rip),%rdi
jmp    1215 <run_pty_shell+0xb5>
mov    %eax,%ebx
mov    0x2e19(%rip),%r14
mov    (%r14),%rsi
mov    %eax,%edi
call   1030 <dup2@plt>
cmp    (%r14),%rax
jne    11fc <run_pty_shell+0x9c>
mov    0x2e33(%rip),%r14
mov    (%r14),%rsi
mov    %ebx,%edi
call   1030 <dup2@plt>
cmp    (%r14),%rax
jne    1205 <run_pty_shell+0xa5>
mov    0x2dfd(%rip),%r14
mov    (%r14),%rsi
mov    %ebx,%edi
call   1030 <dup2@plt>
cmp    (%r14),%rax
jne    120e <run_pty_shell+0xae>
lea    0xef1(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   1080 <execlp@plt>
lea    0xee5(%rip),%rdi
jmp    1215 <run_pty_shell+0xb5>
lea    0xe4e(%rip),%rdi
jmp    1215 <run_pty_shell+0xb5>
lea    0xe70(%rip),%rdi
jmp    1215 <run_pty_shell+0xb5>
lea    0xe93(%rip),%rdi
call   1060 <perror@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [37 x i8] c"Server: Unable to set a new session.\00"
@EXIT_FAILURE = global i32 0
@O_RDWR = global i32 0
@.str.1 = [44 x i8] c"Server: Unable to set controlling terminal.\00"
@STDIN_FILENO = global i64 0
@.str.2 = [43 x i8] c"Server: Unable to redirect standard input.\00"
@STDOUT_FILENO = global i64 0
@.str.3 = [44 x i8] c"Server: Unable to redirect standard output.\00"
@STDERR_FILENO = global i64 0
@.str.4 = [50 x i8] c"Server: Unable to redirect standard error output.\00"
@.str.5 = [5 x i8] c"bash\00"
@.str.6 = [44 x i8] c"Server: Unable to execute Bash in terminal.\00"
define void @run_pty_shell(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = call i32 (...) @setsid()
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %2
%13 = load i8*, i8** %3
%14 = load i32, i32* @O_RDWR
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %5
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %12
%22 = load i32, i32* %5
%23 = load i64, i64* @STDIN_FILENO
%24 = call i64 @dup2(i32 %22, i64 %23)
%25 = load i64, i64* @STDIN_FILENO
%26 = icmp ne i64 %24, %25
br i1 %26, label %27, label %31
27: ; preds = %21
%28 = call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %21
%32 = load i32, i32* %5
%33 = load i64, i64* @STDOUT_FILENO
%34 = call i64 @dup2(i32 %32, i64 %33)
%35 = load i64, i64* @STDOUT_FILENO
%36 = icmp ne i64 %34, %35
br i1 %36, label %37, label %41
37: ; preds = %31
%38 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %31
%42 = load i32, i32* %5
%43 = load i64, i64* @STDERR_FILENO
%44 = call i64 @dup2(i32 %42, i64 %43)
%45 = load i64, i64* @STDERR_FILENO
%46 = icmp ne i64 %44, %45
br i1 %46, label %47, label %51
47: ; preds = %41
%48 = call i32 @perror(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.4, i64 0, i64 0))
%49 = load i32, i32* @EXIT_FAILURE
%50 = call i32 @exit(i32 %49)
unreachable
51: ; preds = %41
%52 = call i32 @execlp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i32* null)
%53 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.6, i64 0, i64 0))
%54 = load i32, i32* @EXIT_FAILURE
%55 = call i32 @exit(i32 %54)
unreachable
}

LLVM IR (-O2):
@.str = [37 x i8] c"Server: Unable to set a new session.\00"
@EXIT_FAILURE = global i32 0
@O_RDWR = global i32 0
@.str.1 = [44 x i8] c"Server: Unable to set controlling terminal.\00"
@STDIN_FILENO = global i64 0
@.str.2 = [43 x i8] c"Server: Unable to redirect standard input.\00"
@STDOUT_FILENO = global i64 0
@.str.3 = [44 x i8] c"Server: Unable to redirect standard output.\00"
@STDERR_FILENO = global i64 0
@.str.4 = [50 x i8] c"Server: Unable to redirect standard error output.\00"
@.str.5 = [5 x i8] c"bash\00"
@.str.6 = [44 x i8] c"Server: Unable to execute Bash in terminal.\00"
define void @run_pty_shell(i8* nocapture %0, i32 %1) {
%3 = tail call i32 (...) @setsid()
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %2
%10 = load i32, i32* @O_RDWR
%11 = tail call i32 @open(i8* %0, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = tail call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %9
%18 = load i64, i64* @STDIN_FILENO
%19 = tail call i64 @dup2(i32 %11, i64 %18)
%20 = load i64, i64* @STDIN_FILENO
%21 = icmp eq i64 %19, %20
br i1 %21, label %26, label %22
22: ; preds = %17
%23 = tail call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %17
%27 = load i64, i64* @STDOUT_FILENO
%28 = tail call i64 @dup2(i32 %11, i64 %27)
%29 = load i64, i64* @STDOUT_FILENO
%30 = icmp eq i64 %28, %29
br i1 %30, label %35, label %31
31: ; preds = %26
%32 = tail call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = tail call i32 @exit(i32 %33)
unreachable
35: ; preds = %26
%36 = load i64, i64* @STDERR_FILENO
%37 = tail call i64 @dup2(i32 %11, i64 %36)
%38 = load i64, i64* @STDERR_FILENO
%39 = icmp eq i64 %37, %38
br i1 %39, label %44, label %40
40: ; preds = %35
%41 = tail call i32 @perror(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.4, i64 0, i64 0))
%42 = load i32, i32* @EXIT_FAILURE
%43 = tail call i32 @exit(i32 %42)
unreachable
44: ; preds = %35
%45 = tail call i32 @execlp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i32* null)
%46 = tail call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.6, i64 0, i64 0))
%47 = load i32, i32* @EXIT_FAILURE
%48 = tail call i32 @exit(i32 %47)
unreachable
}