#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();
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int rand();
int /*<<< orphan*/ read(int, int *, int);
int /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int /*<<< orphan*/ wait(int /*<<< orphan*/);
int /*<<< orphan*/ write(int, int *, int);
int main() {
  srand(time(NULL));
  int p2c[2], c2p[2], n;
  pipe(p2c);
  pipe(c2p);
  int res = fork();
  if (res < 0) {
    printf("Error while fork-ing\n");
    return 0;
  }
  if (res == 0) {
    close(p2c[1]);
    close(c2p[0]);
    read(p2c[0], &n, sizeof(int));
    close(p2c[0]);
    for (int i = 1; i <= n; ++i) {
      int x = rand();
      printf("Son: %d\n", x);
      write(c2p[1], &x, sizeof(int));
    }
    close(c2p[1]);
    exit(0);
  }
  scanf("%d", &n);
  close(p2c[0]);
  close(c2p[1]);
  write(p2c[1], &n, sizeof(int));
  close(p2c[1]);
  for (int i = 1; i <= n; ++i) {
    int x;
    read(c2p[0], &x, sizeof(int));
    printf("Father: %d\n", x);
  }
  close(c2p[0]);
  wait(0);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1090 <time@plt>
mov    %eax,%edi
call   1080 <srand@plt>
lea    -0xc(%rbp),%rdi
call   1060 <pipe@plt>
lea    -0x14(%rbp),%rdi
call   1060 <pipe@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    121f <main+0x5f>
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1340 <main+0x180>
cmpl   $0x0,-0x1c(%rbp)
jne    12ae <main+0xee>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1070 <read@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
movl   $0x1,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x18(%rbp),%eax
jg     129f <main+0xdf>
mov    $0x0,%al
call   10e0 <rand@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xd9d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1259 <main+0x99>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
lea    0xd6a(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   10a0 <scanf@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x1,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x18(%rbp),%eax
jg     132a <main+0x16a>
mov    -0x14(%rbp),%edi
lea    -0x2c(%rbp),%rsi
mov    $0x4,%edx
call   1070 <read@plt>
mov    -0x2c(%rbp),%esi
lea    0xd0d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    12ee <main+0x12e>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   10c0 <wait@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
xor    %edi,%edi
call   10a0 <time@plt>
mov    %eax,%edi
call   1090 <srand@plt>
lea    0x10(%rsp),%rdi
call   1070 <pipe@plt>
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
js     12a9 <main+0xd9>
je     12c2 <main+0xf2>
lea    0xdf1(%rip),%rdi
mov    %rsp,%rbx
mov    %rbx,%rsi
call   10b0 <scanf@plt>
mov    0x10(%rsp),%edi
call   1060 <close@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    0x14(%rsp),%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1040 <write@plt>
mov    0x14(%rsp),%edi
call   1060 <close@plt>
cmpl   $0x0,(%rsp)
jle    1297 <main+0xc7>
lea    0x4(%rsp),%r14
lea    0xdab(%rip),%rbx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x8(%rsp),%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1080 <read@plt>
mov    0x4(%rsp),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%ebp
cmp    (%rsp),%ebp
jl     1270 <main+0xa0>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10d0 <wait@plt>
jmp    12b5 <main+0xe5>
lea    0xd68(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x14(%rsp),%edi
call   1060 <close@plt>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
mov    0x10(%rsp),%edi
mov    %rsp,%rsi
mov    $0x4,%edx
call   1080 <read@plt>
mov    0x10(%rsp),%edi
call   1060 <close@plt>
mov    (%rsp),%ebp
test   %ebp,%ebp
jle    133f <main+0x16f>
lea    0xd04(%rip),%r14
lea    0x4(%rsp),%r15
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   10f0 <rand@plt>
mov    %eax,0x4(%rsp)
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0xc(%rsp),%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1040 <write@plt>
add    $0x1,%ebx
cmp    %ebp,%ebx
jl     1310 <main+0x140>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [22 x i8] c"Error while fork-ing\0A\00"
@.str.1 = [9 x i8] c"Son: %d\0A\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [12 x i8] c"Father: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %1
%10 = call i32 @time(i32* null)
%11 = call i32 @srand(i32 %10)
%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* %5
%17 = load i32, i32* %5
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %0
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %88
21: ; preds = %0
%22 = load i32, i32* %5
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %56
24: ; preds = %21
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%29 = load i32, i32* %28
%30 = call i32 @close(i32 %29)
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%32 = load i32, i32* %31
%33 = call i32 @read(i32 %32, i32* %4, i32 4)
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%35 = load i32, i32* %34
%36 = call i32 @close(i32 %35)
store i32 1, i32* %6
br label %37
37: ; preds = %48, %24
%38 = load i32, i32* %6
%39 = load i32, i32* %4
%40 = icmp sle i32 %38, %39
br i1 %40, label %41, label %51
41: ; preds = %37
%42 = call i32 (...) @rand()
store i32 %42, i32* %7
%43 = load i32, i32* %7
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %43)
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%46 = load i32, i32* %45
%47 = call i32 @write(i32 %46, i32* %7, i32 4)
br label %48
48: ; preds = %41
%49 = load i32, i32* %6
%50 = add nsw i32 %49, 1
store i32 %50, i32* %6
br label %37
51: ; preds = %37
%52 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%53 = load i32, i32* %52
%54 = call i32 @close(i32 %53)
%55 = call i32 @exit(i32 0)
unreachable
56: ; preds = %21
%57 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%58 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%59 = load i32, i32* %58
%60 = call i32 @close(i32 %59)
%61 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%62 = load i32, i32* %61
%63 = call i32 @close(i32 %62)
%64 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%65 = load i32, i32* %64
%66 = call i32 @write(i32 %65, i32* %4, i32 4)
%67 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%68 = load i32, i32* %67
%69 = call i32 @close(i32 %68)
store i32 1, i32* %8
br label %70
70: ; preds = %80, %56
%71 = load i32, i32* %8
%72 = load i32, i32* %4
%73 = icmp sle i32 %71, %72
br i1 %73, label %74, label %83
74: ; preds = %70
%75 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%76 = load i32, i32* %75
%77 = call i32 @read(i32 %76, i32* %9, i32 4)
%78 = load i32, i32* %9
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %78)
br label %80
80: ; preds = %74
%81 = load i32, i32* %8
%82 = add nsw i32 %81, 1
store i32 %82, i32* %8
br label %70
83: ; preds = %70
%84 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%85 = load i32, i32* %84
%86 = call i32 @close(i32 %85)
%87 = call i32 @wait(i32 0)
store i32 0, i32* %1
br label %88
88: ; preds = %83, %19
%89 = load i32, i32* %1
ret i32 %89
}

LLVM IR (-O2):
@.str.1 = [9 x i8] c"Son: %d\0A\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [12 x i8] c"Father: %d\0A\00"
@str = [21 x i8] c"Error while fork-ing\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = tail call i32 @time(i32* null)
%7 = tail call i32 @srand(i32 %6)
%8 = bitcast [2 x i32]* %1 to i8*
%9 = bitcast [2 x i32]* %2 to i8*
%10 = bitcast i32* %3 to i8*
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%12 = call i32 @pipe(i32* %11)
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%14 = call i32 @pipe(i32* %13)
%15 = call i32 (...) @fork()
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %0
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %78
19: ; preds = %0
%20 = icmp eq i32 %15, 0
br i1 %20, label %21, label %49
21: ; preds = %19
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%23 = load i32, i32* %22
%24 = call i32 @close(i32 %23)
%25 = load i32, i32* %13
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* %11
%28 = call i32 @read(i32 %27, i32* %3, i32 4)
%29 = load i32, i32* %11
%30 = call i32 @close(i32 %29)
%31 = load i32, i32* %3
%32 = icmp slt i32 %31, 1
br i1 %32, label %36, label %33
33: ; preds = %21
%34 = bitcast i32* %4 to i8*
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
br label %41
36: ; preds = %41, %21
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%38 = load i32, i32* %37
%39 = call i32 @close(i32 %38)
%40 = call i32 @exit(i32 0)
unreachable
41: ; preds = %33, %41
%42 = phi i32 [ 1, %33 ], [ %47, %41 ]
%43 = call i32 (...) @rand()
store i32 %43, i32* %4
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %43)
%45 = load i32, i32* %35
%46 = call i32 @write(i32 %45, i32* %4, i32 4)
%47 = add nuw nsw i32 %42, 1
%48 = icmp slt i32 %42, %31
br i1 %48, label %41, label %36
49: ; preds = %19
%50 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%51 = load i32, i32* %11
%52 = call i32 @close(i32 %51)
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%54 = load i32, i32* %53
%55 = call i32 @close(i32 %54)
%56 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%57 = load i32, i32* %56
%58 = call i32 @write(i32 %57, i32* %3, i32 4)
%59 = load i32, i32* %56
%60 = call i32 @close(i32 %59)
%61 = load i32, i32* %3
%62 = icmp slt i32 %61, 1
br i1 %62, label %65, label %63
63: ; preds = %49
%64 = bitcast i32* %5 to i8*
br label %69
65: ; preds = %69, %49
%66 = load i32, i32* %13
%67 = call i32 @close(i32 %66)
%68 = call i32 @wait(i32 0)
br label %78
69: ; preds = %63, %69
%70 = phi i32 [ 1, %63 ], [ %75, %69 ]
%71 = load i32, i32* %13
%72 = call i32 @read(i32 %71, i32* %5, i32 4)
%73 = load i32, i32* %5
%74 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %73)
%75 = add nuw nsw i32 %70, 1
%76 = load i32, i32* %3
%77 = icmp slt i32 %70, %76
br i1 %77, label %69, label %65
78: ; preds = %65, %17
ret i32 0
}