#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*/ EXIT_FAILURE;
int /*<<< orphan*/ STDOUT_FILENO;
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ createFile(int);
int dup(int /*<<< orphan*/);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int execvp(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ free(char *);
char **parseCommand(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
char *readCommand();
int /*<<< orphan*/ wait(int *);
void sequentialExec() {
  printf("Nota: Premere INVIO per terminare\n");
  int termination_flag = 0;
  int count = 0;
  pid_t pid;
  do {
    printf("\n> ");
    char *str = readCommand();
    if (str != NULL) {
      termination_flag = 1;
      count++;
      if ((pid = fork()) == 0) {
        int saved_stdout = dup(STDOUT_FILENO);
        char **command = parseCommand(str);
        createFile(count);
        if (execvp(command[0], command) == -1) {
          dup2(saved_stdout, STDOUT_FILENO);
          printf("Comando non riconosciuto.\n");
          exit(EXIT_FAILURE);
        }
      } else {
        int status;
        wait(&status);
        if (WIFEXITED(status))
          printf("Il processo id:%d è terminato con successo.\n", pid);
        else {
          perror("Errore processo: ");
        }
      }
    } else {
      termination_flag = 0;
    }
    free(str);
  } while (termination_flag != 0);
}


x86 assembly (clang, -O0):
<sequentialExec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1080 <readCommand@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12f3 <sequentialExec+0x123>
movl   $0x1,-0x4(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jne    12ac <sequentialExec+0xdc>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
call   1060 <dup@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   10a0 <parseCommand@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
call   1070 <createFile@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
call   10b0 <execvp@plt>
cmp    $0xffffffff,%eax
jne    12a7 <sequentialExec+0xd7>
mov    -0x1c(%rbp),%edi
mov    0x2d6c(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
lea    0xd95(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
jmp    12ee <sequentialExec+0x11e>
lea    -0x2c(%rbp),%rdi
call   10e0 <wait@plt>
mov    -0x2c(%rbp),%edi
call   10c0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     12dd <sequentialExec+0x10d>
mov    -0xc(%rbp),%esi
lea    0xd71(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12e9 <sequentialExec+0x119>
lea    0xd8c(%rip),%rdi
call   1090 <perror@plt>
jmp    12ee <sequentialExec+0x11e>
jmp    12fa <sequentialExec+0x12a>
movl   $0x0,-0x4(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x4(%rbp)
jne    11f4 <sequentialExec+0x24>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sequentialExec>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe52(%rip),%rdi
call   1040 <puts@plt>
lea    0xe02(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1090 <readCommand@plt>
test   %rax,%rax
je     12cb <sequentialExec+0xeb>
mov    %rax,%r13
mov    $0x1,%ebx
lea    0xddc(%rip),%r14
lea    0x4(%rsp),%r15
jmp    1288 <sequentialExec+0xa8>
nopl   0x0(%rax,%rax,1)
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
call   1070 <dup@plt>
mov    %eax,%r12d
mov    %r13,%rdi
call   10b0 <parseCommand@plt>
mov    %rax,%rbp
mov    %ebx,%edi
call   1080 <createFile@plt>
mov    0x0(%rbp),%rdi
mov    %rbp,%rsi
call   10c0 <execvp@plt>
cmp    $0xffffffff,%eax
je     12e1 <sequentialExec+0x101>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1090 <readCommand@plt>
mov    %rax,%r13
add    $0x1,%ebx
test   %rax,%rax
je     12cb <sequentialExec+0xeb>
xor    %eax,%eax
call   1100 <fork@plt>
test   %eax,%eax
je     1230 <sequentialExec+0x50>
mov    %eax,%ebp
mov    %r15,%rdi
call   10f0 <wait@plt>
mov    0x4(%rsp),%edi
call   10d0 <WIFEXITED@plt>
test   %rax,%rax
je     12bd <sequentialExec+0xdd>
lea    0xd52(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1264 <sequentialExec+0x84>
lea    0xd6e(%rip),%rdi
call   10a0 <perror@plt>
jmp    1264 <sequentialExec+0x84>
xor    %edi,%edi
call   1030 <free@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d08(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
call   1050 <dup2@plt>
lea    0xd6d(%rip),%rdi
call   1040 <puts@plt>
mov    0x2cd3(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@.str = [35 x i8] c"Nota: Premere INVIO per terminare\0A\00"
@.str.1 = [4 x i8] c"\0A> \00"
@STDOUT_FILENO = global i32 0
@.str.2 = [27 x i8] c"Comando non riconosciuto.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [46 x i8] c"Il processo id:%d \C3\A8 terminato con successo.\0A\00"
@.str.4 = [18 x i8] c"Errore processo: \00"
define void @sequentialExec() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i32
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
store i32 0, i32* %2
br label %9
9: ; preds = %56, %0
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%11 = call i8* (...) @readCommand()
store i8* %11, i8** %4
%12 = load i8*, i8** %4
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %52
14: ; preds = %9
store i32 1, i32* %1
%15 = load i32, i32* %2
%16 = add nsw i32 %15, 1
store i32 %16, i32* %2
%17 = call i32 (...) @fork()
store i32 %17, i32* %3
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %40
19: ; preds = %14
%20 = load i32, i32* @STDOUT_FILENO
%21 = call i32 @dup(i32 %20)
store i32 %21, i32* %5
%22 = load i8*, i8** %4
%23 = call i8** @parseCommand(i8* %22)
store i8** %23, i8*** %6
%24 = load i32, i32* %2
%25 = call i32 @createFile(i32 %24)
%26 = load i8**, i8*** %6
%27 = getelementptr inbounds i8*, i8** %26, i64 0
%28 = load i8*, i8** %27
%29 = load i8**, i8*** %6
%30 = call i32 @execvp(i8* %28, i8** %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %39
32: ; preds = %19
%33 = load i32, i32* %5
%34 = load i32, i32* @STDOUT_FILENO
%35 = call i32 @dup2(i32 %33, i32 %34)
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %19
br label %51
40: ; preds = %14
%41 = call i32 @wait(i32* %7)
%42 = load i32, i32* %7
%43 = call i64 @WIFEXITED(i32 %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %40
%46 = load i32, i32* %3
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i32 %46)
br label %50
48: ; preds = %40
%49 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %50
50: ; preds = %48, %45
br label %51
51: ; preds = %50, %39
br label %53
52: ; preds = %9
store i32 0, i32* %1
br label %53
53: ; preds = %52, %51
%54 = load i8*, i8** %4
%55 = call i32 @free(i8* %54)
br label %56
56: ; preds = %53
%57 = load i32, i32* %1
%58 = icmp ne i32 %57, 0
br i1 %58, label %9, label %59
59: ; preds = %56
ret void
}

LLVM IR (-O2):
@.str.1 = [4 x i8] c"\0A> \00"
@STDOUT_FILENO = global i32 0
@EXIT_FAILURE = global i32 0
@.str.3 = [46 x i8] c"Il processo id:%d \C3\A8 terminato con successo.\0A\00"
@.str.4 = [18 x i8] c"Errore processo: \00"
@str = [34 x i8] c"Nota: Premere INVIO per terminare\00"
@str.5 = [26 x i8] c"Comando non riconosciuto.\00"
define void @sequentialExec() {
%1 = alloca i32
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%3 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%4 = call i8* (...) @readCommand()
%5 = icmp eq i8* %4, null
br i1 %5, label %43, label %6
6: ; preds = %0
%7 = bitcast i32* %1 to i8*
br label %8
8: ; preds = %6, %38
%9 = phi i8* [ %4, %6 ], [ %41, %38 ]
%10 = phi i32 [ 0, %6 ], [ %11, %38 ]
%11 = add nuw nsw i32 %10, 1
%12 = call i32 (...) @fork()
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %28
14: ; preds = %8
%15 = load i32, i32* @STDOUT_FILENO
%16 = call i32 @dup(i32 %15)
%17 = call i8** @parseCommand(i8* %9)
%18 = call i32 @createFile(i32 %11)
%19 = load i8*, i8** %17
%20 = call i32 @execvp(i8* %19, i8** %17)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %38
22: ; preds = %14
%23 = load i32, i32* @STDOUT_FILENO
%24 = call i32 @dup2(i32 %16, i32 %23)
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.5, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %8
%29 = call i32 @wait(i32* %1)
%30 = load i32, i32* %1
%31 = call i64 @WIFEXITED(i32 %30)
%32 = icmp eq i64 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %28
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i32 %12)
br label %37
35: ; preds = %28
%36 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %37
37: ; preds = %35, %33
br label %38
38: ; preds = %14, %37
%39 = call i32 @free(i8* %9)
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%41 = call i8* (...) @readCommand()
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %8
43: ; preds = %38, %0
%44 = call i32 @free(i8* null)
ret void
}