#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 /*<<< orphan*/ buff;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int getpid();
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int ipc[2], proc;
  int leido;
  char buff[80];
  pipe(ipc);
  printf("ipc[0] = %d ipc[1] =%d \n ", ipc[0], ipc[1]);
  proc = fork();
  if (proc == 0) {
    close(ipc[1]);
    leido = read(ipc[0], buff, sizeof(buff));
    if (leido < 1) {
      write(0, "\nError al leer tuberia", sizeof("\nError al leer tuberia"));
    } else {
      write(0, "\nLeido de la tuberia: ", sizeof("\nLeido de la tuberia: "));
      write(0, buff, leido - 1);
      printf(", por el proceso hijo, pid %d \n", getpid());
    }
    exit(0);
  }
  close(ipc[0]);
  printf("\nIngrese una cadena de caracteres por consola:\n");
  leido = read(0, buff, sizeof(buff));
  write(ipc[1], buff, leido);
  close(ipc[1]);
  wait(NULL);
  write(0, "\nEscrito en la tuberia: ", sizeof("\nEscrito en la tuberia: "));
  write(0, buff, leido - 1);
  printf(", por el proceso padre, pid %d \n", getpid());
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1070 <pipe@plt>
mov    -0xc(%rbp),%esi
mov    -0x8(%rbp),%edx
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    1250 <main+0xc0>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x50,%edx
call   1080 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jge    120e <main+0x7e>
xor    %edi,%edi
lea    0xe1b(%rip),%rsi
mov    $0x17,%edx
call   1030 <write@plt>
jmp    1249 <main+0xb9>
xor    %edi,%edi
lea    0xe1a(%rip),%rsi
mov    $0x17,%edx
call   1030 <write@plt>
lea    -0x70(%rbp),%rsi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
xor    %edi,%edi
call   1030 <write@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x70(%rbp),%rsi
xor    %edi,%edi
mov    $0x50,%edx
call   1080 <read@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
xor    %edi,%edi
lea    0xdf6(%rip),%rsi
mov    $0x19,%edx
call   1030 <write@plt>
lea    -0x70(%rbp),%rsi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
xor    %edi,%edi
call   1030 <write@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x68,%rsp
lea    0x8(%rsp),%rdi
call   1080 <pipe@plt>
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%edx
lea    0xe41(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
jne    120f <main+0x6f>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x50,%edx
call   1090 <read@plt>
test   %eax,%eax
jg     128a <main+0xea>
lea    0xe1e(%rip),%rsi
xor    %edi,%edi
mov    $0x17,%edx
call   1040 <write@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
lea    0xe83(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%rbx
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x50,%edx
call   1090 <read@plt>
mov    %eax,%ebp
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10b0 <wait@plt>
lea    0xe09(%rip),%rsi
xor    %edi,%edi
mov    $0x19,%edx
call   1040 <write@plt>
add    $0xffffffff,%ebp
xor    %edi,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xdf9(%rip),%rdi
jmp    12be <main+0x11e>
mov    %eax,%ebx
lea    0xd9e(%rip),%rsi
xor    %edi,%edi
mov    $0x17,%edx
call   1040 <write@plt>
add    $0xffffffff,%ebx
lea    0x10(%rsp),%rsi
xor    %edi,%edi
mov    %ebx,%edx
call   1040 <write@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xd8a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"ipc[0] = %d ipc[1] =%d \0A \00"
@.str.1 = [23 x i8] c"\0AError al leer tuberia\00"
@.str.2 = [23 x i8] c"\0ALeido de la tuberia: \00"
@.str.3 = [32 x i8] c", por el proceso hijo, pid %d \0A\00"
@.str.4 = [48 x i8] c"\0AIngrese una cadena de caracteres por consola:\0A\00"
@.str.5 = [25 x i8] c"\0AEscrito en la tuberia: \00"
@.str.6 = [33 x i8] c", por el proceso padre, pid %d \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = alloca [80 x i8]
store i32 0, i32* %1
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%7 = call i32 @pipe(i32* %6)
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%9 = load i32, i32* %8
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%11 = load i32, i32* %10
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %11)
%13 = call i32 (...) @fork()
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %38
16: ; preds = %0
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%18 = load i32, i32* %17
%19 = call i32 @close(i32 %18)
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%21 = load i32, i32* %20
%22 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%23 = call i32 @read(i32 %21, i8* %22, i32 80)
store i32 %23, i32* %4
%24 = load i32, i32* %4
%25 = icmp slt i32 %24, 1
br i1 %25, label %26, label %28
26: ; preds = %16
%27 = call i32 @write(i32 0, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 23)
br label %36
28: ; preds = %16
%29 = call i32 @write(i32 0, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 23)
%30 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%31 = load i32, i32* %4
%32 = sub nsw i32 %31, 1
%33 = call i32 @write(i32 0, i8* %30, i32 %32)
%34 = call i32 (...) @getpid()
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %28, %26
%37 = call i32 @exit(i32 0)
unreachable
38: ; preds = %0
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%40 = load i32, i32* %39
%41 = call i32 @close(i32 %40)
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.4, i64 0, i64 0))
%43 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%44 = call i32 @read(i32 0, i8* %43, i32 80)
store i32 %44, i32* %4
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%46 = load i32, i32* %45
%47 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%48 = load i32, i32* %4
%49 = call i32 @write(i32 %46, i8* %47, i32 %48)
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%51 = load i32, i32* %50
%52 = call i32 @close(i32 %51)
%53 = call i32 @wait(i32* null)
%54 = call i32 @write(i32 0, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 25)
%55 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%56 = load i32, i32* %4
%57 = sub nsw i32 %56, 1
%58 = call i32 @write(i32 0, i8* %55, i32 %57)
%59 = call i32 (...) @getpid()
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i32 %59)
%61 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [26 x i8] c"ipc[0] = %d ipc[1] =%d \0A \00"
@.str.1 = [23 x i8] c"\0AError al leer tuberia\00"
@.str.2 = [23 x i8] c"\0ALeido de la tuberia: \00"
@.str.3 = [32 x i8] c", por el proceso hijo, pid %d \0A\00"
@.str.5 = [25 x i8] c"\0AEscrito en la tuberia: \00"
@.str.6 = [33 x i8] c", por el proceso padre, pid %d \0A\00"
@str = [47 x i8] c"\0AIngrese una cadena de caracteres por consola:\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [80 x i8]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = load i32, i32* %5
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%9 = load i32, i32* %8
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %9)
%11 = call i32 (...) @fork()
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %29
13: ; preds = %0
%14 = load i32, i32* %8
%15 = call i32 @close(i32 %14)
%16 = load i32, i32* %5
%17 = call i32 @read(i32 %16, i8* %4, i32 80)
%18 = icmp slt i32 %17, 1
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = call i32 @write(i32 0, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 23)
br label %27
21: ; preds = %13
%22 = call i32 @write(i32 0, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 23)
%23 = add nsw i32 %17, -1
%24 = call i32 @write(i32 0, i8* %4, i32 %23)
%25 = call i32 (...) @getpid()
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %21, %19
%28 = call i32 @exit(i32 0)
unreachable
29: ; preds = %0
%30 = load i32, i32* %5
%31 = call i32 @close(i32 %30)
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str, i64 0, i64 0))
%33 = call i32 @read(i32 0, i8* %4, i32 80)
%34 = load i32, i32* %8
%35 = call i32 @write(i32 %34, i8* %4, i32 %33)
%36 = load i32, i32* %8
%37 = call i32 @close(i32 %36)
%38 = call i32 @wait(i32* null)
%39 = call i32 @write(i32 0, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 25)
%40 = add nsw i32 %33, -1
%41 = call i32 @write(i32 0, i8* %4, i32 %40)
%42 = call i32 (...) @getpid()
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i32 %42)
%44 = call i32 @exit(i32 0)
unreachable
}