#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
scalar_t__ read(int, char *, int);
int strlen(char const *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char const *, int);
int main(int argc, char const *argv[]) {
  if (argc != 2) {
    perror("Per utilizzare il programma, digitare il nome del comando seguito "
           "dalla riga da trasmettere.\n");
    exit(EXIT_FAILURE);
  }
  int tubofd[2];
  char buffer;
  int tubo = pipe(tubofd);
  if (tubo == -1) {
    perror("CREAZIONE PIPE FALLITA\n");
    exit(EXIT_FAILURE);
  }
  pid_t child = fork();
  if (child < 0) {
    perror("FORK FALLITA\n");
    exit(EXIT_FAILURE);
  }
  if (child == 0) {
    close(tubofd[1]);
    while (read(tubofd[0], &buffer, 1) > 0) {
      write(STDOUT_FILENO, &buffer, 1);
    }
    write(STDOUT_FILENO, "\n", 1);
    close(tubofd[0]);
    exit(EXIT_SUCCESS);
  } else {
    close(tubofd[0]);
    write(tubofd[1], argv[1], strlen(argv[1]));
    close(tubofd[1]);
    wait(NULL);
    exit(EXIT_SUCCESS);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11ca <main+0x3a>
lea    0xe49(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    -0x18(%rbp),%rdi
call   1060 <pipe@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11fa <main+0x6a>
lea    0xe77(%rip),%rdi
call   1080 <perror@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    122a <main+0x9a>
lea    0xe5f(%rip),%rdi
call   1080 <perror@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x28(%rbp)
jne    12a4 <main+0x114>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x0,%rax
jle    1274 <main+0xe4>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    123d <main+0xad>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
lea    0xdd8(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
mov    0x2cdf(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jne    11b6 <main+0x26>
mov    %rsi,%rbx
lea    0x10(%rsp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11d0 <main+0x40>
lea    0xeaa(%rip),%rdi
jmp    11bd <main+0x2d>
lea    0xe43(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
jns    11e5 <main+0x55>
lea    0xe93(%rip),%rdi
jmp    11bd <main+0x2d>
jne    126b <main+0xdb>
mov    0x14(%rsp),%edi
call   1050 <close@plt>
mov    0x10(%rsp),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
test   %rax,%rax
jle    1246 <main+0xb6>
mov    0x2ddd(%rip),%rbp
lea    0xf(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    0x0(%rbp),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    0x10(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
test   %rax,%rax
jg     1220 <main+0x90>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
lea    0xe06(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    0x10(%rsp),%edi
call   1050 <close@plt>
jmp    12a0 <main+0x110>
mov    0x10(%rsp),%edi
call   1050 <close@plt>
mov    0x14(%rsp),%ebp
mov    0x8(%rbx),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    0x14(%rsp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   10a0 <wait@plt>
mov    0x2d29(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [94 x i8] c"Per utilizzare il programma, digitare il nome del comando seguito dalla riga da trasmettere.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [24 x i8] c"CREAZIONE PIPE FALLITA\0A\00"
@.str.2 = [14 x i8] c"FORK FALLITA\0A\00"
@STDOUT_FILENO = global i32 0
@.str.3 = [2 x i8] c"\0A\00"
@EXIT_SUCCESS = 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 i8
%8 = alloca i32
%9 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([94 x i8], [94 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %2
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%18 = call i32 @pipe(i32* %17)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %16
%26 = call i64 (...) @fork()
store i64 %26, i64* %9
%27 = load i64, i64* %9
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %25
%34 = load i64, i64* %9
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %56
36: ; preds = %33
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%38 = load i32, i32* %37
%39 = call i32 @close(i32 %38)
br label %40
40: ; preds = %45, %36
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%42 = load i32, i32* %41
%43 = call i64 @read(i32 %42, i8* %7, i32 1)
%44 = icmp sgt i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %40
%46 = load i32, i32* @STDOUT_FILENO
%47 = call i32 @write(i32 %46, i8* %7, i32 1)
br label %40
48: ; preds = %40
%49 = load i32, i32* @STDOUT_FILENO
%50 = call i32 @write(i32 %49, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%52 = load i32, i32* %51
%53 = call i32 @close(i32 %52)
%54 = load i32, i32* @EXIT_SUCCESS
%55 = call i32 @exit(i32 %54)
unreachable
56: ; preds = %33
%57 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%58 = load i32, i32* %57
%59 = call i32 @close(i32 %58)
%60 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%61 = load i32, i32* %60
%62 = load i8**, i8*** %5
%63 = getelementptr inbounds i8*, i8** %62, i64 1
%64 = load i8*, i8** %63
%65 = load i8**, i8*** %5
%66 = getelementptr inbounds i8*, i8** %65, i64 1
%67 = load i8*, i8** %66
%68 = call i32 @strlen(i8* %67)
%69 = call i32 @write(i32 %61, i8* %64, i32 %68)
%70 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%71 = load i32, i32* %70
%72 = call i32 @close(i32 %71)
%73 = call i32 @wait(i32* null)
%74 = load i32, i32* @EXIT_SUCCESS
%75 = call i32 @exit(i32 %74)
unreachable
}

LLVM IR (-O2):
@.str = [94 x i8] c"Per utilizzare il programma, digitare il nome del comando seguito dalla riga da trasmettere.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [24 x i8] c"CREAZIONE PIPE FALLITA\0A\00"
@.str.2 = [14 x i8] c"FORK FALLITA\0A\00"
@STDOUT_FILENO = global i32 0
@.str.3 = [2 x i8] c"\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x i32]
%4 = alloca i8
%5 = icmp eq i32 %0, 2
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([94 x i8], [94 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = bitcast [2 x i32]* %3 to i8*
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %10
%16 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %10
%20 = call i64 (...) @fork()
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %19
%27 = icmp eq i64 %20, 0
br i1 %27, label %28, label %48
28: ; preds = %26
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%30 = load i32, i32* %29
%31 = call i32 @close(i32 %30)
%32 = load i32, i32* %12
%33 = call i64 @read(i32 %32, i8* %4, i32 1)
%34 = icmp sgt i64 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %28, %35
%36 = load i32, i32* @STDOUT_FILENO
%37 = call i32 @write(i32 %36, i8* %4, i32 1)
%38 = load i32, i32* %12
%39 = call i64 @read(i32 %38, i8* %4, i32 1)
%40 = icmp sgt i64 %39, 0
br i1 %40, label %35, label %41
41: ; preds = %35, %28
%42 = load i32, i32* @STDOUT_FILENO
%43 = call i32 @write(i32 %42, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%44 = load i32, i32* %12
%45 = call i32 @close(i32 %44)
%46 = load i32, i32* @EXIT_SUCCESS
%47 = call i32 @exit(i32 %46)
unreachable
48: ; preds = %26
%49 = load i32, i32* %12
%50 = call i32 @close(i32 %49)
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%52 = load i32, i32* %51
%53 = getelementptr inbounds i8*, i8** %1, i64 1
%54 = load i8*, i8** %53
%55 = call i32 @strlen(i8* %54)
%56 = call i32 @write(i32 %52, i8* %54, i32 %55)
%57 = load i32, i32* %51
%58 = call i32 @close(i32 %57)
%59 = call i32 @wait(i32* null)
%60 = load i32, i32* @EXIT_SUCCESS
%61 = call i32 @exit(i32 %60)
unreachable
}