#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 dup(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execv(char *, char **);
int fork();
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, char);
scalar_t__ read(int, char *, int);
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
void main() {
  char *args[4] = {"shell", "ls", "-al", NULL};
  int pipein[2];
  int pipeout[2];
  int stdin = dup(0);
  int stdout = dup(1);
  pipe(pipein);
  pipe(pipeout);
  int pid = fork();
  for (int x = 1; x < 3; x += 1)
    write(pipein[1], args[x], strlen(args[x]));
  write(pipein[1], "\nexit\n", 6);
  if (pid == 0) {
    dup2(pipein[0], 0);
    dup2(pipeout[1], 1);
    execv(args[0], args);
  }
  char c = 0;
  while (read(pipeout[0], &c, 1) > 0) {
    printf("%c", c);
  }
  dup2(stdin, 0);
  dup2(stdout, 1);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    0x2c61(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2c5e(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0x2c5b(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0x2c58(%rip),%rax
mov    %rax,-0x8(%rbp)
xor    %edi,%edi
call   1070 <dup@plt>
mov    %eax,-0x34(%rbp)
mov    $0x1,%edi
call   1070 <dup@plt>
mov    %eax,-0x38(%rbp)
lea    -0x28(%rbp),%rdi
call   1080 <pipe@plt>
lea    -0x30(%rbp),%rdi
call   1080 <pipe@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x3c(%rbp)
movl   $0x1,-0x40(%rbp)
cmpl   $0x3,-0x40(%rbp)
jge    1245 <main+0xb5>
mov    -0x24(%rbp),%eax
mov    %eax,-0x54(%rbp)
movslq -0x40(%rbp),%rax
mov    -0x20(%rbp,%rax,8),%rax
mov    %rax,-0x50(%rbp)
movslq -0x40(%rbp),%rax
mov    -0x20(%rbp,%rax,8),%rdi
call   1040 <strlen@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    11fe <main+0x6e>
mov    -0x24(%rbp),%edi
lea    0xdbe(%rip),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
cmpl   $0x0,-0x3c(%rbp)
jne    1287 <main+0xf7>
mov    -0x28(%rbp),%edi
xor    %esi,%esi
call   1050 <dup2@plt>
mov    -0x2c(%rbp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    -0x20(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   10a0 <execv@plt>
movb   $0x0,-0x41(%rbp)
mov    -0x30(%rbp),%edi
lea    -0x41(%rbp),%rsi
mov    $0x1,%edx
call   1090 <read@plt>
cmp    $0x0,%rax
jle    12bb <main+0x12b>
lea    0xd67(%rip),%rdi
movsbl -0x41(%rbp),%esi
call   1060 <printf@plt>
jmp    128b <main+0xfb>
mov    -0x34(%rbp),%edi
xor    %esi,%esi
call   1050 <dup2@plt>
mov    -0x38(%rbp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
movaps 0x2c6d(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0x2c51(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
xor    %edi,%edi
call   1070 <dup@plt>
mov    %eax,%r14d
mov    $0x1,%edi
call   1070 <dup@plt>
mov    %eax,%r15d
lea    0x10(%rsp),%rdi
call   1080 <pipe@plt>
lea    0x18(%rsp),%rdi
call   1080 <pipe@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%r12d
mov    0x14(%rsp),%ebp
lea    0xe12(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    0x14(%rsp),%ebx
lea    0xdf6(%rip),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    0x14(%rsp),%edi
lea    0xddb(%rip),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
test   %r12d,%r12d
jne    126b <main+0xdb>
mov    0x10(%rsp),%edi
xor    %esi,%esi
call   1050 <dup2@plt>
mov    0x1c(%rsp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
lea    0xd9f(%rip),%rdi
lea    0x20(%rsp),%rsi
call   10a0 <execv@plt>
movb   $0x0,0xf(%rsp)
mov    0x18(%rsp),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1090 <read@plt>
test   %rax,%rax
jle    12c3 <main+0x133>
lea    0xd85(%rip),%rbx
lea    0xf(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsbl 0xf(%rsp),%esi
mov    %rbx,%rdi
call   1060 <printf@plt>
mov    0x18(%rsp),%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1090 <read@plt>
test   %rax,%rax
jg     12a0 <main+0x110>
mov    %r14d,%edi
xor    %esi,%esi
call   1050 <dup2@plt>
mov    %r15d,%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"shell\00"
@.str.1 = [3 x i8] c"ls\00"
@.str.2 = [4 x i8] c"-al\00"
@__const.main.args = [4 x i8*] [i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i32 0, i32 0), i8* null]
@.str.3 = [7 x i8] c"\0Aexit\0A\00"
@.str.4 = [3 x i8] c"%c\00"
define void @main() {
%1 = alloca [4 x i8*]
%2 = alloca [2 x i32]
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
%9 = bitcast [4 x i8*]* %1 to i8*
%10 = call i32 @dup(i32 0)
store i32 %10, i32* %4
%11 = call i32 @dup(i32 1)
store i32 %11, i32* %5
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%15 = call i32 @pipe(i32* %14)
%16 = call i32 (...) @fork()
store i32 %16, i32* %6
store i32 1, i32* %7
br label %17
17: ; preds = %33, %0
%18 = load i32, i32* %7
%19 = icmp slt i32 %18, 3
br i1 %19, label %20, label %36
20: ; preds = %17
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%22 = load i32, i32* %21
%23 = load i32, i32* %7
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [4 x i8*], [4 x i8*]* %1, i64 0, i64 %24
%26 = load i8*, i8** %25
%27 = load i32, i32* %7
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [4 x i8*], [4 x i8*]* %1, i64 0, i64 %28
%30 = load i8*, i8** %29
%31 = call i32 @strlen(i8* %30)
%32 = call i32 @write(i32 %22, i8* %26, i32 %31)
br label %33
33: ; preds = %20
%34 = load i32, i32* %7
%35 = add nsw i32 %34, 1
store i32 %35, i32* %7
br label %17
36: ; preds = %17
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%38 = load i32, i32* %37
%39 = call i32 @write(i32 %38, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
%40 = load i32, i32* %6
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %53
42: ; preds = %36
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%44 = load i32, i32* %43
%45 = call i32 @dup2(i32 %44, i32 0)
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%47 = load i32, i32* %46
%48 = call i32 @dup2(i32 %47, i32 1)
%49 = getelementptr inbounds [4 x i8*], [4 x i8*]* %1, i64 0, i64 0
%50 = load i8*, i8** %49
%51 = getelementptr inbounds [4 x i8*], [4 x i8*]* %1, i64 0, i64 0
%52 = call i32 @execv(i8* %50, i8** %51)
br label %53
53: ; preds = %42, %36
store i8 0, i8* %8
br label %54
54: ; preds = %59, %53
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%56 = load i32, i32* %55
%57 = call i64 @read(i32 %56, i8* %8, i32 1)
%58 = icmp sgt i64 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %54
%60 = load i8, i8* %8
%61 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8 signext %60)
br label %54
62: ; preds = %54
%63 = load i32, i32* %4
%64 = call i32 @dup2(i32 %63, i32 0)
%65 = load i32, i32* %5
%66 = call i32 @dup2(i32 %65, i32 1)
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"shell\00"
@.str.1 = [3 x i8] c"ls\00"
@.str.2 = [4 x i8] c"-al\00"
@__const.main.args = [4 x i8*] [i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i32 0, i32 0), i8* null]
@.str.3 = [7 x i8] c"\0Aexit\0A\00"
@.str.4 = [3 x i8] c"%c\00"
define void @main() {
%1 = alloca [4 x i8*]
%2 = alloca [2 x i32]
%3 = alloca [2 x i32]
%4 = alloca i8
%5 = bitcast [4 x i8*]* %1 to i8*
%6 = bitcast [2 x i32]* %2 to i8*
%7 = bitcast [2 x i32]* %3 to i8*
%8 = tail call i32 @dup(i32 0)
%9 = tail call i32 @dup(i32 1)
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%11 = call i32 @pipe(i32* %10)
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
%14 = call i32 (...) @fork()
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%16 = load i32, i32* %15
%17 = call i32 @strlen(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @write(i32 %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = load i32, i32* %15
%20 = call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @write(i32 %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %20)
%22 = load i32, i32* %15
%23 = call i32 @write(i32 %22, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
%24 = icmp eq i32 %14, 0
br i1 %24, label %25, label %33
25: ; preds = %0
%26 = load i32, i32* %10
%27 = call i32 @dup2(i32 %26, i32 0)
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%29 = load i32, i32* %28
%30 = call i32 @dup2(i32 %29, i32 1)
%31 = getelementptr inbounds [4 x i8*], [4 x i8*]* %1, i64 0, i64 0
%32 = call i32 @execv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %31)
br label %33
33: ; preds = %25, %0
store i8 0, i8* %4
%34 = load i32, i32* %12
%35 = call i64 @read(i32 %34, i8* %4, i32 1)
%36 = icmp sgt i64 %35, 0
br i1 %36, label %37, label %43
37: ; preds = %33, %37
%38 = load i8, i8* %4
%39 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8 signext %38)
%40 = load i32, i32* %12
%41 = call i64 @read(i32 %40, i8* %4, i32 1)
%42 = icmp sgt i64 %41, 0
br i1 %42, label %37, label %43
43: ; preds = %37, %33
%44 = call i32 @dup2(i32 %8, i32 0)
%45 = call i32 @dup2(i32 %9, i32 1)
ret void
}