#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);
int fork();
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, int *, int);
int /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ wait(int /*<<< orphan*/);
int /*<<< orphan*/ write(int, int *, int);
int main() {
  int n;
  int c2p[2], p2c[2];
  pipe(p2c);
  pipe(c2p);
  int res = fork();
  if (res < 0) {
    printf("Error fork()-ing\n");
    return 0;
  }
  if (res == 0) {
    close(p2c[1]);
    close(c2p[0]);
    read(p2c[0], &n, sizeof(int));
    close(p2c[0]);
    int packet = 1 - n % 2;
    write(c2p[1], &packet, sizeof(int));
    close(c2p[0]);
    exit(1);
  }
  scanf("%d", &n);
  close(p2c[0]);
  close(c2p[1]);
  write(p2c[1], &n, sizeof(int));
  close(p2c[1]);
  int response;
  read(c2p[0], &response, sizeof(int));
  close(c2p[0]);
  printf("Father received %d from child\n", response);
  wait(0);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x18(%rbp),%rdi
call   1060 <pipe@plt>
lea    -0x10(%rbp),%rdi
call   1060 <pipe@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11df <main+0x4f>
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12bb <main+0x12b>
cmpl   $0x0,-0x1c(%rbp)
jne    124a <main+0xba>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1070 <read@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    $0x1,%eax
sub    %edx,%eax
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xdc1(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1080 <scanf@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
call   1070 <read@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%esi
lea    0xd6f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   10a0 <wait@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
lea    0xc(%rsp),%rdi
call   1070 <pipe@plt>
lea    0x14(%rsp),%rdi
call   1070 <pipe@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
js     1241 <main+0xa1>
je     1255 <main+0xb5>
lea    0xe2f(%rip),%rdi
lea    0x1c(%rsp),%rbx
mov    %rbx,%rsi
call   1090 <scanf@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    0x18(%rsp),%edi
call   1060 <close@plt>
mov    0x10(%rsp),%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1040 <write@plt>
mov    0x10(%rsp),%edi
call   1060 <close@plt>
mov    0x14(%rsp),%edi
lea    0x8(%rsp),%rsi
mov    $0x4,%edx
call   1080 <read@plt>
mov    0x14(%rsp),%edi
call   1060 <close@plt>
mov    0x8(%rsp),%esi
lea    0xdd2(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   10b0 <wait@plt>
jmp    124d <main+0xad>
lea    0xdda(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret
mov    0x10(%rsp),%edi
call   1060 <close@plt>
mov    0x14(%rsp),%edi
call   1060 <close@plt>
mov    0xc(%rsp),%edi
lea    0x1c(%rsp),%rsi
mov    $0x4,%edx
call   1080 <read@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    0x1c(%rsp),%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
and    $0xfffffffe,%ecx
sub    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,0x8(%rsp)
mov    0x18(%rsp),%edi
lea    0x8(%rsp),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
mov    0x14(%rsp),%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"Error fork()-ing\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [31 x i8] c"Father received %d from child\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [2 x i32]
%4 = alloca [2 x i32]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%9 = call i32 @pipe(i32* %8)
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%11 = call i32 @pipe(i32* %10)
%12 = call i32 (...) @fork()
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %66
17: ; preds = %0
%18 = load i32, i32* %5
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %43
20: ; preds = %17
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%22 = load i32, i32* %21
%23 = call i32 @close(i32 %22)
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%25 = load i32, i32* %24
%26 = call i32 @close(i32 %25)
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%28 = load i32, i32* %27
%29 = call i32 @read(i32 %28, i32* %2, i32 4)
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%31 = load i32, i32* %30
%32 = call i32 @close(i32 %31)
%33 = load i32, i32* %2
%34 = srem i32 %33, 2
%35 = sub nsw i32 1, %34
store i32 %35, i32* %6
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%37 = load i32, i32* %36
%38 = call i32 @write(i32 %37, i32* %6, i32 4)
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%40 = load i32, i32* %39
%41 = call i32 @close(i32 %40)
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %17
%44 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%46 = load i32, i32* %45
%47 = call i32 @close(i32 %46)
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%49 = load i32, i32* %48
%50 = call i32 @close(i32 %49)
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%52 = load i32, i32* %51
%53 = call i32 @write(i32 %52, i32* %2, i32 4)
%54 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%55 = load i32, i32* %54
%56 = call i32 @close(i32 %55)
%57 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%58 = load i32, i32* %57
%59 = call i32 @read(i32 %58, i32* %7, i32 4)
%60 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%61 = load i32, i32* %60
%62 = call i32 @close(i32 %61)
%63 = load i32, i32* %7
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %63)
%65 = call i32 @wait(i32 0)
store i32 0, i32* %1
br label %66
66: ; preds = %43, %15
%67 = load i32, i32* %1
ret i32 %67
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [31 x i8] c"Father received %d from child\0A\00"
@str = [17 x i8] c"Error fork()-ing\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %1 to i8*
%7 = bitcast [2 x i32]* %2 to i8*
%8 = bitcast [2 x i32]* %3 to i8*
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%10 = call i32 @pipe(i32* %9)
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%12 = call i32 @pipe(i32* %11)
%13 = call i32 (...) @fork()
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %59
17: ; preds = %0
%18 = icmp eq i32 %13, 0
br i1 %18, label %19, label %39
19: ; preds = %17
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%21 = load i32, i32* %20
%22 = call i32 @close(i32 %21)
%23 = load i32, i32* %11
%24 = call i32 @close(i32 %23)
%25 = load i32, i32* %9
%26 = call i32 @read(i32 %25, i32* %1, i32 4)
%27 = load i32, i32* %9
%28 = call i32 @close(i32 %27)
%29 = bitcast i32* %4 to i8*
%30 = load i32, i32* %1
%31 = srem i32 %30, 2
%32 = sub nsw i32 1, %31
store i32 %32, i32* %4
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%34 = load i32, i32* %33
%35 = call i32 @write(i32 %34, i32* %4, i32 4)
%36 = load i32, i32* %11
%37 = call i32 @close(i32 %36)
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %17
%40 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%41 = load i32, i32* %9
%42 = call i32 @close(i32 %41)
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%44 = load i32, i32* %43
%45 = call i32 @close(i32 %44)
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%47 = load i32, i32* %46
%48 = call i32 @write(i32 %47, i32* %1, i32 4)
%49 = load i32, i32* %46
%50 = call i32 @close(i32 %49)
%51 = bitcast i32* %5 to i8*
%52 = load i32, i32* %11
%53 = call i32 @read(i32 %52, i32* %5, i32 4)
%54 = load i32, i32* %11
%55 = call i32 @close(i32 %54)
%56 = load i32, i32* %5
%57 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %56)
%58 = call i32 @wait(i32 0)
br label %59
59: ; preds = %39, %15
ret i32 0
}