#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*/ exit(int /*<<< orphan*/);
int fork();
scalar_t__ malloc(int);
int pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, int *, int);
int strtol(char *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ wait(int *);
int write(int, int *, int);
int main(int argc, char **argv) {
  int **fd;
  int k, n, i, j;
  int pid, st, s;
  int num = 0;
  if (argc < 2) {
    printf("wrong parameters\n");
    return 0;
  }
  k = strtol(argv[1], NULL, 10);
  n = strtol(argv[2], NULL, 10);
  if (k == 0 || k == 1) {
    printf("k should be >= 2\n");
    return 0;
  }
  if (n == 0) {
    printf("0 - 0\n");
    return 0;
  }
  fd = (int **)malloc(sizeof(int *) * k);
  for (i = 0; i < k; i++) {
    fd[i] = (int *)malloc(sizeof(int) * 2);
    s = pipe(fd[i]);
    if (s < 0) {
      printf("%d pipe failed\n", i);
      return 0;
    }
  }
  for (i = 1; i < k; i++) {
    if ((pid = fork()) == 0) {
      for (j = 0; j < k; j++) {
        if (i == j) {
          close(fd[i][0]);
          close(fd[i - 1][1]);
          continue;
        }
        if (j == i - 1)
          continue;
        close(fd[j][0]);
        close(fd[j][1]);
      }
      while ((s = read(fd[i - 1][0], &num, sizeof(int))) == sizeof(int)) {
        printf("%d - %d\n", i, num);
        num++;
        if (n == num)
          break;
        s = write(fd[i][1], &num, sizeof(int));
      }
      close(fd[i][1]);
      close(fd[i - 1][0]);
      exit(0);
    } else if (pid < 0) {
      printf("%d fork failed\n", i);
      return 0;
    }
  }
  for (i = 1; i < k - 1; i++) {
    close(fd[i][0]);
    close(fd[i][1]);
  }
  close(fd[0][0]);
  close(fd[k - 1][1]);
  s = write(fd[0][1], &num, sizeof(int));
  while ((s = read(fd[k - 1][0], &num, sizeof(int))) == sizeof(int)) {
    printf("0 - %d\n", num);
    num++;
    if (num == n)
      break;
    s = write(fd[0][1], &num, sizeof(int));
  }
  close(fd[0][1]);
  close(fd[k - 1][0]);
  for (i = 0; i < k; i++)
    wait(&st);
  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,-0x38(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11e1 <main+0x41>
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    160f <main+0x46f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1227 <main+0x87>
cmpl   $0x1,-0x1c(%rbp)
jne    1241 <main+0xa1>
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    160f <main+0x46f>
cmpl   $0x0,-0x20(%rbp)
jne    1265 <main+0xc5>
lea    0xdd2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    160f <main+0x46f>
movslq -0x1c(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12f2 <main+0x152>
mov    $0x8,%edi
call   1090 <malloc@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1060 <pipe@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    12df <main+0x13f>
mov    -0x24(%rbp),%esi
lea    0xd5f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    160f <main+0x46f>
jmp    12e4 <main+0x144>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    127f <main+0xdf>
movl   $0x1,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    14ad <main+0x30d>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jne    146e <main+0x2ce>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    13b3 <main+0x213>
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jne    1368 <main+0x1c8>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rax
mov    0x4(%rax),%edi
call   1050 <close@plt>
jmp    13a5 <main+0x205>
mov    -0x28(%rbp),%eax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    137e <main+0x1de>
jmp    13a5 <main+0x205>
mov    -0x18(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    0x4(%rax),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    131f <main+0x17f>
jmp    13b8 <main+0x218>
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
mov    $0x4,%edx
call   1070 <read@plt>
mov    %eax,-0x34(%rbp)
cltq
cmp    $0x4,%rax
jne    143b <main+0x29b>
mov    -0x24(%rbp),%esi
mov    -0x38(%rbp),%edx
lea    0xc46(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x38(%rbp),%eax
jne    1416 <main+0x276>
jmp    143b <main+0x29b>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    0x4(%rax),%edi
lea    -0x38(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %eax,-0x34(%rbp)
jmp    13b8 <main+0x218>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    0x4(%rax),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x2c(%rbp)
jge    1495 <main+0x2f5>
mov    -0x24(%rbp),%esi
lea    0xbc2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    160f <main+0x46f>
jmp    149a <main+0x2fa>
jmp    149f <main+0x2ff>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    12f9 <main+0x159>
movl   $0x1,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    14fa <main+0x35a>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    0x4(%rax),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    14b4 <main+0x314>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rax
mov    0x4(%rax),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x4(%rax),%edi
lea    -0x38(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
mov    $0x4,%edx
call   1070 <read@plt>
mov    %eax,-0x34(%rbp)
cltq
cmp    $0x4,%rax
jne    15b7 <main+0x417>
mov    -0x38(%rbp),%esi
lea    0xade(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x20(%rbp),%eax
jne    1597 <main+0x3f7>
jmp    15b7 <main+0x417>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x4(%rax),%edi
lea    -0x38(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %eax,-0x34(%rbp)
jmp    153c <main+0x39c>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x4(%rax),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%edi
call   1050 <close@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1608 <main+0x468>
lea    -0x30(%rbp),%rdi
call   10b0 <wait@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    15e5 <main+0x445>
movl   $0x0,-0x4(%rbp)
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    $0x28,%rsp
movl   $0x0,(%rsp)
cmp    $0x1,%edi
jg     11db <main+0x2b>
lea    0xe77(%rip),%rdi
call   1030 <puts@plt>
jmp    13cf <main+0x21f>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1090 <strtol@plt>
mov    %eax,%ebp
mov    0x10(%rbx),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1090 <strtol@plt>
cmp    $0x1,%ebp
ja     1216 <main+0x66>
lea    0xe2b(%rip),%rdi
call   1030 <puts@plt>
jmp    13cf <main+0x21f>
test   %eax,%eax
je     12d6 <main+0x126>
mov    %eax,0x4(%rsp)
lea    0x0(,%rbp,8),%edi
call   10a0 <malloc@plt>
mov    %rax,%r15
test   %ebp,%ebp
jle    12e7 <main+0x137>
mov    %ebp,%r12d
xor    %ebx,%ebx
xchg   %ax,%ax
mov    $0x8,%edi
call   10a0 <malloc@plt>
mov    %rax,(%r15,%rbx,8)
mov    %rax,%rdi
call   1070 <pipe@plt>
test   %eax,%eax
js     13bf <main+0x20f>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1240 <main+0x90>
cmp    $0x2,%r12d
jl     12e7 <main+0x137>
mov    $0x1,%r14d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
je     13ec <main+0x23c>
js     13e0 <main+0x230>
add    $0x1,%r14
add    $0xfffffffffffffff8,%rbx
cmp    %r14d,%r12d
jne    1280 <main+0xd0>
lea    -0x1(%rbp),%r14d
cmp    $0x3,%r12d
jl     12eb <main+0x13b>
mov    %r14d,%r12d
mov    $0x1,%ebx
mov    (%r15,%rbx,8),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    (%r15,%rbx,8),%rax
mov    0x4(%rax),%edi
call   1060 <close@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    12b4 <main+0x104>
jmp    12eb <main+0x13b>
lea    0xd54(%rip),%rdi
call   1030 <puts@plt>
jmp    13cf <main+0x21f>
lea    -0x1(%rbp),%r14d
mov    (%r15),%rax
mov    (%rax),%edi
call   1060 <close@plt>
movslq %r14d,%r14
mov    (%r15,%r14,8),%rax
mov    0x4(%rax),%edi
call   1060 <close@plt>
mov    (%r15),%rax
mov    0x4(%rax),%edi
mov    %rsp,%rbx
mov    %rbx,%rsi
mov    $0x4,%edx
call   1040 <write@plt>
mov    (%r15,%r14,8),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1080 <read@plt>
cmp    $0x4,%eax
jne    1386 <main+0x1d6>
lea    0xcf0(%rip),%r12
mov    %rsp,%r13
nopl   0x0(%rax)
mov    (%rsp),%ebx
mov    %r12,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%ebx
mov    %ebx,(%rsp)
cmp    0x4(%rsp),%ebx
je     1386 <main+0x1d6>
mov    (%r15),%rax
mov    0x4(%rax),%edi
mov    %r13,%rsi
mov    $0x4,%edx
call   1040 <write@plt>
mov    (%r15,%r14,8),%rax
mov    (%rax),%edi
mov    %r13,%rsi
mov    $0x4,%edx
call   1080 <read@plt>
cmp    $0x4,%eax
je     1340 <main+0x190>
mov    (%r15),%rax
mov    0x4(%rax),%edi
call   1060 <close@plt>
mov    (%r15,%r14,8),%rax
mov    (%rax),%edi
call   1060 <close@plt>
test   %ebp,%ebp
jle    13cf <main+0x21f>
lea    0x24(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   10c0 <wait@plt>
add    $0xffffffff,%ebp
jne    13b0 <main+0x200>
jmp    13cf <main+0x21f>
lea    0xc3a(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc32(%rip),%rdi
mov    %r14d,%esi
jmp    13c8 <main+0x218>
test   %ebp,%ebp
jle    144f <main+0x29f>
mov    %r15,%rax
sub    %rbx,%rax
mov    %rax,0x18(%rsp)
add    $0x8,%rax
mov    %rax,0x10(%rsp)
lea    -0x1(%r14),%rax
mov    %rax,0x8(%rsp)
mov    %r15,%rbp
mov    %r14,%rbx
jmp    1435 <main+0x285>
mov    (%rax),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x0(%r13),%rax
mov    0x4(%rax),%edi
call   1060 <close@plt>
add    $0x8,%rbp
add    $0xffffffffffffffff,%r12
je     1458 <main+0x2a8>
mov    0x10(%rsp),%rax
mov    0x18(%rsp),%r13
sub    $0x1,%rbx
jb     1415 <main+0x265>
mov    %rbp,%rax
mov    %rbp,%r13
jne    1415 <main+0x265>
jmp    142b <main+0x27b>
lea    -0x1(%r14),%rax
mov    %rax,0x8(%rsp)
mov    0x8(%rsp),%rax
mov    (%r15,%rax,8),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
mov    $0x4,%edx
call   1080 <read@plt>
cmp    $0x4,%eax
mov    0x4(%rsp),%ebx
jne    14d0 <main+0x320>
lea    0xb90(%rip),%r12
mov    %rsp,%r13
mov    (%rsp),%ebp
mov    %r12,%rdi
mov    %r14d,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%ebp
mov    %ebp,(%rsp)
cmp    %ebp,%ebx
je     14d0 <main+0x320>
mov    (%r15,%r14,8),%rax
mov    0x4(%rax),%edi
mov    %r13,%rsi
mov    $0x4,%edx
call   1040 <write@plt>
mov    0x8(%rsp),%rax
mov    (%r15,%rax,8),%rax
mov    (%rax),%edi
mov    %r13,%rsi
mov    $0x4,%edx
call   1080 <read@plt>
cmp    $0x4,%eax
je     1483 <main+0x2d3>
mov    (%r15,%r14,8),%rax
mov    0x4(%rax),%edi
call   1060 <close@plt>
mov    0x8(%rsp),%rax
mov    (%r15,%rax,8),%rax
mov    (%rax),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"wrong parameters\0A\00"
@.str.1 = [18 x i8] c"k should be >= 2\0A\00"
@.str.2 = [7 x i8] c"0 - 0\0A\00"
@.str.3 = [16 x i8] c"%d pipe failed\0A\00"
@.str.4 = [9 x i8] c"%d - %d\0A\00"
@.str.5 = [16 x i8] c"%d fork failed\0A\00"
@.str.6 = [8 x i8] c"0 - %d\0A\00"
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 i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %14
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 2
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %298
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @strtol(i8* %22, i32* null, i32 10)
store i32 %23, i32* %7
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
%27 = call i32 @strtol(i8* %26, i32* null, i32 10)
store i32 %27, i32* %8
%28 = load i32, i32* %7
%29 = icmp eq i32 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %19
%31 = load i32, i32* %7
%32 = icmp eq i32 %31, 1
br i1 %32, label %33, label %35
33: ; preds = %30, %19
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %298
35: ; preds = %30
%36 = load i32, i32* %8
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %298
40: ; preds = %35
%41 = load i32, i32* %7
%42 = sext i32 %41 to i64
%43 = mul i64 8, %42
%44 = trunc i64 %43 to i32
%45 = call i64 @malloc(i32 %44)
%46 = inttoptr i64 %45 to i32**
store i32** %46, i32*** %6
store i32 0, i32* %9
br label %47
47: ; preds = %70, %40
%48 = load i32, i32* %9
%49 = load i32, i32* %7
%50 = icmp slt i32 %48, %49
br i1 %50, label %51, label %73
51: ; preds = %47
%52 = call i64 @malloc(i32 8)
%53 = inttoptr i64 %52 to i32*
%54 = load i32**, i32*** %6
%55 = load i32, i32* %9
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i32*, i32** %54, i64 %56
store i32* %53, i32** %57
%58 = load i32**, i32*** %6
%59 = load i32, i32* %9
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i32*, i32** %58, i64 %60
%62 = load i32*, i32** %61
%63 = call i32 @pipe(i32* %62)
store i32 %63, i32* %13
%64 = load i32, i32* %13
%65 = icmp slt i32 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %51
%67 = load i32, i32* %9
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %67)
store i32 0, i32* %3
br label %298
69: ; preds = %51
br label %70
70: ; preds = %69
%71 = load i32, i32* %9
%72 = add nsw i32 %71, 1
store i32 %72, i32* %9
br label %47
73: ; preds = %47
store i32 1, i32* %9
br label %74
74: ; preds = %193, %73
%75 = load i32, i32* %9
%76 = load i32, i32* %7
%77 = icmp slt i32 %75, %76
br i1 %77, label %78, label %196
78: ; preds = %74
%79 = call i32 (...) @fork()
store i32 %79, i32* %11
%80 = icmp eq i32 %79, 0
br i1 %80, label %81, label %185
81: ; preds = %78
store i32 0, i32* %10
br label %82
82: ; preds = %131, %81
%83 = load i32, i32* %10
%84 = load i32, i32* %7
%85 = icmp slt i32 %83, %84
br i1 %85, label %86, label %134
86: ; preds = %82
%87 = load i32, i32* %9
%88 = load i32, i32* %10
%89 = icmp eq i32 %87, %88
br i1 %89, label %90, label %108
90: ; preds = %86
%91 = load i32**, i32*** %6
%92 = load i32, i32* %9
%93 = sext i32 %92 to i64
%94 = getelementptr inbounds i32*, i32** %91, i64 %93
%95 = load i32*, i32** %94
%96 = getelementptr inbounds i32, i32* %95, i64 0
%97 = load i32, i32* %96
%98 = call i32 @close(i32 %97)
%99 = load i32**, i32*** %6
%100 = load i32, i32* %9
%101 = sub nsw i32 %100, 1
%102 = sext i32 %101 to i64
%103 = getelementptr inbounds i32*, i32** %99, i64 %102
%104 = load i32*, i32** %103
%105 = getelementptr inbounds i32, i32* %104, i64 1
%106 = load i32, i32* %105
%107 = call i32 @close(i32 %106)
br label %131
108: ; preds = %86
%109 = load i32, i32* %10
%110 = load i32, i32* %9
%111 = sub nsw i32 %110, 1
%112 = icmp eq i32 %109, %111
br i1 %112, label %113, label %114
113: ; preds = %108
br label %131
114: ; preds = %108
%115 = load i32**, i32*** %6
%116 = load i32, i32* %10
%117 = sext i32 %116 to i64
%118 = getelementptr inbounds i32*, i32** %115, i64 %117
%119 = load i32*, i32** %118
%120 = getelementptr inbounds i32, i32* %119, i64 0
%121 = load i32, i32* %120
%122 = call i32 @close(i32 %121)
%123 = load i32**, i32*** %6
%124 = load i32, i32* %10
%125 = sext i32 %124 to i64
%126 = getelementptr inbounds i32*, i32** %123, i64 %125
%127 = load i32*, i32** %126
%128 = getelementptr inbounds i32, i32* %127, i64 1
%129 = load i32, i32* %128
%130 = call i32 @close(i32 %129)
br label %131
131: ; preds = %114, %113, %90
%132 = load i32, i32* %10
%133 = add nsw i32 %132, 1
store i32 %133, i32* %10
br label %82
134: ; preds = %82
br label %135
135: ; preds = %157, %134
%136 = load i32**, i32*** %6
%137 = load i32, i32* %9
%138 = sub nsw i32 %137, 1
%139 = sext i32 %138 to i64
%140 = getelementptr inbounds i32*, i32** %136, i64 %139
%141 = load i32*, i32** %140
%142 = getelementptr inbounds i32, i32* %141, i64 0
%143 = load i32, i32* %142
%144 = call i32 @read(i32 %143, i32* %14, i32 4)
store i32 %144, i32* %13
%145 = sext i32 %144 to i64
%146 = icmp eq i64 %145, 4
br i1 %146, label %147, label %166
147: ; preds = %135
%148 = load i32, i32* %9
%149 = load i32, i32* %14
%150 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %148, i32 %149)
%151 = load i32, i32* %14
%152 = add nsw i32 %151, 1
store i32 %152, i32* %14
%153 = load i32, i32* %8
%154 = load i32, i32* %14
%155 = icmp eq i32 %153, %154
br i1 %155, label %156, label %157
156: ; preds = %147
br label %166
157: ; preds = %147
%158 = load i32**, i32*** %6
%159 = load i32, i32* %9
%160 = sext i32 %159 to i64
%161 = getelementptr inbounds i32*, i32** %158, i64 %160
%162 = load i32*, i32** %161
%163 = getelementptr inbounds i32, i32* %162, i64 1
%164 = load i32, i32* %163
%165 = call i32 @write(i32 %164, i32* %14, i32 4)
store i32 %165, i32* %13
br label %135
166: ; preds = %156, %135
%167 = load i32**, i32*** %6
%168 = load i32, i32* %9
%169 = sext i32 %168 to i64
%170 = getelementptr inbounds i32*, i32** %167, i64 %169
%171 = load i32*, i32** %170
%172 = getelementptr inbounds i32, i32* %171, i64 1
%173 = load i32, i32* %172
%174 = call i32 @close(i32 %173)
%175 = load i32**, i32*** %6
%176 = load i32, i32* %9
%177 = sub nsw i32 %176, 1
%178 = sext i32 %177 to i64
%179 = getelementptr inbounds i32*, i32** %175, i64 %178
%180 = load i32*, i32** %179
%181 = getelementptr inbounds i32, i32* %180, i64 0
%182 = load i32, i32* %181
%183 = call i32 @close(i32 %182)
%184 = call i32 @exit(i32 0)
unreachable
185: ; preds = %78
%186 = load i32, i32* %11
%187 = icmp slt i32 %186, 0
br i1 %187, label %188, label %191
188: ; preds = %185
%189 = load i32, i32* %9
%190 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %189)
store i32 0, i32* %3
br label %298
191: ; preds = %185
br label %192
192: ; preds = %191
br label %193
193: ; preds = %192
%194 = load i32, i32* %9
%195 = add nsw i32 %194, 1
store i32 %195, i32* %9
br label %74
196: ; preds = %74
store i32 1, i32* %9
br label %197
197: ; preds = %219, %196
%198 = load i32, i32* %9
%199 = load i32, i32* %7
%200 = sub nsw i32 %199, 1
%201 = icmp slt i32 %198, %200
br i1 %201, label %202, label %222
202: ; preds = %197
%203 = load i32**, i32*** %6
%204 = load i32, i32* %9
%205 = sext i32 %204 to i64
%206 = getelementptr inbounds i32*, i32** %203, i64 %205
%207 = load i32*, i32** %206
%208 = getelementptr inbounds i32, i32* %207, i64 0
%209 = load i32, i32* %208
%210 = call i32 @close(i32 %209)
%211 = load i32**, i32*** %6
%212 = load i32, i32* %9
%213 = sext i32 %212 to i64
%214 = getelementptr inbounds i32*, i32** %211, i64 %213
%215 = load i32*, i32** %214
%216 = getelementptr inbounds i32, i32* %215, i64 1
%217 = load i32, i32* %216
%218 = call i32 @close(i32 %217)
br label %219
219: ; preds = %202
%220 = load i32, i32* %9
%221 = add nsw i32 %220, 1
store i32 %221, i32* %9
br label %197
222: ; preds = %197
%223 = load i32**, i32*** %6
%224 = getelementptr inbounds i32*, i32** %223, i64 0
%225 = load i32*, i32** %224
%226 = getelementptr inbounds i32, i32* %225, i64 0
%227 = load i32, i32* %226
%228 = call i32 @close(i32 %227)
%229 = load i32**, i32*** %6
%230 = load i32, i32* %7
%231 = sub nsw i32 %230, 1
%232 = sext i32 %231 to i64
%233 = getelementptr inbounds i32*, i32** %229, i64 %232
%234 = load i32*, i32** %233
%235 = getelementptr inbounds i32, i32* %234, i64 1
%236 = load i32, i32* %235
%237 = call i32 @close(i32 %236)
%238 = load i32**, i32*** %6
%239 = getelementptr inbounds i32*, i32** %238, i64 0
%240 = load i32*, i32** %239
%241 = getelementptr inbounds i32, i32* %240, i64 1
%242 = load i32, i32* %241
%243 = call i32 @write(i32 %242, i32* %14, i32 4)
store i32 %243, i32* %13
br label %244
244: ; preds = %265, %222
%245 = load i32**, i32*** %6
%246 = load i32, i32* %7
%247 = sub nsw i32 %246, 1
%248 = sext i32 %247 to i64
%249 = getelementptr inbounds i32*, i32** %245, i64 %248
%250 = load i32*, i32** %249
%251 = getelementptr inbounds i32, i32* %250, i64 0
%252 = load i32, i32* %251
%253 = call i32 @read(i32 %252, i32* %14, i32 4)
store i32 %253, i32* %13
%254 = sext i32 %253 to i64
%255 = icmp eq i64 %254, 4
br i1 %255, label %256, label %272
256: ; preds = %244
%257 = load i32, i32* %14
%258 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i32 %257)
%259 = load i32, i32* %14
%260 = add nsw i32 %259, 1
store i32 %260, i32* %14
%261 = load i32, i32* %14
%262 = load i32, i32* %8
%263 = icmp eq i32 %261, %262
br i1 %263, label %264, label %265
264: ; preds = %256
br label %272
265: ; preds = %256
%266 = load i32**, i32*** %6
%267 = getelementptr inbounds i32*, i32** %266, i64 0
%268 = load i32*, i32** %267
%269 = getelementptr inbounds i32, i32* %268, i64 1
%270 = load i32, i32* %269
%271 = call i32 @write(i32 %270, i32* %14, i32 4)
store i32 %271, i32* %13
br label %244
272: ; preds = %264, %244
%273 = load i32**, i32*** %6
%274 = getelementptr inbounds i32*, i32** %273, i64 0
%275 = load i32*, i32** %274
%276 = getelementptr inbounds i32, i32* %275, i64 1
%277 = load i32, i32* %276
%278 = call i32 @close(i32 %277)
%279 = load i32**, i32*** %6
%280 = load i32, i32* %7
%281 = sub nsw i32 %280, 1
%282 = sext i32 %281 to i64
%283 = getelementptr inbounds i32*, i32** %279, i64 %282
%284 = load i32*, i32** %283
%285 = getelementptr inbounds i32, i32* %284, i64 0
%286 = load i32, i32* %285
%287 = call i32 @close(i32 %286)
store i32 0, i32* %9
br label %288
288: ; preds = %294, %272
%289 = load i32, i32* %9
%290 = load i32, i32* %7
%291 = icmp slt i32 %289, %290
br i1 %291, label %292, label %297
292: ; preds = %288
%293 = call i32 @wait(i32* %12)
br label %294
294: ; preds = %292
%295 = load i32, i32* %9
%296 = add nsw i32 %295, 1
store i32 %296, i32* %9
br label %288
297: ; preds = %288
store i32 0, i32* %3
br label %298
298: ; preds = %297, %188, %66, %38, %33, %17
%299 = load i32, i32* %3
ret i32 %299
}

LLVM IR (-O2):
@.str.3 = [16 x i8] c"%d pipe failed\0A\00"
@.str.4 = [9 x i8] c"%d - %d\0A\00"
@.str.5 = [16 x i8] c"%d fork failed\0A\00"
@.str.6 = [8 x i8] c"0 - %d\0A\00"
@str = [6 x i8] c"0 - 0\00"
@str.7 = [17 x i8] c"k should be >= 2\00"
@str.8 = [17 x i8] c"wrong parameters\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%7 = icmp slt i32 %0, 2
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.8, i64 0, i64 0))
br label %187
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32 @strtol(i8* nocapture %12, i32* null, i32 10)
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = tail call i32 @strtol(i8* nocapture %15, i32* null, i32 10)
%17 = icmp ult i32 %13, 2
br i1 %17, label %18, label %20
18: ; preds = %10
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.7, i64 0, i64 0))
br label %187
20: ; preds = %10
%21 = icmp eq i32 %16, 0
br i1 %21, label %22, label %24
22: ; preds = %20
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
br label %187
24: ; preds = %20
%25 = shl i32 %13, 3
%26 = tail call i64 @malloc(i32 %25)
%27 = inttoptr i64 %26 to i32**
%28 = icmp sgt i32 %13, 0
br i1 %28, label %29, label %46
29: ; preds = %24
%30 = zext i32 %13 to i64
br label %33
31: ; preds = %43
%32 = icmp sgt i32 %13, 1
br i1 %32, label %53, label %46
33: ; preds = %29, %43
%34 = phi i64 [ 0, %29 ], [ %44, %43 ]
%35 = tail call i64 @malloc(i32 8)
%36 = inttoptr i64 %35 to i32*
%37 = getelementptr inbounds i32*, i32** %27, i64 %34
store i32* %36, i32** %37
%38 = tail call i32 @pipe(i32* %36)
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %33
%41 = trunc i64 %34 to i32
%42 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %41)
br label %187
43: ; preds = %33
%44 = add nuw nsw i64 %34, 1
%45 = icmp eq i64 %44, %30
br i1 %45, label %31, label %33
46: ; preds = %31, %24
%47 = add i32 %13, -1
br label %141
48: ; preds = %126
%49 = add i32 %13, -1
%50 = icmp sgt i32 %13, 2
br i1 %50, label %51, label %141
51: ; preds = %48
%52 = zext i32 %49 to i64
br label %129
53: ; preds = %31, %126
%54 = phi i32 [ %127, %126 ], [ 1, %31 ]
%55 = tail call i32 (...) @fork()
%56 = icmp eq i32 %55, 0
br i1 %56, label %57, label %122
57: ; preds = %53
%58 = add nsw i32 %54, -1
br i1 %28, label %61, label %59
59: ; preds = %57
%60 = sext i32 %58 to i64
br label %68
61: ; preds = %57
%62 = zext i32 %54 to i64
%63 = getelementptr inbounds i32*, i32** %27, i64 %62
%64 = sext i32 %58 to i64
%65 = getelementptr inbounds i32*, i32** %27, i64 %64
%66 = zext i32 %58 to i64
%67 = zext i32 %13 to i64
br label %78
68: ; preds = %95, %59
%69 = phi i64 [ %60, %59 ], [ %64, %95 ]
%70 = getelementptr inbounds i32*, i32** %27, i64 %69
%71 = load i32*, i32** %70
%72 = load i32, i32* %71
%73 = call i32 @read(i32 %72, i32* %4, i32 4)
%74 = icmp eq i32 %73, 4
%75 = zext i32 %54 to i64
br i1 %74, label %76, label %112
76: ; preds = %68
%77 = getelementptr inbounds i32*, i32** %27, i64 %75
br label %98
78: ; preds = %61, %95
%79 = phi i64 [ 0, %61 ], [ %96, %95 ]
%80 = icmp eq i64 %79, %62
br i1 %80, label %85, label %81
81: ; preds = %78
%82 = icmp eq i64 %79, %66
br i1 %82, label %95, label %83
83: ; preds = %81
%84 = getelementptr inbounds i32*, i32** %27, i64 %79
br label %85
85: ; preds = %78, %83
%86 = phi i32** [ %84, %83 ], [ %63, %78 ]
%87 = phi i32** [ %84, %83 ], [ %65, %78 ]
%88 = load i32*, i32** %86
%89 = load i32, i32* %88
%90 = tail call i32 @close(i32 %89)
%91 = load i32*, i32** %87
%92 = getelementptr inbounds i32, i32* %91, i64 1
%93 = load i32, i32* %92
%94 = tail call i32 @close(i32 %93)
br label %95
95: ; preds = %85, %81
%96 = add nuw nsw i64 %79, 1
%97 = icmp eq i64 %96, %67
br i1 %97, label %68, label %78
98: ; preds = %76, %103
%99 = load i32, i32* %4
%100 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %54, i32 %99)
%101 = add nsw i32 %99, 1
store i32 %101, i32* %4
%102 = icmp eq i32 %16, %101
br i1 %102, label %112, label %103
103: ; preds = %98
%104 = load i32*, i32** %77
%105 = getelementptr inbounds i32, i32* %104, i64 1
%106 = load i32, i32* %105
%107 = call i32 @write(i32 %106, i32* %4, i32 4)
%108 = load i32*, i32** %70
%109 = load i32, i32* %108
%110 = call i32 @read(i32 %109, i32* %4, i32 4)
%111 = icmp eq i32 %110, 4
br i1 %111, label %98, label %112
112: ; preds = %103, %98, %68
%113 = getelementptr inbounds i32*, i32** %27, i64 %75
%114 = load i32*, i32** %113
%115 = getelementptr inbounds i32, i32* %114, i64 1
%116 = load i32, i32* %115
%117 = tail call i32 @close(i32 %116)
%118 = load i32*, i32** %70
%119 = load i32, i32* %118
%120 = tail call i32 @close(i32 %119)
%121 = tail call i32 @exit(i32 0)
unreachable
122: ; preds = %53
%123 = icmp slt i32 %55, 0
br i1 %123, label %124, label %126
124: ; preds = %122
%125 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %54)
br label %187
126: ; preds = %122
%127 = add nuw nsw i32 %54, 1
%128 = icmp eq i32 %127, %13
br i1 %128, label %48, label %53
129: ; preds = %51, %129
%130 = phi i64 [ 1, %51 ], [ %139, %129 ]
%131 = getelementptr inbounds i32*, i32** %27, i64 %130
%132 = load i32*, i32** %131
%133 = load i32, i32* %132
%134 = tail call i32 @close(i32 %133)
%135 = load i32*, i32** %131
%136 = getelementptr inbounds i32, i32* %135, i64 1
%137 = load i32, i32* %136
%138 = tail call i32 @close(i32 %137)
%139 = add nuw nsw i64 %130, 1
%140 = icmp eq i64 %139, %52
br i1 %140, label %141, label %129
141: ; preds = %129, %46, %48
%142 = phi i32 [ %47, %46 ], [ %49, %48 ], [ %49, %129 ]
%143 = load i32*, i32** %27
%144 = load i32, i32* %143
%145 = tail call i32 @close(i32 %144)
%146 = sext i32 %142 to i64
%147 = getelementptr inbounds i32*, i32** %27, i64 %146
%148 = load i32*, i32** %147
%149 = getelementptr inbounds i32, i32* %148, i64 1
%150 = load i32, i32* %149
%151 = tail call i32 @close(i32 %150)
%152 = load i32*, i32** %27
%153 = getelementptr inbounds i32, i32* %152, i64 1
%154 = load i32, i32* %153
%155 = call i32 @write(i32 %154, i32* %4, i32 4)
%156 = load i32*, i32** %147
%157 = load i32, i32* %156
%158 = call i32 @read(i32 %157, i32* %4, i32 4)
%159 = icmp eq i32 %158, 4
br i1 %159, label %160, label %174
160: ; preds = %141, %165
%161 = load i32, i32* %4
%162 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i32 %161)
%163 = add nsw i32 %161, 1
store i32 %163, i32* %4
%164 = icmp eq i32 %163, %16
br i1 %164, label %174, label %165
165: ; preds = %160
%166 = load i32*, i32** %27
%167 = getelementptr inbounds i32, i32* %166, i64 1
%168 = load i32, i32* %167
%169 = call i32 @write(i32 %168, i32* %4, i32 4)
%170 = load i32*, i32** %147
%171 = load i32, i32* %170
%172 = call i32 @read(i32 %171, i32* %4, i32 4)
%173 = icmp eq i32 %172, 4
br i1 %173, label %160, label %174
174: ; preds = %165, %160, %141
%175 = load i32*, i32** %27
%176 = getelementptr inbounds i32, i32* %175, i64 1
%177 = load i32, i32* %176
%178 = tail call i32 @close(i32 %177)
%179 = load i32*, i32** %147
%180 = load i32, i32* %179
%181 = tail call i32 @close(i32 %180)
br i1 %28, label %182, label %187
182: ; preds = %174, %182
%183 = phi i32 [ %185, %182 ], [ 0, %174 ]
%184 = call i32 @wait(i32* %3)
%185 = add nuw nsw i32 %183, 1
%186 = icmp eq i32 %185, %13
br i1 %186, label %187, label %182
187: ; preds = %182, %174, %124, %40, %22, %18, %8
ret i32 0
}