#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 *);
int /*<<< orphan*/ printf(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]);
  printf("+ Starting async read loop\n");
  async_read(s, sp[1]);
}


x86 assembly (clang, -O0):
<secure_shell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rcx
xor    %edx,%edx
call   1050 <socketpair@plt>
cmp    $0x0,%rax
jge    11c8 <secure_shell+0x48>
lea    0xe47(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11f1 <secure_shell+0x71>
lea    0xe2a(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    1213 <secure_shell+0x93>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%edi
call   1090 <start_shell@plt>
jmp    1218 <secure_shell+0x98>
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
lea    0xdeb(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1060 <async_read@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<secure_shell>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rcx
xor    %edx,%edx
call   1050 <socketpair@plt>
test   %rax,%rax
js     11f6 <secure_shell+0x76>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
js     11ff <secure_shell+0x7f>
jne    11d0 <secure_shell+0x50>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
call   1090 <start_shell@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
lea    0xe32(%rip),%rdi
call   1030 <puts@plt>
mov    0xc(%rsp),%esi
mov    %ebx,%edi
call   1060 <async_read@plt>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe03(%rip),%rdi
jmp    1206 <secure_shell+0x86>
lea    0xe06(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <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"
@.str.2 = [28 x i8] c"+ Starting async read loop\0A\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 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%37 = load i32, i32* %2
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%39 = load i32, i32* %38
%40 = call i32 @async_read(i32 %37, i32 %39)
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"
@str = [27 x i8] c"+ Starting async read loop\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 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%32 = load i32, i32* %31
%33 = call i32 @async_read(i32 %0, i32 %32)
ret void
}