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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ async_read(int, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ socketpair(int /*<<< orphan*/, int /*<<< orphan*/,
                      int /*<<< orphan*/, int *);
int /*<<< orphan*/ start_shell(int);
void secure_shell(int s) {
  pid_t pid;
  int sp[2];
  if ((socketpair(AF_UNIX, SOCK_STREAM, 0, sp)) < 0) {
    perror("socketpair:");
    exit(1);
  }
  if ((pid = fork()) < 0) {
    perror("fork:");
    exit(1);
  } else if (!pid) {
    close(sp[1]);
    close(s);
    start_shell(sp[0]);
  }
  close(sp[0]);
  async_read(s, sp[1]);
}


x86 assembly (clang, -O0):
<secure_shell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rcx
xor    %edx,%edx
call   1040 <socketpair@plt>
cmp    $0x0,%rax
jge    11b8 <secure_shell+0x48>
lea    0xe57(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11e1 <secure_shell+0x71>
lea    0xe3a(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    1203 <secure_shell+0x93>
mov    -0xc(%rbp),%edi
call   1030 <close@plt>
mov    -0x4(%rbp),%edi
call   1030 <close@plt>
mov    -0x10(%rbp),%edi
call   1080 <start_shell@plt>
jmp    1208 <secure_shell+0x98>
mov    -0x10(%rbp),%edi
call   1030 <close@plt>
mov    -0x4(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1050 <async_read@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<secure_shell>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rcx
xor    %edx,%edx
call   1040 <socketpair@plt>
test   %rax,%rax
js     11da <secure_shell+0x6a>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
js     11e3 <secure_shell+0x73>
jne    11c0 <secure_shell+0x50>
mov    0xc(%rsp),%edi
call   1030 <close@plt>
mov    %ebx,%edi
call   1030 <close@plt>
mov    0x8(%rsp),%edi
call   1080 <start_shell@plt>
mov    0x8(%rsp),%edi
call   1030 <close@plt>
mov    0xc(%rsp),%esi
mov    %ebx,%edi
call   1050 <async_read@plt>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe1f(%rip),%rdi
jmp    11ea <secure_shell+0x7a>
lea    0xe22(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [12 x i8] c"socketpair:\00"
@.str.1 = [6 x i8] c"fork:\00"
define void @secure_shell(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [2 x i32]
store i32 %0, i32* %2
%5 = load i32, i32* @AF_UNIX
%6 = load i32, i32* @SOCK_STREAM
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%8 = call i64 @socketpair(i32 %5, i32 %6, i32 0, i32* %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %1
%14 = call i32 (...) @fork()
store i32 %14, i32* %3
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %13
%20 = load i32, i32* %3
%21 = icmp ne i32 %20, 0
br i1 %21, label %31, label %22
22: ; preds = %19
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
%26 = load i32, i32* %2
%27 = call i32 @close(i32 %26)
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%29 = load i32, i32* %28
%30 = call i32 @start_shell(i32 %29)
br label %31
31: ; preds = %22, %19
br label %32
32: ; preds = %31
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%34 = load i32, i32* %33
%35 = call i32 @close(i32 %34)
%36 = load i32, i32* %2
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%38 = load i32, i32* %37
%39 = call i32 @async_read(i32 %36, i32 %38)
ret void
}

LLVM IR (-O2):
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [12 x i8] c"socketpair:\00"
@.str.1 = [6 x i8] c"fork:\00"
define void @secure_shell(i32 %0) {
%2 = alloca [2 x i32]
%3 = bitcast [2 x i32]* %2 to i8*
%4 = load i32, i32* @AF_UNIX
%5 = load i32, i32* @SOCK_STREAM
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%7 = call i64 @socketpair(i32 %4, i32 %5, i32 0, i32* %6)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %1
%13 = call i32 (...) @fork()
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %12
%19 = icmp eq i32 %13, 0
br i1 %19, label %20, label %27
20: ; preds = %18
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%22 = load i32, i32* %21
%23 = call i32 @close(i32 %22)
%24 = call i32 @close(i32 %0)
%25 = load i32, i32* %6
%26 = call i32 @start_shell(i32 %25)
br label %27
27: ; preds = %18, %20
%28 = load i32, i32* %6
%29 = call i32 @close(i32 %28)
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%31 = load i32, i32* %30
%32 = call i32 @async_read(i32 %0, i32 %31)
ret void
}