#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*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ do_shell(char *, int, int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
void sched_create_shell(char *executable, int *request_fd, int *return_fd) {
  pid_t p;
  int pfds_rq[2], pfds_ret[2];
  if (pipe(pfds_rq) < 0 || pipe(pfds_ret) < 0) {
    perror("pipe");
    exit(1);
  }
  p = fork();
  if (p < 0) {
    perror("scheduler: fork");
    exit(1);
  }
  if (p == 0) {
    close(pfds_rq[0]);
    close(pfds_ret[1]);
    do_shell(executable, pfds_rq[1], pfds_ret[0]);
    assert(0);
  }
  close(pfds_rq[1]);
  close(pfds_ret[0]);
  *request_fd = pfds_rq[0];
  *return_fd = pfds_ret[1];
}


x86 assembly (clang, -O0):
<sched_create_shell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
call   1040 <pipe@plt>
cmp    $0x0,%rax
jl     11aa <sched_create_shell+0x3a>
lea    -0x30(%rbp),%rdi
call   1040 <pipe@plt>
cmp    $0x0,%rax
jge    11c0 <sched_create_shell+0x50>
lea    0xe4f(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11ec <sched_create_shell+0x7c>
lea    0xe28(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jne    121d <sched_create_shell+0xad>
mov    -0x28(%rbp),%edi
call   1030 <close@plt>
mov    -0x2c(%rbp),%edi
call   1030 <close@plt>
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x30(%rbp),%edx
call   1090 <do_shell@plt>
xor    %edi,%edi
call   1050 <assert@plt>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    -0x30(%rbp),%edi
call   1030 <close@plt>
mov    -0x28(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sched_create_shell>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0x8(%rsp),%rdi
call   1040 <pipe@plt>
test   %rax,%rax
js     11fc <sched_create_shell+0x8c>
mov    %rsp,%rdi
call   1040 <pipe@plt>
test   %rax,%rax
js     11fc <sched_create_shell+0x8c>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     1205 <sched_create_shell+0x95>
jne    11d4 <sched_create_shell+0x64>
mov    0x8(%rsp),%edi
call   1030 <close@plt>
mov    0x4(%rsp),%edi
call   1030 <close@plt>
mov    0xc(%rsp),%esi
mov    (%rsp),%edx
mov    %r15,%rdi
call   1090 <do_shell@plt>
xor    %edi,%edi
call   1050 <assert@plt>
mov    0xc(%rsp),%edi
call   1030 <close@plt>
mov    (%rsp),%edi
call   1030 <close@plt>
mov    0x8(%rsp),%eax
mov    %eax,(%rbx)
mov    0x4(%rsp),%eax
mov    %eax,(%r14)
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdfd(%rip),%rdi
jmp    120c <sched_create_shell+0x9c>
lea    0xdf9(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [16 x i8] c"scheduler: fork\00"
define void @sched_create_shell(i8* %0, i32* %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i64
%8 = alloca [2 x i32]
%9 = alloca [2 x i32]
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32* %2, i32** %6
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%11 = call i64 @pipe(i32* %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %3
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%15 = call i64 @pipe(i32* %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13, %3
%18 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %13
%21 = call i64 (...) @fork()
store i64 %21, i64* %7
%22 = load i64, i64* %7
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %20
%28 = load i64, i64* %7
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %44
30: ; preds = %27
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%32 = load i32, i32* %31
%33 = call i32 @close(i32 %32)
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%35 = load i32, i32* %34
%36 = call i32 @close(i32 %35)
%37 = load i8*, i8** %4
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%39 = load i32, i32* %38
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%41 = load i32, i32* %40
%42 = call i32 @do_shell(i8* %37, i32 %39, i32 %41)
%43 = call i32 @assert(i32 0)
br label %44
44: ; preds = %30, %27
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%46 = load i32, i32* %45
%47 = call i32 @close(i32 %46)
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%49 = load i32, i32* %48
%50 = call i32 @close(i32 %49)
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%52 = load i32, i32* %51
%53 = load i32*, i32** %5
store i32 %52, i32* %53
%54 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%55 = load i32, i32* %54
%56 = load i32*, i32** %6
store i32 %55, i32* %56
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [16 x i8] c"scheduler: fork\00"
define void @sched_create_shell(i8* %0, i32* nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = alloca [2 x i32]
%5 = alloca [2 x i32]
%6 = bitcast [2 x i32]* %4 to i8*
%7 = bitcast [2 x i32]* %5 to i8*
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%9 = call i64 @pipe(i32* %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %3
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%13 = call i64 @pipe(i32* %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11, %3
%16 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %11
%19 = call i64 (...) @fork()
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %18
%25 = icmp eq i64 %19, 0
br i1 %25, label %26, label %37
26: ; preds = %24
%27 = load i32, i32* %8
%28 = call i32 @close(i32 %27)
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%30 = load i32, i32* %29
%31 = call i32 @close(i32 %30)
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%33 = load i32, i32* %32
%34 = load i32, i32* %12
%35 = call i32 @do_shell(i8* %0, i32 %33, i32 %34)
%36 = call i32 @assert(i32 0)
br label %37
37: ; preds = %26, %24
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%39 = load i32, i32* %38
%40 = call i32 @close(i32 %39)
%41 = load i32, i32* %12
%42 = call i32 @close(i32 %41)
%43 = load i32, i32* %8
store i32 %43, i32* %1
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%45 = load i32, i32* %44
store i32 %45, i32* %2
ret void
}