#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;
typedef int /*<<< orphan*/ file_name;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ close(int);
int dup(int);
int /*<<< orphan*/ dup2(int, int);
int execve(char *, char **, char **);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strncpy(char *, char *, int);
scalar_t__ wait(int *);
int main(int argc, char **argv, char **envp) {
  pid_t child_id;
  int child_status;
  int pipe_desc[2];
  char read_buffer[4096];
  int err;
  char file_name[4096];
  int proc_count;
  int i;
  int last_stdout;
  if (argc < 3) {
    printf("usage: pipe-4 executable-file number-of-procs\n");
    exit(1);
  }
  memset(file_name, 0, sizeof(file_name));
  strncpy(file_name, argv[1], sizeof(file_name));
  proc_count = atoi(argv[2]);
  last_stdout = dup(1);
  for (i = 0; i < proc_count; i++) {
    err = pipe(pipe_desc);
    if (err < 0) {
      printf("error creating pipe\n");
      exit(1);
    }
    child_id = fork();
    if (child_id == 0) {
      close(1);
      if (i < (proc_count - 1)) {
        dup2(pipe_desc[1], 1);
      } else {
        dup2(last_stdout, 1);
        close(pipe_desc[1]);
      }
      err = execve(file_name, &argv[1], envp);
      printf("parent error: %s didn't exec\n", file_name);
      exit(1);
    } else {
      close(0);
      if (i < (proc_count - 1)) {
        dup2(pipe_desc[0], 0);
      }
      close(pipe_desc[0]);
      close(pipe_desc[1]);
    }
  }
  close(1);
  dup2(last_stdout, 1);
  for (i = 0; i < proc_count; i++) {
    child_id = wait(&child_status);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2050,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11ff <main+0x3f>
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    -0x2040(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1070 <memset@plt>
lea    -0x2040(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    $0x1000,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x2044(%rbp)
mov    $0x1,%edi
call   1060 <dup@plt>
mov    %eax,-0x204c(%rbp)
movl   $0x0,-0x2048(%rbp)
mov    -0x2048(%rbp),%eax
cmp    -0x2044(%rbp),%eax
jge    138d <main+0x1cd>
lea    -0x2c(%rbp),%rdi
call   1090 <pipe@plt>
mov    %eax,-0x1034(%rbp)
cmpl   $0x0,-0x1034(%rbp)
jge    129e <main+0xde>
lea    0xda2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    133c <main+0x17c>
mov    $0x1,%edi
call   1080 <close@plt>
mov    -0x2048(%rbp),%eax
mov    -0x2044(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    12e7 <main+0x127>
mov    -0x28(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
jmp    12ff <main+0x13f>
mov    -0x204c(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x28(%rbp),%edi
call   1080 <close@plt>
lea    -0x2040(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
mov    -0x18(%rbp),%rdx
call   10a0 <execve@plt>
mov    %eax,-0x1034(%rbp)
lea    -0x2040(%rbp),%rsi
lea    0xd19(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
xor    %edi,%edi
call   1080 <close@plt>
mov    -0x2048(%rbp),%eax
mov    -0x2044(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1364 <main+0x1a4>
mov    -0x2c(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    -0x2c(%rbp),%edi
call   1080 <close@plt>
mov    -0x28(%rbp),%edi
call   1080 <close@plt>
jmp    1379 <main+0x1b9>
mov    -0x2048(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2048(%rbp)
jmp    1258 <main+0x98>
mov    $0x1,%edi
call   1080 <close@plt>
mov    -0x204c(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
movl   $0x0,-0x2048(%rbp)
mov    -0x2048(%rbp),%eax
cmp    -0x2044(%rbp),%eax
jge    13e4 <main+0x224>
lea    -0x24(%rbp),%rdi
call   10d0 <wait@plt>
mov    %rax,-0x20(%rbp)
mov    -0x2048(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2048(%rbp)
jmp    13b1 <main+0x1f1>
xor    %edi,%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1028,%rsp
cmp    $0x2,%edi
jg     11fc <main+0x2c>
lea    0xe45(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    %rdx,%rbp
mov    %rsi,%r15
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1080 <memset@plt>
mov    0x8(%r15),%rsi
mov    %rbx,%rdi
mov    $0x1000,%edx
call   1030 <strncpy@plt>
mov    0x10(%r15),%rdi
call   10c0 <atoi@plt>
mov    %eax,%ebx
mov    $0x1,%edi
call   1070 <dup@plt>
mov    %eax,%r12d
test   %ebx,%ebx
jle    12ac <main+0xdc>
mov    %rbp,0x10(%rsp)
add    $0x8,%r15
lea    -0x1(%rbx),%ebp
lea    0x8(%rsp),%r13
xor    %r14d,%r14d
jmp    127b <main+0xab>
nopl   0x0(%rax)
mov    0x8(%rsp),%edi
call   1090 <close@plt>
mov    0xc(%rsp),%edi
call   1090 <close@plt>
add    $0x1,%r14d
cmp    %r14d,%ebx
je     12f5 <main+0x125>
mov    %r13,%rdi
call   10a0 <pipe@plt>
test   %eax,%eax
js     12ca <main+0xfa>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
je     12d6 <main+0x106>
xor    %edi,%edi
call   1090 <close@plt>
cmp    %ebp,%r14d
jge    1260 <main+0x90>
mov    0x8(%rsp),%edi
xor    %esi,%esi
call   1050 <dup2@plt>
jmp    1260 <main+0x90>
mov    $0x1,%edi
call   1090 <close@plt>
mov    %r12d,%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
lea    0xd4d(%rip),%rdi
jmp    11ed <main+0x1d>
mov    $0x1,%edi
call   1090 <close@plt>
cmp    %ebp,%r14d
jge    132f <main+0x15f>
mov    0xc(%rsp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
jmp    1345 <main+0x175>
mov    $0x1,%edi
call   1090 <close@plt>
mov    %r12d,%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
test   %ebx,%ebx
jle    12c3 <main+0xf3>
lea    0x1c(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbp,%rdi
call   10e0 <wait@plt>
add    $0xffffffff,%ebx
jne    1320 <main+0x150>
jmp    12c3 <main+0xf3>
mov    %r12d,%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    0xc(%rsp),%edi
call   1090 <close@plt>
mov    0x10(%rsp),%rdx
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
call   10b0 <execve@plt>
lea    0xc9f(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [47 x i8] c"usage: pipe-4 executable-file number-of-procs\0A\00"
@.str.1 = [21 x i8] c"error creating pipe\0A\00"
@.str.2 = [30 x i8] c"parent error: %s didn't exec\0A\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i64
%9 = alloca i32
%10 = alloca [2 x i32]
%11 = alloca [4096 x i8]
%12 = alloca i32
%13 = alloca [4096 x i8]
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%17 = load i32, i32* %5
%18 = icmp slt i32 %17, 3
br i1 %18, label %19, label %22
19: ; preds = %3
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %3
%23 = getelementptr inbounds [4096 x i8], [4096 x i8]* %13, i64 0, i64 0
%24 = call i32 @memset(i8* %23, i32 0, i32 4096)
%25 = getelementptr inbounds [4096 x i8], [4096 x i8]* %13, i64 0, i64 0
%26 = load i8**, i8*** %6
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @strncpy(i8* %25, i8* %28, i32 4096)
%30 = load i8**, i8*** %6
%31 = getelementptr inbounds i8*, i8** %30, i64 2
%32 = load i8*, i8** %31
%33 = call i32 @atoi(i8* %32)
store i32 %33, i32* %14
%34 = call i32 @dup(i32 1)
store i32 %34, i32* %16
store i32 0, i32* %15
br label %35
35: ; preds = %94, %22
%36 = load i32, i32* %15
%37 = load i32, i32* %14
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %97
39: ; preds = %35
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%41 = call i32 @pipe(i32* %40)
store i32 %41, i32* %12
%42 = load i32, i32* %12
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %39
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %39
%48 = call i64 (...) @fork()
store i64 %48, i64* %8
%49 = load i64, i64* %8
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %76
51: ; preds = %47
%52 = call i32 @close(i32 1)
%53 = load i32, i32* %15
%54 = load i32, i32* %14
%55 = sub nsw i32 %54, 1
%56 = icmp slt i32 %53, %55
br i1 %56, label %57, label %61
57: ; preds = %51
%58 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 1
%59 = load i32, i32* %58
%60 = call i32 @dup2(i32 %59, i32 1)
br label %67
61: ; preds = %51
%62 = load i32, i32* %16
%63 = call i32 @dup2(i32 %62, i32 1)
%64 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 1
%65 = load i32, i32* %64
%66 = call i32 @close(i32 %65)
br label %67
67: ; preds = %61, %57
%68 = getelementptr inbounds [4096 x i8], [4096 x i8]* %13, i64 0, i64 0
%69 = load i8**, i8*** %6
%70 = getelementptr inbounds i8*, i8** %69, i64 1
%71 = load i8**, i8*** %7
%72 = call i32 @execve(i8* %68, i8** %70, i8** %71)
store i32 %72, i32* %12
%73 = getelementptr inbounds [4096 x i8], [4096 x i8]* %13, i64 0, i64 0
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %73)
%75 = call i32 @exit(i32 1)
unreachable
76: ; preds = %47
%77 = call i32 @close(i32 0)
%78 = load i32, i32* %15
%79 = load i32, i32* %14
%80 = sub nsw i32 %79, 1
%81 = icmp slt i32 %78, %80
br i1 %81, label %82, label %86
82: ; preds = %76
%83 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%84 = load i32, i32* %83
%85 = call i32 @dup2(i32 %84, i32 0)
br label %86
86: ; preds = %82, %76
%87 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%88 = load i32, i32* %87
%89 = call i32 @close(i32 %88)
%90 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 1
%91 = load i32, i32* %90
%92 = call i32 @close(i32 %91)
br label %93
93: ; preds = %86
br label %94
94: ; preds = %93
%95 = load i32, i32* %15
%96 = add nsw i32 %95, 1
store i32 %96, i32* %15
br label %35
97: ; preds = %35
%98 = call i32 @close(i32 1)
%99 = load i32, i32* %16
%100 = call i32 @dup2(i32 %99, i32 1)
store i32 0, i32* %15
br label %101
101: ; preds = %107, %97
%102 = load i32, i32* %15
%103 = load i32, i32* %14
%104 = icmp slt i32 %102, %103
br i1 %104, label %105, label %110
105: ; preds = %101
%106 = call i64 @wait(i32* %9)
store i64 %106, i64* %8
br label %107
107: ; preds = %105
%108 = load i32, i32* %15
%109 = add nsw i32 %108, 1
store i32 %109, i32* %15
br label %101
110: ; preds = %101
%111 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str.2 = [30 x i8] c"parent error: %s didn't exec\0A\00"
@str = [20 x i8] c"error creating pipe\00"
@str.3 = [46 x i8] c"usage: pipe-4 executable-file number-of-procs\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca [2 x i32]
%6 = alloca [4096 x i8]
%7 = bitcast i32* %4 to i8*
%8 = bitcast [2 x i32]* %5 to i8*
%9 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%10 = icmp slt i32 %0, 3
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str.3, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %3
%15 = call i32 @memset(i8* %9, i32 0, i32 4096)
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @strncpy(i8* %9, i8* %17, i32 4096)
%19 = getelementptr inbounds i8*, i8** %1, i64 2
%20 = load i8*, i8** %19
%21 = call i32 @atoi(i8* %20)
%22 = call i32 @dup(i32 1)
%23 = icmp sgt i32 %21, 0
br i1 %23, label %24, label %73
24: ; preds = %14
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%26 = add nsw i32 %21, -1
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
br label %28
28: ; preds = %24, %58
%29 = phi i32 [ 0, %24 ], [ %63, %58 ]
%30 = call i32 @pipe(i32* %25)
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %28
%36 = call i64 (...) @fork()
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %52
38: ; preds = %35
%39 = call i32 @close(i32 1)
%40 = icmp slt i32 %29, %26
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = load i32, i32* %27
%43 = call i32 @dup2(i32 %42, i32 1)
br label %48
44: ; preds = %38
%45 = call i32 @dup2(i32 %22, i32 1)
%46 = load i32, i32* %27
%47 = call i32 @close(i32 %46)
br label %48
48: ; preds = %44, %41
%49 = call i32 @execve(i8* %9, i8** %16, i8** %2)
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %9)
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %35
%53 = call i32 @close(i32 0)
%54 = icmp slt i32 %29, %26
br i1 %54, label %55, label %58
55: ; preds = %52
%56 = load i32, i32* %25
%57 = call i32 @dup2(i32 %56, i32 0)
br label %58
58: ; preds = %55, %52
%59 = load i32, i32* %25
%60 = call i32 @close(i32 %59)
%61 = load i32, i32* %27
%62 = call i32 @close(i32 %61)
%63 = add nuw nsw i32 %29, 1
%64 = icmp eq i32 %63, %21
br i1 %64, label %65, label %28
65: ; preds = %58
%66 = call i32 @close(i32 1)
%67 = call i32 @dup2(i32 %22, i32 1)
br i1 %23, label %68, label %76
68: ; preds = %65, %68
%69 = phi i32 [ %71, %68 ], [ 0, %65 ]
%70 = call i64 @wait(i32* %4)
%71 = add nuw nsw i32 %69, 1
%72 = icmp eq i32 %71, %21
br i1 %72, label %76, label %68
73: ; preds = %14
%74 = call i32 @close(i32 1)
%75 = call i32 @dup2(i32 %22, i32 1)
br label %76
76: ; preds = %68, %73, %65
%77 = call i32 @exit(i32 0)
unreachable
}