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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ scanf(char *, char *);
int /*<<< orphan*/ sleep(int);
int strlen(char *);
int /*<<< orphan*/ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  int tuberia1[2], tuberia2[2], readb;
  pid_t pid;
  int num = 0;
  pipe(tuberia1);
  pipe(tuberia2);
  pid = fork();
  if (pid == -1)
    perror("Error de pid.\n");
  else if (pid == 0) {
    char buffer[256];
    char mensaje[1] = {'l'};
    close(tuberia1[1]);
    close(tuberia2[0]);
    int i;
    for (i = 0; i < 10; i++) {
      readb = read(tuberia1[0], buffer, 256);
      buffer[readb] = '\0';
      printf("[Hijo], mensaje leído: %s\n", buffer);
      sleep(1);
      if (i == 9)
        mensaje[0] = 'q';
      write(tuberia2[1], mensaje, 1);
    }
    close(tuberia2[1]);
    close(tuberia1[0]);
    exit(0);
  } else {
    close(tuberia1[0]);
    close(tuberia2[1]);
    char buffer_p[256];
    char mensaje_p[1] = {'l'};
    while (mensaje_p[0] != 'q') {
      printf("[Padre], nuevo mensaje: ");
      scanf("%s", buffer_p);
      write(tuberia1[1], buffer_p, strlen(buffer_p));
      while (mensaje_p[0] != 'l' && mensaje_p[0] != 'q') {
        read(tuberia2[0], mensaje_p, 1);
      }
    }
    close(tuberia1[1]);
    close(tuberia2[0]);
  }
  waitpid(pid, NULL, 0);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x260,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x2c(%rbp)
lea    -0x18(%rbp),%rdi
call   1070 <pipe@plt>
lea    -0x20(%rbp),%rdi
call   1070 <pipe@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1217 <main+0x57>
lea    0xdf3(%rip),%rdi
call   10b0 <perror@plt>
jmp    13de <main+0x21e>
cmpl   $0x0,-0x28(%rbp)
jne    12e9 <main+0x129>
mov    0xe20(%rip),%al
mov    %al,-0x131(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x138(%rbp)
cmpl   $0xa,-0x138(%rbp)
jge    12d2 <main+0x112>
mov    -0x18(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0x100,%edx
call   1080 <read@plt>
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rax
movb   $0x0,-0x130(%rbp,%rax,1)
lea    -0x130(%rbp),%rsi
lea    0xd8a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
cmpl   $0x9,-0x138(%rbp)
jne    12aa <main+0xea>
movb   $0x71,-0x131(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x131(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x138(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x138(%rbp)
jmp    1247 <main+0x87>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    0xd49(%rip),%al
mov    %al,-0x241(%rbp)
movsbl -0x241(%rbp),%eax
cmp    $0x71,%eax
je     13c9 <main+0x209>
lea    0xd0f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x240(%rbp),%rsi
lea    0xd13(%rip),%rdi
call   1090 <scanf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x254(%rbp)
lea    -0x240(%rbp),%rax
mov    %rax,-0x250(%rbp)
lea    -0x240(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x254(%rbp),%edi
mov    -0x250(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
movsbl -0x241(%rbp),%ecx
xor    %eax,%eax
cmp    $0x6c,%ecx
mov    %al,-0x255(%rbp)
je     1398 <main+0x1d8>
movsbl -0x241(%rbp),%eax
cmp    $0x71,%eax
setne  %al
mov    %al,-0x255(%rbp)
mov    -0x255(%rbp),%al
test   $0x1,%al
jne    13ab <main+0x1eb>
jmp    13c4 <main+0x204>
mov    -0x20(%rbp),%edi
lea    -0x241(%rbp),%rsi
mov    $0x1,%edx
call   1080 <read@plt>
jmp    136d <main+0x1ad>
jmp    1305 <main+0x145>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
jmp    13de <main+0x21e>
mov    -0x28(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10a0 <waitpid@plt>
xor    %eax,%eax
add    $0x260,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x128,%rsp
lea    0x10(%rsp),%rdi
call   1070 <pipe@plt>
lea    0x18(%rsp),%rdi
call   1070 <pipe@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     124f <main+0x8f>
test   %ebx,%ebx
je     1276 <main+0xb6>
mov    0x10(%rsp),%edi
call   1060 <close@plt>
mov    0x1c(%rsp),%edi
call   1060 <close@plt>
lea    0xe1b(%rip),%r14
lea    0xe2d(%rip),%r15
lea    0x20(%rsp),%rbx
nopl   0x0(%rax)
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1090 <scanf@plt>
mov    0x14(%rsp),%ebp
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    1220 <main+0x60>
lea    0xdaa(%rip),%rdi
call   10b0 <perror@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <waitpid@plt>
xor    %eax,%eax
add    $0x128,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
movb   $0x6c,0xf(%rsp)
mov    0x14(%rsp),%edi
call   1060 <close@plt>
mov    0x18(%rsp),%edi
call   1060 <close@plt>
mov    $0xa,%ebp
lea    0x20(%rsp),%rbx
lea    0xd71(%rip),%r14
lea    0xf(%rsp),%r15
jmp    12c6 <main+0x106>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x1c(%rsp),%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
add    $0xffffffff,%ebp
je     1301 <main+0x141>
mov    0x10(%rsp),%edi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1080 <read@plt>
cltq
movb   $0x0,0x20(%rsp,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
cmp    $0x1,%ebp
jne    12b0 <main+0xf0>
movb   $0x71,0xf(%rsp)
jmp    12b0 <main+0xf0>
mov    0x1c(%rsp),%edi
call   1060 <close@plt>
mov    0x10(%rsp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"Error de pid.\0A\00"
@__const.main.mensaje = [1 x i8] c"l"
@.str.1 = [28 x i8] c"[Hijo], mensaje le\C3\ADdo: %s\0A\00"
@__const.main.mensaje_p = [1 x i8] c"l"
@.str.2 = [25 x i8] c"[Padre], nuevo mensaje: \00"
@.str.3 = [3 x i8] c"%s\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 [256 x i8]
%12 = alloca [1 x i8]
%13 = alloca i32
%14 = alloca [256 x i8]
%15 = alloca [1 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %10
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%17 = call i32 @pipe(i32* %16)
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%19 = call i32 @pipe(i32* %18)
%20 = call i32 (...) @fork()
store i32 %20, i32* %9
%21 = load i32, i32* %9
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %2
%24 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %119
25: ; preds = %2
%26 = load i32, i32* %9
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %70
28: ; preds = %25
%29 = bitcast [1 x i8]* %12 to i8*
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%31 = load i32, i32* %30
%32 = call i32 @close(i32 %31)
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%34 = load i32, i32* %33
%35 = call i32 @close(i32 %34)
store i32 0, i32* %13
br label %36
36: ; preds = %59, %28
%37 = load i32, i32* %13
%38 = icmp slt i32 %37, 10
br i1 %38, label %39, label %62
39: ; preds = %36
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%41 = load i32, i32* %40
%42 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%43 = call i32 @read(i32 %41, i8* %42, i32 256)
store i32 %43, i32* %8
%44 = load i32, i32* %8
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 %45
store i8 0, i8* %46
%47 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %47)
%49 = call i32 @sleep(i32 1)
%50 = load i32, i32* %13
%51 = icmp eq i32 %50, 9
br i1 %51, label %52, label %54
52: ; preds = %39
%53 = getelementptr inbounds [1 x i8], [1 x i8]* %12, i64 0, i64 0
store i8 113, i8* %53
br label %54
54: ; preds = %52, %39
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%56 = load i32, i32* %55
%57 = getelementptr inbounds [1 x i8], [1 x i8]* %12, i64 0, i64 0
%58 = call i32 @write(i32 %56, i8* %57, i32 1)
br label %59
59: ; preds = %54
%60 = load i32, i32* %13
%61 = add nsw i32 %60, 1
store i32 %61, i32* %13
br label %36
62: ; preds = %36
%63 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%64 = load i32, i32* %63
%65 = call i32 @close(i32 %64)
%66 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%67 = load i32, i32* %66
%68 = call i32 @close(i32 %67)
%69 = call i32 @exit(i32 0)
unreachable
70: ; preds = %25
%71 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%72 = load i32, i32* %71
%73 = call i32 @close(i32 %72)
%74 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%75 = load i32, i32* %74
%76 = call i32 @close(i32 %75)
%77 = bitcast [1 x i8]* %15 to i8*
br label %78
78: ; preds = %110, %70
%79 = getelementptr inbounds [1 x i8], [1 x i8]* %15, i64 0, i64 0
%80 = load i8, i8* %79
%81 = sext i8 %80 to i32
%82 = icmp ne i32 %81, 113
br i1 %82, label %83, label %111
83: ; preds = %78
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%85 = getelementptr inbounds [256 x i8], [256 x i8]* %14, i64 0, i64 0
%86 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %85)
%87 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%88 = load i32, i32* %87
%89 = getelementptr inbounds [256 x i8], [256 x i8]* %14, i64 0, i64 0
%90 = getelementptr inbounds [256 x i8], [256 x i8]* %14, i64 0, i64 0
%91 = call i32 @strlen(i8* %90)
%92 = call i32 @write(i32 %88, i8* %89, i32 %91)
br label %93
93: ; preds = %105, %83
%94 = getelementptr inbounds [1 x i8], [1 x i8]* %15, i64 0, i64 0
%95 = load i8, i8* %94
%96 = sext i8 %95 to i32
%97 = icmp ne i32 %96, 108
br i1 %97, label %98, label %103
98: ; preds = %93
%99 = getelementptr inbounds [1 x i8], [1 x i8]* %15, i64 0, i64 0
%100 = load i8, i8* %99
%101 = sext i8 %100 to i32
%102 = icmp ne i32 %101, 113
br label %103
103: ; preds = %98, %93
%104 = phi i1 [ false, %93 ], [ %102, %98 ]
br i1 %104, label %105, label %110
105: ; preds = %103
%106 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%107 = load i32, i32* %106
%108 = getelementptr inbounds [1 x i8], [1 x i8]* %15, i64 0, i64 0
%109 = call i32 @read(i32 %107, i8* %108, i32 1)
br label %93
110: ; preds = %103
br label %78
111: ; preds = %78
%112 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%113 = load i32, i32* %112
%114 = call i32 @close(i32 %113)
%115 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%116 = load i32, i32* %115
%117 = call i32 @close(i32 %116)
br label %118
118: ; preds = %111
br label %119
119: ; preds = %118, %23
%120 = load i32, i32* %9
%121 = call i32 @waitpid(i32 %120, i32* null, i32 0)
ret i32 0
}

LLVM IR (-O2):
@.str = [15 x i8] c"Error de pid.\0A\00"
@.str.1 = [28 x i8] c"[Hijo], mensaje le\C3\ADdo: %s\0A\00"
@.str.2 = [25 x i8] c"[Padre], nuevo mensaje: \00"
@.str.3 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = alloca [2 x i32]
%5 = alloca [256 x i8]
%6 = alloca [1 x i8]
%7 = alloca [256 x i8]
%8 = bitcast [2 x i32]* %3 to i8*
%9 = bitcast [2 x i32]* %4 to i8*
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%11 = call i32 @pipe(i32* %10)
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
%14 = call i32 (...) @fork()
switch i32 %14, label %45 [
i32 -1, label %59
i32 0, label %15
]
15: ; preds = %2
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%17 = getelementptr inbounds [1 x i8], [1 x i8]* %6, i64 0, i64 0
store i8 108, i8* %17
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%19 = load i32, i32* %18
%20 = call i32 @close(i32 %19)
%21 = load i32, i32* %12
%22 = call i32 @close(i32 %21)
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
br label %24
24: ; preds = %15, %34
%25 = phi i32 [ 0, %15 ], [ %37, %34 ]
%26 = load i32, i32* %10
%27 = call i32 @read(i32 %26, i8* %16, i32 256)
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %28
store i8 0, i8* %29
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%31 = call i32 @sleep(i32 1)
%32 = icmp eq i32 %25, 9
br i1 %32, label %33, label %34
33: ; preds = %24
store i8 113, i8* %17
br label %34
34: ; preds = %33, %24
%35 = load i32, i32* %23
%36 = call i32 @write(i32 %35, i8* %17, i32 1)
%37 = add nuw nsw i32 %25, 1
%38 = icmp eq i32 %37, 10
br i1 %38, label %39, label %24
39: ; preds = %34
%40 = load i32, i32* %23
%41 = call i32 @close(i32 %40)
%42 = load i32, i32* %10
%43 = call i32 @close(i32 %42)
%44 = call i32 @exit(i32 0)
unreachable
45: ; preds = %2
%46 = load i32, i32* %10
%47 = call i32 @close(i32 %46)
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%49 = load i32, i32* %48
%50 = call i32 @close(i32 %49)
%51 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%52 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
br label %53
53: ; preds = %45, %53
%54 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%55 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %51)
%56 = load i32, i32* %52
%57 = call i32 @strlen(i8* %51)
%58 = call i32 @write(i32 %56, i8* %51, i32 %57)
br label %53
59: ; preds = %2
%60 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%61 = call i32 @waitpid(i32 %14, i32* null, i32 0)
ret i32 0
}