#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 /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  int p_h[2];
  pipe(p_h);
  int h_p[2];
  pipe(h_p);
  int pid = fork();
  if (pid == -1) {
    perror("fork");
    exit(-1);
  } else if (pid == 0) {
    char msg[128];
    close(p_h[1]);
    close(h_p[0]);
    int i = 0;
    while (1) {
      read(p_h[0], msg, 128);
      printf("Recibido por el hijo: %s", msg);
      sleep(1);
      if (i == 10) {
        write(h_p[1], "q", 1);
        break;
      } else
        write(h_p[1], "l", 1);
      i++;
    }
  } else {
    char msg[128];
    close(p_h[0]);
    close(h_p[1]);
    while (1) {
      fgets(msg, 128, stdin);
      write(p_h[1], msg, 128);
      read(h_p[0], msg, 1);
      if (msg[0] == 'q')
        break;
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
call   1060 <pipe@plt>
lea    -0x20(%rbp),%rdi
call   1060 <pipe@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11f5 <main+0x55>
lea    0xe1a(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x24(%rbp)
jne    129d <main+0xfd>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0xb4(%rbp)
mov    -0x18(%rbp),%edi
lea    -0xb0(%rbp),%rsi
mov    $0x80,%edx
call   1070 <read@plt>
lea    -0xb0(%rbp),%rsi
lea    0xdca(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
cmpl   $0xa,-0xb4(%rbp)
jne    1270 <main+0xd0>
mov    -0x1c(%rbp),%edi
lea    0xdbd(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    1298 <main+0xf8>
mov    -0x1c(%rbp),%edi
lea    0xda6(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0xb4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xb4(%rbp)
jmp    1219 <main+0x79>
jmp    130e <main+0x16e>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
lea    -0x140(%rbp),%rdi
mov    0x2d2d(%rip),%rax
mov    (%rax),%edx
mov    $0x80,%esi
call   1080 <fgets@plt>
mov    -0x14(%rbp),%edi
lea    -0x140(%rbp),%rsi
mov    $0x80,%edx
call   1030 <write@plt>
mov    -0x20(%rbp),%edi
lea    -0x140(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
movsbl -0x140(%rbp),%eax
cmp    $0x71,%eax
jne    1304 <main+0x164>
jmp    1309 <main+0x169>
jmp    12ad <main+0x10d>
jmp    130e <main+0x16e>
jmp    1313 <main+0x173>
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x98,%rsp
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
mov    %rsp,%rdi
call   1060 <pipe@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     123d <main+0x9d>
cmp    $0xffffffff,%eax
je     12f1 <main+0x151>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0x4(%rsp),%edi
call   1050 <close@plt>
mov    0x2dfc(%rip),%rbp
lea    0x10(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x0(%rbp),%edx
mov    %rbx,%rdi
mov    $0x80,%esi
call   1080 <fgets@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0x80,%edx
call   1030 <write@plt>
mov    (%rsp),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmpb   $0x71,0x10(%rsp)
jne    1200 <main+0x60>
jmp    12e1 <main+0x141>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    (%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x80,%edx
call   1070 <read@plt>
lea    0xd9a(%rip),%r14
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    $0xa,%ebp
lea    0xd94(%rip),%r15
nopl   0x0(%rax)
mov    0x4(%rsp),%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x80,%edx
call   1070 <read@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
add    $0xffffffff,%ebp
jne    1290 <main+0xf0>
mov    0x4(%rsp),%edi
lea    0xd47(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
xor    %eax,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd08(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@.str.1 = [25 x i8] c"Recibido por el hijo: %s\00"
@.str.2 = [2 x i8] c"q\00"
@.str.3 = [2 x i8] c"l\00"
@stdin = global i32 0
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 [128 x i8]
%10 = alloca i32
%11 = alloca [128 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%15 = call i32 @pipe(i32* %14)
%16 = call i32 (...) @fork()
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %2
%23 = load i32, i32* %8
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %54
25: ; preds = %22
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%30 = load i32, i32* %29
%31 = call i32 @close(i32 %30)
store i32 0, i32* %10
br label %32
32: ; preds = %25, %50
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%34 = load i32, i32* %33
%35 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%36 = call i32 @read(i32 %34, i8* %35, i32 128)
%37 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%38 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %37)
%39 = call i32 @sleep(i32 1)
%40 = load i32, i32* %10
%41 = icmp eq i32 %40, 10
br i1 %41, label %42, label %46
42: ; preds = %32
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%44 = load i32, i32* %43
%45 = call i32 @write(i32 %44, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
br label %53
46: ; preds = %32
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%48 = load i32, i32* %47
%49 = call i32 @write(i32 %48, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
br label %50
50: ; preds = %46
%51 = load i32, i32* %10
%52 = add nsw i32 %51, 1
store i32 %52, i32* %10
br label %32
53: ; preds = %42
br label %80
54: ; preds = %22
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%56 = load i32, i32* %55
%57 = call i32 @close(i32 %56)
%58 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%59 = load i32, i32* %58
%60 = call i32 @close(i32 %59)
br label %61
61: ; preds = %54, %78
%62 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%63 = load i32, i32* @stdin
%64 = call i32 @fgets(i8* %62, i32 128, i32 %63)
%65 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%66 = load i32, i32* %65
%67 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%68 = call i32 @write(i32 %66, i8* %67, i32 128)
%69 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%70 = load i32, i32* %69
%71 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%72 = call i32 @read(i32 %70, i8* %71, i32 1)
%73 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%74 = load i8, i8* %73
%75 = sext i8 %74 to i32
%76 = icmp eq i32 %75, 113
br i1 %76, label %77, label %78
77: ; preds = %61
br label %79
78: ; preds = %61
br label %61
79: ; preds = %77
br label %80
80: ; preds = %79, %53
br label %81
81: ; preds = %80
%82 = load i32, i32* %3
ret i32 %82
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@.str.1 = [25 x i8] c"Recibido por el hijo: %s\00"
@.str.2 = [2 x i8] c"q\00"
@.str.3 = [2 x i8] c"l\00"
@stdin = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = alloca [2 x i32]
%5 = alloca [128 x i8]
%6 = alloca [128 x i8]
%7 = bitcast [2 x i32]* %3 to i8*
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%9 = call i32 @pipe(i32* %8)
%10 = bitcast [2 x i32]* %4 to i8*
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%12 = call i32 @pipe(i32* %11)
%13 = call i32 (...) @fork()
switch i32 %13, label %42 [
i32 -1, label %14
i32 0, label %17
]
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %2
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%20 = load i32, i32* %19
%21 = call i32 @close(i32 %20)
%22 = load i32, i32* %11
%23 = call i32 @close(i32 %22)
%24 = load i32, i32* %8
%25 = call i32 @read(i32 %24, i8* %18, i32 128)
%26 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%27 = call i32 @sleep(i32 1)
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
br label %32
29: ; preds = %32
%30 = load i32, i32* %28
%31 = call i32 @write(i32 %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
br label %60
32: ; preds = %17, %32
%33 = phi i32 [ 0, %17 ], [ %36, %32 ]
%34 = load i32, i32* %28
%35 = call i32 @write(i32 %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%36 = add nuw nsw i32 %33, 1
%37 = load i32, i32* %8
%38 = call i32 @read(i32 %37, i8* %18, i32 128)
%39 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%40 = call i32 @sleep(i32 1)
%41 = icmp eq i32 %36, 10
br i1 %41, label %29, label %32
42: ; preds = %2
%43 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%44 = load i32, i32* %8
%45 = call i32 @close(i32 %44)
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%47 = load i32, i32* %46
%48 = call i32 @close(i32 %47)
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
br label %50
50: ; preds = %50, %42
%51 = load i32, i32* @stdin
%52 = call i32 @fgets(i8* %43, i32 128, i32 %51)
%53 = load i32, i32* %49
%54 = call i32 @write(i32 %53, i8* %43, i32 128)
%55 = load i32, i32* %11
%56 = call i32 @read(i32 %55, i8* %43, i32 1)
%57 = load i8, i8* %43
%58 = icmp eq i8 %57, 113
br i1 %58, label %59, label %50
59: ; preds = %50
br label %60
60: ; preds = %29, %59
ret i32 0
}