#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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int pipe(int *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
void create_pipe(char **arguments, int pipe_flag) {
  char **arguments_2;
  int fd[2], i, pid, child_pid;
  arguments_2 = &arguments[pipe_flag];
  if (pipe(fd) == -1) {
    printf("ERROR while opening the pipe!\n");
    exit(204);
  }
  switch (pid = fork()) {
  case 0:
    close(fd[0]);
    dup2(fd[1], 1);
    close(fd[1]);
    execvp(arguments[0], arguments);
    fprintf(stderr, "ERROR %s no such program!\n", arguments[0]);
    exit(1);
  case -1:
    fprintf(stderr, "ERROR can't create child process!\n");
    break;
  default:
    close(fd[1]);
    dup2(fd[0], 0);
    close(fd[0]);
    execvp(arguments_2[0], arguments_2);
    fprintf(stderr, "ERROR %s no such program!\n", arguments_2[0]);
    exit(1);
  }
}


x86 assembly (clang, -O0):
<create_pipe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11ca <create_pipe+0x4a>
lea    0xe45(%rip),%rdi
call   1040 <printf@plt>
mov    $0xcc,%edi
call   1090 <exit@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,-0x28(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x30(%rbp)
sub    $0xffffffff,%eax
je     124c <create_pipe+0xcc>
jmp    11e7 <create_pipe+0x67>
mov    -0x30(%rbp),%eax
test   %eax,%eax
jne    1268 <create_pipe+0xe8>
jmp    11f7 <create_pipe+0x77>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
call   1080 <execvp@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rax
mov    (%rax),%rdx
lea    0xde4(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
lea    0xdde(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    12ba <create_pipe+0x13a>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <execvp@plt>
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
lea    0xd76(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_pipe>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rsp,%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
je     11df <create_pipe+0x5f>
xor    %eax,%eax
call   10a0 <fork@plt>
cmp    $0xffffffff,%eax
jne    11bf <create_pipe+0x3f>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
lea    0xe6a(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
test   %eax,%eax
jne    11f5 <create_pipe+0x75>
mov    (%rsp),%edi
call   1050 <close@plt>
mov    0x4(%rsp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    0x4(%rsp),%edi
jmp    1212 <create_pipe+0x92>
lea    0xe58(%rip),%rdi
call   1030 <puts@plt>
mov    $0xcc,%edi
call   1090 <exit@plt>
movslq %ebp,%rax
lea    (%rbx,%rax,8),%rbx
mov    0x4(%rsp),%edi
call   1050 <close@plt>
mov    (%rsp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    (%rsp),%edi
call   1050 <close@plt>
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   1080 <execvp@plt>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xdcb(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [31 x i8] c"ERROR while opening the pipe!\0A\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"ERROR %s no such program!\0A\00"
@.str.2 = [35 x i8] c"ERROR can't create child process!\0A\00"
define void @create_pipe(i8** %0, i32 %1) {
%3 = alloca i8**
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i32]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8** %0, i8*** %3
store i32 %1, i32* %4
%10 = load i8**, i8*** %3
%11 = load i32, i32* %4
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8*, i8** %10, i64 %12
store i8** %13, i8*** %5
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%15 = call i32 @pipe(i32* %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 204)
unreachable
20: ; preds = %2
%21 = call i32 (...) @fork()
store i32 %21, i32* %8
switch i32 %21, label %46 [
i32 0, label %22
i32 -1, label %43
]
22: ; preds = %20
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%27 = load i32, i32* %26
%28 = call i32 @dup2(i32 %27, i32 1)
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%30 = load i32, i32* %29
%31 = call i32 @close(i32 %30)
%32 = load i8**, i8*** %3
%33 = getelementptr inbounds i8*, i8** %32, i64 0
%34 = load i8*, i8** %33
%35 = load i8**, i8*** %3
%36 = call i32 @execvp(i8* %34, i8** %35)
%37 = load i32, i32* @stderr
%38 = load i8**, i8*** %3
%39 = getelementptr inbounds i8*, i8** %38, i64 0
%40 = load i8*, i8** %39
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %40)
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %20
%44 = load i32, i32* @stderr
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
br label %67
46: ; preds = %20
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%48 = load i32, i32* %47
%49 = call i32 @close(i32 %48)
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%51 = load i32, i32* %50
%52 = call i32 @dup2(i32 %51, i32 0)
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%54 = load i32, i32* %53
%55 = call i32 @close(i32 %54)
%56 = load i8**, i8*** %5
%57 = getelementptr inbounds i8*, i8** %56, i64 0
%58 = load i8*, i8** %57
%59 = load i8**, i8*** %5
%60 = call i32 @execvp(i8* %58, i8** %59)
%61 = load i32, i32* @stderr
%62 = load i8**, i8*** %5
%63 = getelementptr inbounds i8*, i8** %62, i64 0
%64 = load i8*, i8** %63
%65 = call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %64)
%66 = call i32 @exit(i32 1)
unreachable
67: ; preds = %43
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str.1 = [27 x i8] c"ERROR %s no such program!\0A\00"
@.str.2 = [35 x i8] c"ERROR can't create child process!\0A\00"
@str = [30 x i8] c"ERROR while opening the pipe!\00"
define void @create_pipe(i8** %0, i32 %1) {
%3 = alloca [2 x i32]
%4 = bitcast [2 x i32]* %3 to i8*
%5 = sext i32 %1 to i64
%6 = getelementptr inbounds i8*, i8** %0, i64 %5
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
%12 = call i32 @exit(i32 204)
unreachable
13: ; preds = %2
%14 = call i32 (...) @fork()
switch i32 %14, label %32 [
i32 0, label %15
i32 -1, label %29
]
15: ; preds = %13
%16 = load i32, i32* %7
%17 = call i32 @close(i32 %16)
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%19 = load i32, i32* %18
%20 = call i32 @dup2(i32 %19, i32 1)
%21 = load i32, i32* %18
%22 = call i32 @close(i32 %21)
%23 = load i8*, i8** %0
%24 = call i32 @execvp(i8* %23, i8** %0)
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %0
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %13
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
ret void
32: ; preds = %13
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%34 = load i32, i32* %33
%35 = call i32 @close(i32 %34)
%36 = load i32, i32* %7
%37 = call i32 @dup2(i32 %36, i32 0)
%38 = load i32, i32* %7
%39 = call i32 @close(i32 %38)
%40 = load i8*, i8** %6
%41 = call i32 @execvp(i8* %40, i8** %6)
%42 = load i32, i32* @stderr
%43 = load i8*, i8** %6
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %43)
%45 = call i32 @exit(i32 1)
unreachable
}