#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;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
int getpid();
char *malloc(int);
scalar_t__ pipe(int *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int pipe_father[2], pipe_child[2], len, max_iter, i;
  char *c;
  pid_t pid;
  if (argc < 2) {
    fprintf(stdout, "Usage: %s <MAX_ITERATIONS>\n", argv[0]);
    exit(1);
  }
  max_iter = atoi(argv[1]);
  c = malloc(sizeof(char) * 2);
  if (pipe(pipe_father) || pipe(pipe_child)) {
    fprintf(stderr, "Error while creating one of the pipes\n");
    exit(-1);
  }
  pid = fork();
  if (pid) {
    close(pipe_father[1]);
    close(pipe_child[0]);
  } else {
    close(pipe_child[1]);
    close(pipe_father[0]);
  }
  for (i = 0;; i++) {
    if (pid) {
      if (i != max_iter) {
        fprintf(stdout, "I am the father: %d\n", getpid());
        write(pipe_child[1], "?", sizeof(char) * 2);
        read(pipe_father[0], c, sizeof(char) * 2);
      } else {
        fprintf(stdout, "Father: STOP\n");
        write(pipe_child[1], "!", sizeof(char) * 2);
        break;
      }
    } else {
      read(pipe_child[0], c, sizeof(char) * 2);
      if (strcmp(c, "?") == 0) {
        fprintf(stdout, "I am the child: %d\n", getpid());
        write(pipe_father[1], " ", sizeof(char) * 2);
      } else {
        fprintf(stdout, "Child: OK\n");
        break;
      }
    }
  }
  free(c);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1208 <main+0x48>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe09(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x28(%rbp)
mov    $0x2,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x38(%rbp)
lea    -0x18(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0x0,%rax
jne    124c <main+0x8c>
lea    -0x20(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0x0,%rax
je     126d <main+0xad>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
lea    0xdc0(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1298 <main+0xd8>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
jmp    12a8 <main+0xe8>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x2c(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     134b <main+0x18b>
mov    -0x2c(%rbp),%eax
cmp    -0x28(%rbp),%eax
je     1316 <main+0x156>
mov    0x2d0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0x44(%rbp),%edi
mov    %eax,%edx
lea    0xd5e(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    -0x1c(%rbp),%edi
lea    0xd62(%rip),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
mov    -0x18(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    $0x2,%edx
call   1080 <read@plt>
jmp    1346 <main+0x186>
mov    0x2cbb(%rip),%rax
mov    (%rax),%edi
lea    0xd34(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    -0x1c(%rbp),%edi
lea    0xd31(%rip),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
jmp    13e9 <main+0x229>
jmp    13d6 <main+0x216>
mov    -0x20(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    $0x2,%edx
call   1080 <read@plt>
mov    -0x38(%rbp),%rdi
lea    0xcf1(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    13b5 <main+0x1f5>
mov    0x2c5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0x48(%rbp),%edi
mov    %eax,%edx
lea    0xcd5(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    -0x14(%rbp),%edi
lea    0xcd8(%rip),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
jmp    13d1 <main+0x211>
mov    0x2c1c(%rip),%rax
mov    (%rax),%edi
lea    0xcbb(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
jmp    13e9 <main+0x229>
jmp    13d6 <main+0x216>
jmp    13db <main+0x21b>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    12af <main+0xef>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x50,%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    $0x1,%edi
jle    1389 <main+0x1c9>
mov    0x8(%rsi),%rdi
call   10c0 <atoi@plt>
mov    %eax,%r15d
mov    $0x2,%edi
call   10b0 <malloc@plt>
mov    %rax,%r14
lea    0x10(%rsp),%rdi
call   1070 <pipe@plt>
test   %rax,%rax
jne    1368 <main+0x1a8>
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
test   %rax,%rax
jne    1368 <main+0x1a8>
xor    %eax,%eax
call   10e0 <fork@plt>
mov    %rax,%r12
test   %rax,%rax
je     1234 <main+0x74>
mov    0x14(%rsp),%edi
call   1060 <close@plt>
mov    0x8(%rsp),%edi
jmp    1241 <main+0x81>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    0x10(%rsp),%edi
call   1060 <close@plt>
lea    0xe0b(%rip),%rbx
nopl   (%rax)
test   %r12,%r12
je     12b0 <main+0xf0>
mov    0x2d7c(%rip),%rax
mov    (%rax),%r13d
test   %r15d,%r15d
je     1317 <main+0x157>
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %r13d,%edi
lea    0xdca(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0x2,%edx
call   1040 <write@plt>
mov    0x10(%rsp),%edi
mov    %r14,%rsi
mov    $0x2,%edx
call   1080 <read@plt>
add    $0xffffffff,%r15d
jmp    1250 <main+0x90>
nopw   0x0(%rax,%rax,1)
mov    0x8(%rsp),%edi
mov    %r14,%rsi
mov    $0x2,%edx
call   1080 <read@plt>
mov    %r14,%rdi
mov    %rbx,%rbp
mov    %rbx,%rsi
call   1090 <strcmp@plt>
mov    0x2d02(%rip),%rcx
mov    (%rcx),%ebx
test   %rax,%rax
jne    133f <main+0x17f>
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %ebx,%edi
lea    0xd7d(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    0x14(%rsp),%edi
lea    0xd7d(%rip),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
add    $0xffffffff,%r15d
mov    %rbp,%rbx
jmp    1250 <main+0x90>
lea    0xd3c(%rip),%rsi
mov    %r13d,%edi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    0xc(%rsp),%edi
lea    0xd35(%rip),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
jmp    134f <main+0x18f>
lea    0xd3a(%rip),%rsi
mov    %ebx,%edi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c71(%rip),%rax
mov    (%rax),%edi
lea    0xca4(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    0x2c48(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xc64(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@stdout = global i32 0
@.str = [28 x i8] c"Usage: %s <MAX_ITERATIONS>\0A\00"
@stderr = global i32 0
@.str.1 = [39 x i8] c"Error while creating one of the pipes\0A\00"
@.str.2 = [21 x i8] c"I am the father: %d\0A\00"
@.str.3 = [2 x i8] c"?\00"
@.str.4 = [14 x i8] c"Father: STOP\0A\00"
@.str.5 = [2 x i8] c"!\00"
@.str.6 = [20 x i8] c"I am the child: %d\0A\00"
@.str.7 = [2 x i8] c" \00"
@.str.8 = [11 x i8] c"Child: OK\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i32]
%7 = alloca [2 x i32]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 2
br i1 %14, label %15, label %22
15: ; preds = %2
%16 = load i32, i32* @stdout
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32 @atoi(i8* %25)
store i32 %26, i32* %9
%27 = call i8* @malloc(i32 2)
store i8* %27, i8** %11
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%29 = call i64 @pipe(i32* %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %35, label %31
31: ; preds = %22
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%33 = call i64 @pipe(i32* %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %31, %22
%36 = load i32, i32* @stderr
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%38 = call i32 @exit(i32 -1)
unreachable
39: ; preds = %31
%40 = call i64 (...) @fork()
store i64 %40, i64* %12
%41 = load i64, i64* %12
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %50
43: ; preds = %39
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%45 = load i32, i32* %44
%46 = call i32 @close(i32 %45)
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%48 = load i32, i32* %47
%49 = call i32 @close(i32 %48)
br label %57
50: ; preds = %39
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%52 = load i32, i32* %51
%53 = call i32 @close(i32 %52)
%54 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%55 = load i32, i32* %54
%56 = call i32 @close(i32 %55)
br label %57
57: ; preds = %50, %43
store i32 0, i32* %10
br label %58
58: ; preds = %103, %57
%59 = load i64, i64* %12
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %83
61: ; preds = %58
%62 = load i32, i32* %10
%63 = load i32, i32* %9
%64 = icmp ne i32 %62, %63
br i1 %64, label %65, label %76
65: ; preds = %61
%66 = load i32, i32* @stdout
%67 = call i32 (...) @getpid()
%68 = call i32 (i32, i8*, ...) @fprintf(i32 %66, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %67)
%69 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%70 = load i32, i32* %69
%71 = call i32 @write(i32 %70, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 2)
%72 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%73 = load i32, i32* %72
%74 = load i8*, i8** %11
%75 = call i32 @read(i32 %73, i8* %74, i32 2)
br label %82
76: ; preds = %61
%77 = load i32, i32* @stdout
%78 = call i32 (i32, i8*, ...) @fprintf(i32 %77, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%79 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%80 = load i32, i32* %79
%81 = call i32 @write(i32 %80, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 2)
br label %106
82: ; preds = %65
br label %102
83: ; preds = %58
%84 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%85 = load i32, i32* %84
%86 = load i8*, i8** %11
%87 = call i32 @read(i32 %85, i8* %86, i32 2)
%88 = load i8*, i8** %11
%89 = call i64 @strcmp(i8* %88, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%90 = icmp eq i64 %89, 0
br i1 %90, label %91, label %98
91: ; preds = %83
%92 = load i32, i32* @stdout
%93 = call i32 (...) @getpid()
%94 = call i32 (i32, i8*, ...) @fprintf(i32 %92, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i32 %93)
%95 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%96 = load i32, i32* %95
%97 = call i32 @write(i32 %96, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0), i32 2)
br label %101
98: ; preds = %83
%99 = load i32, i32* @stdout
%100 = call i32 (i32, i8*, ...) @fprintf(i32 %99, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0))
br label %106
101: ; preds = %91
br label %102
102: ; preds = %101, %82
br label %103
103: ; preds = %102
%104 = load i32, i32* %10
%105 = add nsw i32 %104, 1
store i32 %105, i32* %10
br label %58
106: ; preds = %98, %76
%107 = load i8*, i8** %11
%108 = call i32 @free(i8* %107)
ret i32 0
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [28 x i8] c"Usage: %s <MAX_ITERATIONS>\0A\00"
@stderr = global i32 0
@.str.1 = [39 x i8] c"Error while creating one of the pipes\0A\00"
@.str.2 = [21 x i8] c"I am the father: %d\0A\00"
@.str.3 = [2 x i8] c"?\00"
@.str.4 = [14 x i8] c"Father: STOP\0A\00"
@.str.5 = [2 x i8] c"!\00"
@.str.6 = [20 x i8] c"I am the child: %d\0A\00"
@.str.7 = [2 x i8] c" \00"
@.str.8 = [11 x i8] c"Child: OK\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x i32]
%4 = alloca [2 x i32]
%5 = bitcast [2 x i32]* %3 to i8*
%6 = bitcast [2 x i32]* %4 to i8*
%7 = icmp slt i32 %0, 2
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = load i32, i32* @stdout
%10 = load i8*, i8** %1
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i32 @atoi(i8* %15)
%17 = tail call dereferenceable_or_null(2) i8* @malloc(i32 2)
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%19 = call i64 @pipe(i32* %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %13
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%23 = call i64 @pipe(i32* %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %29, label %25
25: ; preds = %21, %13
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %21
%30 = call i64 (...) @fork()
%31 = icmp eq i64 %30, 0
br i1 %31, label %37, label %32
32: ; preds = %29
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%34 = load i32, i32* %33
%35 = call i32 @close(i32 %34)
%36 = load i32, i32* %22
br label %42
37: ; preds = %29
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%39 = load i32, i32* %38
%40 = call i32 @close(i32 %39)
%41 = load i32, i32* %18
br label %42
42: ; preds = %37, %32
%43 = phi i32 [ %41, %37 ], [ %36, %32 ]
%44 = call i32 @close(i32 %43)
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
br label %47
47: ; preds = %76, %42
%48 = phi i32 [ 0, %42 ], [ %77, %76 ]
br i1 %31, label %63, label %49
49: ; preds = %47
%50 = icmp eq i32 %48, %16
%51 = load i32, i32* @stdout
br i1 %50, label %59, label %52
52: ; preds = %49
%53 = call i32 (...) @getpid()
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %53)
%55 = load i32, i32* %45
%56 = call i32 @write(i32 %55, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 2)
%57 = load i32, i32* %18
%58 = call i32 @read(i32 %57, i8* %17, i32 2)
br label %76
59: ; preds = %49
%60 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%61 = load i32, i32* %45
%62 = call i32 @write(i32 %61, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 2)
br label %78
63: ; preds = %47
%64 = load i32, i32* %22
%65 = call i32 @read(i32 %64, i8* %17, i32 2)
%66 = call i64 @strcmp(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%67 = icmp eq i64 %66, 0
%68 = load i32, i32* @stdout
br i1 %67, label %69, label %74
69: ; preds = %63
%70 = call i32 (...) @getpid()
%71 = call i32 (i32, i8*, ...) @fprintf(i32 %68, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i32 %70)
%72 = load i32, i32* %46
%73 = call i32 @write(i32 %72, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0), i32 2)
br label %76
74: ; preds = %63
%75 = call i32 (i32, i8*, ...) @fprintf(i32 %68, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0))
br label %78
76: ; preds = %52, %69
%77 = add nuw nsw i32 %48, 1
br label %47
78: ; preds = %74, %59
%79 = call i32 @free(i8* %17)
ret i32 0
}