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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
scalar_t__ execl(char *, char *, char *, char *, char *, int /*<<< orphan*/ *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char const *, int /*<<< orphan*/);
scalar_t__ setsid();
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void open_shell(const char *tty) {
  pid_t pid = fork();
  if (pid == 0) {
    if (setsid() < 0) {
      fprintf(stderr, "Error setting SID: %s\n", strerror(errno));
    }
    int fd = open(tty, O_RDWR);
    if (fd < 0) {
      fprintf(stderr, "Unable to open tty: %s\n", strerror(errno));
    } else {
      dup2(fd, STDIN_FILENO);
      dup2(fd, STDOUT_FILENO);
      dup2(fd, STDERR_FILENO);
    }
    if (execl("/bin/bash", "/bin/bash", "--noprofile", "--norc", "-l", NULL) <
        0) {
      fprintf(stderr, "Unable to open shell: %s\n", strerror(errno));
    }
  } else if (pid < 0) {
    fprintf(stderr, "Unable to spawn process: %s\n", strerror(errno));
  }
}


x86 assembly (clang, -O0):
<open_shell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    12b6 <open_shell+0x146>
mov    $0x0,%al
call   1040 <setsid@plt>
cmp    $0x0,%rax
jge    11cf <open_shell+0x5f>
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rdx
lea    0xe36(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    121f <open_shell+0xaf>
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe02(%rip),%rsi
call   1050 <fprintf@plt>
jmp    1252 <open_shell+0xe2>
mov    -0x14(%rbp),%edi
mov    0x2d8f(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x14(%rbp),%edi
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x14(%rbp),%edi
mov    0x2d75(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
lea    0xdd6(%rip),%rsi
lea    0xdd9(%rip),%rdx
lea    0xdde(%rip),%rcx
lea    0xdde(%rip),%r8
xor    %eax,%eax
mov    %eax,%r9d
mov    %rsi,%rdi
call   1080 <execl@plt>
cmp    $0x0,%rax
jge    12b1 <open_shell+0x141>
mov    0x2d44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xda3(%rip),%rsi
call   1050 <fprintf@plt>
jmp    12f2 <open_shell+0x182>
cmpq   $0x0,-0x10(%rbp)
jge    12ed <open_shell+0x17d>
mov    0x2d08(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2cf4(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xd81(%rip),%rsi
call   1050 <fprintf@plt>
jmp    12f2 <open_shell+0x182>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_shell>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     1194 <open_shell+0x24>
jns    1265 <open_shell+0xf5>
lea    0xeda(%rip),%rbx
jmp    1273 <open_shell+0x103>
xor    %eax,%eax
call   1040 <setsid@plt>
test   %rax,%rax
jns    11c8 <open_shell+0x58>
mov    0x2e29(%rip),%rax
mov    (%rax),%ebp
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe42(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     1211 <open_shell+0xa1>
mov    %eax,%ebx
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <dup2@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <dup2@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <dup2@plt>
jmp    1239 <open_shell+0xc9>
mov    0x2db8(%rip),%rax
mov    (%rax),%ebx
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xde8(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
lea    0xdef(%rip),%rdi
lea    0xdf2(%rip),%rdx
lea    0xdf7(%rip),%rcx
lea    0xdf7(%rip),%r8
mov    %rdi,%rsi
xor    %r9d,%r9d
call   1080 <execl@plt>
test   %rax,%rax
js     126c <open_shell+0xfc>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xddc(%rip),%rbx
mov    0x2d56(%rip),%rax
mov    (%rax),%ebp
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <fprintf@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [23 x i8] c"Error setting SID: %s\0A\00"
@errno = global i32 0
@O_RDWR = global i32 0
@.str.1 = [24 x i8] c"Unable to open tty: %s\0A\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.2 = [10 x i8] c"/bin/bash\00"
@.str.3 = [12 x i8] c"--noprofile\00"
@.str.4 = [7 x i8] c"--norc\00"
@.str.5 = [3 x i8] c"-l\00"
@.str.6 = [26 x i8] c"Unable to open shell: %s\0A\00"
@.str.7 = [29 x i8] c"Unable to spawn process: %s\0A\00"
define void @open_shell(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32
store i8* %0, i8** %2
%5 = call i64 (...) @fork()
store i64 %5, i64* %3
%6 = load i64, i64* %3
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %46
8: ; preds = %1
%9 = call i64 (...) @setsid()
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %8
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %11, %8
%17 = load i8*, i8** %2
%18 = load i32, i32* @O_RDWR
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %16
%23 = load i32, i32* @stderr
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %25)
br label %37
27: ; preds = %16
%28 = load i32, i32* %4
%29 = load i32, i32* @STDIN_FILENO
%30 = call i32 @dup2(i32 %28, i32 %29)
%31 = load i32, i32* %4
%32 = load i32, i32* @STDOUT_FILENO
%33 = call i32 @dup2(i32 %31, i32 %32)
%34 = load i32, i32* %4
%35 = load i32, i32* @STDERR_FILENO
%36 = call i32 @dup2(i32 %34, i32 %35)
br label %37
37: ; preds = %27, %22
%38 = call i64 @execl(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* null)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %37
%41 = load i32, i32* @stderr
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0), i8* %43)
br label %45
45: ; preds = %40, %37
br label %55
46: ; preds = %1
%47 = load i64, i64* %3
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %46
%50 = load i32, i32* @stderr
%51 = load i32, i32* @errno
%52 = call i8* @strerror(i32 %51)
%53 = call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), i8* %52)
br label %54
54: ; preds = %49, %46
br label %55
55: ; preds = %54, %45
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [23 x i8] c"Error setting SID: %s\0A\00"
@errno = global i32 0
@O_RDWR = global i32 0
@.str.1 = [24 x i8] c"Unable to open tty: %s\0A\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.2 = [10 x i8] c"/bin/bash\00"
@.str.3 = [12 x i8] c"--noprofile\00"
@.str.4 = [7 x i8] c"--norc\00"
@.str.5 = [3 x i8] c"-l\00"
@.str.6 = [26 x i8] c"Unable to open shell: %s\0A\00"
@.str.7 = [29 x i8] c"Unable to spawn process: %s\0A\00"
define void @open_shell(i8* nocapture %0) {
%2 = tail call i64 (...) @fork()
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %31
4: ; preds = %1
%5 = tail call i64 (...) @setsid()
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %4
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %12
12: ; preds = %7, %4
%13 = load i32, i32* @O_RDWR
%14 = tail call i32 @open(i8* %0, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = tail call i8* @strerror(i32 %18)
%20 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %19)
br label %28
21: ; preds = %12
%22 = load i32, i32* @STDIN_FILENO
%23 = tail call i32 @dup2(i32 %14, i32 %22)
%24 = load i32, i32* @STDOUT_FILENO
%25 = tail call i32 @dup2(i32 %14, i32 %24)
%26 = load i32, i32* @STDERR_FILENO
%27 = tail call i32 @dup2(i32 %14, i32 %26)
br label %28
28: ; preds = %21, %16
%29 = tail call i64 @execl(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* null)
%30 = icmp slt i64 %29, 0
br i1 %30, label %33, label %39
31: ; preds = %1
%32 = icmp slt i64 %2, 0
br i1 %32, label %33, label %39
33: ; preds = %31, %28
%34 = phi i8* [ getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0), %28 ], [ getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), %31 ]
%35 = load i32, i32* @stderr
%36 = load i32, i32* @errno
%37 = tail call i8* @strerror(i32 %36)
%38 = tail call i32 @fprintf(i32 %35, i8* %34, i8* %37)
br label %39
39: ; preds = %33, %28, %31
ret void
}