#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*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdout;
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ wait(int *);
int main(int argc, char *argv[]) {
  int k = 0;
  int holder;
  if (argc < 4) {
    printf("Usage ./connect command <:> <argument>\n");
    return 1;
  } else if (strcmp(argv[1], ":") == 0) {
    printf("Usage ./connect command <:> <argument>\n");
    return 1;
  }
  for (int i = 0; i < argc; i++) {
    if (strcmp(argv[i], ":") == 0) {
      k++;
      holder = i;
    }
  }
  if (k == 0) {
    printf("Usage ./connect command <:> <argument>\n");
    return 1;
  }
  if (k > 1) {
    printf("Only one piping command is allowed at this time\n");
    exit(1);
  }
  int fd[2], reader, writer, child_status;
  pipe(fd);
  reader = fd[0];
  writer = fd[1];
  pid_t child;
  argv[holder] = NULL;
  fflush(stdout);
  if ((child = fork()) != 0) {
    close(writer);
    dup2(reader, 0);
    execvp(argv[holder + 1], &argv[holder + 1]);
    wait(&child_status);
    return child_status;
  } else {
    close(reader);
    close(0);
    dup2(writer, 1);
    close(writer);
    execvp(argv[1], &argv[1]);
    exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    11df <main+0x3f>
lea    0xe32(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    138b <main+0x1eb>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe3a(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1215 <main+0x75>
lea    0xdfc(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    138b <main+0x1eb>
jmp    121a <main+0x7a>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1271 <main+0xd1>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xde8(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    125e <main+0xbe>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1263 <main+0xc3>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1221 <main+0x81>
cmpl   $0x0,-0x14(%rbp)
jne    1293 <main+0xf3>
lea    0xd7e(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    138b <main+0x1eb>
cmpl   $0x1,-0x14(%rbp)
jle    12b3 <main+0x113>
lea    0xd86(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    -0x24(%rbp),%rdi
call   1060 <pipe@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    0x2d01(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
je     1348 <main+0x1a8>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
add    %rax,%rsi
call   1090 <execvp@plt>
lea    -0x30(%rbp),%rdi
call   10b0 <wait@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    138b <main+0x1eb>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    -0x2c(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   1090 <execvp@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x3,%edi
jle    1291 <main+0xf1>
mov    %rsi,%r13
mov    %edi,%ebp
mov    0x8(%rsi),%rdi
lea    0xe39(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1291 <main+0xf1>
lea    0x8(%r13),%rax
mov    %rax,0x10(%rsp)
mov    %ebp,%r12d
xor    %r14d,%r14d
lea    0xe15(%rip),%r15
xor    %ebx,%ebx
nopl   (%rax)
mov    0x0(%r13,%r14,8),%rdi
mov    %r15,%rsi
call   1070 <strcmp@plt>
xor    %ecx,%ecx
test   %rax,%rax
cmove  %r14d,%ebp
sete   %cl
add    %ecx,%ebx
add    $0x1,%r14
cmp    %r14,%r12
jne    11f0 <main+0x50>
test   %ebx,%ebx
je     1291 <main+0xf1>
cmp    $0x1,%ebx
jne    12b1 <main+0x111>
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
mov    0x8(%rsp),%r12d
mov    0xc(%rsp),%r15d
movslq %ebp,%rbx
movq   $0x0,0x0(%r13,%rbx,8)
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     12c7 <main+0x127>
mov    %r15d,%edi
call   1050 <close@plt>
mov    %r12d,%edi
xor    %esi,%esi
call   1040 <dup2@plt>
lea    0x8(,%rbx,8),%rsi
add    %r13,%rsi
mov    0x8(%r13,%rbx,8),%rdi
call   1090 <execvp@plt>
lea    0x4(%rsp),%rdi
call   10b0 <wait@plt>
mov    0x4(%rsp),%eax
jmp    12a2 <main+0x102>
lea    0xd9a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd4a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    %r12d,%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    %r15d,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    %r15d,%edi
call   1050 <close@plt>
mov    0x10(%rsp),%rsi
mov    (%rsi),%rdi
call   1090 <execvp@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [40 x i8] c"Usage ./connect command <:> <argument>\0A\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [49 x i8] c"Only one piping command is allowed at this time\0A\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [2 x i32]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 4
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %108
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i64 @strcmp(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %108
26: ; preds = %18
br label %27
27: ; preds = %26
store i32 0, i32* %8
br label %28
28: ; preds = %45, %27
%29 = load i32, i32* %8
%30 = load i32, i32* %4
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %48
32: ; preds = %28
%33 = load i8**, i8*** %5
%34 = load i32, i32* %8
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8*, i8** %33, i64 %35
%37 = load i8*, i8** %36
%38 = call i64 @strcmp(i8* %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %32
%41 = load i32, i32* %6
%42 = add nsw i32 %41, 1
store i32 %42, i32* %6
%43 = load i32, i32* %8
store i32 %43, i32* %7
br label %44
44: ; preds = %40, %32
br label %45
45: ; preds = %44
%46 = load i32, i32* %8
%47 = add nsw i32 %46, 1
store i32 %47, i32* %8
br label %28
48: ; preds = %28
%49 = load i32, i32* %6
%50 = icmp eq i32 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %48
%52 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %108
53: ; preds = %48
%54 = load i32, i32* %6
%55 = icmp sgt i32 %54, 1
br i1 %55, label %56, label %59
56: ; preds = %53
%57 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0))
%58 = call i32 @exit(i32 1)
unreachable
59: ; preds = %53
%60 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%61 = call i32 @pipe(i32* %60)
%62 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%63 = load i32, i32* %62
store i32 %63, i32* %10
%64 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%65 = load i32, i32* %64
store i32 %65, i32* %11
%66 = load i8**, i8*** %5
%67 = load i32, i32* %7
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8*, i8** %66, i64 %68
store i8* null, i8** %69
%70 = load i32, i32* @stdout
%71 = call i32 @fflush(i32 %70)
%72 = call i32 (...) @fork()
store i32 %72, i32* %13
%73 = icmp ne i32 %72, 0
br i1 %73, label %74, label %93
74: ; preds = %59
%75 = load i32, i32* %11
%76 = call i32 @close(i32 %75)
%77 = load i32, i32* %10
%78 = call i32 @dup2(i32 %77, i32 0)
%79 = load i8**, i8*** %5
%80 = load i32, i32* %7
%81 = add nsw i32 %80, 1
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds i8*, i8** %79, i64 %82
%84 = load i8*, i8** %83
%85 = load i8**, i8*** %5
%86 = load i32, i32* %7
%87 = add nsw i32 %86, 1
%88 = sext i32 %87 to i64
%89 = getelementptr inbounds i8*, i8** %85, i64 %88
%90 = call i32 @execvp(i8* %84, i8** %89)
%91 = call i32 @wait(i32* %12)
%92 = load i32, i32* %12
store i32 %92, i32* %3
br label %108
93: ; preds = %59
%94 = load i32, i32* %10
%95 = call i32 @close(i32 %94)
%96 = call i32 @close(i32 0)
%97 = load i32, i32* %11
%98 = call i32 @dup2(i32 %97, i32 1)
%99 = load i32, i32* %11
%100 = call i32 @close(i32 %99)
%101 = load i8**, i8*** %5
%102 = getelementptr inbounds i8*, i8** %101, i64 1
%103 = load i8*, i8** %102
%104 = load i8**, i8*** %5
%105 = getelementptr inbounds i8*, i8** %104, i64 1
%106 = call i32 @execvp(i8* %103, i8** %105)
%107 = call i32 @exit(i32 1)
unreachable
108: ; preds = %74, %51, %24, %16
%109 = load i32, i32* %3
ret i32 %109
}

LLVM IR (-O2):
@.str.1 = [2 x i8] c":\00"
@stdout = global i32 0
@str = [48 x i8] c"Only one piping command is allowed at this time\00"
@str.5 = [39 x i8] c"Usage ./connect command <:> <argument>\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = icmp slt i32 %0, 4
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str.5, i64 0, i64 0))
br label %69
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i64 @strcmp(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %8
%14 = zext i32 %0 to i64
br label %18
15: ; preds = %8
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str.5, i64 0, i64 0))
br label %69
17: ; preds = %18
switch i32 %29, label %34 [
i32 0, label %32
i32 1, label %37
]
18: ; preds = %13, %18
%19 = phi i64 [ 0, %13 ], [ %30, %18 ]
%20 = phi i32 [ 0, %13 ], [ %29, %18 ]
%21 = phi i32 [ undef, %13 ], [ %27, %18 ]
%22 = getelementptr inbounds i8*, i8** %1, i64 %19
%23 = load i8*, i8** %22
%24 = tail call i64 @strcmp(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
%26 = trunc i64 %19 to i32
%27 = select i1 %25, i32 %26, i32 %21
%28 = zext i1 %25 to i32
%29 = add nuw nsw i32 %20, %28
%30 = add nuw nsw i64 %19, 1
%31 = icmp eq i64 %30, %14
br i1 %31, label %17, label %18
32: ; preds = %17
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str.5, i64 0, i64 0))
br label %69
34: ; preds = %17
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str, i64 0, i64 0))
%36 = tail call i32 @exit(i32 1)
unreachable
37: ; preds = %17
%38 = bitcast [2 x i32]* %3 to i8*
%39 = bitcast i32* %4 to i8*
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%41 = call i32 @pipe(i32* %40)
%42 = load i32, i32* %40
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%44 = load i32, i32* %43
%45 = sext i32 %27 to i64
%46 = getelementptr inbounds i8*, i8** %1, i64 %45
store i8* null, i8** %46
%47 = load i32, i32* @stdout
%48 = call i32 @fflush(i32 %47)
%49 = call i32 (...) @fork()
%50 = icmp eq i32 %49, 0
br i1 %50, label %61, label %51
51: ; preds = %37
%52 = call i32 @close(i32 %44)
%53 = call i32 @dup2(i32 %42, i32 0)
%54 = add nsw i32 %27, 1
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8*, i8** %1, i64 %55
%57 = load i8*, i8** %56
%58 = call i32 @execvp(i8* %57, i8** %56)
%59 = call i32 @wait(i32* %4)
%60 = load i32, i32* %4
br label %69
61: ; preds = %37
%62 = call i32 @close(i32 %42)
%63 = call i32 @close(i32 0)
%64 = call i32 @dup2(i32 %44, i32 1)
%65 = call i32 @close(i32 %44)
%66 = load i8*, i8** %9
%67 = call i32 @execvp(i8* %66, i8** %9)
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %51, %32, %15, %6
%70 = phi i32 [ 1, %6 ], [ 1, %15 ], [ 1, %32 ], [ %60, %51 ]
ret i32 %70
}